@orderly.network/perp 5.0.4 → 5.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +22 -5
- package/dist/index.d.ts +22 -5
- package/dist/index.js +38 -18
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +38 -18
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.d.mts
CHANGED
|
@@ -8,7 +8,7 @@ declare global {
|
|
|
8
8
|
};
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
|
-
declare const _default: "5.0
|
|
11
|
+
declare const _default: "5.1.0";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Calculates the notional value of a single position.
|
|
@@ -472,7 +472,7 @@ declare function maxPositionLeverage(inputs: {
|
|
|
472
472
|
* - `cost_position' = cost_position`
|
|
473
473
|
*
|
|
474
474
|
* ### 2. Open/Add Position (position_qty = 0 or order_side = sign(position_qty))
|
|
475
|
-
* - `isolated_position_margin' = isolated_position_margin + order_qty * reference_price / leverage`
|
|
475
|
+
* - `isolated_position_margin' = isolated_position_margin + order_qty * reference_price * (1 / leverage + iso_taker_fee_buffer)`
|
|
476
476
|
* - `cost_position' = cost_position + order_side * order_qty * reference_price`
|
|
477
477
|
*
|
|
478
478
|
* ### 3. Close/Reduce Position (order_side ≠ sign(position_qty) and sign(position_qty') = sign(position_qty))
|
|
@@ -480,7 +480,7 @@ declare function maxPositionLeverage(inputs: {
|
|
|
480
480
|
* - `cost_position' = cost_position + order_side * order_qty * reference_price`
|
|
481
481
|
*
|
|
482
482
|
* ### 4. Flip Position (order_side ≠ sign(position_qty) and sign(position_qty') ≠ sign(position_qty))
|
|
483
|
-
* - `isolated_position_margin' = abs(position_qty') * reference_price / leverage`
|
|
483
|
+
* - `isolated_position_margin' = abs(position_qty') * reference_price * (1 / leverage + iso_taker_fee_buffer)`
|
|
484
484
|
* - `cost_position' = position_qty' * reference_price`
|
|
485
485
|
*
|
|
486
486
|
* ## Example
|
|
@@ -550,6 +550,10 @@ declare function liquidationPriceIsolated(inputs: {
|
|
|
550
550
|
* @description Leverage for the position
|
|
551
551
|
*/
|
|
552
552
|
leverage: number;
|
|
553
|
+
/**
|
|
554
|
+
* @description Fee buffer reserved in isolated margin estimates (default: 0.0006)
|
|
555
|
+
*/
|
|
556
|
+
isoTakerFeeBuffer?: number;
|
|
553
557
|
}): number | null;
|
|
554
558
|
|
|
555
559
|
type index$1_LiqPriceInputs = LiqPriceInputs;
|
|
@@ -1151,12 +1155,16 @@ declare function maxQtyByShort(inputs: Omit<MaxQtyInputs, "side">, options?: Res
|
|
|
1151
1155
|
* current_order_reference_price = 99900 USDC
|
|
1152
1156
|
* max_notional = 10059467.44 USDC
|
|
1153
1157
|
* pending_long_notional = 299200 USDC
|
|
1154
|
-
* max_qty = MIN(1000 / 99900 *
|
|
1158
|
+
* max_qty = MIN(1000 / (1 / 25 + 0.0006) / 99900 * 0.995, (10059467.44 - 100000 * 5 - 299200) / 99900) = 0.245 BTC
|
|
1155
1159
|
* ```
|
|
1156
1160
|
*
|
|
1157
1161
|
* @param inputs Input parameters for calculating maximum tradeable quantity
|
|
1158
1162
|
* @returns Maximum tradeable quantity
|
|
1159
1163
|
*/
|
|
1164
|
+
declare function isolatedMarginRate(inputs: {
|
|
1165
|
+
leverage: number;
|
|
1166
|
+
isoTakerFeeBuffer?: number;
|
|
1167
|
+
}): Decimal;
|
|
1160
1168
|
declare function maxQtyForIsolatedMargin(inputs: {
|
|
1161
1169
|
/**
|
|
1162
1170
|
* @description Trading symbol
|
|
@@ -1224,6 +1232,10 @@ declare function maxQtyForIsolatedMargin(inputs: {
|
|
|
1224
1232
|
* @description Precision threshold for binary search (default: 1)
|
|
1225
1233
|
*/
|
|
1226
1234
|
epsilon?: number;
|
|
1235
|
+
/**
|
|
1236
|
+
* @description Fee buffer reserved in isolated frozen margin (default: 0.0006)
|
|
1237
|
+
*/
|
|
1238
|
+
isoTakerFeeBuffer?: number;
|
|
1227
1239
|
}): number;
|
|
1228
1240
|
|
|
1229
1241
|
/**
|
|
@@ -1512,6 +1524,7 @@ declare const index_getPositonsAndOrdersNotionalBySymbol: typeof getPositonsAndO
|
|
|
1512
1524
|
declare const index_getQtyFromOrdersBySide: typeof getQtyFromOrdersBySide;
|
|
1513
1525
|
declare const index_getQtyFromPositions: typeof getQtyFromPositions;
|
|
1514
1526
|
declare const index_groupOrdersBySymbol: typeof groupOrdersBySymbol;
|
|
1527
|
+
declare const index_isolatedMarginRate: typeof isolatedMarginRate;
|
|
1515
1528
|
declare const index_maxAdd: typeof maxAdd;
|
|
1516
1529
|
declare const index_maxQty: typeof maxQty;
|
|
1517
1530
|
declare const index_maxQtyByLong: typeof maxQtyByLong;
|
|
@@ -1530,7 +1543,7 @@ declare const index_totalMarginRatio: typeof totalMarginRatio;
|
|
|
1530
1543
|
declare const index_totalUnrealizedROI: typeof totalUnrealizedROI;
|
|
1531
1544
|
declare const index_totalValue: typeof totalValue;
|
|
1532
1545
|
declare namespace index {
|
|
1533
|
-
export { type index_FreeCollateralInputs as FreeCollateralInputs, type index_FreeCollateralUSDCOnlyInputs as FreeCollateralUSDCOnlyInputs, index_IMR as IMR, index_LTV as LTV, index_MMR as MMR, type index_MaxQtyInputs as MaxQtyInputs, type index_ResultOptions as ResultOptions, type index_TotalValueInputs as TotalValueInputs, index_availableBalance as availableBalance, index_availableBalanceForIsolatedMargin as availableBalanceForIsolatedMargin, index_buyOrdersFilter_by_symbol as buyOrdersFilter_by_symbol, index_calcMinimumReceived as calcMinimumReceived, index_collateralContribution as collateralContribution, index_collateralRatio as collateralRatio, index_currentLeverage as currentLeverage, index_extractSymbols as extractSymbols, index_freeCollateral as freeCollateral, index_freeCollateralUSDCOnly as freeCollateralUSDCOnly, index_getPositonsAndOrdersNotionalBySymbol as getPositonsAndOrdersNotionalBySymbol, index_getQtyFromOrdersBySide as getQtyFromOrdersBySide, index_getQtyFromPositions as getQtyFromPositions, index_groupOrdersBySymbol as groupOrdersBySymbol, index_maxAdd as maxAdd, index_maxQty as maxQty, index_maxQtyByLong as maxQtyByLong, index_maxQtyByShort as maxQtyByShort, index_maxQtyForIsolatedMargin as maxQtyForIsolatedMargin, index_maxReduce as maxReduce, index_maxWithdrawalOtherCollateral as maxWithdrawalOtherCollateral, index_maxWithdrawalUSDC as maxWithdrawalUSDC, index_otherIMs as otherIMs, index_positionNotionalWithOrder_by_symbol as positionNotionalWithOrder_by_symbol, index_positionQtyWithOrders_by_symbol as positionQtyWithOrders_by_symbol, index_sellOrdersFilter_by_symbol as sellOrdersFilter_by_symbol, index_totalCollateral as totalCollateral, index_totalInitialMarginWithQty as totalInitialMarginWithQty, index_totalMarginRatio as totalMarginRatio, index_totalUnrealizedROI as totalUnrealizedROI, index_totalValue as totalValue };
|
|
1546
|
+
export { type index_FreeCollateralInputs as FreeCollateralInputs, type index_FreeCollateralUSDCOnlyInputs as FreeCollateralUSDCOnlyInputs, index_IMR as IMR, index_LTV as LTV, index_MMR as MMR, type index_MaxQtyInputs as MaxQtyInputs, type index_ResultOptions as ResultOptions, type index_TotalValueInputs as TotalValueInputs, index_availableBalance as availableBalance, index_availableBalanceForIsolatedMargin as availableBalanceForIsolatedMargin, index_buyOrdersFilter_by_symbol as buyOrdersFilter_by_symbol, index_calcMinimumReceived as calcMinimumReceived, index_collateralContribution as collateralContribution, index_collateralRatio as collateralRatio, index_currentLeverage as currentLeverage, index_extractSymbols as extractSymbols, index_freeCollateral as freeCollateral, index_freeCollateralUSDCOnly as freeCollateralUSDCOnly, index_getPositonsAndOrdersNotionalBySymbol as getPositonsAndOrdersNotionalBySymbol, index_getQtyFromOrdersBySide as getQtyFromOrdersBySide, index_getQtyFromPositions as getQtyFromPositions, index_groupOrdersBySymbol as groupOrdersBySymbol, index_isolatedMarginRate as isolatedMarginRate, index_maxAdd as maxAdd, index_maxQty as maxQty, index_maxQtyByLong as maxQtyByLong, index_maxQtyByShort as maxQtyByShort, index_maxQtyForIsolatedMargin as maxQtyForIsolatedMargin, index_maxReduce as maxReduce, index_maxWithdrawalOtherCollateral as maxWithdrawalOtherCollateral, index_maxWithdrawalUSDC as maxWithdrawalUSDC, index_otherIMs as otherIMs, index_positionNotionalWithOrder_by_symbol as positionNotionalWithOrder_by_symbol, index_positionQtyWithOrders_by_symbol as positionQtyWithOrders_by_symbol, index_sellOrdersFilter_by_symbol as sellOrdersFilter_by_symbol, index_totalCollateral as totalCollateral, index_totalInitialMarginWithQty as totalInitialMarginWithQty, index_totalMarginRatio as totalMarginRatio, index_totalUnrealizedROI as totalUnrealizedROI, index_totalValue as totalValue };
|
|
1534
1547
|
}
|
|
1535
1548
|
|
|
1536
1549
|
/** @deprecated Use inline type or the new input type instead */
|
|
@@ -1714,6 +1727,10 @@ declare function estLiqPriceIsolated(inputs: {
|
|
|
1714
1727
|
* @description Leverage for this isolated position
|
|
1715
1728
|
*/
|
|
1716
1729
|
leverage: number;
|
|
1730
|
+
/**
|
|
1731
|
+
* @description Fee buffer reserved in isolated margin estimates (default: 0.0006)
|
|
1732
|
+
*/
|
|
1733
|
+
isoTakerFeeBuffer?: number;
|
|
1717
1734
|
/**
|
|
1718
1735
|
* @description New order information used for estimation
|
|
1719
1736
|
*/
|
package/dist/index.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ declare global {
|
|
|
8
8
|
};
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
|
-
declare const _default: "5.0
|
|
11
|
+
declare const _default: "5.1.0";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Calculates the notional value of a single position.
|
|
@@ -472,7 +472,7 @@ declare function maxPositionLeverage(inputs: {
|
|
|
472
472
|
* - `cost_position' = cost_position`
|
|
473
473
|
*
|
|
474
474
|
* ### 2. Open/Add Position (position_qty = 0 or order_side = sign(position_qty))
|
|
475
|
-
* - `isolated_position_margin' = isolated_position_margin + order_qty * reference_price / leverage`
|
|
475
|
+
* - `isolated_position_margin' = isolated_position_margin + order_qty * reference_price * (1 / leverage + iso_taker_fee_buffer)`
|
|
476
476
|
* - `cost_position' = cost_position + order_side * order_qty * reference_price`
|
|
477
477
|
*
|
|
478
478
|
* ### 3. Close/Reduce Position (order_side ≠ sign(position_qty) and sign(position_qty') = sign(position_qty))
|
|
@@ -480,7 +480,7 @@ declare function maxPositionLeverage(inputs: {
|
|
|
480
480
|
* - `cost_position' = cost_position + order_side * order_qty * reference_price`
|
|
481
481
|
*
|
|
482
482
|
* ### 4. Flip Position (order_side ≠ sign(position_qty) and sign(position_qty') ≠ sign(position_qty))
|
|
483
|
-
* - `isolated_position_margin' = abs(position_qty') * reference_price / leverage`
|
|
483
|
+
* - `isolated_position_margin' = abs(position_qty') * reference_price * (1 / leverage + iso_taker_fee_buffer)`
|
|
484
484
|
* - `cost_position' = position_qty' * reference_price`
|
|
485
485
|
*
|
|
486
486
|
* ## Example
|
|
@@ -550,6 +550,10 @@ declare function liquidationPriceIsolated(inputs: {
|
|
|
550
550
|
* @description Leverage for the position
|
|
551
551
|
*/
|
|
552
552
|
leverage: number;
|
|
553
|
+
/**
|
|
554
|
+
* @description Fee buffer reserved in isolated margin estimates (default: 0.0006)
|
|
555
|
+
*/
|
|
556
|
+
isoTakerFeeBuffer?: number;
|
|
553
557
|
}): number | null;
|
|
554
558
|
|
|
555
559
|
type index$1_LiqPriceInputs = LiqPriceInputs;
|
|
@@ -1151,12 +1155,16 @@ declare function maxQtyByShort(inputs: Omit<MaxQtyInputs, "side">, options?: Res
|
|
|
1151
1155
|
* current_order_reference_price = 99900 USDC
|
|
1152
1156
|
* max_notional = 10059467.44 USDC
|
|
1153
1157
|
* pending_long_notional = 299200 USDC
|
|
1154
|
-
* max_qty = MIN(1000 / 99900 *
|
|
1158
|
+
* max_qty = MIN(1000 / (1 / 25 + 0.0006) / 99900 * 0.995, (10059467.44 - 100000 * 5 - 299200) / 99900) = 0.245 BTC
|
|
1155
1159
|
* ```
|
|
1156
1160
|
*
|
|
1157
1161
|
* @param inputs Input parameters for calculating maximum tradeable quantity
|
|
1158
1162
|
* @returns Maximum tradeable quantity
|
|
1159
1163
|
*/
|
|
1164
|
+
declare function isolatedMarginRate(inputs: {
|
|
1165
|
+
leverage: number;
|
|
1166
|
+
isoTakerFeeBuffer?: number;
|
|
1167
|
+
}): Decimal;
|
|
1160
1168
|
declare function maxQtyForIsolatedMargin(inputs: {
|
|
1161
1169
|
/**
|
|
1162
1170
|
* @description Trading symbol
|
|
@@ -1224,6 +1232,10 @@ declare function maxQtyForIsolatedMargin(inputs: {
|
|
|
1224
1232
|
* @description Precision threshold for binary search (default: 1)
|
|
1225
1233
|
*/
|
|
1226
1234
|
epsilon?: number;
|
|
1235
|
+
/**
|
|
1236
|
+
* @description Fee buffer reserved in isolated frozen margin (default: 0.0006)
|
|
1237
|
+
*/
|
|
1238
|
+
isoTakerFeeBuffer?: number;
|
|
1227
1239
|
}): number;
|
|
1228
1240
|
|
|
1229
1241
|
/**
|
|
@@ -1512,6 +1524,7 @@ declare const index_getPositonsAndOrdersNotionalBySymbol: typeof getPositonsAndO
|
|
|
1512
1524
|
declare const index_getQtyFromOrdersBySide: typeof getQtyFromOrdersBySide;
|
|
1513
1525
|
declare const index_getQtyFromPositions: typeof getQtyFromPositions;
|
|
1514
1526
|
declare const index_groupOrdersBySymbol: typeof groupOrdersBySymbol;
|
|
1527
|
+
declare const index_isolatedMarginRate: typeof isolatedMarginRate;
|
|
1515
1528
|
declare const index_maxAdd: typeof maxAdd;
|
|
1516
1529
|
declare const index_maxQty: typeof maxQty;
|
|
1517
1530
|
declare const index_maxQtyByLong: typeof maxQtyByLong;
|
|
@@ -1530,7 +1543,7 @@ declare const index_totalMarginRatio: typeof totalMarginRatio;
|
|
|
1530
1543
|
declare const index_totalUnrealizedROI: typeof totalUnrealizedROI;
|
|
1531
1544
|
declare const index_totalValue: typeof totalValue;
|
|
1532
1545
|
declare namespace index {
|
|
1533
|
-
export { type index_FreeCollateralInputs as FreeCollateralInputs, type index_FreeCollateralUSDCOnlyInputs as FreeCollateralUSDCOnlyInputs, index_IMR as IMR, index_LTV as LTV, index_MMR as MMR, type index_MaxQtyInputs as MaxQtyInputs, type index_ResultOptions as ResultOptions, type index_TotalValueInputs as TotalValueInputs, index_availableBalance as availableBalance, index_availableBalanceForIsolatedMargin as availableBalanceForIsolatedMargin, index_buyOrdersFilter_by_symbol as buyOrdersFilter_by_symbol, index_calcMinimumReceived as calcMinimumReceived, index_collateralContribution as collateralContribution, index_collateralRatio as collateralRatio, index_currentLeverage as currentLeverage, index_extractSymbols as extractSymbols, index_freeCollateral as freeCollateral, index_freeCollateralUSDCOnly as freeCollateralUSDCOnly, index_getPositonsAndOrdersNotionalBySymbol as getPositonsAndOrdersNotionalBySymbol, index_getQtyFromOrdersBySide as getQtyFromOrdersBySide, index_getQtyFromPositions as getQtyFromPositions, index_groupOrdersBySymbol as groupOrdersBySymbol, index_maxAdd as maxAdd, index_maxQty as maxQty, index_maxQtyByLong as maxQtyByLong, index_maxQtyByShort as maxQtyByShort, index_maxQtyForIsolatedMargin as maxQtyForIsolatedMargin, index_maxReduce as maxReduce, index_maxWithdrawalOtherCollateral as maxWithdrawalOtherCollateral, index_maxWithdrawalUSDC as maxWithdrawalUSDC, index_otherIMs as otherIMs, index_positionNotionalWithOrder_by_symbol as positionNotionalWithOrder_by_symbol, index_positionQtyWithOrders_by_symbol as positionQtyWithOrders_by_symbol, index_sellOrdersFilter_by_symbol as sellOrdersFilter_by_symbol, index_totalCollateral as totalCollateral, index_totalInitialMarginWithQty as totalInitialMarginWithQty, index_totalMarginRatio as totalMarginRatio, index_totalUnrealizedROI as totalUnrealizedROI, index_totalValue as totalValue };
|
|
1546
|
+
export { type index_FreeCollateralInputs as FreeCollateralInputs, type index_FreeCollateralUSDCOnlyInputs as FreeCollateralUSDCOnlyInputs, index_IMR as IMR, index_LTV as LTV, index_MMR as MMR, type index_MaxQtyInputs as MaxQtyInputs, type index_ResultOptions as ResultOptions, type index_TotalValueInputs as TotalValueInputs, index_availableBalance as availableBalance, index_availableBalanceForIsolatedMargin as availableBalanceForIsolatedMargin, index_buyOrdersFilter_by_symbol as buyOrdersFilter_by_symbol, index_calcMinimumReceived as calcMinimumReceived, index_collateralContribution as collateralContribution, index_collateralRatio as collateralRatio, index_currentLeverage as currentLeverage, index_extractSymbols as extractSymbols, index_freeCollateral as freeCollateral, index_freeCollateralUSDCOnly as freeCollateralUSDCOnly, index_getPositonsAndOrdersNotionalBySymbol as getPositonsAndOrdersNotionalBySymbol, index_getQtyFromOrdersBySide as getQtyFromOrdersBySide, index_getQtyFromPositions as getQtyFromPositions, index_groupOrdersBySymbol as groupOrdersBySymbol, index_isolatedMarginRate as isolatedMarginRate, index_maxAdd as maxAdd, index_maxQty as maxQty, index_maxQtyByLong as maxQtyByLong, index_maxQtyByShort as maxQtyByShort, index_maxQtyForIsolatedMargin as maxQtyForIsolatedMargin, index_maxReduce as maxReduce, index_maxWithdrawalOtherCollateral as maxWithdrawalOtherCollateral, index_maxWithdrawalUSDC as maxWithdrawalUSDC, index_otherIMs as otherIMs, index_positionNotionalWithOrder_by_symbol as positionNotionalWithOrder_by_symbol, index_positionQtyWithOrders_by_symbol as positionQtyWithOrders_by_symbol, index_sellOrdersFilter_by_symbol as sellOrdersFilter_by_symbol, index_totalCollateral as totalCollateral, index_totalInitialMarginWithQty as totalInitialMarginWithQty, index_totalMarginRatio as totalMarginRatio, index_totalUnrealizedROI as totalUnrealizedROI, index_totalValue as totalValue };
|
|
1534
1547
|
}
|
|
1535
1548
|
|
|
1536
1549
|
/** @deprecated Use inline type or the new input type instead */
|
|
@@ -1714,6 +1727,10 @@ declare function estLiqPriceIsolated(inputs: {
|
|
|
1714
1727
|
* @description Leverage for this isolated position
|
|
1715
1728
|
*/
|
|
1716
1729
|
leverage: number;
|
|
1730
|
+
/**
|
|
1731
|
+
* @description Fee buffer reserved in isolated margin estimates (default: 0.0006)
|
|
1732
|
+
*/
|
|
1733
|
+
isoTakerFeeBuffer?: number;
|
|
1717
1734
|
/**
|
|
1718
1735
|
* @description New order information used for estimation
|
|
1719
1736
|
*/
|
package/dist/index.js
CHANGED
|
@@ -12,9 +12,9 @@ var __export = (target, all) => {
|
|
|
12
12
|
// src/version.ts
|
|
13
13
|
if (typeof window !== "undefined") {
|
|
14
14
|
window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};
|
|
15
|
-
window.__ORDERLY_VERSION__["@orderly.network/perp"] = "5.0
|
|
15
|
+
window.__ORDERLY_VERSION__["@orderly.network/perp"] = "5.1.0";
|
|
16
16
|
}
|
|
17
|
-
var version_default = "5.0
|
|
17
|
+
var version_default = "5.1.0";
|
|
18
18
|
|
|
19
19
|
// src/positions/index.ts
|
|
20
20
|
var positions_exports = {};
|
|
@@ -67,6 +67,8 @@ function totalUnrealizedPnL(positions) {
|
|
|
67
67
|
|
|
68
68
|
// src/constants.ts
|
|
69
69
|
var IMRFactorPower = 4 / 5;
|
|
70
|
+
var IsoTakerFeeBuffer = 6e-4;
|
|
71
|
+
var MaxQtySafetyFactor = 0.995;
|
|
70
72
|
var DMax = (...values) => {
|
|
71
73
|
if (values.length === 0) {
|
|
72
74
|
throw new Error("DMax requires at least one argument");
|
|
@@ -302,7 +304,8 @@ function liquidationPriceIsolated(inputs) {
|
|
|
302
304
|
referencePrice,
|
|
303
305
|
orderSide,
|
|
304
306
|
orderQty = 0,
|
|
305
|
-
leverage
|
|
307
|
+
leverage,
|
|
308
|
+
isoTakerFeeBuffer = IsoTakerFeeBuffer
|
|
306
309
|
} = inputs;
|
|
307
310
|
const refPrice = referencePrice != null ? referencePrice : 0;
|
|
308
311
|
if (refPrice <= 0 && orderQty !== 0) {
|
|
@@ -315,12 +318,13 @@ function liquidationPriceIsolated(inputs) {
|
|
|
315
318
|
}
|
|
316
319
|
let newIsolatedPositionMargin;
|
|
317
320
|
let newCostPosition;
|
|
321
|
+
const getMarginRate = () => new utils.Decimal(1).div(leverage).add(isoTakerFeeBuffer);
|
|
318
322
|
if (orderQty === 0) {
|
|
319
323
|
newIsolatedPositionMargin = new utils.Decimal(isolatedPositionMargin);
|
|
320
324
|
newCostPosition = new utils.Decimal(costPosition);
|
|
321
325
|
} else if (positionQty === 0 || orderSideMultiplier > 0 && positionQty > 0 || orderSideMultiplier < 0 && positionQty < 0) {
|
|
322
326
|
newIsolatedPositionMargin = new utils.Decimal(isolatedPositionMargin).add(
|
|
323
|
-
new utils.Decimal(orderQty).mul(refPrice).
|
|
327
|
+
new utils.Decimal(orderQty).mul(refPrice).mul(getMarginRate())
|
|
324
328
|
);
|
|
325
329
|
newCostPosition = new utils.Decimal(costPosition).add(
|
|
326
330
|
new utils.Decimal(orderSideMultiplier).mul(orderQty).mul(refPrice)
|
|
@@ -334,7 +338,7 @@ function liquidationPriceIsolated(inputs) {
|
|
|
334
338
|
new utils.Decimal(orderSideMultiplier).mul(orderQty).mul(refPrice)
|
|
335
339
|
);
|
|
336
340
|
} else {
|
|
337
|
-
newIsolatedPositionMargin = new utils.Decimal(Math.abs(newPositionQty)).mul(refPrice).
|
|
341
|
+
newIsolatedPositionMargin = new utils.Decimal(Math.abs(newPositionQty)).mul(refPrice).mul(getMarginRate());
|
|
338
342
|
newCostPosition = new utils.Decimal(newPositionQty).mul(refPrice);
|
|
339
343
|
}
|
|
340
344
|
}
|
|
@@ -378,6 +382,7 @@ __export(account_exports, {
|
|
|
378
382
|
getQtyFromOrdersBySide: () => getQtyFromOrdersBySide,
|
|
379
383
|
getQtyFromPositions: () => getQtyFromPositions,
|
|
380
384
|
groupOrdersBySymbol: () => groupOrdersBySymbol,
|
|
385
|
+
isolatedMarginRate: () => isolatedMarginRate,
|
|
381
386
|
maxAdd: () => maxAdd,
|
|
382
387
|
maxQty: () => maxQty,
|
|
383
388
|
maxQtyByLong: () => maxQtyByLong,
|
|
@@ -766,6 +771,10 @@ function maxQtyByShort(inputs, options) {
|
|
|
766
771
|
return 0;
|
|
767
772
|
}
|
|
768
773
|
}
|
|
774
|
+
function isolatedMarginRate(inputs) {
|
|
775
|
+
var _a;
|
|
776
|
+
return new utils.Decimal(1).div(inputs.leverage).add((_a = inputs.isoTakerFeeBuffer) != null ? _a : IsoTakerFeeBuffer);
|
|
777
|
+
}
|
|
769
778
|
function maxQtyForIsolatedMargin(inputs) {
|
|
770
779
|
const {
|
|
771
780
|
orderSide,
|
|
@@ -778,8 +787,10 @@ function maxQtyForIsolatedMargin(inputs) {
|
|
|
778
787
|
pendingLongOrders,
|
|
779
788
|
pendingSellOrders,
|
|
780
789
|
symbolMaxNotional,
|
|
781
|
-
epsilon = 1
|
|
790
|
+
epsilon = 1,
|
|
791
|
+
isoTakerFeeBuffer = IsoTakerFeeBuffer
|
|
782
792
|
} = inputs;
|
|
793
|
+
const marginRate = isolatedMarginRate({ leverage, isoTakerFeeBuffer });
|
|
783
794
|
const maxNotional = Math.min(
|
|
784
795
|
new utils.Decimal(1).div(new utils.Decimal(leverage).mul(IMR_Factor)).pow(5 / 4).toNumber(),
|
|
785
796
|
symbolMaxNotional
|
|
@@ -790,7 +801,7 @@ function maxQtyForIsolatedMargin(inputs) {
|
|
|
790
801
|
(acc, order) => acc + new utils.Decimal(order.referencePrice).mul(order.quantity).toNumber(),
|
|
791
802
|
0
|
|
792
803
|
);
|
|
793
|
-
const maxQtyByBalance = new utils.Decimal(availableBalance2).div(currentOrderReferencePrice).mul(
|
|
804
|
+
const maxQtyByBalance = new utils.Decimal(availableBalance2).div(marginRate).div(currentOrderReferencePrice).mul(MaxQtySafetyFactor).toNumber();
|
|
794
805
|
const maxQtyByNotional = new utils.Decimal(maxNotional).sub(new utils.Decimal(markPrice).mul(positionQty)).sub(pendingLongNotional).div(currentOrderReferencePrice).toNumber();
|
|
795
806
|
return Math.max(0, Math.min(maxQtyByBalance, maxQtyByNotional));
|
|
796
807
|
} else {
|
|
@@ -805,7 +816,8 @@ function maxQtyForIsolatedMargin(inputs) {
|
|
|
805
816
|
pendingLongOrders,
|
|
806
817
|
pendingSellOrders,
|
|
807
818
|
isoOrderFrozenLong: inputs.isoOrderFrozenLong,
|
|
808
|
-
isoOrderFrozenShort: inputs.isoOrderFrozenShort
|
|
819
|
+
isoOrderFrozenShort: inputs.isoOrderFrozenShort,
|
|
820
|
+
isoTakerFeeBuffer
|
|
809
821
|
},
|
|
810
822
|
maxNotional,
|
|
811
823
|
epsilon,
|
|
@@ -818,7 +830,7 @@ function maxQtyForIsolatedMargin(inputs) {
|
|
|
818
830
|
(acc, order) => acc + new utils.Decimal(order.referencePrice).mul(order.quantity).toNumber(),
|
|
819
831
|
0
|
|
820
832
|
);
|
|
821
|
-
const maxQtyByBalance = new utils.Decimal(availableBalance2).div(currentOrderReferencePrice).mul(
|
|
833
|
+
const maxQtyByBalance = new utils.Decimal(availableBalance2).div(marginRate).div(currentOrderReferencePrice).mul(MaxQtySafetyFactor).toNumber();
|
|
822
834
|
const maxQtyByNotional = new utils.Decimal(maxNotional).sub(new utils.Decimal(markPrice).mul(Math.abs(positionQty))).sub(pendingSellNotional).div(currentOrderReferencePrice).toNumber();
|
|
823
835
|
return Math.max(0, Math.min(maxQtyByBalance, maxQtyByNotional));
|
|
824
836
|
} else {
|
|
@@ -833,7 +845,8 @@ function maxQtyForIsolatedMargin(inputs) {
|
|
|
833
845
|
pendingLongOrders,
|
|
834
846
|
pendingSellOrders,
|
|
835
847
|
isoOrderFrozenLong: inputs.isoOrderFrozenLong,
|
|
836
|
-
isoOrderFrozenShort: inputs.isoOrderFrozenShort
|
|
848
|
+
isoOrderFrozenShort: inputs.isoOrderFrozenShort,
|
|
849
|
+
isoTakerFeeBuffer
|
|
837
850
|
},
|
|
838
851
|
maxNotional,
|
|
839
852
|
epsilon,
|
|
@@ -851,7 +864,8 @@ function maxQtyIsolatedBinarySearch(inputs, maxNotional, epsilon, orderSide) {
|
|
|
851
864
|
pendingLongOrders,
|
|
852
865
|
pendingSellOrders,
|
|
853
866
|
isoOrderFrozenLong,
|
|
854
|
-
isoOrderFrozenShort
|
|
867
|
+
isoOrderFrozenShort,
|
|
868
|
+
isoTakerFeeBuffer
|
|
855
869
|
} = inputs;
|
|
856
870
|
inputs.baseIMR;
|
|
857
871
|
inputs.IMR_Factor;
|
|
@@ -860,18 +874,22 @@ function maxQtyIsolatedBinarySearch(inputs, maxNotional, epsilon, orderSide) {
|
|
|
860
874
|
let right = new utils.Decimal(maxNotional).div(currentOrderReferencePrice).add(Math.abs(positionQty)).toNumber();
|
|
861
875
|
for (let i = 0; i < 30; i++) {
|
|
862
876
|
const mid = (left + right) / 2;
|
|
863
|
-
const
|
|
864
|
-
const
|
|
865
|
-
const
|
|
877
|
+
const pendingOrders = orderSide === types.OrderSide.BUY ? pendingLongOrders : pendingSellOrders;
|
|
878
|
+
const existingFrozen = orderSide === types.OrderSide.BUY ? isoOrderFrozenLong : isoOrderFrozenShort;
|
|
879
|
+
const totalOrderNotional = pendingOrders.reduce(
|
|
880
|
+
(acc, order) => acc.add(new utils.Decimal(order.referencePrice).mul(order.quantity)),
|
|
881
|
+
new utils.Decimal(mid).mul(currentOrderReferencePrice)
|
|
882
|
+
);
|
|
883
|
+
const orderFrozen = totalOrderNotional.mul(isolatedMarginRate({ leverage, isoTakerFeeBuffer })).sub(existingFrozen);
|
|
866
884
|
const newPositionQty = orderSide === types.OrderSide.BUY ? positionQty + mid : positionQty - mid;
|
|
867
885
|
const openNotional = new utils.Decimal(Math.abs(newPositionQty)).mul(
|
|
868
886
|
currentOrderReferencePrice
|
|
869
887
|
);
|
|
870
|
-
const frozenOk =
|
|
888
|
+
const frozenOk = orderFrozen.lte(availableBalance2);
|
|
871
889
|
const notionalOk = openNotional.lte(maxNotional);
|
|
872
890
|
if (frozenOk && notionalOk) {
|
|
873
891
|
left = mid;
|
|
874
|
-
if (new utils.Decimal(availableBalance2).sub(
|
|
892
|
+
if (new utils.Decimal(availableBalance2).sub(orderFrozen).lte(epsilon)) {
|
|
875
893
|
break;
|
|
876
894
|
}
|
|
877
895
|
} else {
|
|
@@ -1108,6 +1126,7 @@ function estLiqPriceIsolated(inputs) {
|
|
|
1108
1126
|
baseIMR,
|
|
1109
1127
|
IMR_Factor: IMRFactor,
|
|
1110
1128
|
leverage,
|
|
1129
|
+
isoTakerFeeBuffer = IsoTakerFeeBuffer,
|
|
1111
1130
|
newOrder
|
|
1112
1131
|
} = inputs;
|
|
1113
1132
|
const signedOrderQty = (_a = newOrder == null ? void 0 : newOrder.qty) != null ? _a : 0;
|
|
@@ -1132,6 +1151,7 @@ function estLiqPriceIsolated(inputs) {
|
|
|
1132
1151
|
const decCostPosition = new utils.Decimal(costPosition);
|
|
1133
1152
|
const decIsolatedMargin = new utils.Decimal(isolatedPositionMargin);
|
|
1134
1153
|
const decOrderCost = new utils.Decimal(signedOrderQty).mul(orderRefPrice);
|
|
1154
|
+
const getMarginRate = () => new utils.Decimal(1).div(leverage).add(isoTakerFeeBuffer);
|
|
1135
1155
|
let newIsolatedPositionMargin;
|
|
1136
1156
|
let newCostPosition;
|
|
1137
1157
|
switch (getScenario()) {
|
|
@@ -1141,7 +1161,7 @@ function estLiqPriceIsolated(inputs) {
|
|
|
1141
1161
|
break;
|
|
1142
1162
|
case "OPEN_ADD":
|
|
1143
1163
|
newIsolatedPositionMargin = decIsolatedMargin.add(
|
|
1144
|
-
new utils.Decimal(Math.abs(signedOrderQty)).mul(orderRefPrice).
|
|
1164
|
+
new utils.Decimal(Math.abs(signedOrderQty)).mul(orderRefPrice).mul(getMarginRate())
|
|
1145
1165
|
);
|
|
1146
1166
|
newCostPosition = decCostPosition.add(decOrderCost);
|
|
1147
1167
|
break;
|
|
@@ -1150,7 +1170,7 @@ function estLiqPriceIsolated(inputs) {
|
|
|
1150
1170
|
newCostPosition = decCostPosition.add(decOrderCost);
|
|
1151
1171
|
break;
|
|
1152
1172
|
case "FLIP":
|
|
1153
|
-
newIsolatedPositionMargin = decAbsNewPositionQty.mul(orderRefPrice).
|
|
1173
|
+
newIsolatedPositionMargin = decAbsNewPositionQty.mul(orderRefPrice).mul(getMarginRate());
|
|
1154
1174
|
newCostPosition = decNewPositionQty.mul(orderRefPrice);
|
|
1155
1175
|
break;
|
|
1156
1176
|
}
|