@orderly.network/perp 4.6.3 → 4.7.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 +36 -3
- package/dist/index.d.ts +36 -3
- package/dist/index.js +40 -15
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +40 -15
- 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: "4.
|
|
11
|
+
declare const _default: "4.7.0";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Calculates the notional value of a single position.
|
|
@@ -154,6 +154,22 @@ declare function estPnLForSL(inputs: {
|
|
|
154
154
|
positionQty: number;
|
|
155
155
|
entryPrice: number;
|
|
156
156
|
}): number;
|
|
157
|
+
/**
|
|
158
|
+
* calculate the max position notional
|
|
159
|
+
* max_notional = ( (1/ (leverage * imr_factor) ) ^ (1/0.8)
|
|
160
|
+
*/
|
|
161
|
+
declare function maxPositionNotional(inputs: {
|
|
162
|
+
/** symbol leverage */
|
|
163
|
+
leverage: number;
|
|
164
|
+
IMRFactor: number;
|
|
165
|
+
}): number;
|
|
166
|
+
/**
|
|
167
|
+
* symbol_leverage_max = 1 / ( imr_factor * notional ^ 0.8 )
|
|
168
|
+
*/
|
|
169
|
+
declare function maxPositionLeverage(inputs: {
|
|
170
|
+
IMRFactor: number;
|
|
171
|
+
notional: number;
|
|
172
|
+
}): number;
|
|
157
173
|
|
|
158
174
|
type positions_LiqPriceInputs = LiqPriceInputs;
|
|
159
175
|
type positions_MMInputs = MMInputs;
|
|
@@ -169,6 +185,8 @@ declare const positions_estPriceForTP: typeof estPriceForTP;
|
|
|
169
185
|
declare const positions_estPriceFromOffsetForTP: typeof estPriceFromOffsetForTP;
|
|
170
186
|
declare const positions_liqPrice: typeof liqPrice;
|
|
171
187
|
declare const positions_maintenanceMargin: typeof maintenanceMargin;
|
|
188
|
+
declare const positions_maxPositionLeverage: typeof maxPositionLeverage;
|
|
189
|
+
declare const positions_maxPositionNotional: typeof maxPositionNotional;
|
|
172
190
|
declare const positions_notional: typeof notional;
|
|
173
191
|
declare const positions_totalNotional: typeof totalNotional;
|
|
174
192
|
declare const positions_totalUnrealizedPnL: typeof totalUnrealizedPnL;
|
|
@@ -177,7 +195,7 @@ declare const positions_unrealizedPnL: typeof unrealizedPnL;
|
|
|
177
195
|
declare const positions_unrealizedPnLROI: typeof unrealizedPnLROI;
|
|
178
196
|
declare const positions_unsettlementPnL: typeof unsettlementPnL;
|
|
179
197
|
declare namespace positions {
|
|
180
|
-
export { type positions_LiqPriceInputs as LiqPriceInputs, type positions_MMInputs as MMInputs, MMR$1 as MMR, type positions_MMRInputs as MMRInputs, type positions_TotalUnsettlementPnLInputs as TotalUnsettlementPnLInputs, type positions_UnrealPnLInputs as UnrealPnLInputs, type positions_UnrealPnLROIInputs as UnrealPnLROIInputs, type positions_UnsettlementPnLInputs as UnsettlementPnLInputs, positions_estOffsetForTP as estOffsetForTP, positions_estPnLForSL as estPnLForSL, positions_estPnLForTP as estPnLForTP, positions_estPriceForTP as estPriceForTP, positions_estPriceFromOffsetForTP as estPriceFromOffsetForTP, positions_liqPrice as liqPrice, positions_maintenanceMargin as maintenanceMargin, positions_notional as notional, positions_totalNotional as totalNotional, positions_totalUnrealizedPnL as totalUnrealizedPnL, positions_totalUnsettlementPnL as totalUnsettlementPnL, positions_unrealizedPnL as unrealizedPnL, positions_unrealizedPnLROI as unrealizedPnLROI, positions_unsettlementPnL as unsettlementPnL };
|
|
198
|
+
export { type positions_LiqPriceInputs as LiqPriceInputs, type positions_MMInputs as MMInputs, MMR$1 as MMR, type positions_MMRInputs as MMRInputs, type positions_TotalUnsettlementPnLInputs as TotalUnsettlementPnLInputs, type positions_UnrealPnLInputs as UnrealPnLInputs, type positions_UnrealPnLROIInputs as UnrealPnLROIInputs, type positions_UnsettlementPnLInputs as UnsettlementPnLInputs, positions_estOffsetForTP as estOffsetForTP, positions_estPnLForSL as estPnLForSL, positions_estPnLForTP as estPnLForTP, positions_estPriceForTP as estPriceForTP, positions_estPriceFromOffsetForTP as estPriceFromOffsetForTP, positions_liqPrice as liqPrice, positions_maintenanceMargin as maintenanceMargin, positions_maxPositionLeverage as maxPositionLeverage, positions_maxPositionNotional as maxPositionNotional, positions_notional as notional, positions_totalNotional as totalNotional, positions_totalUnrealizedPnL as totalUnrealizedPnL, positions_totalUnsettlementPnL as totalUnsettlementPnL, positions_unrealizedPnL as unrealizedPnL, positions_unrealizedPnLROI as unrealizedPnLROI, positions_unsettlementPnL as unsettlementPnL };
|
|
181
199
|
}
|
|
182
200
|
|
|
183
201
|
type ResultOptions = {
|
|
@@ -239,6 +257,9 @@ type PositionQtyWithOrderInputs = {
|
|
|
239
257
|
*/
|
|
240
258
|
declare function positionQtyWithOrders_by_symbol(inputs: PositionQtyWithOrderInputs): number;
|
|
241
259
|
type IMRInputs = {
|
|
260
|
+
/**
|
|
261
|
+
* effective max leverage
|
|
262
|
+
*/
|
|
242
263
|
maxLeverage: number;
|
|
243
264
|
baseIMR: number;
|
|
244
265
|
IMR_Factor: number;
|
|
@@ -279,6 +300,7 @@ type TotalInitialMarginWithOrdersInputs = {
|
|
|
279
300
|
};
|
|
280
301
|
} & Pick<IMRInputs, "maxLeverage">;
|
|
281
302
|
/**
|
|
303
|
+
* @deprecated
|
|
282
304
|
* Calculate the total initial margin used by the user (including positions and orders).
|
|
283
305
|
*/
|
|
284
306
|
declare function totalInitialMarginWithOrders(inputs: TotalInitialMarginWithOrdersInputs): number;
|
|
@@ -291,6 +313,9 @@ declare function totalInitialMarginWithQty(inputs: {
|
|
|
291
313
|
IMR_Factors: {
|
|
292
314
|
[key: string]: number;
|
|
293
315
|
};
|
|
316
|
+
/**
|
|
317
|
+
* account max leverage
|
|
318
|
+
*/
|
|
294
319
|
maxLeverage: number;
|
|
295
320
|
}): number;
|
|
296
321
|
/**
|
|
@@ -311,6 +336,9 @@ type OtherIMsInputs = {
|
|
|
311
336
|
markPrices: {
|
|
312
337
|
[key: string]: number;
|
|
313
338
|
};
|
|
339
|
+
/**
|
|
340
|
+
* account max leverage
|
|
341
|
+
*/
|
|
314
342
|
maxLeverage: number;
|
|
315
343
|
symbolInfo: any;
|
|
316
344
|
IMR_Factors: {
|
|
@@ -429,6 +457,10 @@ declare const calcMinimumReceived: (inputs: {
|
|
|
429
457
|
amount: number;
|
|
430
458
|
slippage: number;
|
|
431
459
|
}) => number;
|
|
460
|
+
declare const maxLeverage: (inputs: {
|
|
461
|
+
symbolLeverage?: number;
|
|
462
|
+
accountLeverage: number;
|
|
463
|
+
}) => number;
|
|
432
464
|
|
|
433
465
|
type account_AccountMMRInputs = AccountMMRInputs;
|
|
434
466
|
type account_AvailableBalanceInputs = AvailableBalanceInputs;
|
|
@@ -460,6 +492,7 @@ declare const account_getQtyFromOrdersBySide: typeof getQtyFromOrdersBySide;
|
|
|
460
492
|
declare const account_getQtyFromPositions: typeof getQtyFromPositions;
|
|
461
493
|
declare const account_groupOrdersBySymbol: typeof groupOrdersBySymbol;
|
|
462
494
|
declare const account_initialMarginWithOrder: typeof initialMarginWithOrder;
|
|
495
|
+
declare const account_maxLeverage: typeof maxLeverage;
|
|
463
496
|
declare const account_maxQty: typeof maxQty;
|
|
464
497
|
declare const account_maxQtyByLong: typeof maxQtyByLong;
|
|
465
498
|
declare const account_maxQtyByShort: typeof maxQtyByShort;
|
|
@@ -476,7 +509,7 @@ declare const account_totalMarginRatio: typeof totalMarginRatio;
|
|
|
476
509
|
declare const account_totalUnrealizedROI: typeof totalUnrealizedROI;
|
|
477
510
|
declare const account_totalValue: typeof totalValue;
|
|
478
511
|
declare namespace account {
|
|
479
|
-
export { type account_AccountMMRInputs as AccountMMRInputs, type account_AvailableBalanceInputs as AvailableBalanceInputs, type account_FreeCollateralInputs as FreeCollateralInputs, account_IMR as IMR, type account_IMRInputs as IMRInputs, account_LTV as LTV, account_MMR as MMR, type account_MaxQtyInputs as MaxQtyInputs, type account_OtherIMsInputs as OtherIMsInputs, type account_PositionNotionalWithOrderInputs as PositionNotionalWithOrderInputs, type account_PositionQtyWithOrderInputs as PositionQtyWithOrderInputs, type account_ResultOptions as ResultOptions, type account_TotalCollateralValueInputs as TotalCollateralValueInputs, type account_TotalInitialMarginWithOrdersInputs as TotalInitialMarginWithOrdersInputs, type account_TotalMarginRatioInputs as TotalMarginRatioInputs, type account_TotalUnrealizedROIInputs as TotalUnrealizedROIInputs, type account_TotalValueInputs as TotalValueInputs, account_availableBalance as availableBalance, account_buyOrdersFilter_by_symbol as buyOrdersFilter_by_symbol, account_calcMinimumReceived as calcMinimumReceived, account_collateralContribution as collateralContribution, account_collateralRatio as collateralRatio, account_currentLeverage as currentLeverage, account_extractSymbols as extractSymbols, account_freeCollateral as freeCollateral, account_getPositonsAndOrdersNotionalBySymbol as getPositonsAndOrdersNotionalBySymbol, account_getQtyFromOrdersBySide as getQtyFromOrdersBySide, account_getQtyFromPositions as getQtyFromPositions, account_groupOrdersBySymbol as groupOrdersBySymbol, account_initialMarginWithOrder as initialMarginWithOrder, account_maxQty as maxQty, account_maxQtyByLong as maxQtyByLong, account_maxQtyByShort as maxQtyByShort, account_maxWithdrawalOtherCollateral as maxWithdrawalOtherCollateral, account_maxWithdrawalUSDC as maxWithdrawalUSDC, account_otherIMs as otherIMs, account_positionNotionalWithOrder_by_symbol as positionNotionalWithOrder_by_symbol, account_positionQtyWithOrders_by_symbol as positionQtyWithOrders_by_symbol, account_sellOrdersFilter_by_symbol as sellOrdersFilter_by_symbol, account_totalCollateral as totalCollateral, account_totalInitialMarginWithOrders as totalInitialMarginWithOrders, account_totalInitialMarginWithQty as totalInitialMarginWithQty, account_totalMarginRatio as totalMarginRatio, account_totalUnrealizedROI as totalUnrealizedROI, account_totalValue as totalValue };
|
|
512
|
+
export { type account_AccountMMRInputs as AccountMMRInputs, type account_AvailableBalanceInputs as AvailableBalanceInputs, type account_FreeCollateralInputs as FreeCollateralInputs, account_IMR as IMR, type account_IMRInputs as IMRInputs, account_LTV as LTV, account_MMR as MMR, type account_MaxQtyInputs as MaxQtyInputs, type account_OtherIMsInputs as OtherIMsInputs, type account_PositionNotionalWithOrderInputs as PositionNotionalWithOrderInputs, type account_PositionQtyWithOrderInputs as PositionQtyWithOrderInputs, type account_ResultOptions as ResultOptions, type account_TotalCollateralValueInputs as TotalCollateralValueInputs, type account_TotalInitialMarginWithOrdersInputs as TotalInitialMarginWithOrdersInputs, type account_TotalMarginRatioInputs as TotalMarginRatioInputs, type account_TotalUnrealizedROIInputs as TotalUnrealizedROIInputs, type account_TotalValueInputs as TotalValueInputs, account_availableBalance as availableBalance, account_buyOrdersFilter_by_symbol as buyOrdersFilter_by_symbol, account_calcMinimumReceived as calcMinimumReceived, account_collateralContribution as collateralContribution, account_collateralRatio as collateralRatio, account_currentLeverage as currentLeverage, account_extractSymbols as extractSymbols, account_freeCollateral as freeCollateral, account_getPositonsAndOrdersNotionalBySymbol as getPositonsAndOrdersNotionalBySymbol, account_getQtyFromOrdersBySide as getQtyFromOrdersBySide, account_getQtyFromPositions as getQtyFromPositions, account_groupOrdersBySymbol as groupOrdersBySymbol, account_initialMarginWithOrder as initialMarginWithOrder, account_maxLeverage as maxLeverage, account_maxQty as maxQty, account_maxQtyByLong as maxQtyByLong, account_maxQtyByShort as maxQtyByShort, account_maxWithdrawalOtherCollateral as maxWithdrawalOtherCollateral, account_maxWithdrawalUSDC as maxWithdrawalUSDC, account_otherIMs as otherIMs, account_positionNotionalWithOrder_by_symbol as positionNotionalWithOrder_by_symbol, account_positionQtyWithOrders_by_symbol as positionQtyWithOrders_by_symbol, account_sellOrdersFilter_by_symbol as sellOrdersFilter_by_symbol, account_totalCollateral as totalCollateral, account_totalInitialMarginWithOrders as totalInitialMarginWithOrders, account_totalInitialMarginWithQty as totalInitialMarginWithQty, account_totalMarginRatio as totalMarginRatio, account_totalUnrealizedROI as totalUnrealizedROI, account_totalValue as totalValue };
|
|
480
513
|
}
|
|
481
514
|
|
|
482
515
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ declare global {
|
|
|
8
8
|
};
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
|
-
declare const _default: "4.
|
|
11
|
+
declare const _default: "4.7.0";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Calculates the notional value of a single position.
|
|
@@ -154,6 +154,22 @@ declare function estPnLForSL(inputs: {
|
|
|
154
154
|
positionQty: number;
|
|
155
155
|
entryPrice: number;
|
|
156
156
|
}): number;
|
|
157
|
+
/**
|
|
158
|
+
* calculate the max position notional
|
|
159
|
+
* max_notional = ( (1/ (leverage * imr_factor) ) ^ (1/0.8)
|
|
160
|
+
*/
|
|
161
|
+
declare function maxPositionNotional(inputs: {
|
|
162
|
+
/** symbol leverage */
|
|
163
|
+
leverage: number;
|
|
164
|
+
IMRFactor: number;
|
|
165
|
+
}): number;
|
|
166
|
+
/**
|
|
167
|
+
* symbol_leverage_max = 1 / ( imr_factor * notional ^ 0.8 )
|
|
168
|
+
*/
|
|
169
|
+
declare function maxPositionLeverage(inputs: {
|
|
170
|
+
IMRFactor: number;
|
|
171
|
+
notional: number;
|
|
172
|
+
}): number;
|
|
157
173
|
|
|
158
174
|
type positions_LiqPriceInputs = LiqPriceInputs;
|
|
159
175
|
type positions_MMInputs = MMInputs;
|
|
@@ -169,6 +185,8 @@ declare const positions_estPriceForTP: typeof estPriceForTP;
|
|
|
169
185
|
declare const positions_estPriceFromOffsetForTP: typeof estPriceFromOffsetForTP;
|
|
170
186
|
declare const positions_liqPrice: typeof liqPrice;
|
|
171
187
|
declare const positions_maintenanceMargin: typeof maintenanceMargin;
|
|
188
|
+
declare const positions_maxPositionLeverage: typeof maxPositionLeverage;
|
|
189
|
+
declare const positions_maxPositionNotional: typeof maxPositionNotional;
|
|
172
190
|
declare const positions_notional: typeof notional;
|
|
173
191
|
declare const positions_totalNotional: typeof totalNotional;
|
|
174
192
|
declare const positions_totalUnrealizedPnL: typeof totalUnrealizedPnL;
|
|
@@ -177,7 +195,7 @@ declare const positions_unrealizedPnL: typeof unrealizedPnL;
|
|
|
177
195
|
declare const positions_unrealizedPnLROI: typeof unrealizedPnLROI;
|
|
178
196
|
declare const positions_unsettlementPnL: typeof unsettlementPnL;
|
|
179
197
|
declare namespace positions {
|
|
180
|
-
export { type positions_LiqPriceInputs as LiqPriceInputs, type positions_MMInputs as MMInputs, MMR$1 as MMR, type positions_MMRInputs as MMRInputs, type positions_TotalUnsettlementPnLInputs as TotalUnsettlementPnLInputs, type positions_UnrealPnLInputs as UnrealPnLInputs, type positions_UnrealPnLROIInputs as UnrealPnLROIInputs, type positions_UnsettlementPnLInputs as UnsettlementPnLInputs, positions_estOffsetForTP as estOffsetForTP, positions_estPnLForSL as estPnLForSL, positions_estPnLForTP as estPnLForTP, positions_estPriceForTP as estPriceForTP, positions_estPriceFromOffsetForTP as estPriceFromOffsetForTP, positions_liqPrice as liqPrice, positions_maintenanceMargin as maintenanceMargin, positions_notional as notional, positions_totalNotional as totalNotional, positions_totalUnrealizedPnL as totalUnrealizedPnL, positions_totalUnsettlementPnL as totalUnsettlementPnL, positions_unrealizedPnL as unrealizedPnL, positions_unrealizedPnLROI as unrealizedPnLROI, positions_unsettlementPnL as unsettlementPnL };
|
|
198
|
+
export { type positions_LiqPriceInputs as LiqPriceInputs, type positions_MMInputs as MMInputs, MMR$1 as MMR, type positions_MMRInputs as MMRInputs, type positions_TotalUnsettlementPnLInputs as TotalUnsettlementPnLInputs, type positions_UnrealPnLInputs as UnrealPnLInputs, type positions_UnrealPnLROIInputs as UnrealPnLROIInputs, type positions_UnsettlementPnLInputs as UnsettlementPnLInputs, positions_estOffsetForTP as estOffsetForTP, positions_estPnLForSL as estPnLForSL, positions_estPnLForTP as estPnLForTP, positions_estPriceForTP as estPriceForTP, positions_estPriceFromOffsetForTP as estPriceFromOffsetForTP, positions_liqPrice as liqPrice, positions_maintenanceMargin as maintenanceMargin, positions_maxPositionLeverage as maxPositionLeverage, positions_maxPositionNotional as maxPositionNotional, positions_notional as notional, positions_totalNotional as totalNotional, positions_totalUnrealizedPnL as totalUnrealizedPnL, positions_totalUnsettlementPnL as totalUnsettlementPnL, positions_unrealizedPnL as unrealizedPnL, positions_unrealizedPnLROI as unrealizedPnLROI, positions_unsettlementPnL as unsettlementPnL };
|
|
181
199
|
}
|
|
182
200
|
|
|
183
201
|
type ResultOptions = {
|
|
@@ -239,6 +257,9 @@ type PositionQtyWithOrderInputs = {
|
|
|
239
257
|
*/
|
|
240
258
|
declare function positionQtyWithOrders_by_symbol(inputs: PositionQtyWithOrderInputs): number;
|
|
241
259
|
type IMRInputs = {
|
|
260
|
+
/**
|
|
261
|
+
* effective max leverage
|
|
262
|
+
*/
|
|
242
263
|
maxLeverage: number;
|
|
243
264
|
baseIMR: number;
|
|
244
265
|
IMR_Factor: number;
|
|
@@ -279,6 +300,7 @@ type TotalInitialMarginWithOrdersInputs = {
|
|
|
279
300
|
};
|
|
280
301
|
} & Pick<IMRInputs, "maxLeverage">;
|
|
281
302
|
/**
|
|
303
|
+
* @deprecated
|
|
282
304
|
* Calculate the total initial margin used by the user (including positions and orders).
|
|
283
305
|
*/
|
|
284
306
|
declare function totalInitialMarginWithOrders(inputs: TotalInitialMarginWithOrdersInputs): number;
|
|
@@ -291,6 +313,9 @@ declare function totalInitialMarginWithQty(inputs: {
|
|
|
291
313
|
IMR_Factors: {
|
|
292
314
|
[key: string]: number;
|
|
293
315
|
};
|
|
316
|
+
/**
|
|
317
|
+
* account max leverage
|
|
318
|
+
*/
|
|
294
319
|
maxLeverage: number;
|
|
295
320
|
}): number;
|
|
296
321
|
/**
|
|
@@ -311,6 +336,9 @@ type OtherIMsInputs = {
|
|
|
311
336
|
markPrices: {
|
|
312
337
|
[key: string]: number;
|
|
313
338
|
};
|
|
339
|
+
/**
|
|
340
|
+
* account max leverage
|
|
341
|
+
*/
|
|
314
342
|
maxLeverage: number;
|
|
315
343
|
symbolInfo: any;
|
|
316
344
|
IMR_Factors: {
|
|
@@ -429,6 +457,10 @@ declare const calcMinimumReceived: (inputs: {
|
|
|
429
457
|
amount: number;
|
|
430
458
|
slippage: number;
|
|
431
459
|
}) => number;
|
|
460
|
+
declare const maxLeverage: (inputs: {
|
|
461
|
+
symbolLeverage?: number;
|
|
462
|
+
accountLeverage: number;
|
|
463
|
+
}) => number;
|
|
432
464
|
|
|
433
465
|
type account_AccountMMRInputs = AccountMMRInputs;
|
|
434
466
|
type account_AvailableBalanceInputs = AvailableBalanceInputs;
|
|
@@ -460,6 +492,7 @@ declare const account_getQtyFromOrdersBySide: typeof getQtyFromOrdersBySide;
|
|
|
460
492
|
declare const account_getQtyFromPositions: typeof getQtyFromPositions;
|
|
461
493
|
declare const account_groupOrdersBySymbol: typeof groupOrdersBySymbol;
|
|
462
494
|
declare const account_initialMarginWithOrder: typeof initialMarginWithOrder;
|
|
495
|
+
declare const account_maxLeverage: typeof maxLeverage;
|
|
463
496
|
declare const account_maxQty: typeof maxQty;
|
|
464
497
|
declare const account_maxQtyByLong: typeof maxQtyByLong;
|
|
465
498
|
declare const account_maxQtyByShort: typeof maxQtyByShort;
|
|
@@ -476,7 +509,7 @@ declare const account_totalMarginRatio: typeof totalMarginRatio;
|
|
|
476
509
|
declare const account_totalUnrealizedROI: typeof totalUnrealizedROI;
|
|
477
510
|
declare const account_totalValue: typeof totalValue;
|
|
478
511
|
declare namespace account {
|
|
479
|
-
export { type account_AccountMMRInputs as AccountMMRInputs, type account_AvailableBalanceInputs as AvailableBalanceInputs, type account_FreeCollateralInputs as FreeCollateralInputs, account_IMR as IMR, type account_IMRInputs as IMRInputs, account_LTV as LTV, account_MMR as MMR, type account_MaxQtyInputs as MaxQtyInputs, type account_OtherIMsInputs as OtherIMsInputs, type account_PositionNotionalWithOrderInputs as PositionNotionalWithOrderInputs, type account_PositionQtyWithOrderInputs as PositionQtyWithOrderInputs, type account_ResultOptions as ResultOptions, type account_TotalCollateralValueInputs as TotalCollateralValueInputs, type account_TotalInitialMarginWithOrdersInputs as TotalInitialMarginWithOrdersInputs, type account_TotalMarginRatioInputs as TotalMarginRatioInputs, type account_TotalUnrealizedROIInputs as TotalUnrealizedROIInputs, type account_TotalValueInputs as TotalValueInputs, account_availableBalance as availableBalance, account_buyOrdersFilter_by_symbol as buyOrdersFilter_by_symbol, account_calcMinimumReceived as calcMinimumReceived, account_collateralContribution as collateralContribution, account_collateralRatio as collateralRatio, account_currentLeverage as currentLeverage, account_extractSymbols as extractSymbols, account_freeCollateral as freeCollateral, account_getPositonsAndOrdersNotionalBySymbol as getPositonsAndOrdersNotionalBySymbol, account_getQtyFromOrdersBySide as getQtyFromOrdersBySide, account_getQtyFromPositions as getQtyFromPositions, account_groupOrdersBySymbol as groupOrdersBySymbol, account_initialMarginWithOrder as initialMarginWithOrder, account_maxQty as maxQty, account_maxQtyByLong as maxQtyByLong, account_maxQtyByShort as maxQtyByShort, account_maxWithdrawalOtherCollateral as maxWithdrawalOtherCollateral, account_maxWithdrawalUSDC as maxWithdrawalUSDC, account_otherIMs as otherIMs, account_positionNotionalWithOrder_by_symbol as positionNotionalWithOrder_by_symbol, account_positionQtyWithOrders_by_symbol as positionQtyWithOrders_by_symbol, account_sellOrdersFilter_by_symbol as sellOrdersFilter_by_symbol, account_totalCollateral as totalCollateral, account_totalInitialMarginWithOrders as totalInitialMarginWithOrders, account_totalInitialMarginWithQty as totalInitialMarginWithQty, account_totalMarginRatio as totalMarginRatio, account_totalUnrealizedROI as totalUnrealizedROI, account_totalValue as totalValue };
|
|
512
|
+
export { type account_AccountMMRInputs as AccountMMRInputs, type account_AvailableBalanceInputs as AvailableBalanceInputs, type account_FreeCollateralInputs as FreeCollateralInputs, account_IMR as IMR, type account_IMRInputs as IMRInputs, account_LTV as LTV, account_MMR as MMR, type account_MaxQtyInputs as MaxQtyInputs, type account_OtherIMsInputs as OtherIMsInputs, type account_PositionNotionalWithOrderInputs as PositionNotionalWithOrderInputs, type account_PositionQtyWithOrderInputs as PositionQtyWithOrderInputs, type account_ResultOptions as ResultOptions, type account_TotalCollateralValueInputs as TotalCollateralValueInputs, type account_TotalInitialMarginWithOrdersInputs as TotalInitialMarginWithOrdersInputs, type account_TotalMarginRatioInputs as TotalMarginRatioInputs, type account_TotalUnrealizedROIInputs as TotalUnrealizedROIInputs, type account_TotalValueInputs as TotalValueInputs, account_availableBalance as availableBalance, account_buyOrdersFilter_by_symbol as buyOrdersFilter_by_symbol, account_calcMinimumReceived as calcMinimumReceived, account_collateralContribution as collateralContribution, account_collateralRatio as collateralRatio, account_currentLeverage as currentLeverage, account_extractSymbols as extractSymbols, account_freeCollateral as freeCollateral, account_getPositonsAndOrdersNotionalBySymbol as getPositonsAndOrdersNotionalBySymbol, account_getQtyFromOrdersBySide as getQtyFromOrdersBySide, account_getQtyFromPositions as getQtyFromPositions, account_groupOrdersBySymbol as groupOrdersBySymbol, account_initialMarginWithOrder as initialMarginWithOrder, account_maxLeverage as maxLeverage, account_maxQty as maxQty, account_maxQtyByLong as maxQtyByLong, account_maxQtyByShort as maxQtyByShort, account_maxWithdrawalOtherCollateral as maxWithdrawalOtherCollateral, account_maxWithdrawalUSDC as maxWithdrawalUSDC, account_otherIMs as otherIMs, account_positionNotionalWithOrder_by_symbol as positionNotionalWithOrder_by_symbol, account_positionQtyWithOrders_by_symbol as positionQtyWithOrders_by_symbol, account_sellOrdersFilter_by_symbol as sellOrdersFilter_by_symbol, account_totalCollateral as totalCollateral, account_totalInitialMarginWithOrders as totalInitialMarginWithOrders, account_totalInitialMarginWithQty as totalInitialMarginWithQty, account_totalMarginRatio as totalMarginRatio, account_totalUnrealizedROI as totalUnrealizedROI, account_totalValue as totalValue };
|
|
480
513
|
}
|
|
481
514
|
|
|
482
515
|
/**
|
package/dist/index.js
CHANGED
|
@@ -31,9 +31,9 @@ module.exports = __toCommonJS(src_exports);
|
|
|
31
31
|
// src/version.ts
|
|
32
32
|
if (typeof window !== "undefined") {
|
|
33
33
|
window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};
|
|
34
|
-
window.__ORDERLY_VERSION__["@orderly.network/perp"] = "4.
|
|
34
|
+
window.__ORDERLY_VERSION__["@orderly.network/perp"] = "4.7.0";
|
|
35
35
|
}
|
|
36
|
-
var version_default = "4.
|
|
36
|
+
var version_default = "4.7.0";
|
|
37
37
|
|
|
38
38
|
// src/positions.ts
|
|
39
39
|
var positions_exports = {};
|
|
@@ -46,6 +46,8 @@ __export(positions_exports, {
|
|
|
46
46
|
estPriceFromOffsetForTP: () => estPriceFromOffsetForTP,
|
|
47
47
|
liqPrice: () => liqPrice,
|
|
48
48
|
maintenanceMargin: () => maintenanceMargin,
|
|
49
|
+
maxPositionLeverage: () => maxPositionLeverage,
|
|
50
|
+
maxPositionNotional: () => maxPositionNotional,
|
|
49
51
|
notional: () => notional,
|
|
50
52
|
totalNotional: () => totalNotional,
|
|
51
53
|
totalUnrealizedPnL: () => totalUnrealizedPnL,
|
|
@@ -160,6 +162,14 @@ function estPriceFromOffsetForTP(inputs) {
|
|
|
160
162
|
function estPnLForSL(inputs) {
|
|
161
163
|
return 0;
|
|
162
164
|
}
|
|
165
|
+
function maxPositionNotional(inputs) {
|
|
166
|
+
const { leverage, IMRFactor } = inputs;
|
|
167
|
+
return new import_utils.Decimal(1).div(new import_utils.Decimal(leverage).mul(IMRFactor)).pow(1 / 0.8).toNumber();
|
|
168
|
+
}
|
|
169
|
+
function maxPositionLeverage(inputs) {
|
|
170
|
+
const { IMRFactor, notional: notional2 } = inputs;
|
|
171
|
+
return new import_utils.Decimal(1).div(new import_utils.Decimal(IMRFactor).mul(new import_utils.Decimal(notional2).pow(0.8))).toNumber();
|
|
172
|
+
}
|
|
163
173
|
|
|
164
174
|
// src/account.ts
|
|
165
175
|
var account_exports = {};
|
|
@@ -180,6 +190,7 @@ __export(account_exports, {
|
|
|
180
190
|
getQtyFromPositions: () => getQtyFromPositions,
|
|
181
191
|
groupOrdersBySymbol: () => groupOrdersBySymbol,
|
|
182
192
|
initialMarginWithOrder: () => initialMarginWithOrder,
|
|
193
|
+
maxLeverage: () => maxLeverage,
|
|
183
194
|
maxQty: () => maxQty,
|
|
184
195
|
maxQtyByLong: () => maxQtyByLong,
|
|
185
196
|
maxQtyByShort: () => maxQtyByShort,
|
|
@@ -234,7 +245,7 @@ function positionQtyWithOrders_by_symbol(inputs) {
|
|
|
234
245
|
}
|
|
235
246
|
function IMR(inputs) {
|
|
236
247
|
const {
|
|
237
|
-
maxLeverage,
|
|
248
|
+
maxLeverage: maxLeverage2,
|
|
238
249
|
baseIMR,
|
|
239
250
|
IMR_Factor,
|
|
240
251
|
positionNotional,
|
|
@@ -242,7 +253,7 @@ function IMR(inputs) {
|
|
|
242
253
|
IMR_factor_power = IMRFactorPower
|
|
243
254
|
} = inputs;
|
|
244
255
|
return Math.max(
|
|
245
|
-
1 /
|
|
256
|
+
1 / maxLeverage2,
|
|
246
257
|
baseIMR,
|
|
247
258
|
new import_utils2.Decimal(IMR_Factor).mul(
|
|
248
259
|
new import_utils2.Decimal(positionNotional).add(orderNotional).abs().toPower(IMR_factor_power)
|
|
@@ -286,7 +297,7 @@ function totalInitialMarginWithOrders(inputs) {
|
|
|
286
297
|
orders,
|
|
287
298
|
markPrices,
|
|
288
299
|
IMR_Factors,
|
|
289
|
-
maxLeverage,
|
|
300
|
+
maxLeverage: maxLeverage2,
|
|
290
301
|
symbolInfo
|
|
291
302
|
} = inputs;
|
|
292
303
|
const symbols = extractSymbols(positions, orders);
|
|
@@ -313,7 +324,7 @@ function totalInitialMarginWithOrders(inputs) {
|
|
|
313
324
|
const imr = IMR({
|
|
314
325
|
positionNotional: markPriceDecimal.mul(positionQty).toNumber(),
|
|
315
326
|
ordersNotional: markPriceDecimal.mul(new import_utils2.Decimal(buyOrdersQty).add(sellOrdersQty)).toNumber(),
|
|
316
|
-
maxLeverage,
|
|
327
|
+
maxLeverage: maxLeverage2,
|
|
317
328
|
IMR_Factor: IMR_Factors[symbol],
|
|
318
329
|
baseIMR: symbolInfo[symbol]("base_imr", 0)
|
|
319
330
|
});
|
|
@@ -322,9 +333,10 @@ function totalInitialMarginWithOrders(inputs) {
|
|
|
322
333
|
return total_initial_margin_with_orders;
|
|
323
334
|
}
|
|
324
335
|
function totalInitialMarginWithQty(inputs) {
|
|
325
|
-
const { positions, markPrices, IMR_Factors, symbolInfo
|
|
336
|
+
const { positions, markPrices, IMR_Factors, symbolInfo } = inputs;
|
|
326
337
|
const symbols = positions.map((item) => item.symbol);
|
|
327
338
|
const total_initial_margin_with_orders = symbols.reduce((acc, cur) => {
|
|
339
|
+
var _a;
|
|
328
340
|
const symbol = cur;
|
|
329
341
|
const position = positions.find((item) => item.symbol === symbol);
|
|
330
342
|
const positionQty = (position == null ? void 0 : position.position_qty) || 0;
|
|
@@ -344,7 +356,10 @@ function totalInitialMarginWithQty(inputs) {
|
|
|
344
356
|
const imr = IMR({
|
|
345
357
|
positionNotional: markPriceDecimal.mul(positionQty).toNumber(),
|
|
346
358
|
ordersNotional: markPriceDecimal.mul(new import_utils2.Decimal(buyOrdersQty).add(sellOrdersQty)).toNumber(),
|
|
347
|
-
maxLeverage
|
|
359
|
+
maxLeverage: maxLeverage({
|
|
360
|
+
symbolLeverage: (_a = position == null ? void 0 : position.leverage) != null ? _a : inputs.maxLeverage,
|
|
361
|
+
accountLeverage: inputs.maxLeverage
|
|
362
|
+
}),
|
|
348
363
|
IMR_Factor: IMR_Factors[symbol],
|
|
349
364
|
baseIMR: symbolInfo[symbol]("base_imr", 0)
|
|
350
365
|
});
|
|
@@ -376,7 +391,6 @@ function otherIMs(inputs) {
|
|
|
376
391
|
const {
|
|
377
392
|
// orders,
|
|
378
393
|
positions,
|
|
379
|
-
maxLeverage,
|
|
380
394
|
IMR_Factors,
|
|
381
395
|
symbolInfo,
|
|
382
396
|
markPrices
|
|
@@ -401,7 +415,10 @@ function otherIMs(inputs) {
|
|
|
401
415
|
return acc;
|
|
402
416
|
}
|
|
403
417
|
const imr = IMR({
|
|
404
|
-
maxLeverage
|
|
418
|
+
maxLeverage: maxLeverage({
|
|
419
|
+
symbolLeverage: position.leverage,
|
|
420
|
+
accountLeverage: inputs.maxLeverage
|
|
421
|
+
}),
|
|
405
422
|
IMR_Factor,
|
|
406
423
|
baseIMR: symbolInfo[symbol]("base_imr", 0),
|
|
407
424
|
positionNotional,
|
|
@@ -431,7 +448,7 @@ function maxQtyByLong(inputs, options) {
|
|
|
431
448
|
baseMaxQty,
|
|
432
449
|
totalCollateral: totalCollateral2,
|
|
433
450
|
otherIMs: otherIMs2,
|
|
434
|
-
maxLeverage,
|
|
451
|
+
maxLeverage: maxLeverage2,
|
|
435
452
|
baseIMR,
|
|
436
453
|
markPrice,
|
|
437
454
|
IMR_Factor,
|
|
@@ -444,7 +461,7 @@ function maxQtyByLong(inputs, options) {
|
|
|
444
461
|
}
|
|
445
462
|
const totalCollateralDecimal = new import_utils2.Decimal(totalCollateral2);
|
|
446
463
|
const factor_1 = totalCollateralDecimal.sub(otherIMs2).div(
|
|
447
|
-
new import_utils2.Decimal(takerFeeRate).mul(2).mul(1e-4).add(Math.max(1 /
|
|
464
|
+
new import_utils2.Decimal(takerFeeRate).mul(2).mul(1e-4).add(Math.max(1 / maxLeverage2, baseIMR))
|
|
448
465
|
).div(markPrice).mul(0.995).sub(new import_utils2.Decimal(positionQty).add(buyOrdersQty)).toNumber();
|
|
449
466
|
if (positionQty === 0 && buyOrdersQty === 0) {
|
|
450
467
|
return Math.min(baseMaxQty, factor_1);
|
|
@@ -465,7 +482,7 @@ function maxQtyByShort(inputs, options) {
|
|
|
465
482
|
baseMaxQty,
|
|
466
483
|
totalCollateral: totalCollateral2,
|
|
467
484
|
otherIMs: otherIMs2,
|
|
468
|
-
maxLeverage,
|
|
485
|
+
maxLeverage: maxLeverage2,
|
|
469
486
|
baseIMR,
|
|
470
487
|
markPrice,
|
|
471
488
|
IMR_Factor,
|
|
@@ -476,7 +493,7 @@ function maxQtyByShort(inputs, options) {
|
|
|
476
493
|
} = inputs;
|
|
477
494
|
const totalCollateralDecimal = new import_utils2.Decimal(totalCollateral2);
|
|
478
495
|
const factor_1 = totalCollateralDecimal.sub(otherIMs2).div(
|
|
479
|
-
new import_utils2.Decimal(takerFeeRate).mul(2).mul(1e-4).add(Math.max(1 /
|
|
496
|
+
new import_utils2.Decimal(takerFeeRate).mul(2).mul(1e-4).add(Math.max(1 / maxLeverage2, baseIMR))
|
|
480
497
|
).div(markPrice).mul(0.995).add(positionQty).sub(Math.abs(sellOrdersQty)).toNumber();
|
|
481
498
|
if (positionQty === 0 && sellOrdersQty === 0) {
|
|
482
499
|
return Math.min(baseMaxQty, factor_1);
|
|
@@ -587,6 +604,10 @@ var calcMinimumReceived = (inputs) => {
|
|
|
587
604
|
const slippageRatio = new import_utils2.Decimal(slippage).div(100);
|
|
588
605
|
return new import_utils2.Decimal(amount).mul(new import_utils2.Decimal(1).minus(slippageRatio)).toNumber();
|
|
589
606
|
};
|
|
607
|
+
var maxLeverage = (inputs) => {
|
|
608
|
+
const { symbolLeverage, accountLeverage } = inputs;
|
|
609
|
+
return Math.min(symbolLeverage != null ? symbolLeverage : accountLeverage, accountLeverage);
|
|
610
|
+
};
|
|
590
611
|
|
|
591
612
|
// src/order.ts
|
|
592
613
|
var order_exports = {};
|
|
@@ -659,8 +680,12 @@ function estLiqPrice(inputs) {
|
|
|
659
680
|
if (newQty.eq(0)) {
|
|
660
681
|
return 0;
|
|
661
682
|
}
|
|
683
|
+
const denominator = newQty.abs().mul(newMMR).sub(newQty);
|
|
684
|
+
if (denominator.eq(import_utils3.zero)) {
|
|
685
|
+
return 0;
|
|
686
|
+
}
|
|
662
687
|
const price = new import_utils3.Decimal(basePrice).add(
|
|
663
|
-
new import_utils3.Decimal(totalCollateral2).sub(newTotalMM).sub(orderFee2).div(
|
|
688
|
+
new import_utils3.Decimal(totalCollateral2).sub(newTotalMM).sub(orderFee2).div(denominator)
|
|
664
689
|
).toNumber();
|
|
665
690
|
return Math.max(0, price);
|
|
666
691
|
}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/version.ts","../src/positions.ts","../src/constants.ts","../src/account.ts","../src/order.ts"],"sourcesContent":["export { default as version } from \"./version\";\nexport * as positions from \"./positions\";\nexport * as account from \"./account\";\nexport * as orderUtils from \"./order\";\n\nexport * as order from \"./order\";\n","declare global {\n interface Window {\n __ORDERLY_VERSION__?: {\n [key: string]: string;\n };\n }\n}\nif (typeof window !== \"undefined\") {\n window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};\n window.__ORDERLY_VERSION__[\"@orderly.network/perp\"] = \"4.6.3\";\n}\n\nexport default \"4.6.3\";\n","import { API } from \"@orderly.network/types\";\nimport { Decimal, zero } from \"@orderly.network/utils\";\nimport { IMRFactorPower } from \"./constants\";\n\n/**\n * Calculates the notional value of a single position.\n * @param qty The quantity of the position.\n * @param mark_price The price of the position.\n * @returns The notional value of the position.\n */\nexport function notional(qty: number, mark_price: number): number {\n return new Decimal(qty).mul(mark_price).abs().toNumber();\n}\n\n/**\n * Calculates the total notional value of all positions.\n * @param positions The array of positions.\n * @returns The total notional value of all positions.\n */\nexport function totalNotional(positions: API.Position[]): number {\n return positions.reduce((acc, cur) => {\n return acc + notional(cur.position_qty, cur.mark_price);\n }, 0);\n}\n\nexport type UnrealPnLInputs = {\n markPrice: number;\n openPrice: number;\n qty: number;\n};\n\n/**\n * Calculates the unrealized profit or loss of a single position.\n * @param inputs The inputs for calculating the unrealized profit or loss.\n * @returns The unrealized profit or loss of the position.\n */\nexport function unrealizedPnL(inputs: UnrealPnLInputs): number {\n return new Decimal(inputs.qty)\n .mul(inputs.markPrice - inputs.openPrice)\n .toNumber();\n}\n\nexport type UnrealPnLROIInputs = {\n positionQty: number;\n openPrice: number;\n IMR: number;\n unrealizedPnL: number;\n};\n\n/**\n * Calculates the return on investment (ROI) of a single position's unrealized profit or loss.\n * @param inputs The inputs for calculating the ROI.\n * @returns The ROI of the position's unrealized profit or loss.\n */\nexport function unrealizedPnLROI(inputs: UnrealPnLROIInputs): number {\n const { openPrice, IMR } = inputs;\n\n if (\n inputs.unrealizedPnL === 0 ||\n inputs.positionQty === 0 ||\n openPrice === 0 ||\n IMR === 0\n )\n return 0;\n\n return new Decimal(inputs.unrealizedPnL)\n .div(new Decimal(Math.abs(inputs.positionQty)).mul(openPrice).mul(IMR))\n .toNumber();\n}\n\n/**\n * Calculates the total unrealized profit or loss of all positions.\n * @param positions The array of positions.\n * @returns The total unrealized profit or loss of all positions.\n */\nexport function totalUnrealizedPnL(positions: API.Position[]): number {\n return positions.reduce((acc, cur) => {\n return (\n acc +\n unrealizedPnL({\n qty: cur.position_qty,\n openPrice: cur.average_open_price,\n markPrice: cur.mark_price,\n })\n );\n }, 0);\n}\n\nexport type LiqPriceInputs = {\n markPrice: number;\n totalCollateral: number;\n positionQty: number;\n positions: Pick<API.PositionExt, \"position_qty\" | \"mark_price\" | \"mmr\">[];\n MMR: number;\n};\n\n/**\n * Calculates the liquidation price of a single position.\n * @param inputs The inputs for calculating the liquidation price.\n * @returns The liquidation price of the position.\n */\nexport function liqPrice(inputs: LiqPriceInputs): number | null {\n const { markPrice, totalCollateral, positions, positionQty, MMR } = inputs;\n\n // console.log(\"inputs\", inputs);\n\n if (positionQty === 0 || totalCollateral === 0) {\n return null;\n }\n\n // totalNotional of all poisitions\n const totalNotional = positions.reduce<Decimal>((acc, cur) => {\n return acc.add(\n new Decimal(notional(cur.position_qty, cur.mark_price)).mul(cur.mmr),\n );\n }, zero);\n\n return Math.max(\n new Decimal(markPrice)\n .add(\n new Decimal(totalCollateral)\n .sub(totalNotional)\n .div(new Decimal(positionQty).abs().mul(MMR).sub(positionQty)),\n )\n .toNumber(),\n 0,\n );\n}\n\nexport type MMInputs = {\n positionQty: number;\n markPrice: number;\n MMR: number;\n};\n\n/**\n * Calculates the maintenance margin of a position.\n * @param inputs The inputs for calculating the maintenance margin.\n * @returns The maintenance margin of the position.\n */\nexport function maintenanceMargin(inputs: MMInputs) {\n const { positionQty, markPrice, MMR } = inputs;\n\n return new Decimal(positionQty).mul(markPrice).mul(MMR).abs().toNumber();\n}\n\nexport type UnsettlementPnLInputs = {\n positionQty: number;\n markPrice: number;\n costPosition: number;\n sumUnitaryFunding: number;\n lastSumUnitaryFunding: number;\n};\n\n/**\n * Calculates the unrealized profit or loss of each position.\n * @param inputs The inputs for calculating the unrealized profit or loss.\n * @returns The unrealized profit or loss of each position.\n */\nexport function unsettlementPnL(inputs: UnsettlementPnLInputs): number {\n const {\n positionQty,\n markPrice,\n costPosition,\n sumUnitaryFunding,\n lastSumUnitaryFunding,\n } = inputs;\n\n const qty = new Decimal(positionQty);\n\n return qty\n .mul(markPrice)\n .sub(costPosition)\n .sub(qty.mul(new Decimal(sumUnitaryFunding).sub(lastSumUnitaryFunding)))\n .toNumber();\n}\n\nexport type TotalUnsettlementPnLInputs = {\n positions: (API.Position & {\n sum_unitary_funding: number;\n })[];\n sumUnitaryFunding: number;\n};\n\n/**\n * Calculates the total unrealized profit or loss of all positions.\n * @param positions The array of positions.\n * @returns The total unrealized profit or loss of all positions.\n */\nexport function totalUnsettlementPnL(\n positions: (API.Position & { sum_unitary_funding: number })[],\n): number {\n if (!Array.isArray(positions) || positions.length === 0) {\n return 0;\n }\n\n return positions.reduce((acc, cur) => {\n return (\n acc +\n unsettlementPnL({\n positionQty: cur.position_qty,\n markPrice: cur.mark_price,\n costPosition: cur.cost_position,\n sumUnitaryFunding: cur.sum_unitary_funding,\n lastSumUnitaryFunding: cur.last_sum_unitary_funding,\n })\n );\n }, 0);\n}\n\nexport type MMRInputs = {\n baseMMR: number;\n baseIMR: number;\n IMRFactor: number;\n positionNotional: number;\n IMR_factor_power: number;\n};\n\n/**\n * Calculates the maintenance margin requirement (MMR) of a position.\n * @param inputs The inputs for calculating the MMR.\n * @returns The MMR of the position.\n */\nexport function MMR(inputs: MMRInputs): number {\n const {\n baseMMR,\n baseIMR,\n IMRFactor,\n positionNotional,\n IMR_factor_power = IMRFactorPower,\n } = inputs;\n return Math.max(\n baseMMR,\n new Decimal(baseMMR)\n .div(baseIMR)\n .mul(IMRFactor)\n .mul(Math.pow(Math.abs(positionNotional), IMR_factor_power))\n // .toPower(IMR_factor_power)\n .toNumber(),\n );\n}\n\n/**\n * Calculates the profit or loss for take profit.\n * @returns The profit or loss for take profit.\n */\nexport function estPnLForTP(inputs: {\n positionQty: number;\n entryPrice: number;\n price: number;\n}): number {\n return new Decimal(inputs.positionQty)\n .mul(new Decimal(inputs.price).sub(inputs.entryPrice))\n .toNumber();\n}\n\n/**\n * Calculates the estimated price for take profit.\n */\nexport function estPriceForTP(inputs: {\n positionQty: number;\n entryPrice: number;\n pnl: number;\n}): number {\n return new Decimal(inputs.pnl)\n .add(inputs.entryPrice)\n .div(inputs.positionQty)\n .toNumber();\n}\n\n/**\n * Calculates the estimated offset for take profit.\n */\nexport function estOffsetForTP(inputs: {\n price: number;\n entryPrice: number;\n}): number {\n return new Decimal(inputs.price).div(inputs.entryPrice).toNumber();\n}\n\n/**\n * Calculates the estimated price from offset for take profit.\n */\nexport function estPriceFromOffsetForTP(inputs: {\n offset: number;\n entryPrice: number;\n}): number {\n return new Decimal(inputs.offset).add(inputs.entryPrice).toNumber();\n}\n\n/**\n * Calculates the PnL for stop loss.\n */\nexport function estPnLForSL(inputs: {\n positionQty: number;\n entryPrice: number;\n}): number {\n return 0;\n}\n","/**\n * The power of the IMR factor.\n * @constant\n * @default\n */\nexport const IMRFactorPower = 4 / 5;\n","import { API, OrderSide } from \"@orderly.network/types\";\nimport { Decimal, zero } from \"@orderly.network/utils\";\nimport { IMRFactorPower } from \"./constants\";\n\nexport type ResultOptions = {\n dp: number;\n};\n\nexport type TotalValueInputs = {\n totalUnsettlementPnL: number;\n\n USDCHolding: number;\n nonUSDCHolding: {\n holding: number;\n indexPrice: number;\n }[];\n};\n\n/**\n * User's total asset value (denominated in USDC), including assets that cannot be used as collateral.\n */\nexport function totalValue(inputs: TotalValueInputs): Decimal {\n const { totalUnsettlementPnL, USDCHolding, nonUSDCHolding } = inputs;\n const nonUSDCHoldingValue = nonUSDCHolding.reduce((acc, cur) => {\n return new Decimal(cur.holding).mul(cur.indexPrice).add(acc);\n }, zero);\n return nonUSDCHoldingValue.add(USDCHolding).add(totalUnsettlementPnL);\n}\n\n/**\n * Total value of available collateral in the user's account (denominated in USDC).\n */\nexport type FreeCollateralInputs = {\n // Total collateral\n totalCollateral: Decimal;\n // Total initial margin with orders\n totalInitialMarginWithOrders: number;\n};\n/**\n * Calculate free collateral.\n */\nexport function freeCollateral(inputs: FreeCollateralInputs): Decimal {\n const value = inputs.totalCollateral.sub(inputs.totalInitialMarginWithOrders);\n // free collateral cannot be less than 0\n return value.isNegative() ? zero : value;\n}\n\nexport type TotalCollateralValueInputs = {\n USDCHolding: number;\n nonUSDCHolding: {\n holding: number;\n indexPrice: number;\n collateralCap: number;\n collateralRatio: Decimal;\n }[];\n unsettlementPnL: number;\n};\n\n/**\n * Calculate total collateral.\n */\nexport function totalCollateral(inputs: TotalCollateralValueInputs): Decimal {\n const { USDCHolding, nonUSDCHolding, unsettlementPnL } = inputs;\n const nonUSDCHoldingValue = nonUSDCHolding.reduce<Decimal>((acc, cur) => {\n const finalHolding = Math.min(cur.holding, cur.collateralCap);\n const value = new Decimal(finalHolding)\n .mul(cur.collateralRatio)\n .mul(cur.indexPrice);\n return acc.add(value);\n }, zero);\n\n return new Decimal(USDCHolding).add(nonUSDCHoldingValue).add(unsettlementPnL);\n}\n\nexport function initialMarginWithOrder() {}\n\nexport type PositionNotionalWithOrderInputs = {\n markPrice: number;\n positionQtyWithOrders: number;\n};\n/**\n * Sum of notional value for a symbol's position and orders.\n */\nexport function positionNotionalWithOrder_by_symbol(\n inputs: PositionNotionalWithOrderInputs,\n): Decimal {\n return new Decimal(inputs.markPrice).mul(inputs.positionQtyWithOrders);\n}\n\nexport type PositionQtyWithOrderInputs = {\n positionQty: number;\n // Total quantity of buy orders for a symbol\n buyOrdersQty: number;\n // Total quantity of sell orders for a symbol\n sellOrdersQty: number;\n};\n/**\n * Sum of position quantity and orders quantity for a symbol.\n */\nexport function positionQtyWithOrders_by_symbol(\n inputs: PositionQtyWithOrderInputs,\n): number {\n const { positionQty, buyOrdersQty, sellOrdersQty } = inputs;\n const positionQtyDecimal = new Decimal(positionQty);\n const qty = Math.max(\n positionQtyDecimal.add(buyOrdersQty).abs().toNumber(),\n positionQtyDecimal.sub(sellOrdersQty).abs().toNumber(),\n );\n\n return qty;\n}\n\nexport type IMRInputs = {\n maxLeverage: number;\n baseIMR: number;\n IMR_Factor: number;\n positionNotional: number;\n ordersNotional: number;\n IMR_factor_power?: number;\n};\n\n/**\n * Initial margin rate for a symbol.\n * Max(1 / Max Account Leverage, Base IMR i, IMR Factor i * Abs(Position Notional i + Order Notional i)^(4/5))\n */\nexport function IMR(inputs: IMRInputs): number {\n const {\n maxLeverage,\n baseIMR,\n IMR_Factor,\n positionNotional,\n ordersNotional: orderNotional,\n IMR_factor_power = IMRFactorPower,\n } = inputs;\n return Math.max(\n 1 / maxLeverage,\n baseIMR,\n new Decimal(IMR_Factor)\n .mul(\n new Decimal(positionNotional)\n .add(orderNotional)\n .abs()\n .toPower(IMR_factor_power),\n )\n .toNumber(),\n );\n}\n\nexport function buyOrdersFilter_by_symbol(\n orders: API.Order[],\n symbol: string,\n): API.Order[] {\n return orders.filter(\n (item) => item.symbol === symbol && item.side === OrderSide.BUY,\n );\n}\n\nexport function sellOrdersFilter_by_symbol(\n orders: API.Order[],\n symbol: string,\n): API.Order[] {\n return orders.filter(\n (item) => item.symbol === symbol && item.side === OrderSide.SELL,\n );\n}\n\n/**\n * Get the quantity of a specified symbol from the list of positions.\n */\nexport function getQtyFromPositions(\n positions: API.Position[],\n symbol: string,\n): number {\n if (!positions) {\n return 0;\n }\n const position = positions.find((item) => item.symbol === symbol);\n return position?.position_qty || 0;\n}\n\n/**\n * Get the quantity of long and short orders for a specified symbol from the list of orders.\n */\nexport function getQtyFromOrdersBySide(\n orders: API.Order[],\n symbol: string,\n side: OrderSide,\n): number {\n const ordersBySide =\n side === OrderSide.SELL\n ? sellOrdersFilter_by_symbol(orders, symbol)\n : buyOrdersFilter_by_symbol(orders, symbol);\n return ordersBySide.reduce((acc, cur) => {\n return acc + cur.quantity;\n }, 0);\n}\n\nexport function getPositonsAndOrdersNotionalBySymbol(inputs: {\n positions: API.Position[];\n orders: API.Order[];\n symbol: string;\n markPrice: number;\n}): number {\n const { positions, orders, symbol, markPrice } = inputs;\n const positionQty = getQtyFromPositions(positions, symbol);\n const buyOrdersQty = getQtyFromOrdersBySide(orders, symbol, OrderSide.BUY);\n const sellOrdersQty = getQtyFromOrdersBySide(orders, symbol, OrderSide.SELL);\n\n const markPriceDecimal = new Decimal(markPrice);\n\n return markPriceDecimal\n .mul(positionQty)\n .add(markPriceDecimal.mul(new Decimal(buyOrdersQty).add(sellOrdersQty)))\n .abs()\n .toNumber();\n}\n\nexport type TotalInitialMarginWithOrdersInputs = {\n positions: API.Position[];\n orders: API.Order[];\n // account: API.AccountInfo;\n markPrices: { [key: string]: number };\n symbolInfo: any;\n IMR_Factors: { [key: string]: number };\n} & Pick<IMRInputs, \"maxLeverage\">;\n\n/**\n * Calculate the total initial margin used by the user (including positions and orders).\n */\nexport function totalInitialMarginWithOrders(\n inputs: TotalInitialMarginWithOrdersInputs,\n): number {\n const {\n positions,\n orders,\n markPrices,\n IMR_Factors,\n maxLeverage,\n symbolInfo,\n } = inputs;\n\n const symbols = extractSymbols(positions, orders);\n\n const total_initial_margin_with_orders = symbols.reduce((acc, cur) => {\n const symbol = cur;\n const positionQty = getQtyFromPositions(positions, symbol);\n const buyOrdersQty = getQtyFromOrdersBySide(orders, symbol, OrderSide.BUY);\n const sellOrdersQty = getQtyFromOrdersBySide(\n orders,\n symbol,\n OrderSide.SELL,\n );\n\n const markPrice = markPrices[symbol] || 0;\n\n //---\n const positionQtyWithOrders = positionQtyWithOrders_by_symbol({\n positionQty,\n buyOrdersQty,\n sellOrdersQty,\n });\n\n //---\n const position_notional_with_orders = positionNotionalWithOrder_by_symbol({\n markPrice,\n positionQtyWithOrders,\n });\n\n //----\n const markPriceDecimal = new Decimal(markPrice);\n\n const imr = IMR({\n positionNotional: markPriceDecimal.mul(positionQty).toNumber(),\n ordersNotional: markPriceDecimal\n .mul(new Decimal(buyOrdersQty).add(sellOrdersQty))\n .toNumber(),\n maxLeverage,\n IMR_Factor: IMR_Factors[symbol],\n baseIMR: symbolInfo[symbol](\"base_imr\", 0),\n });\n\n return position_notional_with_orders.mul(imr).add(acc).toNumber();\n }, 0);\n\n return total_initial_margin_with_orders;\n}\n\nexport function totalInitialMarginWithQty(inputs: {\n positions: API.Position[];\n // account: API.AccountInfo;\n markPrices: { [key: string]: number };\n symbolInfo: any;\n IMR_Factors: { [key: string]: number };\n maxLeverage: number;\n}) {\n const { positions, markPrices, IMR_Factors, symbolInfo, maxLeverage } =\n inputs;\n const symbols = positions.map((item) => item.symbol);\n\n const total_initial_margin_with_orders = symbols.reduce((acc, cur) => {\n const symbol = cur;\n const position = positions.find((item) => item.symbol === symbol);\n const positionQty = position?.position_qty || 0;\n\n const buyOrdersQty = position?.pending_long_qty || 0;\n const sellOrdersQty = position?.pending_short_qty || 0;\n\n const markPrice = markPrices[symbol] || 0;\n\n //---\n const positionQtyWithOrders = positionQtyWithOrders_by_symbol({\n positionQty,\n buyOrdersQty,\n sellOrdersQty,\n });\n\n //---\n const position_notional_with_orders = positionNotionalWithOrder_by_symbol({\n markPrice,\n positionQtyWithOrders,\n });\n\n //----\n const markPriceDecimal = new Decimal(markPrice);\n\n const imr = IMR({\n positionNotional: markPriceDecimal.mul(positionQty).toNumber(),\n ordersNotional: markPriceDecimal\n .mul(new Decimal(buyOrdersQty).add(sellOrdersQty))\n .toNumber(),\n maxLeverage,\n IMR_Factor: IMR_Factors[symbol],\n baseIMR: symbolInfo[symbol](\"base_imr\", 0),\n });\n\n return position_notional_with_orders.mul(imr).add(acc).toNumber();\n }, 0);\n\n return total_initial_margin_with_orders;\n}\n\n/**\n * Group orders by symbol, as a symbol can have multiple orders.\n */\nexport function groupOrdersBySymbol(orders: API.Order[]) {\n const symbols: { [key: string]: API.Order[] } = {};\n\n orders.forEach((item) => {\n if (!symbols[item.symbol]) {\n symbols[item.symbol] = [];\n }\n\n symbols[item.symbol].push(item);\n });\n\n return symbols;\n}\n\n/**\n * Extracts all unique symbols from positions and orders.\n * @param positions - An array of position objects.\n * @param orders - An array of order objects.\n * @returns An array of unique symbols.\n */\nexport function extractSymbols(\n positions: Pick<API.Position, \"symbol\">[],\n orders: Pick<API.Order, \"symbol\">[],\n): string[] {\n const symbols = new Set<string>();\n\n positions.forEach((item) => {\n symbols.add(item.symbol);\n });\n\n orders.forEach((item) => {\n symbols.add(item.symbol);\n });\n\n return Array.from(symbols);\n}\n\n//=========== max qty ==================\n\n// function otherIM(inputs: {}): number {}\n\nexport type OtherIMsInputs = {\n // the position list for other symbols except the current symbol\n positions: API.Position[];\n\n markPrices: { [key: string]: number };\n maxLeverage: number;\n symbolInfo: any;\n IMR_Factors: { [key: string]: number };\n};\n/**\n * Total margin used by other symbols (except the current symbol).\n */\nexport function otherIMs(inputs: OtherIMsInputs): number {\n const {\n // orders,\n positions,\n maxLeverage,\n IMR_Factors,\n symbolInfo,\n markPrices,\n } = inputs;\n\n const symbols = positions.map((item) => item.symbol);\n\n return symbols\n .reduce((acc, cur) => {\n const symbol = cur;\n\n if (typeof markPrices[symbol] === \"undefined\") {\n console.warn(\"markPrices[%s] is undefined\", symbol);\n return acc;\n }\n\n const markPriceDecimal = new Decimal(markPrices[symbol] || 0);\n\n const position = positions.find((item) => item.symbol === symbol);\n\n const positionQty = getQtyFromPositions(positions, symbol);\n const positionNotional = markPriceDecimal.mul(positionQty).toNumber();\n\n const buyOrdersQty = position!.pending_long_qty;\n const sellOrdersQty = position!.pending_short_qty;\n\n const ordersNotional = markPriceDecimal\n .mul(new Decimal(buyOrdersQty).add(sellOrdersQty))\n .toNumber();\n\n const IMR_Factor = IMR_Factors[symbol];\n\n if (!IMR_Factor) {\n console.warn(\"IMR_Factor is not found:\", symbol);\n return acc;\n }\n\n const imr = IMR({\n maxLeverage,\n\n IMR_Factor,\n baseIMR: symbolInfo[symbol](\"base_imr\", 0),\n positionNotional,\n ordersNotional,\n });\n\n const positionQtyWithOrders = positionQtyWithOrders_by_symbol({\n positionQty,\n buyOrdersQty,\n sellOrdersQty,\n });\n\n const positionNotionalWithOrders = positionNotionalWithOrder_by_symbol({\n markPrice: markPrices[symbol] || 0,\n positionQtyWithOrders,\n });\n\n return acc.add(positionNotionalWithOrders.mul(imr));\n }, zero)\n .toNumber();\n}\n\nexport type MaxQtyInputs = {\n symbol: string;\n\n // Maximum quantity limit for opening a single position, /v1/public/info.base_max\n baseMaxQty: number;\n /**\n * Total collateral of the user (denominated in USDC), can be calculated from totalCollateral.\n * @see totalCollateral\n */\n totalCollateral: number;\n maxLeverage: number;\n baseIMR: number;\n /**\n * @see otherIMs\n */\n otherIMs: number;\n markPrice: number;\n // Quantity of open positions\n positionQty: number;\n // Quantity of long orders\n buyOrdersQty: number;\n // Quantity of short orders\n sellOrdersQty: number;\n\n IMR_Factor: number;\n\n takerFeeRate: number;\n};\n\n/**\n * Maximum order quantity.\n */\nexport function maxQty(\n side: OrderSide,\n inputs: MaxQtyInputs,\n options?: ResultOptions,\n): number {\n if (side === OrderSide.BUY) {\n return maxQtyByLong(inputs);\n }\n return maxQtyByShort(inputs);\n}\n\nexport function maxQtyByLong(\n inputs: Omit<MaxQtyInputs, \"side\">,\n options?: ResultOptions,\n): number {\n try {\n const {\n baseMaxQty,\n totalCollateral,\n otherIMs,\n maxLeverage,\n baseIMR,\n markPrice,\n IMR_Factor,\n positionQty,\n buyOrdersQty,\n takerFeeRate,\n } = inputs;\n\n if (totalCollateral === 0) {\n return 0;\n }\n\n const totalCollateralDecimal = new Decimal(totalCollateral);\n\n const factor_1 = totalCollateralDecimal\n .sub(otherIMs)\n .div(\n new Decimal(takerFeeRate)\n .mul(2)\n .mul(0.0001)\n .add(Math.max(1 / maxLeverage, baseIMR)),\n )\n .div(markPrice)\n .mul(0.995)\n .sub(new Decimal(positionQty).add(buyOrdersQty))\n .toNumber();\n\n if (positionQty === 0 && buyOrdersQty === 0) {\n return Math.min(baseMaxQty, factor_1);\n }\n\n const factor_2 = totalCollateralDecimal\n .sub(otherIMs)\n .div(IMR_Factor)\n .toPower(1 / 1.8)\n .div(markPrice)\n .sub(\n new Decimal(positionQty).add(buyOrdersQty),\n // .abs()\n // .div(new Decimal(takerFeeRate).mul(2).mul(0.0001).add(1))\n )\n .div(new Decimal(takerFeeRate).mul(2).mul(0.0001).add(1))\n .mul(0.995)\n .toNumber();\n\n return Math.min(baseMaxQty, factor_1, factor_2);\n } catch (error) {\n return 0;\n }\n}\n\nexport function maxQtyByShort(\n inputs: Omit<MaxQtyInputs, \"side\">,\n options?: ResultOptions,\n): number {\n try {\n const {\n baseMaxQty,\n totalCollateral,\n otherIMs,\n maxLeverage,\n baseIMR,\n markPrice,\n IMR_Factor,\n positionQty,\n buyOrdersQty,\n sellOrdersQty,\n takerFeeRate,\n } = inputs;\n\n const totalCollateralDecimal = new Decimal(totalCollateral);\n\n const factor_1 = totalCollateralDecimal\n .sub(otherIMs)\n .div(\n new Decimal(takerFeeRate)\n .mul(2)\n .mul(0.0001)\n .add(Math.max(1 / maxLeverage, baseIMR)),\n )\n .div(markPrice)\n .mul(0.995)\n // .add(new Decimal(positionQty).add(sellOrdersQty))\n .add(positionQty)\n .sub(Math.abs(sellOrdersQty))\n .toNumber();\n\n if (positionQty === 0 && sellOrdersQty === 0) {\n return Math.min(baseMaxQty, factor_1);\n }\n\n const factor_2 = totalCollateralDecimal\n .sub(otherIMs)\n .div(IMR_Factor)\n .toPower(1 / 1.8)\n .div(markPrice)\n // .add(\n // new Decimal(positionQty)\n // .add(sellOrdersQty)\n // // .abs()\n // )\n .add(positionQty)\n .sub(sellOrdersQty)\n .div(new Decimal(takerFeeRate).mul(2).mul(0.0001).add(1))\n .mul(0.995)\n .toNumber();\n\n return Math.min(baseMaxQty, factor_1, factor_2);\n } catch (error) {\n return 0;\n }\n}\n\nexport type TotalMarginRatioInputs = {\n totalCollateral: number;\n markPrices: { [key: string]: number };\n positions: API.Position[];\n};\n/**\n * total margin ratio\n */\nexport function totalMarginRatio(\n inputs: TotalMarginRatioInputs,\n dp?: number,\n): number {\n const { totalCollateral, markPrices, positions } = inputs;\n\n if (totalCollateral === 0) {\n return 0;\n }\n\n const totalCollateralDecimal = new Decimal(totalCollateral);\n\n const totalPositionNotional = positions.reduce((acc, cur) => {\n const markPrice = markPrices[cur.symbol] || 0;\n return acc.add(new Decimal(cur.position_qty).mul(markPrice).abs());\n }, zero);\n\n if (totalPositionNotional.eq(zero)) {\n return 0;\n }\n\n return totalCollateralDecimal.div(totalPositionNotional).toNumber();\n}\n\nexport type TotalUnrealizedROIInputs = {\n totalUnrealizedPnL: number;\n totalValue: number;\n};\n\n/**\n * totalUnrealizedROI\n */\nexport function totalUnrealizedROI(inputs: TotalUnrealizedROIInputs) {\n const { totalUnrealizedPnL, totalValue } = inputs;\n\n return new Decimal(totalUnrealizedPnL)\n .div(totalValue - totalUnrealizedPnL)\n .toNumber();\n}\n\n/**\n * current account leverage\n */\nexport function currentLeverage(totalMarginRatio: number) {\n if (totalMarginRatio === 0) {\n return 0;\n }\n return 1 / totalMarginRatio;\n}\n\nexport type AvailableBalanceInputs = {\n USDCHolding: number;\n unsettlementPnL: number;\n};\nexport function availableBalance(inputs: AvailableBalanceInputs) {\n const { USDCHolding, unsettlementPnL } = inputs;\n\n return new Decimal(USDCHolding).add(unsettlementPnL).toNumber();\n}\n\nexport type AccountMMRInputs = {\n // Total Maintenance Margin of all positions of the user (USDC)\n positionsMMR: number;\n /**\n * Notional sum of all positions,\n * positions.totalNotional()\n */\n positionsNotional: number;\n};\n\n/**\n * total maintenance margin ratio\n * @param inputs AccountMMRInputs\n * @returns number|null\n */\nexport function MMR(inputs: AccountMMRInputs): number | null {\n // If the user does not have any positions, return null\n if (inputs.positionsNotional === 0) {\n return null;\n }\n if (inputs.positionsMMR === 0) {\n return null;\n }\n return new Decimal(inputs.positionsMMR)\n .div(inputs.positionsNotional)\n .toNumber();\n}\n\nexport const collateralRatio = (params: {\n baseWeight: number;\n discountFactor: number | null;\n collateralQty: number;\n collateralCap: number;\n indexPrice: number;\n}) => {\n const {\n baseWeight,\n discountFactor,\n collateralQty,\n collateralCap,\n indexPrice,\n } = params;\n\n // if collateralCap is -1, it means the collateral is unlimited\n const cap = collateralCap === -1 ? collateralQty : collateralCap;\n\n const K = new Decimal(1.2);\n const DCF = new Decimal(discountFactor || 0);\n const qty = new Decimal(Math.min(collateralQty, cap));\n\n const notionalAbs = qty.mul(indexPrice).abs();\n const dynamicWeight = DCF.mul(notionalAbs).toPower(IMRFactorPower);\n const result = K.div(new Decimal(1).add(dynamicWeight));\n\n return result.lt(baseWeight) ? result : new Decimal(baseWeight);\n};\n\n/** collateral_value_i = min(collateral_qty_i , collateral_cap_i) * weight_i * index_price_i */\nexport const collateralContribution = (params: {\n collateralQty: number;\n collateralCap: number;\n collateralRatio: number;\n indexPrice: number;\n}) => {\n const { collateralQty, collateralCap, collateralRatio, indexPrice } = params;\n\n // if collateralCap is -1, it means the collateral is unlimited\n const cap = collateralCap === -1 ? collateralQty : collateralCap;\n\n return new Decimal(Math.min(collateralQty, cap))\n .mul(collateralRatio)\n .mul(indexPrice)\n .toNumber();\n};\n\nexport const LTV = (params: {\n usdcBalance: number;\n upnl: number;\n assets: Array<{ qty: number; indexPrice: number; weight: number }>;\n}) => {\n const { usdcBalance, upnl, assets } = params;\n\n const usdcLoss = new Decimal(Math.min(usdcBalance, 0)).abs();\n const upnlLoss = new Decimal(Math.min(upnl, 0)).abs();\n const numerator = usdcLoss.add(upnlLoss);\n\n const collateralSum = assets.reduce<Decimal>((acc, asset) => {\n return acc.add(\n new Decimal(Math.max(asset.qty, 0))\n .mul(new Decimal(asset.indexPrice))\n .mul(new Decimal(asset.weight)),\n );\n }, zero);\n\n const denominator = collateralSum.add(new Decimal(Math.max(upnl, 0)));\n\n if (numerator.isZero() || denominator.isZero()) {\n return 0;\n }\n\n return numerator.div(denominator).toNumber();\n};\n\nexport const maxWithdrawalUSDC = (inputs: {\n USDCBalance: number;\n freeCollateral: Decimal;\n upnl: number;\n}) => {\n const { USDCBalance, freeCollateral, upnl } = inputs;\n const value = Math.min(\n new Decimal(USDCBalance).toNumber(),\n new Decimal(freeCollateral).sub(Math.max(upnl, 0)).toNumber(),\n );\n return Math.max(0, value);\n};\n\nexport const maxWithdrawalOtherCollateral = (inputs: {\n USDCBalance: number;\n collateralQty: number;\n freeCollateral: Decimal;\n indexPrice: number;\n weight: Decimal;\n}) => {\n const { USDCBalance, collateralQty, freeCollateral, indexPrice, weight } =\n inputs;\n const usdcBalance = new Decimal(USDCBalance);\n const denominator = usdcBalance.isNegative()\n ? new Decimal(indexPrice).mul(weight).mul(new Decimal(1).add(0.001))\n : new Decimal(indexPrice).mul(weight);\n if (denominator.isZero()) {\n return zero;\n }\n const qty = new Decimal(collateralQty);\n const maxQtyByValue = new Decimal(freeCollateral).div(denominator);\n return maxQtyByValue.lt(qty) ? maxQtyByValue : qty;\n};\n\nexport const calcMinimumReceived = (inputs: {\n amount: number;\n slippage: number;\n}) => {\n const { amount, slippage } = inputs;\n const slippageRatio = new Decimal(slippage).div(100);\n return new Decimal(amount)\n .mul(new Decimal(1).minus(slippageRatio))\n .toNumber();\n};\n","import { API as orderUtils } from \"@orderly.network/types\";\nimport { Decimal, zero } from \"@orderly.network/utils\";\nimport { notional } from \"./positions\";\n\n/**\n * Maximum price when placing an order\n */\nexport function maxPrice(markprice: number, range: number) {\n return markprice * (1 + range);\n}\n\n/**\n * Minimum price when placing an order\n */\nexport function minPrice(markprice: number, range: number) {\n return markprice * (1 - range);\n}\n\n/**\n * Scope price when placing an order\n * @returns number\n */\nexport function scopePrice(\n price: number,\n scope: number,\n side: \"BUY\" | \"SELL\",\n): number {\n if (side === \"BUY\") {\n return price * (1 - scope);\n }\n return price * (1 + scope);\n}\n\n/**\n * Calculate the order fee\n */\nexport function orderFee(inputs: {\n /**\n * Order quantity\n */\n qty: number;\n price: number;\n futuresTakeFeeRate: number;\n}): number {\n return new Decimal(inputs.qty)\n .mul(inputs.price)\n .mul(inputs.futuresTakeFeeRate)\n .toNumber();\n}\n\nexport type EstimatedLiquidationPriceInputs = {\n totalCollateral: number;\n markPrice: number;\n baseMMR: number;\n baseIMR: number;\n IMR_Factor: number;\n orderFee: number;\n positions: Pick<\n orderUtils.PositionExt,\n \"position_qty\" | \"mark_price\" | \"symbol\" | \"mmr\"\n >[];\n newOrder: {\n symbol: string;\n qty: number;\n price: number;\n };\n};\n\n/**\n * Estimated liquidation price\n * @param inputs\n * @returns\n */\nexport function estLiqPrice(inputs: EstimatedLiquidationPriceInputs): number {\n const {\n positions,\n newOrder,\n totalCollateral,\n markPrice,\n baseIMR,\n baseMMR,\n orderFee,\n IMR_Factor,\n } = inputs;\n // opened positions for the symbol\n let currentPosition:\n | Pick<\n orderUtils.PositionExt,\n \"position_qty\" | \"mark_price\" | \"symbol\" | \"mmr\"\n >\n | undefined = undefined;\n\n let newTotalMM = zero;\n\n const hasPosition =\n positions.filter((item) => item.position_qty > 0).length > 0;\n\n const basePrice = hasPosition ? markPrice : newOrder.price;\n\n const newOrderNotional = new Decimal(newOrder.qty).mul(newOrder.price);\n\n for (let index = 0; index < positions.length; index++) {\n const position = positions[index];\n let notional = new Decimal(position.position_qty).mul(position.mark_price);\n if (newOrder.symbol === position.symbol) {\n currentPosition = position;\n notional = notional.add(newOrderNotional);\n }\n\n newTotalMM = newTotalMM.add(notional.abs().mul(position.mmr));\n }\n\n // if no position\n if (!currentPosition) {\n newTotalMM = newTotalMM.add(newOrderNotional.mul(baseMMR));\n }\n\n const newMMR = Math.max(\n baseMMR,\n new Decimal(baseMMR)\n .div(baseIMR)\n .mul(IMR_Factor)\n .mul(\n newOrderNotional\n .add(\n !!currentPosition\n ? new Decimal(currentPosition.position_qty).mul(\n currentPosition.mark_price,\n )\n : zero,\n )\n .abs(),\n )\n .toPower(4 / 5)\n .toNumber(),\n );\n\n // console.log(\"new MMR\", newMMR, newTotalMM.toNumber());\n\n const newQty = new Decimal(newOrder.qty).add(\n currentPosition?.position_qty ?? 0,\n );\n\n if (newQty.eq(0)) {\n return 0;\n }\n\n const price = new Decimal(basePrice)\n .add(\n new Decimal(totalCollateral)\n .sub(newTotalMM)\n .sub(orderFee)\n .div(newQty.abs().mul(newMMR).sub(newQty)),\n )\n .toNumber();\n\n return Math.max(0, price);\n}\n\nexport type EstimatedLeverageInputs = {\n totalCollateral: number;\n positions: Pick<\n orderUtils.PositionExt,\n \"position_qty\" | \"mark_price\" | \"symbol\"\n >[];\n newOrder: {\n symbol: string;\n qty: number;\n price: number;\n };\n};\n\n/**\n * Estimated leverage\n * @param inputs EstimtedLeverageInputs\n * @returns number\n */\nexport function estLeverage(inputs: EstimatedLeverageInputs): number | null {\n const { totalCollateral, positions, newOrder } = inputs;\n if (totalCollateral <= 0) {\n return null;\n }\n let hasPosition = false;\n let sumPositionNotional = positions.reduce((acc, cur) => {\n let count = new Decimal(cur.position_qty).mul(cur.mark_price);\n // acc = acc.add(\n // new Decimal(cur.position_qty).mul(cur.mark_price)\n // // .abs()\n // );\n\n if (cur.symbol === newOrder.symbol) {\n hasPosition = true;\n // acc = acc.add(new Decimal(newOrder.qty).mul(newOrder.price));\n count = count.add(new Decimal(newOrder.qty).mul(newOrder.price));\n }\n\n return acc.add(count.abs());\n }, zero);\n\n if (!hasPosition) {\n sumPositionNotional = sumPositionNotional.add(\n new Decimal(newOrder.qty).mul(newOrder.price).abs(),\n );\n }\n\n if (sumPositionNotional.eq(zero)) {\n return null;\n }\n\n const totalMarginRatio = new Decimal(totalCollateral).div(\n sumPositionNotional,\n );\n\n return new Decimal(1)\n .div(totalMarginRatio)\n .toDec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MAAI,CAAC,aAAa;AAChB,0BAAsB,oBAAoB;AAAA,MACxC,IAAI,sBAAQ,SAAS,GAAG,EAAE,IAAI,SAAS,KAAK,EAAE,IAAI;AAAA,IACpD;AAAA,EACF;AAEA,MAAI,oBAAoB,GAAG,kBAAI,GAAG;AAChC,WAAO;AAAA,EACT;AAEA,QAAMG,oBAAmB,IAAI,sBAAQH,gBAAe,EAAE;AAAA,IACpD;AAAA,EACF;AAEA,SAAO,IAAI,sBAAQ,CAAC,EACjB,IAAIG,iBAAgB,EACpB,gBAAgB,GAAG,sBAAQ,eAAe,EAC1C,SAAS;AACd;","names":["IMR","totalCollateral","MMR","totalNotional","MMR","import_utils","totalUnsettlementPnL","unsettlementPnL","totalCollateral","otherIMs","totalUnrealizedPnL","totalValue","totalMarginRatio","MMR","collateralRatio","freeCollateral","import_utils","totalCollateral","orderFee","notional","totalMarginRatio"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/version.ts","../src/positions.ts","../src/constants.ts","../src/account.ts","../src/order.ts"],"sourcesContent":["export { default as version } from \"./version\";\nexport * as positions from \"./positions\";\nexport * as account from \"./account\";\nexport * as orderUtils from \"./order\";\n\nexport * as order from \"./order\";\n","declare global {\n interface Window {\n __ORDERLY_VERSION__?: {\n [key: string]: string;\n };\n }\n}\nif (typeof window !== \"undefined\") {\n window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};\n window.__ORDERLY_VERSION__[\"@orderly.network/perp\"] = \"4.7.0\";\n}\n\nexport default \"4.7.0\";\n","import { API } from \"@orderly.network/types\";\nimport { Decimal, zero } from \"@orderly.network/utils\";\nimport { IMRFactorPower } from \"./constants\";\n\n/**\n * Calculates the notional value of a single position.\n * @param qty The quantity of the position.\n * @param mark_price The price of the position.\n * @returns The notional value of the position.\n */\nexport function notional(qty: number, mark_price: number): number {\n return new Decimal(qty).mul(mark_price).abs().toNumber();\n}\n\n/**\n * Calculates the total notional value of all positions.\n * @param positions The array of positions.\n * @returns The total notional value of all positions.\n */\nexport function totalNotional(positions: API.Position[]): number {\n return positions.reduce((acc, cur) => {\n return acc + notional(cur.position_qty, cur.mark_price);\n }, 0);\n}\n\nexport type UnrealPnLInputs = {\n markPrice: number;\n openPrice: number;\n qty: number;\n};\n\n/**\n * Calculates the unrealized profit or loss of a single position.\n * @param inputs The inputs for calculating the unrealized profit or loss.\n * @returns The unrealized profit or loss of the position.\n */\nexport function unrealizedPnL(inputs: UnrealPnLInputs): number {\n return new Decimal(inputs.qty)\n .mul(inputs.markPrice - inputs.openPrice)\n .toNumber();\n}\n\nexport type UnrealPnLROIInputs = {\n positionQty: number;\n openPrice: number;\n IMR: number;\n unrealizedPnL: number;\n};\n\n/**\n * Calculates the return on investment (ROI) of a single position's unrealized profit or loss.\n * @param inputs The inputs for calculating the ROI.\n * @returns The ROI of the position's unrealized profit or loss.\n */\nexport function unrealizedPnLROI(inputs: UnrealPnLROIInputs): number {\n const { openPrice, IMR } = inputs;\n\n if (\n inputs.unrealizedPnL === 0 ||\n inputs.positionQty === 0 ||\n openPrice === 0 ||\n IMR === 0\n )\n return 0;\n\n return new Decimal(inputs.unrealizedPnL)\n .div(new Decimal(Math.abs(inputs.positionQty)).mul(openPrice).mul(IMR))\n .toNumber();\n}\n\n/**\n * Calculates the total unrealized profit or loss of all positions.\n * @param positions The array of positions.\n * @returns The total unrealized profit or loss of all positions.\n */\nexport function totalUnrealizedPnL(positions: API.Position[]): number {\n return positions.reduce((acc, cur) => {\n return (\n acc +\n unrealizedPnL({\n qty: cur.position_qty,\n openPrice: cur.average_open_price,\n markPrice: cur.mark_price,\n })\n );\n }, 0);\n}\n\nexport type LiqPriceInputs = {\n markPrice: number;\n totalCollateral: number;\n positionQty: number;\n positions: Pick<API.PositionExt, \"position_qty\" | \"mark_price\" | \"mmr\">[];\n MMR: number;\n};\n\n/**\n * Calculates the liquidation price of a single position.\n * @param inputs The inputs for calculating the liquidation price.\n * @returns The liquidation price of the position.\n */\nexport function liqPrice(inputs: LiqPriceInputs): number | null {\n const { markPrice, totalCollateral, positions, positionQty, MMR } = inputs;\n\n // console.log(\"inputs\", inputs);\n\n if (positionQty === 0 || totalCollateral === 0) {\n return null;\n }\n\n // totalNotional of all poisitions\n const totalNotional = positions.reduce<Decimal>((acc, cur) => {\n return acc.add(\n new Decimal(notional(cur.position_qty, cur.mark_price)).mul(cur.mmr),\n );\n }, zero);\n\n return Math.max(\n new Decimal(markPrice)\n .add(\n new Decimal(totalCollateral)\n .sub(totalNotional)\n .div(new Decimal(positionQty).abs().mul(MMR).sub(positionQty)),\n )\n .toNumber(),\n 0,\n );\n}\n\nexport type MMInputs = {\n positionQty: number;\n markPrice: number;\n MMR: number;\n};\n\n/**\n * Calculates the maintenance margin of a position.\n * @param inputs The inputs for calculating the maintenance margin.\n * @returns The maintenance margin of the position.\n */\nexport function maintenanceMargin(inputs: MMInputs) {\n const { positionQty, markPrice, MMR } = inputs;\n\n return new Decimal(positionQty).mul(markPrice).mul(MMR).abs().toNumber();\n}\n\nexport type UnsettlementPnLInputs = {\n positionQty: number;\n markPrice: number;\n costPosition: number;\n sumUnitaryFunding: number;\n lastSumUnitaryFunding: number;\n};\n\n/**\n * Calculates the unrealized profit or loss of each position.\n * @param inputs The inputs for calculating the unrealized profit or loss.\n * @returns The unrealized profit or loss of each position.\n */\nexport function unsettlementPnL(inputs: UnsettlementPnLInputs): number {\n const {\n positionQty,\n markPrice,\n costPosition,\n sumUnitaryFunding,\n lastSumUnitaryFunding,\n } = inputs;\n\n const qty = new Decimal(positionQty);\n\n return qty\n .mul(markPrice)\n .sub(costPosition)\n .sub(qty.mul(new Decimal(sumUnitaryFunding).sub(lastSumUnitaryFunding)))\n .toNumber();\n}\n\nexport type TotalUnsettlementPnLInputs = {\n positions: (API.Position & {\n sum_unitary_funding: number;\n })[];\n sumUnitaryFunding: number;\n};\n\n/**\n * Calculates the total unrealized profit or loss of all positions.\n * @param positions The array of positions.\n * @returns The total unrealized profit or loss of all positions.\n */\nexport function totalUnsettlementPnL(\n positions: (API.Position & { sum_unitary_funding: number })[],\n): number {\n if (!Array.isArray(positions) || positions.length === 0) {\n return 0;\n }\n\n return positions.reduce((acc, cur) => {\n return (\n acc +\n unsettlementPnL({\n positionQty: cur.position_qty,\n markPrice: cur.mark_price,\n costPosition: cur.cost_position,\n sumUnitaryFunding: cur.sum_unitary_funding,\n lastSumUnitaryFunding: cur.last_sum_unitary_funding,\n })\n );\n }, 0);\n}\n\nexport type MMRInputs = {\n baseMMR: number;\n baseIMR: number;\n IMRFactor: number;\n positionNotional: number;\n IMR_factor_power: number;\n};\n\n/**\n * Calculates the maintenance margin requirement (MMR) of a position.\n * @param inputs The inputs for calculating the MMR.\n * @returns The MMR of the position.\n */\nexport function MMR(inputs: MMRInputs): number {\n const {\n baseMMR,\n baseIMR,\n IMRFactor,\n positionNotional,\n IMR_factor_power = IMRFactorPower,\n } = inputs;\n return Math.max(\n baseMMR,\n new Decimal(baseMMR)\n .div(baseIMR)\n .mul(IMRFactor)\n .mul(Math.pow(Math.abs(positionNotional), IMR_factor_power))\n // .toPower(IMR_factor_power)\n .toNumber(),\n );\n}\n\n/**\n * Calculates the profit or loss for take profit.\n * @returns The profit or loss for take profit.\n */\nexport function estPnLForTP(inputs: {\n positionQty: number;\n entryPrice: number;\n price: number;\n}): number {\n return new Decimal(inputs.positionQty)\n .mul(new Decimal(inputs.price).sub(inputs.entryPrice))\n .toNumber();\n}\n\n/**\n * Calculates the estimated price for take profit.\n */\nexport function estPriceForTP(inputs: {\n positionQty: number;\n entryPrice: number;\n pnl: number;\n}): number {\n return new Decimal(inputs.pnl)\n .add(inputs.entryPrice)\n .div(inputs.positionQty)\n .toNumber();\n}\n\n/**\n * Calculates the estimated offset for take profit.\n */\nexport function estOffsetForTP(inputs: {\n price: number;\n entryPrice: number;\n}): number {\n return new Decimal(inputs.price).div(inputs.entryPrice).toNumber();\n}\n\n/**\n * Calculates the estimated price from offset for take profit.\n */\nexport function estPriceFromOffsetForTP(inputs: {\n offset: number;\n entryPrice: number;\n}): number {\n return new Decimal(inputs.offset).add(inputs.entryPrice).toNumber();\n}\n\n/**\n * Calculates the PnL for stop loss.\n */\nexport function estPnLForSL(inputs: {\n positionQty: number;\n entryPrice: number;\n}): number {\n return 0;\n}\n\n/**\n * calculate the max position notional\n * max_notional = ( (1/ (leverage * imr_factor) ) ^ (1/0.8)\n */\nexport function maxPositionNotional(inputs: {\n /** symbol leverage */\n leverage: number;\n IMRFactor: number;\n}) {\n const { leverage, IMRFactor } = inputs;\n return new Decimal(1)\n .div(new Decimal(leverage).mul(IMRFactor))\n .pow(1 / 0.8)\n .toNumber();\n}\n\n/**\n * symbol_leverage_max = 1 / ( imr_factor * notional ^ 0.8 )\n */\nexport function maxPositionLeverage(inputs: {\n IMRFactor: number;\n notional: number;\n}) {\n const { IMRFactor, notional } = inputs;\n return new Decimal(1)\n .div(new Decimal(IMRFactor).mul(new Decimal(notional).pow(0.8)))\n .toNumber();\n}\n","/**\n * The power of the IMR factor.\n * @constant\n * @default\n */\nexport const IMRFactorPower = 4 / 5;\n","import { API, OrderSide } from \"@orderly.network/types\";\nimport { Decimal, zero } from \"@orderly.network/utils\";\nimport { IMRFactorPower } from \"./constants\";\n\nexport type ResultOptions = {\n dp: number;\n};\n\nexport type TotalValueInputs = {\n totalUnsettlementPnL: number;\n\n USDCHolding: number;\n nonUSDCHolding: {\n holding: number;\n indexPrice: number;\n }[];\n};\n\n/**\n * User's total asset value (denominated in USDC), including assets that cannot be used as collateral.\n */\nexport function totalValue(inputs: TotalValueInputs): Decimal {\n const { totalUnsettlementPnL, USDCHolding, nonUSDCHolding } = inputs;\n const nonUSDCHoldingValue = nonUSDCHolding.reduce((acc, cur) => {\n return new Decimal(cur.holding).mul(cur.indexPrice).add(acc);\n }, zero);\n return nonUSDCHoldingValue.add(USDCHolding).add(totalUnsettlementPnL);\n}\n\n/**\n * Total value of available collateral in the user's account (denominated in USDC).\n */\nexport type FreeCollateralInputs = {\n // Total collateral\n totalCollateral: Decimal;\n // Total initial margin with orders\n totalInitialMarginWithOrders: number;\n};\n/**\n * Calculate free collateral.\n */\nexport function freeCollateral(inputs: FreeCollateralInputs): Decimal {\n const value = inputs.totalCollateral.sub(inputs.totalInitialMarginWithOrders);\n // free collateral cannot be less than 0\n return value.isNegative() ? zero : value;\n}\n\nexport type TotalCollateralValueInputs = {\n USDCHolding: number;\n nonUSDCHolding: {\n holding: number;\n indexPrice: number;\n collateralCap: number;\n collateralRatio: Decimal;\n }[];\n unsettlementPnL: number;\n};\n\n/**\n * Calculate total collateral.\n */\nexport function totalCollateral(inputs: TotalCollateralValueInputs): Decimal {\n const { USDCHolding, nonUSDCHolding, unsettlementPnL } = inputs;\n const nonUSDCHoldingValue = nonUSDCHolding.reduce<Decimal>((acc, cur) => {\n const finalHolding = Math.min(cur.holding, cur.collateralCap);\n const value = new Decimal(finalHolding)\n .mul(cur.collateralRatio)\n .mul(cur.indexPrice);\n return acc.add(value);\n }, zero);\n\n return new Decimal(USDCHolding).add(nonUSDCHoldingValue).add(unsettlementPnL);\n}\n\nexport function initialMarginWithOrder() {}\n\nexport type PositionNotionalWithOrderInputs = {\n markPrice: number;\n positionQtyWithOrders: number;\n};\n/**\n * Sum of notional value for a symbol's position and orders.\n */\nexport function positionNotionalWithOrder_by_symbol(\n inputs: PositionNotionalWithOrderInputs,\n): Decimal {\n return new Decimal(inputs.markPrice).mul(inputs.positionQtyWithOrders);\n}\n\nexport type PositionQtyWithOrderInputs = {\n positionQty: number;\n // Total quantity of buy orders for a symbol\n buyOrdersQty: number;\n // Total quantity of sell orders for a symbol\n sellOrdersQty: number;\n};\n/**\n * Sum of position quantity and orders quantity for a symbol.\n */\nexport function positionQtyWithOrders_by_symbol(\n inputs: PositionQtyWithOrderInputs,\n): number {\n const { positionQty, buyOrdersQty, sellOrdersQty } = inputs;\n const positionQtyDecimal = new Decimal(positionQty);\n const qty = Math.max(\n positionQtyDecimal.add(buyOrdersQty).abs().toNumber(),\n positionQtyDecimal.sub(sellOrdersQty).abs().toNumber(),\n );\n\n return qty;\n}\n\nexport type IMRInputs = {\n /**\n * effective max leverage\n */\n maxLeverage: number;\n baseIMR: number;\n IMR_Factor: number;\n positionNotional: number;\n ordersNotional: number;\n IMR_factor_power?: number;\n};\n\n/**\n * Initial margin rate for a symbol.\n * Max(1 / Max Account Leverage, Base IMR i, IMR Factor i * Abs(Position Notional i + Order Notional i)^(4/5))\n */\nexport function IMR(inputs: IMRInputs): number {\n const {\n maxLeverage,\n baseIMR,\n IMR_Factor,\n positionNotional,\n ordersNotional: orderNotional,\n IMR_factor_power = IMRFactorPower,\n } = inputs;\n return Math.max(\n 1 / maxLeverage,\n baseIMR,\n new Decimal(IMR_Factor)\n .mul(\n new Decimal(positionNotional)\n .add(orderNotional)\n .abs()\n .toPower(IMR_factor_power),\n )\n .toNumber(),\n );\n}\n\nexport function buyOrdersFilter_by_symbol(\n orders: API.Order[],\n symbol: string,\n): API.Order[] {\n return orders.filter(\n (item) => item.symbol === symbol && item.side === OrderSide.BUY,\n );\n}\n\nexport function sellOrdersFilter_by_symbol(\n orders: API.Order[],\n symbol: string,\n): API.Order[] {\n return orders.filter(\n (item) => item.symbol === symbol && item.side === OrderSide.SELL,\n );\n}\n\n/**\n * Get the quantity of a specified symbol from the list of positions.\n */\nexport function getQtyFromPositions(\n positions: API.Position[],\n symbol: string,\n): number {\n if (!positions) {\n return 0;\n }\n const position = positions.find((item) => item.symbol === symbol);\n return position?.position_qty || 0;\n}\n\n/**\n * Get the quantity of long and short orders for a specified symbol from the list of orders.\n */\nexport function getQtyFromOrdersBySide(\n orders: API.Order[],\n symbol: string,\n side: OrderSide,\n): number {\n const ordersBySide =\n side === OrderSide.SELL\n ? sellOrdersFilter_by_symbol(orders, symbol)\n : buyOrdersFilter_by_symbol(orders, symbol);\n return ordersBySide.reduce((acc, cur) => {\n return acc + cur.quantity;\n }, 0);\n}\n\nexport function getPositonsAndOrdersNotionalBySymbol(inputs: {\n positions: API.Position[];\n orders: API.Order[];\n symbol: string;\n markPrice: number;\n}): number {\n const { positions, orders, symbol, markPrice } = inputs;\n const positionQty = getQtyFromPositions(positions, symbol);\n const buyOrdersQty = getQtyFromOrdersBySide(orders, symbol, OrderSide.BUY);\n const sellOrdersQty = getQtyFromOrdersBySide(orders, symbol, OrderSide.SELL);\n\n const markPriceDecimal = new Decimal(markPrice);\n\n return markPriceDecimal\n .mul(positionQty)\n .add(markPriceDecimal.mul(new Decimal(buyOrdersQty).add(sellOrdersQty)))\n .abs()\n .toNumber();\n}\n\nexport type TotalInitialMarginWithOrdersInputs = {\n positions: API.Position[];\n orders: API.Order[];\n // account: API.AccountInfo;\n markPrices: { [key: string]: number };\n symbolInfo: any;\n IMR_Factors: { [key: string]: number };\n} & Pick<IMRInputs, \"maxLeverage\">;\n\n/**\n * @deprecated\n * Calculate the total initial margin used by the user (including positions and orders).\n */\nexport function totalInitialMarginWithOrders(\n inputs: TotalInitialMarginWithOrdersInputs,\n): number {\n const {\n positions,\n orders,\n markPrices,\n IMR_Factors,\n maxLeverage,\n symbolInfo,\n } = inputs;\n\n const symbols = extractSymbols(positions, orders);\n\n const total_initial_margin_with_orders = symbols.reduce((acc, cur) => {\n const symbol = cur;\n const positionQty = getQtyFromPositions(positions, symbol);\n const buyOrdersQty = getQtyFromOrdersBySide(orders, symbol, OrderSide.BUY);\n const sellOrdersQty = getQtyFromOrdersBySide(\n orders,\n symbol,\n OrderSide.SELL,\n );\n\n const markPrice = markPrices[symbol] || 0;\n\n //---\n const positionQtyWithOrders = positionQtyWithOrders_by_symbol({\n positionQty,\n buyOrdersQty,\n sellOrdersQty,\n });\n\n //---\n const position_notional_with_orders = positionNotionalWithOrder_by_symbol({\n markPrice,\n positionQtyWithOrders,\n });\n\n //----\n const markPriceDecimal = new Decimal(markPrice);\n\n const imr = IMR({\n positionNotional: markPriceDecimal.mul(positionQty).toNumber(),\n ordersNotional: markPriceDecimal\n .mul(new Decimal(buyOrdersQty).add(sellOrdersQty))\n .toNumber(),\n maxLeverage,\n IMR_Factor: IMR_Factors[symbol],\n baseIMR: symbolInfo[symbol](\"base_imr\", 0),\n });\n\n return position_notional_with_orders.mul(imr).add(acc).toNumber();\n }, 0);\n\n return total_initial_margin_with_orders;\n}\n\nexport function totalInitialMarginWithQty(inputs: {\n positions: API.Position[];\n // account: API.AccountInfo;\n markPrices: { [key: string]: number };\n symbolInfo: any;\n IMR_Factors: { [key: string]: number };\n /**\n * account max leverage\n */\n maxLeverage: number;\n}) {\n const { positions, markPrices, IMR_Factors, symbolInfo } = inputs;\n const symbols = positions.map((item) => item.symbol);\n\n const total_initial_margin_with_orders = symbols.reduce((acc, cur) => {\n const symbol = cur;\n const position = positions.find((item) => item.symbol === symbol);\n const positionQty = position?.position_qty || 0;\n\n const buyOrdersQty = position?.pending_long_qty || 0;\n const sellOrdersQty = position?.pending_short_qty || 0;\n\n const markPrice = markPrices[symbol] || 0;\n\n //---\n const positionQtyWithOrders = positionQtyWithOrders_by_symbol({\n positionQty,\n buyOrdersQty,\n sellOrdersQty,\n });\n\n //---\n const position_notional_with_orders = positionNotionalWithOrder_by_symbol({\n markPrice,\n positionQtyWithOrders,\n });\n\n //----\n const markPriceDecimal = new Decimal(markPrice);\n\n const imr = IMR({\n positionNotional: markPriceDecimal.mul(positionQty).toNumber(),\n ordersNotional: markPriceDecimal\n .mul(new Decimal(buyOrdersQty).add(sellOrdersQty))\n .toNumber(),\n maxLeverage: maxLeverage({\n symbolLeverage: position?.leverage ?? inputs.maxLeverage,\n accountLeverage: inputs.maxLeverage,\n }),\n IMR_Factor: IMR_Factors[symbol],\n baseIMR: symbolInfo[symbol](\"base_imr\", 0),\n });\n\n return position_notional_with_orders.mul(imr).add(acc).toNumber();\n }, 0);\n\n return total_initial_margin_with_orders;\n}\n\n/**\n * Group orders by symbol, as a symbol can have multiple orders.\n */\nexport function groupOrdersBySymbol(orders: API.Order[]) {\n const symbols: { [key: string]: API.Order[] } = {};\n\n orders.forEach((item) => {\n if (!symbols[item.symbol]) {\n symbols[item.symbol] = [];\n }\n\n symbols[item.symbol].push(item);\n });\n\n return symbols;\n}\n\n/**\n * Extracts all unique symbols from positions and orders.\n * @param positions - An array of position objects.\n * @param orders - An array of order objects.\n * @returns An array of unique symbols.\n */\nexport function extractSymbols(\n positions: Pick<API.Position, \"symbol\">[],\n orders: Pick<API.Order, \"symbol\">[],\n): string[] {\n const symbols = new Set<string>();\n\n positions.forEach((item) => {\n symbols.add(item.symbol);\n });\n\n orders.forEach((item) => {\n symbols.add(item.symbol);\n });\n\n return Array.from(symbols);\n}\n\n//=========== max qty ==================\n\n// function otherIM(inputs: {}): number {}\n\nexport type OtherIMsInputs = {\n // the position list for other symbols except the current symbol\n positions: API.Position[];\n markPrices: { [key: string]: number };\n /**\n * account max leverage\n */\n maxLeverage: number;\n symbolInfo: any;\n IMR_Factors: { [key: string]: number };\n};\n/**\n * Total margin used by other symbols (except the current symbol).\n */\nexport function otherIMs(inputs: OtherIMsInputs): number {\n const {\n // orders,\n positions,\n IMR_Factors,\n symbolInfo,\n markPrices,\n } = inputs;\n\n const symbols = positions.map((item) => item.symbol);\n\n return symbols\n .reduce((acc, cur) => {\n const symbol = cur;\n\n if (typeof markPrices[symbol] === \"undefined\") {\n console.warn(\"markPrices[%s] is undefined\", symbol);\n return acc;\n }\n\n const markPriceDecimal = new Decimal(markPrices[symbol] || 0);\n\n const position = positions.find((item) => item.symbol === symbol);\n\n const positionQty = getQtyFromPositions(positions, symbol);\n const positionNotional = markPriceDecimal.mul(positionQty).toNumber();\n\n const buyOrdersQty = position!.pending_long_qty;\n const sellOrdersQty = position!.pending_short_qty;\n\n const ordersNotional = markPriceDecimal\n .mul(new Decimal(buyOrdersQty).add(sellOrdersQty))\n .toNumber();\n\n const IMR_Factor = IMR_Factors[symbol];\n\n if (!IMR_Factor) {\n console.warn(\"IMR_Factor is not found:\", symbol);\n return acc;\n }\n\n const imr = IMR({\n maxLeverage: maxLeverage({\n symbolLeverage: position!.leverage,\n accountLeverage: inputs.maxLeverage,\n }),\n IMR_Factor,\n baseIMR: symbolInfo[symbol](\"base_imr\", 0),\n positionNotional,\n ordersNotional,\n });\n\n const positionQtyWithOrders = positionQtyWithOrders_by_symbol({\n positionQty,\n buyOrdersQty,\n sellOrdersQty,\n });\n\n const positionNotionalWithOrders = positionNotionalWithOrder_by_symbol({\n markPrice: markPrices[symbol] || 0,\n positionQtyWithOrders,\n });\n\n return acc.add(positionNotionalWithOrders.mul(imr));\n }, zero)\n .toNumber();\n}\n\nexport type MaxQtyInputs = {\n symbol: string;\n\n // Maximum quantity limit for opening a single position, /v1/public/info.base_max\n baseMaxQty: number;\n /**\n * Total collateral of the user (denominated in USDC), can be calculated from totalCollateral.\n * @see totalCollateral\n */\n totalCollateral: number;\n maxLeverage: number;\n baseIMR: number;\n /**\n * @see otherIMs\n */\n otherIMs: number;\n markPrice: number;\n // Quantity of open positions\n positionQty: number;\n // Quantity of long orders\n buyOrdersQty: number;\n // Quantity of short orders\n sellOrdersQty: number;\n\n IMR_Factor: number;\n\n takerFeeRate: number;\n};\n\n/**\n * Maximum order quantity.\n */\nexport function maxQty(\n side: OrderSide,\n inputs: MaxQtyInputs,\n options?: ResultOptions,\n): number {\n if (side === OrderSide.BUY) {\n return maxQtyByLong(inputs);\n }\n return maxQtyByShort(inputs);\n}\n\nexport function maxQtyByLong(\n inputs: Omit<MaxQtyInputs, \"side\">,\n options?: ResultOptions,\n): number {\n try {\n const {\n baseMaxQty,\n totalCollateral,\n otherIMs,\n maxLeverage,\n baseIMR,\n markPrice,\n IMR_Factor,\n positionQty,\n buyOrdersQty,\n takerFeeRate,\n } = inputs;\n\n if (totalCollateral === 0) {\n return 0;\n }\n\n const totalCollateralDecimal = new Decimal(totalCollateral);\n\n const factor_1 = totalCollateralDecimal\n .sub(otherIMs)\n .div(\n new Decimal(takerFeeRate)\n .mul(2)\n .mul(0.0001)\n .add(Math.max(1 / maxLeverage, baseIMR)),\n )\n .div(markPrice)\n .mul(0.995)\n .sub(new Decimal(positionQty).add(buyOrdersQty))\n .toNumber();\n\n if (positionQty === 0 && buyOrdersQty === 0) {\n return Math.min(baseMaxQty, factor_1);\n }\n\n const factor_2 = totalCollateralDecimal\n .sub(otherIMs)\n .div(IMR_Factor)\n .toPower(1 / 1.8)\n .div(markPrice)\n .sub(\n new Decimal(positionQty).add(buyOrdersQty),\n // .abs()\n // .div(new Decimal(takerFeeRate).mul(2).mul(0.0001).add(1))\n )\n .div(new Decimal(takerFeeRate).mul(2).mul(0.0001).add(1))\n .mul(0.995)\n .toNumber();\n\n return Math.min(baseMaxQty, factor_1, factor_2);\n } catch (error) {\n return 0;\n }\n}\n\nexport function maxQtyByShort(\n inputs: Omit<MaxQtyInputs, \"side\">,\n options?: ResultOptions,\n): number {\n try {\n const {\n baseMaxQty,\n totalCollateral,\n otherIMs,\n maxLeverage,\n baseIMR,\n markPrice,\n IMR_Factor,\n positionQty,\n buyOrdersQty,\n sellOrdersQty,\n takerFeeRate,\n } = inputs;\n\n const totalCollateralDecimal = new Decimal(totalCollateral);\n\n const factor_1 = totalCollateralDecimal\n .sub(otherIMs)\n .div(\n new Decimal(takerFeeRate)\n .mul(2)\n .mul(0.0001)\n .add(Math.max(1 / maxLeverage, baseIMR)),\n )\n .div(markPrice)\n .mul(0.995)\n // .add(new Decimal(positionQty).add(sellOrdersQty))\n .add(positionQty)\n .sub(Math.abs(sellOrdersQty))\n .toNumber();\n\n if (positionQty === 0 && sellOrdersQty === 0) {\n return Math.min(baseMaxQty, factor_1);\n }\n\n const factor_2 = totalCollateralDecimal\n .sub(otherIMs)\n .div(IMR_Factor)\n .toPower(1 / 1.8)\n .div(markPrice)\n // .add(\n // new Decimal(positionQty)\n // .add(sellOrdersQty)\n // // .abs()\n // )\n .add(positionQty)\n .sub(sellOrdersQty)\n .div(new Decimal(takerFeeRate).mul(2).mul(0.0001).add(1))\n .mul(0.995)\n .toNumber();\n\n return Math.min(baseMaxQty, factor_1, factor_2);\n } catch (error) {\n return 0;\n }\n}\n\nexport type TotalMarginRatioInputs = {\n totalCollateral: number;\n markPrices: { [key: string]: number };\n positions: API.Position[];\n};\n/**\n * total margin ratio\n */\nexport function totalMarginRatio(\n inputs: TotalMarginRatioInputs,\n dp?: number,\n): number {\n const { totalCollateral, markPrices, positions } = inputs;\n\n if (totalCollateral === 0) {\n return 0;\n }\n\n const totalCollateralDecimal = new Decimal(totalCollateral);\n\n const totalPositionNotional = positions.reduce((acc, cur) => {\n const markPrice = markPrices[cur.symbol] || 0;\n return acc.add(new Decimal(cur.position_qty).mul(markPrice).abs());\n }, zero);\n\n if (totalPositionNotional.eq(zero)) {\n return 0;\n }\n\n return totalCollateralDecimal.div(totalPositionNotional).toNumber();\n}\n\nexport type TotalUnrealizedROIInputs = {\n totalUnrealizedPnL: number;\n totalValue: number;\n};\n\n/**\n * totalUnrealizedROI\n */\nexport function totalUnrealizedROI(inputs: TotalUnrealizedROIInputs) {\n const { totalUnrealizedPnL, totalValue } = inputs;\n\n return new Decimal(totalUnrealizedPnL)\n .div(totalValue - totalUnrealizedPnL)\n .toNumber();\n}\n\n/**\n * current account leverage\n */\nexport function currentLeverage(totalMarginRatio: number) {\n if (totalMarginRatio === 0) {\n return 0;\n }\n return 1 / totalMarginRatio;\n}\n\nexport type AvailableBalanceInputs = {\n USDCHolding: number;\n unsettlementPnL: number;\n};\nexport function availableBalance(inputs: AvailableBalanceInputs) {\n const { USDCHolding, unsettlementPnL } = inputs;\n\n return new Decimal(USDCHolding).add(unsettlementPnL).toNumber();\n}\n\nexport type AccountMMRInputs = {\n // Total Maintenance Margin of all positions of the user (USDC)\n positionsMMR: number;\n /**\n * Notional sum of all positions,\n * positions.totalNotional()\n */\n positionsNotional: number;\n};\n\n/**\n * total maintenance margin ratio\n * @param inputs AccountMMRInputs\n * @returns number|null\n */\nexport function MMR(inputs: AccountMMRInputs): number | null {\n // If the user does not have any positions, return null\n if (inputs.positionsNotional === 0) {\n return null;\n }\n if (inputs.positionsMMR === 0) {\n return null;\n }\n return new Decimal(inputs.positionsMMR)\n .div(inputs.positionsNotional)\n .toNumber();\n}\n\nexport const collateralRatio = (params: {\n baseWeight: number;\n discountFactor: number | null;\n collateralQty: number;\n collateralCap: number;\n indexPrice: number;\n}) => {\n const {\n baseWeight,\n discountFactor,\n collateralQty,\n collateralCap,\n indexPrice,\n } = params;\n\n // if collateralCap is -1, it means the collateral is unlimited\n const cap = collateralCap === -1 ? collateralQty : collateralCap;\n\n const K = new Decimal(1.2);\n const DCF = new Decimal(discountFactor || 0);\n const qty = new Decimal(Math.min(collateralQty, cap));\n\n const notionalAbs = qty.mul(indexPrice).abs();\n const dynamicWeight = DCF.mul(notionalAbs).toPower(IMRFactorPower);\n const result = K.div(new Decimal(1).add(dynamicWeight));\n\n return result.lt(baseWeight) ? result : new Decimal(baseWeight);\n};\n\n/** collateral_value_i = min(collateral_qty_i , collateral_cap_i) * weight_i * index_price_i */\nexport const collateralContribution = (params: {\n collateralQty: number;\n collateralCap: number;\n collateralRatio: number;\n indexPrice: number;\n}) => {\n const { collateralQty, collateralCap, collateralRatio, indexPrice } = params;\n\n // if collateralCap is -1, it means the collateral is unlimited\n const cap = collateralCap === -1 ? collateralQty : collateralCap;\n\n return new Decimal(Math.min(collateralQty, cap))\n .mul(collateralRatio)\n .mul(indexPrice)\n .toNumber();\n};\n\nexport const LTV = (params: {\n usdcBalance: number;\n upnl: number;\n assets: Array<{ qty: number; indexPrice: number; weight: number }>;\n}) => {\n const { usdcBalance, upnl, assets } = params;\n\n const usdcLoss = new Decimal(Math.min(usdcBalance, 0)).abs();\n const upnlLoss = new Decimal(Math.min(upnl, 0)).abs();\n const numerator = usdcLoss.add(upnlLoss);\n\n const collateralSum = assets.reduce<Decimal>((acc, asset) => {\n return acc.add(\n new Decimal(Math.max(asset.qty, 0))\n .mul(new Decimal(asset.indexPrice))\n .mul(new Decimal(asset.weight)),\n );\n }, zero);\n\n const denominator = collateralSum.add(new Decimal(Math.max(upnl, 0)));\n\n if (numerator.isZero() || denominator.isZero()) {\n return 0;\n }\n\n return numerator.div(denominator).toNumber();\n};\n\nexport const maxWithdrawalUSDC = (inputs: {\n USDCBalance: number;\n freeCollateral: Decimal;\n upnl: number;\n}) => {\n const { USDCBalance, freeCollateral, upnl } = inputs;\n const value = Math.min(\n new Decimal(USDCBalance).toNumber(),\n new Decimal(freeCollateral).sub(Math.max(upnl, 0)).toNumber(),\n );\n return Math.max(0, value);\n};\n\nexport const maxWithdrawalOtherCollateral = (inputs: {\n USDCBalance: number;\n collateralQty: number;\n freeCollateral: Decimal;\n indexPrice: number;\n weight: Decimal;\n}) => {\n const { USDCBalance, collateralQty, freeCollateral, indexPrice, weight } =\n inputs;\n const usdcBalance = new Decimal(USDCBalance);\n const denominator = usdcBalance.isNegative()\n ? new Decimal(indexPrice).mul(weight).mul(new Decimal(1).add(0.001))\n : new Decimal(indexPrice).mul(weight);\n if (denominator.isZero()) {\n return zero;\n }\n const qty = new Decimal(collateralQty);\n const maxQtyByValue = new Decimal(freeCollateral).div(denominator);\n return maxQtyByValue.lt(qty) ? maxQtyByValue : qty;\n};\n\nexport const calcMinimumReceived = (inputs: {\n amount: number;\n slippage: number;\n}) => {\n const { amount, slippage } = inputs;\n const slippageRatio = new Decimal(slippage).div(100);\n return new Decimal(amount)\n .mul(new Decimal(1).minus(slippageRatio))\n .toNumber();\n};\n\nexport const maxLeverage = (inputs: {\n symbolLeverage?: number;\n accountLeverage: number;\n}) => {\n const { symbolLeverage, accountLeverage } = inputs;\n\n return Math.min(symbolLeverage ?? accountLeverage, accountLeverage);\n};\n","import { API as orderUtils } from \"@orderly.network/types\";\nimport { Decimal, zero } from \"@orderly.network/utils\";\nimport { notional } from \"./positions\";\n\n/**\n * Maximum price when placing an order\n */\nexport function maxPrice(markprice: number, range: number) {\n return markprice * (1 + range);\n}\n\n/**\n * Minimum price when placing an order\n */\nexport function minPrice(markprice: number, range: number) {\n return markprice * (1 - range);\n}\n\n/**\n * Scope price when placing an order\n * @returns number\n */\nexport function scopePrice(\n price: number,\n scope: number,\n side: \"BUY\" | \"SELL\",\n): number {\n if (side === \"BUY\") {\n return price * (1 - scope);\n }\n return price * (1 + scope);\n}\n\n/**\n * Calculate the order fee\n */\nexport function orderFee(inputs: {\n /**\n * Order quantity\n */\n qty: number;\n price: number;\n futuresTakeFeeRate: number;\n}): number {\n return new Decimal(inputs.qty)\n .mul(inputs.price)\n .mul(inputs.futuresTakeFeeRate)\n .toNumber();\n}\n\nexport type EstimatedLiquidationPriceInputs = {\n totalCollateral: number;\n markPrice: number;\n baseMMR: number;\n baseIMR: number;\n IMR_Factor: number;\n orderFee: number;\n positions: Pick<\n orderUtils.PositionExt,\n \"position_qty\" | \"mark_price\" | \"symbol\" | \"mmr\"\n >[];\n newOrder: {\n symbol: string;\n qty: number;\n price: number;\n };\n};\n\n/**\n * Estimated liquidation price\n * @param inputs\n * @returns\n */\nexport function estLiqPrice(inputs: EstimatedLiquidationPriceInputs): number {\n const {\n positions,\n newOrder,\n totalCollateral,\n markPrice,\n baseIMR,\n baseMMR,\n orderFee,\n IMR_Factor,\n } = inputs;\n // opened positions for the symbol\n let currentPosition:\n | Pick<\n orderUtils.PositionExt,\n \"position_qty\" | \"mark_price\" | \"symbol\" | \"mmr\"\n >\n | undefined = undefined;\n\n let newTotalMM = zero;\n\n const hasPosition =\n positions.filter((item) => item.position_qty > 0).length > 0;\n\n const basePrice = hasPosition ? markPrice : newOrder.price;\n\n const newOrderNotional = new Decimal(newOrder.qty).mul(newOrder.price);\n\n for (let index = 0; index < positions.length; index++) {\n const position = positions[index];\n let notional = new Decimal(position.position_qty).mul(position.mark_price);\n if (newOrder.symbol === position.symbol) {\n currentPosition = position;\n notional = notional.add(newOrderNotional);\n }\n\n newTotalMM = newTotalMM.add(notional.abs().mul(position.mmr));\n }\n\n // if no position\n if (!currentPosition) {\n newTotalMM = newTotalMM.add(newOrderNotional.mul(baseMMR));\n }\n\n const newMMR = Math.max(\n baseMMR,\n new Decimal(baseMMR)\n .div(baseIMR)\n .mul(IMR_Factor)\n .mul(\n newOrderNotional\n .add(\n !!currentPosition\n ? new Decimal(currentPosition.position_qty).mul(\n currentPosition.mark_price,\n )\n : zero,\n )\n .abs(),\n )\n .toPower(4 / 5)\n .toNumber(),\n );\n\n // console.log(\"new MMR\", newMMR, newTotalMM.toNumber());\n\n const newQty = new Decimal(newOrder.qty).add(\n currentPosition?.position_qty ?? 0,\n );\n\n if (newQty.eq(0)) {\n return 0;\n }\n\n const denominator = newQty.abs().mul(newMMR).sub(newQty);\n\n if (denominator.eq(zero)) {\n return 0;\n }\n\n const price = new Decimal(basePrice)\n .add(\n new Decimal(totalCollateral)\n .sub(newTotalMM)\n .sub(orderFee)\n .div(denominator),\n )\n .toNumber();\n\n return Math.max(0, price);\n}\n\nexport type EstimatedLeverageInputs = {\n totalCollateral: number;\n positions: Pick<\n orderUtils.PositionExt,\n \"position_qty\" | \"mark_price\" | \"symbol\"\n >[];\n newOrder: {\n symbol: string;\n qty: number;\n price: number;\n };\n};\n\n/**\n * Estimated leverage\n * @param inputs EstimtedLeverageInputs\n * @returns number\n */\nexport function estLeverage(inputs: EstimatedLeverageInputs): number | null {\n const { totalCollateral, positions, newOrder } = inputs;\n if (totalCollateral <= 0) {\n return null;\n }\n let hasPosition = false;\n let sumPositionNotional = positions.reduce((acc, cur) => {\n let count = new Decimal(cur.position_qty).mul(cur.mark_price);\n // acc = acc.add(\n // new Decimal(cur.position_qty).mul(cur.mark_price)\n // // .abs()\n // );\n\n if (cur.symbol === newOrder.symbol) {\n hasPosition = true;\n // acc = acc.add(new Decimal(newOrder.qty).mul(newOrder.price));\n count = count.add(new Decimal(newOrder.qty).mul(newOrder.price));\n }\n\n return acc.add(count.abs());\n }, zero);\n\n if (!hasPosition) {\n sumPositionNotional = sumPositionNotional.add(\n new Decimal(newOrder.qty).mul(newOrder.price).abs(),\n );\n }\n\n if (sumPositionNotional.eq(zero)) {\n return null;\n }\n\n const totalMarginRatio = new Decimal(totalCollateral).div(\n sumPositionNotional,\n );\n\n return new Decimal(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G,sBAAQ,eAAe,EAC1C,SAAS;AACd;","names":["IMR","totalCollateral","MMR","totalNotional","notional","MMR","import_utils","totalUnsettlementPnL","unsettlementPnL","maxLeverage","totalCollateral","otherIMs","totalUnrealizedPnL","totalValue","totalMarginRatio","MMR","collateralRatio","freeCollateral","import_utils","totalCollateral","orderFee","notional","totalMarginRatio"]}
|
package/dist/index.mjs
CHANGED
|
@@ -7,9 +7,9 @@ var __export = (target, all) => {
|
|
|
7
7
|
// src/version.ts
|
|
8
8
|
if (typeof window !== "undefined") {
|
|
9
9
|
window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};
|
|
10
|
-
window.__ORDERLY_VERSION__["@orderly.network/perp"] = "4.
|
|
10
|
+
window.__ORDERLY_VERSION__["@orderly.network/perp"] = "4.7.0";
|
|
11
11
|
}
|
|
12
|
-
var version_default = "4.
|
|
12
|
+
var version_default = "4.7.0";
|
|
13
13
|
|
|
14
14
|
// src/positions.ts
|
|
15
15
|
var positions_exports = {};
|
|
@@ -22,6 +22,8 @@ __export(positions_exports, {
|
|
|
22
22
|
estPriceFromOffsetForTP: () => estPriceFromOffsetForTP,
|
|
23
23
|
liqPrice: () => liqPrice,
|
|
24
24
|
maintenanceMargin: () => maintenanceMargin,
|
|
25
|
+
maxPositionLeverage: () => maxPositionLeverage,
|
|
26
|
+
maxPositionNotional: () => maxPositionNotional,
|
|
25
27
|
notional: () => notional,
|
|
26
28
|
totalNotional: () => totalNotional,
|
|
27
29
|
totalUnrealizedPnL: () => totalUnrealizedPnL,
|
|
@@ -136,6 +138,14 @@ function estPriceFromOffsetForTP(inputs) {
|
|
|
136
138
|
function estPnLForSL(inputs) {
|
|
137
139
|
return 0;
|
|
138
140
|
}
|
|
141
|
+
function maxPositionNotional(inputs) {
|
|
142
|
+
const { leverage, IMRFactor } = inputs;
|
|
143
|
+
return new Decimal(1).div(new Decimal(leverage).mul(IMRFactor)).pow(1 / 0.8).toNumber();
|
|
144
|
+
}
|
|
145
|
+
function maxPositionLeverage(inputs) {
|
|
146
|
+
const { IMRFactor, notional: notional2 } = inputs;
|
|
147
|
+
return new Decimal(1).div(new Decimal(IMRFactor).mul(new Decimal(notional2).pow(0.8))).toNumber();
|
|
148
|
+
}
|
|
139
149
|
|
|
140
150
|
// src/account.ts
|
|
141
151
|
var account_exports = {};
|
|
@@ -156,6 +166,7 @@ __export(account_exports, {
|
|
|
156
166
|
getQtyFromPositions: () => getQtyFromPositions,
|
|
157
167
|
groupOrdersBySymbol: () => groupOrdersBySymbol,
|
|
158
168
|
initialMarginWithOrder: () => initialMarginWithOrder,
|
|
169
|
+
maxLeverage: () => maxLeverage,
|
|
159
170
|
maxQty: () => maxQty,
|
|
160
171
|
maxQtyByLong: () => maxQtyByLong,
|
|
161
172
|
maxQtyByShort: () => maxQtyByShort,
|
|
@@ -210,7 +221,7 @@ function positionQtyWithOrders_by_symbol(inputs) {
|
|
|
210
221
|
}
|
|
211
222
|
function IMR(inputs) {
|
|
212
223
|
const {
|
|
213
|
-
maxLeverage,
|
|
224
|
+
maxLeverage: maxLeverage2,
|
|
214
225
|
baseIMR,
|
|
215
226
|
IMR_Factor,
|
|
216
227
|
positionNotional,
|
|
@@ -218,7 +229,7 @@ function IMR(inputs) {
|
|
|
218
229
|
IMR_factor_power = IMRFactorPower
|
|
219
230
|
} = inputs;
|
|
220
231
|
return Math.max(
|
|
221
|
-
1 /
|
|
232
|
+
1 / maxLeverage2,
|
|
222
233
|
baseIMR,
|
|
223
234
|
new Decimal2(IMR_Factor).mul(
|
|
224
235
|
new Decimal2(positionNotional).add(orderNotional).abs().toPower(IMR_factor_power)
|
|
@@ -262,7 +273,7 @@ function totalInitialMarginWithOrders(inputs) {
|
|
|
262
273
|
orders,
|
|
263
274
|
markPrices,
|
|
264
275
|
IMR_Factors,
|
|
265
|
-
maxLeverage,
|
|
276
|
+
maxLeverage: maxLeverage2,
|
|
266
277
|
symbolInfo
|
|
267
278
|
} = inputs;
|
|
268
279
|
const symbols = extractSymbols(positions, orders);
|
|
@@ -289,7 +300,7 @@ function totalInitialMarginWithOrders(inputs) {
|
|
|
289
300
|
const imr = IMR({
|
|
290
301
|
positionNotional: markPriceDecimal.mul(positionQty).toNumber(),
|
|
291
302
|
ordersNotional: markPriceDecimal.mul(new Decimal2(buyOrdersQty).add(sellOrdersQty)).toNumber(),
|
|
292
|
-
maxLeverage,
|
|
303
|
+
maxLeverage: maxLeverage2,
|
|
293
304
|
IMR_Factor: IMR_Factors[symbol],
|
|
294
305
|
baseIMR: symbolInfo[symbol]("base_imr", 0)
|
|
295
306
|
});
|
|
@@ -298,9 +309,10 @@ function totalInitialMarginWithOrders(inputs) {
|
|
|
298
309
|
return total_initial_margin_with_orders;
|
|
299
310
|
}
|
|
300
311
|
function totalInitialMarginWithQty(inputs) {
|
|
301
|
-
const { positions, markPrices, IMR_Factors, symbolInfo
|
|
312
|
+
const { positions, markPrices, IMR_Factors, symbolInfo } = inputs;
|
|
302
313
|
const symbols = positions.map((item) => item.symbol);
|
|
303
314
|
const total_initial_margin_with_orders = symbols.reduce((acc, cur) => {
|
|
315
|
+
var _a;
|
|
304
316
|
const symbol = cur;
|
|
305
317
|
const position = positions.find((item) => item.symbol === symbol);
|
|
306
318
|
const positionQty = (position == null ? void 0 : position.position_qty) || 0;
|
|
@@ -320,7 +332,10 @@ function totalInitialMarginWithQty(inputs) {
|
|
|
320
332
|
const imr = IMR({
|
|
321
333
|
positionNotional: markPriceDecimal.mul(positionQty).toNumber(),
|
|
322
334
|
ordersNotional: markPriceDecimal.mul(new Decimal2(buyOrdersQty).add(sellOrdersQty)).toNumber(),
|
|
323
|
-
maxLeverage
|
|
335
|
+
maxLeverage: maxLeverage({
|
|
336
|
+
symbolLeverage: (_a = position == null ? void 0 : position.leverage) != null ? _a : inputs.maxLeverage,
|
|
337
|
+
accountLeverage: inputs.maxLeverage
|
|
338
|
+
}),
|
|
324
339
|
IMR_Factor: IMR_Factors[symbol],
|
|
325
340
|
baseIMR: symbolInfo[symbol]("base_imr", 0)
|
|
326
341
|
});
|
|
@@ -352,7 +367,6 @@ function otherIMs(inputs) {
|
|
|
352
367
|
const {
|
|
353
368
|
// orders,
|
|
354
369
|
positions,
|
|
355
|
-
maxLeverage,
|
|
356
370
|
IMR_Factors,
|
|
357
371
|
symbolInfo,
|
|
358
372
|
markPrices
|
|
@@ -377,7 +391,10 @@ function otherIMs(inputs) {
|
|
|
377
391
|
return acc;
|
|
378
392
|
}
|
|
379
393
|
const imr = IMR({
|
|
380
|
-
maxLeverage
|
|
394
|
+
maxLeverage: maxLeverage({
|
|
395
|
+
symbolLeverage: position.leverage,
|
|
396
|
+
accountLeverage: inputs.maxLeverage
|
|
397
|
+
}),
|
|
381
398
|
IMR_Factor,
|
|
382
399
|
baseIMR: symbolInfo[symbol]("base_imr", 0),
|
|
383
400
|
positionNotional,
|
|
@@ -407,7 +424,7 @@ function maxQtyByLong(inputs, options) {
|
|
|
407
424
|
baseMaxQty,
|
|
408
425
|
totalCollateral: totalCollateral2,
|
|
409
426
|
otherIMs: otherIMs2,
|
|
410
|
-
maxLeverage,
|
|
427
|
+
maxLeverage: maxLeverage2,
|
|
411
428
|
baseIMR,
|
|
412
429
|
markPrice,
|
|
413
430
|
IMR_Factor,
|
|
@@ -420,7 +437,7 @@ function maxQtyByLong(inputs, options) {
|
|
|
420
437
|
}
|
|
421
438
|
const totalCollateralDecimal = new Decimal2(totalCollateral2);
|
|
422
439
|
const factor_1 = totalCollateralDecimal.sub(otherIMs2).div(
|
|
423
|
-
new Decimal2(takerFeeRate).mul(2).mul(1e-4).add(Math.max(1 /
|
|
440
|
+
new Decimal2(takerFeeRate).mul(2).mul(1e-4).add(Math.max(1 / maxLeverage2, baseIMR))
|
|
424
441
|
).div(markPrice).mul(0.995).sub(new Decimal2(positionQty).add(buyOrdersQty)).toNumber();
|
|
425
442
|
if (positionQty === 0 && buyOrdersQty === 0) {
|
|
426
443
|
return Math.min(baseMaxQty, factor_1);
|
|
@@ -441,7 +458,7 @@ function maxQtyByShort(inputs, options) {
|
|
|
441
458
|
baseMaxQty,
|
|
442
459
|
totalCollateral: totalCollateral2,
|
|
443
460
|
otherIMs: otherIMs2,
|
|
444
|
-
maxLeverage,
|
|
461
|
+
maxLeverage: maxLeverage2,
|
|
445
462
|
baseIMR,
|
|
446
463
|
markPrice,
|
|
447
464
|
IMR_Factor,
|
|
@@ -452,7 +469,7 @@ function maxQtyByShort(inputs, options) {
|
|
|
452
469
|
} = inputs;
|
|
453
470
|
const totalCollateralDecimal = new Decimal2(totalCollateral2);
|
|
454
471
|
const factor_1 = totalCollateralDecimal.sub(otherIMs2).div(
|
|
455
|
-
new Decimal2(takerFeeRate).mul(2).mul(1e-4).add(Math.max(1 /
|
|
472
|
+
new Decimal2(takerFeeRate).mul(2).mul(1e-4).add(Math.max(1 / maxLeverage2, baseIMR))
|
|
456
473
|
).div(markPrice).mul(0.995).add(positionQty).sub(Math.abs(sellOrdersQty)).toNumber();
|
|
457
474
|
if (positionQty === 0 && sellOrdersQty === 0) {
|
|
458
475
|
return Math.min(baseMaxQty, factor_1);
|
|
@@ -563,6 +580,10 @@ var calcMinimumReceived = (inputs) => {
|
|
|
563
580
|
const slippageRatio = new Decimal2(slippage).div(100);
|
|
564
581
|
return new Decimal2(amount).mul(new Decimal2(1).minus(slippageRatio)).toNumber();
|
|
565
582
|
};
|
|
583
|
+
var maxLeverage = (inputs) => {
|
|
584
|
+
const { symbolLeverage, accountLeverage } = inputs;
|
|
585
|
+
return Math.min(symbolLeverage != null ? symbolLeverage : accountLeverage, accountLeverage);
|
|
586
|
+
};
|
|
566
587
|
|
|
567
588
|
// src/order.ts
|
|
568
589
|
var order_exports = {};
|
|
@@ -635,8 +656,12 @@ function estLiqPrice(inputs) {
|
|
|
635
656
|
if (newQty.eq(0)) {
|
|
636
657
|
return 0;
|
|
637
658
|
}
|
|
659
|
+
const denominator = newQty.abs().mul(newMMR).sub(newQty);
|
|
660
|
+
if (denominator.eq(zero3)) {
|
|
661
|
+
return 0;
|
|
662
|
+
}
|
|
638
663
|
const price = new Decimal3(basePrice).add(
|
|
639
|
-
new Decimal3(totalCollateral2).sub(newTotalMM).sub(orderFee2).div(
|
|
664
|
+
new Decimal3(totalCollateral2).sub(newTotalMM).sub(orderFee2).div(denominator)
|
|
640
665
|
).toNumber();
|
|
641
666
|
return Math.max(0, price);
|
|
642
667
|
}
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/version.ts","../src/positions.ts","../src/constants.ts","../src/account.ts","../src/order.ts"],"sourcesContent":["declare global {\n interface Window {\n __ORDERLY_VERSION__?: {\n [key: string]: string;\n };\n }\n}\nif (typeof window !== \"undefined\") {\n window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};\n window.__ORDERLY_VERSION__[\"@orderly.network/perp\"] = \"4.6.3\";\n}\n\nexport default \"4.6.3\";\n","import { API } from \"@orderly.network/types\";\nimport { Decimal, zero } from \"@orderly.network/utils\";\nimport { IMRFactorPower } from \"./constants\";\n\n/**\n * Calculates the notional value of a single position.\n * @param qty The quantity of the position.\n * @param mark_price The price of the position.\n * @returns The notional value of the position.\n */\nexport function notional(qty: number, mark_price: number): number {\n return new Decimal(qty).mul(mark_price).abs().toNumber();\n}\n\n/**\n * Calculates the total notional value of all positions.\n * @param positions The array of positions.\n * @returns The total notional value of all positions.\n */\nexport function totalNotional(positions: API.Position[]): number {\n return positions.reduce((acc, cur) => {\n return acc + notional(cur.position_qty, cur.mark_price);\n }, 0);\n}\n\nexport type UnrealPnLInputs = {\n markPrice: number;\n openPrice: number;\n qty: number;\n};\n\n/**\n * Calculates the unrealized profit or loss of a single position.\n * @param inputs The inputs for calculating the unrealized profit or loss.\n * @returns The unrealized profit or loss of the position.\n */\nexport function unrealizedPnL(inputs: UnrealPnLInputs): number {\n return new Decimal(inputs.qty)\n .mul(inputs.markPrice - inputs.openPrice)\n .toNumber();\n}\n\nexport type UnrealPnLROIInputs = {\n positionQty: number;\n openPrice: number;\n IMR: number;\n unrealizedPnL: number;\n};\n\n/**\n * Calculates the return on investment (ROI) of a single position's unrealized profit or loss.\n * @param inputs The inputs for calculating the ROI.\n * @returns The ROI of the position's unrealized profit or loss.\n */\nexport function unrealizedPnLROI(inputs: UnrealPnLROIInputs): number {\n const { openPrice, IMR } = inputs;\n\n if (\n inputs.unrealizedPnL === 0 ||\n inputs.positionQty === 0 ||\n openPrice === 0 ||\n IMR === 0\n )\n return 0;\n\n return new Decimal(inputs.unrealizedPnL)\n .div(new Decimal(Math.abs(inputs.positionQty)).mul(openPrice).mul(IMR))\n .toNumber();\n}\n\n/**\n * Calculates the total unrealized profit or loss of all positions.\n * @param positions The array of positions.\n * @returns The total unrealized profit or loss of all positions.\n */\nexport function totalUnrealizedPnL(positions: API.Position[]): number {\n return positions.reduce((acc, cur) => {\n return (\n acc +\n unrealizedPnL({\n qty: cur.position_qty,\n openPrice: cur.average_open_price,\n markPrice: cur.mark_price,\n })\n );\n }, 0);\n}\n\nexport type LiqPriceInputs = {\n markPrice: number;\n totalCollateral: number;\n positionQty: number;\n positions: Pick<API.PositionExt, \"position_qty\" | \"mark_price\" | \"mmr\">[];\n MMR: number;\n};\n\n/**\n * Calculates the liquidation price of a single position.\n * @param inputs The inputs for calculating the liquidation price.\n * @returns The liquidation price of the position.\n */\nexport function liqPrice(inputs: LiqPriceInputs): number | null {\n const { markPrice, totalCollateral, positions, positionQty, MMR } = inputs;\n\n // console.log(\"inputs\", inputs);\n\n if (positionQty === 0 || totalCollateral === 0) {\n return null;\n }\n\n // totalNotional of all poisitions\n const totalNotional = positions.reduce<Decimal>((acc, cur) => {\n return acc.add(\n new Decimal(notional(cur.position_qty, cur.mark_price)).mul(cur.mmr),\n );\n }, zero);\n\n return Math.max(\n new Decimal(markPrice)\n .add(\n new Decimal(totalCollateral)\n .sub(totalNotional)\n .div(new Decimal(positionQty).abs().mul(MMR).sub(positionQty)),\n )\n .toNumber(),\n 0,\n );\n}\n\nexport type MMInputs = {\n positionQty: number;\n markPrice: number;\n MMR: number;\n};\n\n/**\n * Calculates the maintenance margin of a position.\n * @param inputs The inputs for calculating the maintenance margin.\n * @returns The maintenance margin of the position.\n */\nexport function maintenanceMargin(inputs: MMInputs) {\n const { positionQty, markPrice, MMR } = inputs;\n\n return new Decimal(positionQty).mul(markPrice).mul(MMR).abs().toNumber();\n}\n\nexport type UnsettlementPnLInputs = {\n positionQty: number;\n markPrice: number;\n costPosition: number;\n sumUnitaryFunding: number;\n lastSumUnitaryFunding: number;\n};\n\n/**\n * Calculates the unrealized profit or loss of each position.\n * @param inputs The inputs for calculating the unrealized profit or loss.\n * @returns The unrealized profit or loss of each position.\n */\nexport function unsettlementPnL(inputs: UnsettlementPnLInputs): number {\n const {\n positionQty,\n markPrice,\n costPosition,\n sumUnitaryFunding,\n lastSumUnitaryFunding,\n } = inputs;\n\n const qty = new Decimal(positionQty);\n\n return qty\n .mul(markPrice)\n .sub(costPosition)\n .sub(qty.mul(new Decimal(sumUnitaryFunding).sub(lastSumUnitaryFunding)))\n .toNumber();\n}\n\nexport type TotalUnsettlementPnLInputs = {\n positions: (API.Position & {\n sum_unitary_funding: number;\n })[];\n sumUnitaryFunding: number;\n};\n\n/**\n * Calculates the total unrealized profit or loss of all positions.\n * @param positions The array of positions.\n * @returns The total unrealized profit or loss of all positions.\n */\nexport function totalUnsettlementPnL(\n positions: (API.Position & { sum_unitary_funding: number })[],\n): number {\n if (!Array.isArray(positions) || positions.length === 0) {\n return 0;\n }\n\n return positions.reduce((acc, cur) => {\n return (\n acc +\n unsettlementPnL({\n positionQty: cur.position_qty,\n markPrice: cur.mark_price,\n costPosition: cur.cost_position,\n sumUnitaryFunding: cur.sum_unitary_funding,\n lastSumUnitaryFunding: cur.last_sum_unitary_funding,\n })\n );\n }, 0);\n}\n\nexport type MMRInputs = {\n baseMMR: number;\n baseIMR: number;\n IMRFactor: number;\n positionNotional: number;\n IMR_factor_power: number;\n};\n\n/**\n * Calculates the maintenance margin requirement (MMR) of a position.\n * @param inputs The inputs for calculating the MMR.\n * @returns The MMR of the position.\n */\nexport function MMR(inputs: MMRInputs): number {\n const {\n baseMMR,\n baseIMR,\n IMRFactor,\n positionNotional,\n IMR_factor_power = IMRFactorPower,\n } = inputs;\n return Math.max(\n baseMMR,\n new Decimal(baseMMR)\n .div(baseIMR)\n .mul(IMRFactor)\n .mul(Math.pow(Math.abs(positionNotional), IMR_factor_power))\n // .toPower(IMR_factor_power)\n .toNumber(),\n );\n}\n\n/**\n * Calculates the profit or loss for take profit.\n * @returns The profit or loss for take profit.\n */\nexport function estPnLForTP(inputs: {\n positionQty: number;\n entryPrice: number;\n price: number;\n}): number {\n return new Decimal(inputs.positionQty)\n .mul(new Decimal(inputs.price).sub(inputs.entryPrice))\n .toNumber();\n}\n\n/**\n * Calculates the estimated price for take profit.\n */\nexport function estPriceForTP(inputs: {\n positionQty: number;\n entryPrice: number;\n pnl: number;\n}): number {\n return new Decimal(inputs.pnl)\n .add(inputs.entryPrice)\n .div(inputs.positionQty)\n .toNumber();\n}\n\n/**\n * Calculates the estimated offset for take profit.\n */\nexport function estOffsetForTP(inputs: {\n price: number;\n entryPrice: number;\n}): number {\n return new Decimal(inputs.price).div(inputs.entryPrice).toNumber();\n}\n\n/**\n * Calculates the estimated price from offset for take profit.\n */\nexport function estPriceFromOffsetForTP(inputs: {\n offset: number;\n entryPrice: number;\n}): number {\n return new Decimal(inputs.offset).add(inputs.entryPrice).toNumber();\n}\n\n/**\n * Calculates the PnL for stop loss.\n */\nexport function estPnLForSL(inputs: {\n positionQty: number;\n entryPrice: number;\n}): number {\n return 0;\n}\n","/**\n * The power of the IMR factor.\n * @constant\n * @default\n */\nexport const IMRFactorPower = 4 / 5;\n","import { API, OrderSide } from \"@orderly.network/types\";\nimport { Decimal, zero } from \"@orderly.network/utils\";\nimport { IMRFactorPower } from \"./constants\";\n\nexport type ResultOptions = {\n dp: number;\n};\n\nexport type TotalValueInputs = {\n totalUnsettlementPnL: number;\n\n USDCHolding: number;\n nonUSDCHolding: {\n holding: number;\n indexPrice: number;\n }[];\n};\n\n/**\n * User's total asset value (denominated in USDC), including assets that cannot be used as collateral.\n */\nexport function totalValue(inputs: TotalValueInputs): Decimal {\n const { totalUnsettlementPnL, USDCHolding, nonUSDCHolding } = inputs;\n const nonUSDCHoldingValue = nonUSDCHolding.reduce((acc, cur) => {\n return new Decimal(cur.holding).mul(cur.indexPrice).add(acc);\n }, zero);\n return nonUSDCHoldingValue.add(USDCHolding).add(totalUnsettlementPnL);\n}\n\n/**\n * Total value of available collateral in the user's account (denominated in USDC).\n */\nexport type FreeCollateralInputs = {\n // Total collateral\n totalCollateral: Decimal;\n // Total initial margin with orders\n totalInitialMarginWithOrders: number;\n};\n/**\n * Calculate free collateral.\n */\nexport function freeCollateral(inputs: FreeCollateralInputs): Decimal {\n const value = inputs.totalCollateral.sub(inputs.totalInitialMarginWithOrders);\n // free collateral cannot be less than 0\n return value.isNegative() ? zero : value;\n}\n\nexport type TotalCollateralValueInputs = {\n USDCHolding: number;\n nonUSDCHolding: {\n holding: number;\n indexPrice: number;\n collateralCap: number;\n collateralRatio: Decimal;\n }[];\n unsettlementPnL: number;\n};\n\n/**\n * Calculate total collateral.\n */\nexport function totalCollateral(inputs: TotalCollateralValueInputs): Decimal {\n const { USDCHolding, nonUSDCHolding, unsettlementPnL } = inputs;\n const nonUSDCHoldingValue = nonUSDCHolding.reduce<Decimal>((acc, cur) => {\n const finalHolding = Math.min(cur.holding, cur.collateralCap);\n const value = new Decimal(finalHolding)\n .mul(cur.collateralRatio)\n .mul(cur.indexPrice);\n return acc.add(value);\n }, zero);\n\n return new Decimal(USDCHolding).add(nonUSDCHoldingValue).add(unsettlementPnL);\n}\n\nexport function initialMarginWithOrder() {}\n\nexport type PositionNotionalWithOrderInputs = {\n markPrice: number;\n positionQtyWithOrders: number;\n};\n/**\n * Sum of notional value for a symbol's position and orders.\n */\nexport function positionNotionalWithOrder_by_symbol(\n inputs: PositionNotionalWithOrderInputs,\n): Decimal {\n return new Decimal(inputs.markPrice).mul(inputs.positionQtyWithOrders);\n}\n\nexport type PositionQtyWithOrderInputs = {\n positionQty: number;\n // Total quantity of buy orders for a symbol\n buyOrdersQty: number;\n // Total quantity of sell orders for a symbol\n sellOrdersQty: number;\n};\n/**\n * Sum of position quantity and orders quantity for a symbol.\n */\nexport function positionQtyWithOrders_by_symbol(\n inputs: PositionQtyWithOrderInputs,\n): number {\n const { positionQty, buyOrdersQty, sellOrdersQty } = inputs;\n const positionQtyDecimal = new Decimal(positionQty);\n const qty = Math.max(\n positionQtyDecimal.add(buyOrdersQty).abs().toNumber(),\n positionQtyDecimal.sub(sellOrdersQty).abs().toNumber(),\n );\n\n return qty;\n}\n\nexport type IMRInputs = {\n maxLeverage: number;\n baseIMR: number;\n IMR_Factor: number;\n positionNotional: number;\n ordersNotional: number;\n IMR_factor_power?: number;\n};\n\n/**\n * Initial margin rate for a symbol.\n * Max(1 / Max Account Leverage, Base IMR i, IMR Factor i * Abs(Position Notional i + Order Notional i)^(4/5))\n */\nexport function IMR(inputs: IMRInputs): number {\n const {\n maxLeverage,\n baseIMR,\n IMR_Factor,\n positionNotional,\n ordersNotional: orderNotional,\n IMR_factor_power = IMRFactorPower,\n } = inputs;\n return Math.max(\n 1 / maxLeverage,\n baseIMR,\n new Decimal(IMR_Factor)\n .mul(\n new Decimal(positionNotional)\n .add(orderNotional)\n .abs()\n .toPower(IMR_factor_power),\n )\n .toNumber(),\n );\n}\n\nexport function buyOrdersFilter_by_symbol(\n orders: API.Order[],\n symbol: string,\n): API.Order[] {\n return orders.filter(\n (item) => item.symbol === symbol && item.side === OrderSide.BUY,\n );\n}\n\nexport function sellOrdersFilter_by_symbol(\n orders: API.Order[],\n symbol: string,\n): API.Order[] {\n return orders.filter(\n (item) => item.symbol === symbol && item.side === OrderSide.SELL,\n );\n}\n\n/**\n * Get the quantity of a specified symbol from the list of positions.\n */\nexport function getQtyFromPositions(\n positions: API.Position[],\n symbol: string,\n): number {\n if (!positions) {\n return 0;\n }\n const position = positions.find((item) => item.symbol === symbol);\n return position?.position_qty || 0;\n}\n\n/**\n * Get the quantity of long and short orders for a specified symbol from the list of orders.\n */\nexport function getQtyFromOrdersBySide(\n orders: API.Order[],\n symbol: string,\n side: OrderSide,\n): number {\n const ordersBySide =\n side === OrderSide.SELL\n ? sellOrdersFilter_by_symbol(orders, symbol)\n : buyOrdersFilter_by_symbol(orders, symbol);\n return ordersBySide.reduce((acc, cur) => {\n return acc + cur.quantity;\n }, 0);\n}\n\nexport function getPositonsAndOrdersNotionalBySymbol(inputs: {\n positions: API.Position[];\n orders: API.Order[];\n symbol: string;\n markPrice: number;\n}): number {\n const { positions, orders, symbol, markPrice } = inputs;\n const positionQty = getQtyFromPositions(positions, symbol);\n const buyOrdersQty = getQtyFromOrdersBySide(orders, symbol, OrderSide.BUY);\n const sellOrdersQty = getQtyFromOrdersBySide(orders, symbol, OrderSide.SELL);\n\n const markPriceDecimal = new Decimal(markPrice);\n\n return markPriceDecimal\n .mul(positionQty)\n .add(markPriceDecimal.mul(new Decimal(buyOrdersQty).add(sellOrdersQty)))\n .abs()\n .toNumber();\n}\n\nexport type TotalInitialMarginWithOrdersInputs = {\n positions: API.Position[];\n orders: API.Order[];\n // account: API.AccountInfo;\n markPrices: { [key: string]: number };\n symbolInfo: any;\n IMR_Factors: { [key: string]: number };\n} & Pick<IMRInputs, \"maxLeverage\">;\n\n/**\n * Calculate the total initial margin used by the user (including positions and orders).\n */\nexport function totalInitialMarginWithOrders(\n inputs: TotalInitialMarginWithOrdersInputs,\n): number {\n const {\n positions,\n orders,\n markPrices,\n IMR_Factors,\n maxLeverage,\n symbolInfo,\n } = inputs;\n\n const symbols = extractSymbols(positions, orders);\n\n const total_initial_margin_with_orders = symbols.reduce((acc, cur) => {\n const symbol = cur;\n const positionQty = getQtyFromPositions(positions, symbol);\n const buyOrdersQty = getQtyFromOrdersBySide(orders, symbol, OrderSide.BUY);\n const sellOrdersQty = getQtyFromOrdersBySide(\n orders,\n symbol,\n OrderSide.SELL,\n );\n\n const markPrice = markPrices[symbol] || 0;\n\n //---\n const positionQtyWithOrders = positionQtyWithOrders_by_symbol({\n positionQty,\n buyOrdersQty,\n sellOrdersQty,\n });\n\n //---\n const position_notional_with_orders = positionNotionalWithOrder_by_symbol({\n markPrice,\n positionQtyWithOrders,\n });\n\n //----\n const markPriceDecimal = new Decimal(markPrice);\n\n const imr = IMR({\n positionNotional: markPriceDecimal.mul(positionQty).toNumber(),\n ordersNotional: markPriceDecimal\n .mul(new Decimal(buyOrdersQty).add(sellOrdersQty))\n .toNumber(),\n maxLeverage,\n IMR_Factor: IMR_Factors[symbol],\n baseIMR: symbolInfo[symbol](\"base_imr\", 0),\n });\n\n return position_notional_with_orders.mul(imr).add(acc).toNumber();\n }, 0);\n\n return total_initial_margin_with_orders;\n}\n\nexport function totalInitialMarginWithQty(inputs: {\n positions: API.Position[];\n // account: API.AccountInfo;\n markPrices: { [key: string]: number };\n symbolInfo: any;\n IMR_Factors: { [key: string]: number };\n maxLeverage: number;\n}) {\n const { positions, markPrices, IMR_Factors, symbolInfo, maxLeverage } =\n inputs;\n const symbols = positions.map((item) => item.symbol);\n\n const total_initial_margin_with_orders = symbols.reduce((acc, cur) => {\n const symbol = cur;\n const position = positions.find((item) => item.symbol === symbol);\n const positionQty = position?.position_qty || 0;\n\n const buyOrdersQty = position?.pending_long_qty || 0;\n const sellOrdersQty = position?.pending_short_qty || 0;\n\n const markPrice = markPrices[symbol] || 0;\n\n //---\n const positionQtyWithOrders = positionQtyWithOrders_by_symbol({\n positionQty,\n buyOrdersQty,\n sellOrdersQty,\n });\n\n //---\n const position_notional_with_orders = positionNotionalWithOrder_by_symbol({\n markPrice,\n positionQtyWithOrders,\n });\n\n //----\n const markPriceDecimal = new Decimal(markPrice);\n\n const imr = IMR({\n positionNotional: markPriceDecimal.mul(positionQty).toNumber(),\n ordersNotional: markPriceDecimal\n .mul(new Decimal(buyOrdersQty).add(sellOrdersQty))\n .toNumber(),\n maxLeverage,\n IMR_Factor: IMR_Factors[symbol],\n baseIMR: symbolInfo[symbol](\"base_imr\", 0),\n });\n\n return position_notional_with_orders.mul(imr).add(acc).toNumber();\n }, 0);\n\n return total_initial_margin_with_orders;\n}\n\n/**\n * Group orders by symbol, as a symbol can have multiple orders.\n */\nexport function groupOrdersBySymbol(orders: API.Order[]) {\n const symbols: { [key: string]: API.Order[] } = {};\n\n orders.forEach((item) => {\n if (!symbols[item.symbol]) {\n symbols[item.symbol] = [];\n }\n\n symbols[item.symbol].push(item);\n });\n\n return symbols;\n}\n\n/**\n * Extracts all unique symbols from positions and orders.\n * @param positions - An array of position objects.\n * @param orders - An array of order objects.\n * @returns An array of unique symbols.\n */\nexport function extractSymbols(\n positions: Pick<API.Position, \"symbol\">[],\n orders: Pick<API.Order, \"symbol\">[],\n): string[] {\n const symbols = new Set<string>();\n\n positions.forEach((item) => {\n symbols.add(item.symbol);\n });\n\n orders.forEach((item) => {\n symbols.add(item.symbol);\n });\n\n return Array.from(symbols);\n}\n\n//=========== max qty ==================\n\n// function otherIM(inputs: {}): number {}\n\nexport type OtherIMsInputs = {\n // the position list for other symbols except the current symbol\n positions: API.Position[];\n\n markPrices: { [key: string]: number };\n maxLeverage: number;\n symbolInfo: any;\n IMR_Factors: { [key: string]: number };\n};\n/**\n * Total margin used by other symbols (except the current symbol).\n */\nexport function otherIMs(inputs: OtherIMsInputs): number {\n const {\n // orders,\n positions,\n maxLeverage,\n IMR_Factors,\n symbolInfo,\n markPrices,\n } = inputs;\n\n const symbols = positions.map((item) => item.symbol);\n\n return symbols\n .reduce((acc, cur) => {\n const symbol = cur;\n\n if (typeof markPrices[symbol] === \"undefined\") {\n console.warn(\"markPrices[%s] is undefined\", symbol);\n return acc;\n }\n\n const markPriceDecimal = new Decimal(markPrices[symbol] || 0);\n\n const position = positions.find((item) => item.symbol === symbol);\n\n const positionQty = getQtyFromPositions(positions, symbol);\n const positionNotional = markPriceDecimal.mul(positionQty).toNumber();\n\n const buyOrdersQty = position!.pending_long_qty;\n const sellOrdersQty = position!.pending_short_qty;\n\n const ordersNotional = markPriceDecimal\n .mul(new Decimal(buyOrdersQty).add(sellOrdersQty))\n .toNumber();\n\n const IMR_Factor = IMR_Factors[symbol];\n\n if (!IMR_Factor) {\n console.warn(\"IMR_Factor is not found:\", symbol);\n return acc;\n }\n\n const imr = IMR({\n maxLeverage,\n\n IMR_Factor,\n baseIMR: symbolInfo[symbol](\"base_imr\", 0),\n positionNotional,\n ordersNotional,\n });\n\n const positionQtyWithOrders = positionQtyWithOrders_by_symbol({\n positionQty,\n buyOrdersQty,\n sellOrdersQty,\n });\n\n const positionNotionalWithOrders = positionNotionalWithOrder_by_symbol({\n markPrice: markPrices[symbol] || 0,\n positionQtyWithOrders,\n });\n\n return acc.add(positionNotionalWithOrders.mul(imr));\n }, zero)\n .toNumber();\n}\n\nexport type MaxQtyInputs = {\n symbol: string;\n\n // Maximum quantity limit for opening a single position, /v1/public/info.base_max\n baseMaxQty: number;\n /**\n * Total collateral of the user (denominated in USDC), can be calculated from totalCollateral.\n * @see totalCollateral\n */\n totalCollateral: number;\n maxLeverage: number;\n baseIMR: number;\n /**\n * @see otherIMs\n */\n otherIMs: number;\n markPrice: number;\n // Quantity of open positions\n positionQty: number;\n // Quantity of long orders\n buyOrdersQty: number;\n // Quantity of short orders\n sellOrdersQty: number;\n\n IMR_Factor: number;\n\n takerFeeRate: number;\n};\n\n/**\n * Maximum order quantity.\n */\nexport function maxQty(\n side: OrderSide,\n inputs: MaxQtyInputs,\n options?: ResultOptions,\n): number {\n if (side === OrderSide.BUY) {\n return maxQtyByLong(inputs);\n }\n return maxQtyByShort(inputs);\n}\n\nexport function maxQtyByLong(\n inputs: Omit<MaxQtyInputs, \"side\">,\n options?: ResultOptions,\n): number {\n try {\n const {\n baseMaxQty,\n totalCollateral,\n otherIMs,\n maxLeverage,\n baseIMR,\n markPrice,\n IMR_Factor,\n positionQty,\n buyOrdersQty,\n takerFeeRate,\n } = inputs;\n\n if (totalCollateral === 0) {\n return 0;\n }\n\n const totalCollateralDecimal = new Decimal(totalCollateral);\n\n const factor_1 = totalCollateralDecimal\n .sub(otherIMs)\n .div(\n new Decimal(takerFeeRate)\n .mul(2)\n .mul(0.0001)\n .add(Math.max(1 / maxLeverage, baseIMR)),\n )\n .div(markPrice)\n .mul(0.995)\n .sub(new Decimal(positionQty).add(buyOrdersQty))\n .toNumber();\n\n if (positionQty === 0 && buyOrdersQty === 0) {\n return Math.min(baseMaxQty, factor_1);\n }\n\n const factor_2 = totalCollateralDecimal\n .sub(otherIMs)\n .div(IMR_Factor)\n .toPower(1 / 1.8)\n .div(markPrice)\n .sub(\n new Decimal(positionQty).add(buyOrdersQty),\n // .abs()\n // .div(new Decimal(takerFeeRate).mul(2).mul(0.0001).add(1))\n )\n .div(new Decimal(takerFeeRate).mul(2).mul(0.0001).add(1))\n .mul(0.995)\n .toNumber();\n\n return Math.min(baseMaxQty, factor_1, factor_2);\n } catch (error) {\n return 0;\n }\n}\n\nexport function maxQtyByShort(\n inputs: Omit<MaxQtyInputs, \"side\">,\n options?: ResultOptions,\n): number {\n try {\n const {\n baseMaxQty,\n totalCollateral,\n otherIMs,\n maxLeverage,\n baseIMR,\n markPrice,\n IMR_Factor,\n positionQty,\n buyOrdersQty,\n sellOrdersQty,\n takerFeeRate,\n } = inputs;\n\n const totalCollateralDecimal = new Decimal(totalCollateral);\n\n const factor_1 = totalCollateralDecimal\n .sub(otherIMs)\n .div(\n new Decimal(takerFeeRate)\n .mul(2)\n .mul(0.0001)\n .add(Math.max(1 / maxLeverage, baseIMR)),\n )\n .div(markPrice)\n .mul(0.995)\n // .add(new Decimal(positionQty).add(sellOrdersQty))\n .add(positionQty)\n .sub(Math.abs(sellOrdersQty))\n .toNumber();\n\n if (positionQty === 0 && sellOrdersQty === 0) {\n return Math.min(baseMaxQty, factor_1);\n }\n\n const factor_2 = totalCollateralDecimal\n .sub(otherIMs)\n .div(IMR_Factor)\n .toPower(1 / 1.8)\n .div(markPrice)\n // .add(\n // new Decimal(positionQty)\n // .add(sellOrdersQty)\n // // .abs()\n // )\n .add(positionQty)\n .sub(sellOrdersQty)\n .div(new Decimal(takerFeeRate).mul(2).mul(0.0001).add(1))\n .mul(0.995)\n .toNumber();\n\n return Math.min(baseMaxQty, factor_1, factor_2);\n } catch (error) {\n return 0;\n }\n}\n\nexport type TotalMarginRatioInputs = {\n totalCollateral: number;\n markPrices: { [key: string]: number };\n positions: API.Position[];\n};\n/**\n * total margin ratio\n */\nexport function totalMarginRatio(\n inputs: TotalMarginRatioInputs,\n dp?: number,\n): number {\n const { totalCollateral, markPrices, positions } = inputs;\n\n if (totalCollateral === 0) {\n return 0;\n }\n\n const totalCollateralDecimal = new Decimal(totalCollateral);\n\n const totalPositionNotional = positions.reduce((acc, cur) => {\n const markPrice = markPrices[cur.symbol] || 0;\n return acc.add(new Decimal(cur.position_qty).mul(markPrice).abs());\n }, zero);\n\n if (totalPositionNotional.eq(zero)) {\n return 0;\n }\n\n return totalCollateralDecimal.div(totalPositionNotional).toNumber();\n}\n\nexport type TotalUnrealizedROIInputs = {\n totalUnrealizedPnL: number;\n totalValue: number;\n};\n\n/**\n * totalUnrealizedROI\n */\nexport function totalUnrealizedROI(inputs: TotalUnrealizedROIInputs) {\n const { totalUnrealizedPnL, totalValue } = inputs;\n\n return new Decimal(totalUnrealizedPnL)\n .div(totalValue - totalUnrealizedPnL)\n .toNumber();\n}\n\n/**\n * current account leverage\n */\nexport function currentLeverage(totalMarginRatio: number) {\n if (totalMarginRatio === 0) {\n return 0;\n }\n return 1 / totalMarginRatio;\n}\n\nexport type AvailableBalanceInputs = {\n USDCHolding: number;\n unsettlementPnL: number;\n};\nexport function availableBalance(inputs: AvailableBalanceInputs) {\n const { USDCHolding, unsettlementPnL } = inputs;\n\n return new Decimal(USDCHolding).add(unsettlementPnL).toNumber();\n}\n\nexport type AccountMMRInputs = {\n // Total Maintenance Margin of all positions of the user (USDC)\n positionsMMR: number;\n /**\n * Notional sum of all positions,\n * positions.totalNotional()\n */\n positionsNotional: number;\n};\n\n/**\n * total maintenance margin ratio\n * @param inputs AccountMMRInputs\n * @returns number|null\n */\nexport function MMR(inputs: AccountMMRInputs): number | null {\n // If the user does not have any positions, return null\n if (inputs.positionsNotional === 0) {\n return null;\n }\n if (inputs.positionsMMR === 0) {\n return null;\n }\n return new Decimal(inputs.positionsMMR)\n .div(inputs.positionsNotional)\n .toNumber();\n}\n\nexport const collateralRatio = (params: {\n baseWeight: number;\n discountFactor: number | null;\n collateralQty: number;\n collateralCap: number;\n indexPrice: number;\n}) => {\n const {\n baseWeight,\n discountFactor,\n collateralQty,\n collateralCap,\n indexPrice,\n } = params;\n\n // if collateralCap is -1, it means the collateral is unlimited\n const cap = collateralCap === -1 ? collateralQty : collateralCap;\n\n const K = new Decimal(1.2);\n const DCF = new Decimal(discountFactor || 0);\n const qty = new Decimal(Math.min(collateralQty, cap));\n\n const notionalAbs = qty.mul(indexPrice).abs();\n const dynamicWeight = DCF.mul(notionalAbs).toPower(IMRFactorPower);\n const result = K.div(new Decimal(1).add(dynamicWeight));\n\n return result.lt(baseWeight) ? result : new Decimal(baseWeight);\n};\n\n/** collateral_value_i = min(collateral_qty_i , collateral_cap_i) * weight_i * index_price_i */\nexport const collateralContribution = (params: {\n collateralQty: number;\n collateralCap: number;\n collateralRatio: number;\n indexPrice: number;\n}) => {\n const { collateralQty, collateralCap, collateralRatio, indexPrice } = params;\n\n // if collateralCap is -1, it means the collateral is unlimited\n const cap = collateralCap === -1 ? collateralQty : collateralCap;\n\n return new Decimal(Math.min(collateralQty, cap))\n .mul(collateralRatio)\n .mul(indexPrice)\n .toNumber();\n};\n\nexport const LTV = (params: {\n usdcBalance: number;\n upnl: number;\n assets: Array<{ qty: number; indexPrice: number; weight: number }>;\n}) => {\n const { usdcBalance, upnl, assets } = params;\n\n const usdcLoss = new Decimal(Math.min(usdcBalance, 0)).abs();\n const upnlLoss = new Decimal(Math.min(upnl, 0)).abs();\n const numerator = usdcLoss.add(upnlLoss);\n\n const collateralSum = assets.reduce<Decimal>((acc, asset) => {\n return acc.add(\n new Decimal(Math.max(asset.qty, 0))\n .mul(new Decimal(asset.indexPrice))\n .mul(new Decimal(asset.weight)),\n );\n }, zero);\n\n const denominator = collateralSum.add(new Decimal(Math.max(upnl, 0)));\n\n if (numerator.isZero() || denominator.isZero()) {\n return 0;\n }\n\n return numerator.div(denominator).toNumber();\n};\n\nexport const maxWithdrawalUSDC = (inputs: {\n USDCBalance: number;\n freeCollateral: Decimal;\n upnl: number;\n}) => {\n const { USDCBalance, freeCollateral, upnl } = inputs;\n const value = Math.min(\n new Decimal(USDCBalance).toNumber(),\n new Decimal(freeCollateral).sub(Math.max(upnl, 0)).toNumber(),\n );\n return Math.max(0, value);\n};\n\nexport const maxWithdrawalOtherCollateral = (inputs: {\n USDCBalance: number;\n collateralQty: number;\n freeCollateral: Decimal;\n indexPrice: number;\n weight: Decimal;\n}) => {\n const { USDCBalance, collateralQty, freeCollateral, indexPrice, weight } =\n inputs;\n const usdcBalance = new Decimal(USDCBalance);\n const denominator = usdcBalance.isNegative()\n ? new Decimal(indexPrice).mul(weight).mul(new Decimal(1).add(0.001))\n : new Decimal(indexPrice).mul(weight);\n if (denominator.isZero()) {\n return zero;\n }\n const qty = new Decimal(collateralQty);\n const maxQtyByValue = new Decimal(freeCollateral).div(denominator);\n return maxQtyByValue.lt(qty) ? maxQtyByValue : qty;\n};\n\nexport const calcMinimumReceived = (inputs: {\n amount: number;\n slippage: number;\n}) => {\n const { amount, slippage } = inputs;\n const slippageRatio = new Decimal(slippage).div(100);\n return new Decimal(amount)\n .mul(new Decimal(1).minus(slippageRatio))\n .toNumber();\n};\n","import { API as orderUtils } from \"@orderly.network/types\";\nimport { Decimal, zero } from \"@orderly.network/utils\";\nimport { notional } from \"./positions\";\n\n/**\n * Maximum price when placing an order\n */\nexport function maxPrice(markprice: number, range: number) {\n return markprice * (1 + range);\n}\n\n/**\n * Minimum price when placing an order\n */\nexport function minPrice(markprice: number, range: number) {\n return markprice * (1 - range);\n}\n\n/**\n * Scope price when placing an order\n * @returns number\n */\nexport function scopePrice(\n price: number,\n scope: number,\n side: \"BUY\" | \"SELL\",\n): number {\n if (side === \"BUY\") {\n return price * (1 - scope);\n }\n return price * (1 + scope);\n}\n\n/**\n * Calculate the order fee\n */\nexport function orderFee(inputs: {\n /**\n * Order quantity\n */\n qty: number;\n price: number;\n futuresTakeFeeRate: number;\n}): number {\n return new Decimal(inputs.qty)\n .mul(inputs.price)\n .mul(inputs.futuresTakeFeeRate)\n .toNumber();\n}\n\nexport type EstimatedLiquidationPriceInputs = {\n totalCollateral: number;\n markPrice: number;\n baseMMR: number;\n baseIMR: number;\n IMR_Factor: number;\n orderFee: number;\n positions: Pick<\n orderUtils.PositionExt,\n \"position_qty\" | \"mark_price\" | \"symbol\" | \"mmr\"\n >[];\n newOrder: {\n symbol: string;\n qty: number;\n price: number;\n };\n};\n\n/**\n * Estimated liquidation price\n * @param inputs\n * @returns\n */\nexport function estLiqPrice(inputs: EstimatedLiquidationPriceInputs): number {\n const {\n positions,\n newOrder,\n totalCollateral,\n markPrice,\n baseIMR,\n baseMMR,\n orderFee,\n IMR_Factor,\n } = inputs;\n // opened positions for the symbol\n let currentPosition:\n | Pick<\n orderUtils.PositionExt,\n \"position_qty\" | \"mark_price\" | \"symbol\" | \"mmr\"\n >\n | undefined = undefined;\n\n let newTotalMM = zero;\n\n const hasPosition =\n positions.filter((item) => item.position_qty > 0).length > 0;\n\n const basePrice = hasPosition ? markPrice : newOrder.price;\n\n const newOrderNotional = new Decimal(newOrder.qty).mul(newOrder.price);\n\n for (let index = 0; index < positions.length; index++) {\n const position = positions[index];\n let notional = new Decimal(position.position_qty).mul(position.mark_price);\n if (newOrder.symbol === position.symbol) {\n currentPosition = position;\n notional = notional.add(newOrderNotional);\n }\n\n newTotalMM = newTotalMM.add(notional.abs().mul(position.mmr));\n }\n\n // if no position\n if (!currentPosition) {\n newTotalMM = newTotalMM.add(newOrderNotional.mul(baseMMR));\n }\n\n const newMMR = Math.max(\n baseMMR,\n new Decimal(baseMMR)\n .div(baseIMR)\n .mul(IMR_Factor)\n .mul(\n newOrderNotional\n .add(\n !!currentPosition\n ? new Decimal(currentPosition.position_qty).mul(\n currentPosition.mark_price,\n )\n : zero,\n )\n .abs(),\n )\n .toPower(4 / 5)\n .toNumber(),\n );\n\n // console.log(\"new MMR\", newMMR, newTotalMM.toNumber());\n\n const newQty = new Decimal(newOrder.qty).add(\n currentPosition?.position_qty ?? 0,\n );\n\n if (newQty.eq(0)) {\n return 0;\n }\n\n const price = new Decimal(basePrice)\n .add(\n new Decimal(totalCollateral)\n .sub(newTotalMM)\n .sub(orderFee)\n .div(newQty.abs().mul(newMMR).sub(newQty)),\n )\n .toNumber();\n\n return Math.max(0, price);\n}\n\nexport type EstimatedLeverageInputs = {\n totalCollateral: number;\n positions: Pick<\n orderUtils.PositionExt,\n \"position_qty\" | \"mark_price\" | \"symbol\"\n >[];\n newOrder: {\n symbol: string;\n qty: number;\n price: number;\n };\n};\n\n/**\n * Estimated leverage\n * @param inputs EstimtedLeverageInputs\n * @returns number\n */\nexport function estLeverage(inputs: EstimatedLeverageInputs): number | null {\n const { totalCollateral, positions, newOrder } = inputs;\n if (totalCollateral <= 0) {\n return null;\n }\n let hasPosition = false;\n let sumPositionNotional = positions.reduce((acc, cur) => {\n let count = new Decimal(cur.position_qty).mul(cur.mark_price);\n // acc = acc.add(\n // new Decimal(cur.position_qty).mul(cur.mark_price)\n // // .abs()\n // );\n\n if (cur.symbol === newOrder.symbol) {\n hasPosition = true;\n // acc = acc.add(new Decimal(newOrder.qty).mul(newOrder.price));\n count = count.add(new Decimal(newOrder.qty).mul(newOrder.price));\n }\n\n return acc.add(count.abs());\n }, zero);\n\n if (!hasPosition) {\n sumPositionNotional = sumPositionNotional.add(\n new Decimal(newOrder.qty).mul(newOrder.price).abs(),\n );\n }\n\n if (sumPositionNotional.eq(zero)) {\n return null;\n }\n\n const totalMarginRatio = new Decimal(totalCollateral).div(\n sumPositionNotional,\n );\n\n return new Decimal(1)\n .div(totalMarginRatio)\n .toDecimalPlaces(2, Decimal.ROUND_HALF_EVEN)\n .toNumber();\n}\n"],"mappings":";;;;;;;AAOA,IAAI,OAAO,WAAW,aAAa;AACjC,SAAO,sBAAsB,OAAO,uBAAuB,CAAC;AAC5D,SAAO,oBAAoB,uBAAuB,IAAI;AACxD;AAEA,IAAO,kBAAQ;;;ACZf;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,SAAS,SAAS,YAAY;;;ACIvB,IAAM,iBAAiB,IAAI;;;ADK3B,SAAS,SAAS,KAAa,YAA4B;AAChE,SAAO,IAAI,QAAQ,GAAG,EAAE,IAAI,UAAU,EAAE,IAAI,EAAE,SAAS;AACzD;AAOO,SAAS,cAAc,WAAmC;AAC/D,SAAO,UAAU,OAAO,CAAC,KAAK,QAAQ;AACpC,WAAO,MAAM,SAAS,IAAI,cAAc,IAAI,UAAU;AAAA,EACxD,GAAG,CAAC;AACN;AAaO,SAAS,cAAc,QAAiC;AAC7D,SAAO,IAAI,QAAQ,OAAO,GAAG,EAC1B,IAAI,OAAO,YAAY,OAAO,SAAS,EACvC,SAAS;AACd;AAcO,SAAS,iBAAiB,QAAoC;AACnE,QAAM,EAAE,WAAW,KAAAA,KAAI,IAAI;AAE3B,MACE,OAAO,kBAAkB,KACzB,OAAO,gBAAgB,KACvB,cAAc,KACdA,SAAQ;AAER,WAAO;AAET,SAAO,IAAI,QAAQ,OAAO,aAAa,EACpC,IAAI,IAAI,QAAQ,KAAK,IAAI,OAAO,WAAW,CAAC,EAAE,IAAI,SAAS,EAAE,IAAIA,IAAG,CAAC,EACrE,SAAS;AACd;AAOO,SAAS,mBAAmB,WAAmC;AACpE,SAAO,UAAU,OAAO,CAAC,KAAK,QAAQ;AACpC,WACE,MACA,cAAc;AAAA,M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AAA,EAC5B,GAAGC,KAAI;AAEP,MAAI,CAAC,aAAa;AAChB,0BAAsB,oBAAoB;AAAA,MACxC,IAAID,SAAQ,SAAS,GAAG,EAAE,IAAI,SAAS,KAAK,EAAE,IAAI;AAAA,IACpD;AAAA,EACF;AAEA,MAAI,oBAAoB,GAAGC,KAAI,GAAG;AAChC,WAAO;AAAA,EACT;AAEA,QAAMI,oBAAmB,IAAIL,SAAQE,gBAAe,EAAE;AAAA,IACpD;AAAA,EACF;AAEA,SAAO,IAAIF,SAAQ,CAAC,EACjB,IAAIK,iBAAgB,EACpB,gBAAgB,GAAGL,SAAQ,eAAe,EAC1C,SAAS;AACd;","names":["IMR","totalCollateral","MMR","totalNotional","MMR","Decimal","zero","totalUnsettlementPnL","Decimal","zero","unsettlementPnL","totalCollateral","otherIMs","totalUnrealizedPnL","totalValue","totalMarginRatio","MMR","collateralRatio","freeCollateral","Decimal","zero","totalCollateral","orderFee","notional","totalMarginRatio"]}
|
|
1
|
+
{"version":3,"sources":["../src/version.ts","../src/positions.ts","../src/constants.ts","../src/account.ts","../src/order.ts"],"sourcesContent":["declare global {\n interface Window {\n __ORDERLY_VERSION__?: {\n [key: string]: string;\n };\n }\n}\nif (typeof window !== \"undefined\") {\n window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};\n window.__ORDERLY_VERSION__[\"@orderly.network/perp\"] = \"4.7.0\";\n}\n\nexport default \"4.7.0\";\n","import { API } from \"@orderly.network/types\";\nimport { Decimal, zero } from \"@orderly.network/utils\";\nimport { IMRFactorPower } from \"./constants\";\n\n/**\n * Calculates the notional value of a single position.\n * @param qty The quantity of the position.\n * @param mark_price The price of the position.\n * @returns The notional value of the position.\n */\nexport function notional(qty: number, mark_price: number): number {\n return new Decimal(qty).mul(mark_price).abs().toNumber();\n}\n\n/**\n * Calculates the total notional value of all positions.\n * @param positions The array of positions.\n * @returns The total notional value of all positions.\n */\nexport function totalNotional(positions: API.Position[]): number {\n return positions.reduce((acc, cur) => {\n return acc + notional(cur.position_qty, cur.mark_price);\n }, 0);\n}\n\nexport type UnrealPnLInputs = {\n markPrice: number;\n openPrice: number;\n qty: number;\n};\n\n/**\n * Calculates the unrealized profit or loss of a single position.\n * @param inputs The inputs for calculating the unrealized profit or loss.\n * @returns The unrealized profit or loss of the position.\n */\nexport function unrealizedPnL(inputs: UnrealPnLInputs): number {\n return new Decimal(inputs.qty)\n .mul(inputs.markPrice - inputs.openPrice)\n .toNumber();\n}\n\nexport type UnrealPnLROIInputs = {\n positionQty: number;\n openPrice: number;\n IMR: number;\n unrealizedPnL: number;\n};\n\n/**\n * Calculates the return on investment (ROI) of a single position's unrealized profit or loss.\n * @param inputs The inputs for calculating the ROI.\n * @returns The ROI of the position's unrealized profit or loss.\n */\nexport function unrealizedPnLROI(inputs: UnrealPnLROIInputs): number {\n const { openPrice, IMR } = inputs;\n\n if (\n inputs.unrealizedPnL === 0 ||\n inputs.positionQty === 0 ||\n openPrice === 0 ||\n IMR === 0\n )\n return 0;\n\n return new Decimal(inputs.unrealizedPnL)\n .div(new Decimal(Math.abs(inputs.positionQty)).mul(openPrice).mul(IMR))\n .toNumber();\n}\n\n/**\n * Calculates the total unrealized profit or loss of all positions.\n * @param positions The array of positions.\n * @returns The total unrealized profit or loss of all positions.\n */\nexport function totalUnrealizedPnL(positions: API.Position[]): number {\n return positions.reduce((acc, cur) => {\n return (\n acc +\n unrealizedPnL({\n qty: cur.position_qty,\n openPrice: cur.average_open_price,\n markPrice: cur.mark_price,\n })\n );\n }, 0);\n}\n\nexport type LiqPriceInputs = {\n markPrice: number;\n totalCollateral: number;\n positionQty: number;\n positions: Pick<API.PositionExt, \"position_qty\" | \"mark_price\" | \"mmr\">[];\n MMR: number;\n};\n\n/**\n * Calculates the liquidation price of a single position.\n * @param inputs The inputs for calculating the liquidation price.\n * @returns The liquidation price of the position.\n */\nexport function liqPrice(inputs: LiqPriceInputs): number | null {\n const { markPrice, totalCollateral, positions, positionQty, MMR } = inputs;\n\n // console.log(\"inputs\", inputs);\n\n if (positionQty === 0 || totalCollateral === 0) {\n return null;\n }\n\n // totalNotional of all poisitions\n const totalNotional = positions.reduce<Decimal>((acc, cur) => {\n return acc.add(\n new Decimal(notional(cur.position_qty, cur.mark_price)).mul(cur.mmr),\n );\n }, zero);\n\n return Math.max(\n new Decimal(markPrice)\n .add(\n new Decimal(totalCollateral)\n .sub(totalNotional)\n .div(new Decimal(positionQty).abs().mul(MMR).sub(positionQty)),\n )\n .toNumber(),\n 0,\n );\n}\n\nexport type MMInputs = {\n positionQty: number;\n markPrice: number;\n MMR: number;\n};\n\n/**\n * Calculates the maintenance margin of a position.\n * @param inputs The inputs for calculating the maintenance margin.\n * @returns The maintenance margin of the position.\n */\nexport function maintenanceMargin(inputs: MMInputs) {\n const { positionQty, markPrice, MMR } = inputs;\n\n return new Decimal(positionQty).mul(markPrice).mul(MMR).abs().toNumber();\n}\n\nexport type UnsettlementPnLInputs = {\n positionQty: number;\n markPrice: number;\n costPosition: number;\n sumUnitaryFunding: number;\n lastSumUnitaryFunding: number;\n};\n\n/**\n * Calculates the unrealized profit or loss of each position.\n * @param inputs The inputs for calculating the unrealized profit or loss.\n * @returns The unrealized profit or loss of each position.\n */\nexport function unsettlementPnL(inputs: UnsettlementPnLInputs): number {\n const {\n positionQty,\n markPrice,\n costPosition,\n sumUnitaryFunding,\n lastSumUnitaryFunding,\n } = inputs;\n\n const qty = new Decimal(positionQty);\n\n return qty\n .mul(markPrice)\n .sub(costPosition)\n .sub(qty.mul(new Decimal(sumUnitaryFunding).sub(lastSumUnitaryFunding)))\n .toNumber();\n}\n\nexport type TotalUnsettlementPnLInputs = {\n positions: (API.Position & {\n sum_unitary_funding: number;\n })[];\n sumUnitaryFunding: number;\n};\n\n/**\n * Calculates the total unrealized profit or loss of all positions.\n * @param positions The array of positions.\n * @returns The total unrealized profit or loss of all positions.\n */\nexport function totalUnsettlementPnL(\n positions: (API.Position & { sum_unitary_funding: number })[],\n): number {\n if (!Array.isArray(positions) || positions.length === 0) {\n return 0;\n }\n\n return positions.reduce((acc, cur) => {\n return (\n acc +\n unsettlementPnL({\n positionQty: cur.position_qty,\n markPrice: cur.mark_price,\n costPosition: cur.cost_position,\n sumUnitaryFunding: cur.sum_unitary_funding,\n lastSumUnitaryFunding: cur.last_sum_unitary_funding,\n })\n );\n }, 0);\n}\n\nexport type MMRInputs = {\n baseMMR: number;\n baseIMR: number;\n IMRFactor: number;\n positionNotional: number;\n IMR_factor_power: number;\n};\n\n/**\n * Calculates the maintenance margin requirement (MMR) of a position.\n * @param inputs The inputs for calculating the MMR.\n * @returns The MMR of the position.\n */\nexport function MMR(inputs: MMRInputs): number {\n const {\n baseMMR,\n baseIMR,\n IMRFactor,\n positionNotional,\n IMR_factor_power = IMRFactorPower,\n } = inputs;\n return Math.max(\n baseMMR,\n new Decimal(baseMMR)\n .div(baseIMR)\n .mul(IMRFactor)\n .mul(Math.pow(Math.abs(positionNotional), IMR_factor_power))\n // .toPower(IMR_factor_power)\n .toNumber(),\n );\n}\n\n/**\n * Calculates the profit or loss for take profit.\n * @returns The profit or loss for take profit.\n */\nexport function estPnLForTP(inputs: {\n positionQty: number;\n entryPrice: number;\n price: number;\n}): number {\n return new Decimal(inputs.positionQty)\n .mul(new Decimal(inputs.price).sub(inputs.entryPrice))\n .toNumber();\n}\n\n/**\n * Calculates the estimated price for take profit.\n */\nexport function estPriceForTP(inputs: {\n positionQty: number;\n entryPrice: number;\n pnl: number;\n}): number {\n return new Decimal(inputs.pnl)\n .add(inputs.entryPrice)\n .div(inputs.positionQty)\n .toNumber();\n}\n\n/**\n * Calculates the estimated offset for take profit.\n */\nexport function estOffsetForTP(inputs: {\n price: number;\n entryPrice: number;\n}): number {\n return new Decimal(inputs.price).div(inputs.entryPrice).toNumber();\n}\n\n/**\n * Calculates the estimated price from offset for take profit.\n */\nexport function estPriceFromOffsetForTP(inputs: {\n offset: number;\n entryPrice: number;\n}): number {\n return new Decimal(inputs.offset).add(inputs.entryPrice).toNumber();\n}\n\n/**\n * Calculates the PnL for stop loss.\n */\nexport function estPnLForSL(inputs: {\n positionQty: number;\n entryPrice: number;\n}): number {\n return 0;\n}\n\n/**\n * calculate the max position notional\n * max_notional = ( (1/ (leverage * imr_factor) ) ^ (1/0.8)\n */\nexport function maxPositionNotional(inputs: {\n /** symbol leverage */\n leverage: number;\n IMRFactor: number;\n}) {\n const { leverage, IMRFactor } = inputs;\n return new Decimal(1)\n .div(new Decimal(leverage).mul(IMRFactor))\n .pow(1 / 0.8)\n .toNumber();\n}\n\n/**\n * symbol_leverage_max = 1 / ( imr_factor * notional ^ 0.8 )\n */\nexport function maxPositionLeverage(inputs: {\n IMRFactor: number;\n notional: number;\n}) {\n const { IMRFactor, notional } = inputs;\n return new Decimal(1)\n .div(new Decimal(IMRFactor).mul(new Decimal(notional).pow(0.8)))\n .toNumber();\n}\n","/**\n * The power of the IMR factor.\n * @constant\n * @default\n */\nexport const IMRFactorPower = 4 / 5;\n","import { API, OrderSide } from \"@orderly.network/types\";\nimport { Decimal, zero } from \"@orderly.network/utils\";\nimport { IMRFactorPower } from \"./constants\";\n\nexport type ResultOptions = {\n dp: number;\n};\n\nexport type TotalValueInputs = {\n totalUnsettlementPnL: number;\n\n USDCHolding: number;\n nonUSDCHolding: {\n holding: number;\n indexPrice: number;\n }[];\n};\n\n/**\n * User's total asset value (denominated in USDC), including assets that cannot be used as collateral.\n */\nexport function totalValue(inputs: TotalValueInputs): Decimal {\n const { totalUnsettlementPnL, USDCHolding, nonUSDCHolding } = inputs;\n const nonUSDCHoldingValue = nonUSDCHolding.reduce((acc, cur) => {\n return new Decimal(cur.holding).mul(cur.indexPrice).add(acc);\n }, zero);\n return nonUSDCHoldingValue.add(USDCHolding).add(totalUnsettlementPnL);\n}\n\n/**\n * Total value of available collateral in the user's account (denominated in USDC).\n */\nexport type FreeCollateralInputs = {\n // Total collateral\n totalCollateral: Decimal;\n // Total initial margin with orders\n totalInitialMarginWithOrders: number;\n};\n/**\n * Calculate free collateral.\n */\nexport function freeCollateral(inputs: FreeCollateralInputs): Decimal {\n const value = inputs.totalCollateral.sub(inputs.totalInitialMarginWithOrders);\n // free collateral cannot be less than 0\n return value.isNegative() ? zero : value;\n}\n\nexport type TotalCollateralValueInputs = {\n USDCHolding: number;\n nonUSDCHolding: {\n holding: number;\n indexPrice: number;\n collateralCap: number;\n collateralRatio: Decimal;\n }[];\n unsettlementPnL: number;\n};\n\n/**\n * Calculate total collateral.\n */\nexport function totalCollateral(inputs: TotalCollateralValueInputs): Decimal {\n const { USDCHolding, nonUSDCHolding, unsettlementPnL } = inputs;\n const nonUSDCHoldingValue = nonUSDCHolding.reduce<Decimal>((acc, cur) => {\n const finalHolding = Math.min(cur.holding, cur.collateralCap);\n const value = new Decimal(finalHolding)\n .mul(cur.collateralRatio)\n .mul(cur.indexPrice);\n return acc.add(value);\n }, zero);\n\n return new Decimal(USDCHolding).add(nonUSDCHoldingValue).add(unsettlementPnL);\n}\n\nexport function initialMarginWithOrder() {}\n\nexport type PositionNotionalWithOrderInputs = {\n markPrice: number;\n positionQtyWithOrders: number;\n};\n/**\n * Sum of notional value for a symbol's position and orders.\n */\nexport function positionNotionalWithOrder_by_symbol(\n inputs: PositionNotionalWithOrderInputs,\n): Decimal {\n return new Decimal(inputs.markPrice).mul(inputs.positionQtyWithOrders);\n}\n\nexport type PositionQtyWithOrderInputs = {\n positionQty: number;\n // Total quantity of buy orders for a symbol\n buyOrdersQty: number;\n // Total quantity of sell orders for a symbol\n sellOrdersQty: number;\n};\n/**\n * Sum of position quantity and orders quantity for a symbol.\n */\nexport function positionQtyWithOrders_by_symbol(\n inputs: PositionQtyWithOrderInputs,\n): number {\n const { positionQty, buyOrdersQty, sellOrdersQty } = inputs;\n const positionQtyDecimal = new Decimal(positionQty);\n const qty = Math.max(\n positionQtyDecimal.add(buyOrdersQty).abs().toNumber(),\n positionQtyDecimal.sub(sellOrdersQty).abs().toNumber(),\n );\n\n return qty;\n}\n\nexport type IMRInputs = {\n /**\n * effective max leverage\n */\n maxLeverage: number;\n baseIMR: number;\n IMR_Factor: number;\n positionNotional: number;\n ordersNotional: number;\n IMR_factor_power?: number;\n};\n\n/**\n * Initial margin rate for a symbol.\n * Max(1 / Max Account Leverage, Base IMR i, IMR Factor i * Abs(Position Notional i + Order Notional i)^(4/5))\n */\nexport function IMR(inputs: IMRInputs): number {\n const {\n maxLeverage,\n baseIMR,\n IMR_Factor,\n positionNotional,\n ordersNotional: orderNotional,\n IMR_factor_power = IMRFactorPower,\n } = inputs;\n return Math.max(\n 1 / maxLeverage,\n baseIMR,\n new Decimal(IMR_Factor)\n .mul(\n new Decimal(positionNotional)\n .add(orderNotional)\n .abs()\n .toPower(IMR_factor_power),\n )\n .toNumber(),\n );\n}\n\nexport function buyOrdersFilter_by_symbol(\n orders: API.Order[],\n symbol: string,\n): API.Order[] {\n return orders.filter(\n (item) => item.symbol === symbol && item.side === OrderSide.BUY,\n );\n}\n\nexport function sellOrdersFilter_by_symbol(\n orders: API.Order[],\n symbol: string,\n): API.Order[] {\n return orders.filter(\n (item) => item.symbol === symbol && item.side === OrderSide.SELL,\n );\n}\n\n/**\n * Get the quantity of a specified symbol from the list of positions.\n */\nexport function getQtyFromPositions(\n positions: API.Position[],\n symbol: string,\n): number {\n if (!positions) {\n return 0;\n }\n const position = positions.find((item) => item.symbol === symbol);\n return position?.position_qty || 0;\n}\n\n/**\n * Get the quantity of long and short orders for a specified symbol from the list of orders.\n */\nexport function getQtyFromOrdersBySide(\n orders: API.Order[],\n symbol: string,\n side: OrderSide,\n): number {\n const ordersBySide =\n side === OrderSide.SELL\n ? sellOrdersFilter_by_symbol(orders, symbol)\n : buyOrdersFilter_by_symbol(orders, symbol);\n return ordersBySide.reduce((acc, cur) => {\n return acc + cur.quantity;\n }, 0);\n}\n\nexport function getPositonsAndOrdersNotionalBySymbol(inputs: {\n positions: API.Position[];\n orders: API.Order[];\n symbol: string;\n markPrice: number;\n}): number {\n const { positions, orders, symbol, markPrice } = inputs;\n const positionQty = getQtyFromPositions(positions, symbol);\n const buyOrdersQty = getQtyFromOrdersBySide(orders, symbol, OrderSide.BUY);\n const sellOrdersQty = getQtyFromOrdersBySide(orders, symbol, OrderSide.SELL);\n\n const markPriceDecimal = new Decimal(markPrice);\n\n return markPriceDecimal\n .mul(positionQty)\n .add(markPriceDecimal.mul(new Decimal(buyOrdersQty).add(sellOrdersQty)))\n .abs()\n .toNumber();\n}\n\nexport type TotalInitialMarginWithOrdersInputs = {\n positions: API.Position[];\n orders: API.Order[];\n // account: API.AccountInfo;\n markPrices: { [key: string]: number };\n symbolInfo: any;\n IMR_Factors: { [key: string]: number };\n} & Pick<IMRInputs, \"maxLeverage\">;\n\n/**\n * @deprecated\n * Calculate the total initial margin used by the user (including positions and orders).\n */\nexport function totalInitialMarginWithOrders(\n inputs: TotalInitialMarginWithOrdersInputs,\n): number {\n const {\n positions,\n orders,\n markPrices,\n IMR_Factors,\n maxLeverage,\n symbolInfo,\n } = inputs;\n\n const symbols = extractSymbols(positions, orders);\n\n const total_initial_margin_with_orders = symbols.reduce((acc, cur) => {\n const symbol = cur;\n const positionQty = getQtyFromPositions(positions, symbol);\n const buyOrdersQty = getQtyFromOrdersBySide(orders, symbol, OrderSide.BUY);\n const sellOrdersQty = getQtyFromOrdersBySide(\n orders,\n symbol,\n OrderSide.SELL,\n );\n\n const markPrice = markPrices[symbol] || 0;\n\n //---\n const positionQtyWithOrders = positionQtyWithOrders_by_symbol({\n positionQty,\n buyOrdersQty,\n sellOrdersQty,\n });\n\n //---\n const position_notional_with_orders = positionNotionalWithOrder_by_symbol({\n markPrice,\n positionQtyWithOrders,\n });\n\n //----\n const markPriceDecimal = new Decimal(markPrice);\n\n const imr = IMR({\n positionNotional: markPriceDecimal.mul(positionQty).toNumber(),\n ordersNotional: markPriceDecimal\n .mul(new Decimal(buyOrdersQty).add(sellOrdersQty))\n .toNumber(),\n maxLeverage,\n IMR_Factor: IMR_Factors[symbol],\n baseIMR: symbolInfo[symbol](\"base_imr\", 0),\n });\n\n return position_notional_with_orders.mul(imr).add(acc).toNumber();\n }, 0);\n\n return total_initial_margin_with_orders;\n}\n\nexport function totalInitialMarginWithQty(inputs: {\n positions: API.Position[];\n // account: API.AccountInfo;\n markPrices: { [key: string]: number };\n symbolInfo: any;\n IMR_Factors: { [key: string]: number };\n /**\n * account max leverage\n */\n maxLeverage: number;\n}) {\n const { positions, markPrices, IMR_Factors, symbolInfo } = inputs;\n const symbols = positions.map((item) => item.symbol);\n\n const total_initial_margin_with_orders = symbols.reduce((acc, cur) => {\n const symbol = cur;\n const position = positions.find((item) => item.symbol === symbol);\n const positionQty = position?.position_qty || 0;\n\n const buyOrdersQty = position?.pending_long_qty || 0;\n const sellOrdersQty = position?.pending_short_qty || 0;\n\n const markPrice = markPrices[symbol] || 0;\n\n //---\n const positionQtyWithOrders = positionQtyWithOrders_by_symbol({\n positionQty,\n buyOrdersQty,\n sellOrdersQty,\n });\n\n //---\n const position_notional_with_orders = positionNotionalWithOrder_by_symbol({\n markPrice,\n positionQtyWithOrders,\n });\n\n //----\n const markPriceDecimal = new Decimal(markPrice);\n\n const imr = IMR({\n positionNotional: markPriceDecimal.mul(positionQty).toNumber(),\n ordersNotional: markPriceDecimal\n .mul(new Decimal(buyOrdersQty).add(sellOrdersQty))\n .toNumber(),\n maxLeverage: maxLeverage({\n symbolLeverage: position?.leverage ?? inputs.maxLeverage,\n accountLeverage: inputs.maxLeverage,\n }),\n IMR_Factor: IMR_Factors[symbol],\n baseIMR: symbolInfo[symbol](\"base_imr\", 0),\n });\n\n return position_notional_with_orders.mul(imr).add(acc).toNumber();\n }, 0);\n\n return total_initial_margin_with_orders;\n}\n\n/**\n * Group orders by symbol, as a symbol can have multiple orders.\n */\nexport function groupOrdersBySymbol(orders: API.Order[]) {\n const symbols: { [key: string]: API.Order[] } = {};\n\n orders.forEach((item) => {\n if (!symbols[item.symbol]) {\n symbols[item.symbol] = [];\n }\n\n symbols[item.symbol].push(item);\n });\n\n return symbols;\n}\n\n/**\n * Extracts all unique symbols from positions and orders.\n * @param positions - An array of position objects.\n * @param orders - An array of order objects.\n * @returns An array of unique symbols.\n */\nexport function extractSymbols(\n positions: Pick<API.Position, \"symbol\">[],\n orders: Pick<API.Order, \"symbol\">[],\n): string[] {\n const symbols = new Set<string>();\n\n positions.forEach((item) => {\n symbols.add(item.symbol);\n });\n\n orders.forEach((item) => {\n symbols.add(item.symbol);\n });\n\n return Array.from(symbols);\n}\n\n//=========== max qty ==================\n\n// function otherIM(inputs: {}): number {}\n\nexport type OtherIMsInputs = {\n // the position list for other symbols except the current symbol\n positions: API.Position[];\n markPrices: { [key: string]: number };\n /**\n * account max leverage\n */\n maxLeverage: number;\n symbolInfo: any;\n IMR_Factors: { [key: string]: number };\n};\n/**\n * Total margin used by other symbols (except the current symbol).\n */\nexport function otherIMs(inputs: OtherIMsInputs): number {\n const {\n // orders,\n positions,\n IMR_Factors,\n symbolInfo,\n markPrices,\n } = inputs;\n\n const symbols = positions.map((item) => item.symbol);\n\n return symbols\n .reduce((acc, cur) => {\n const symbol = cur;\n\n if (typeof markPrices[symbol] === \"undefined\") {\n console.warn(\"markPrices[%s] is undefined\", symbol);\n return acc;\n }\n\n const markPriceDecimal = new Decimal(markPrices[symbol] || 0);\n\n const position = positions.find((item) => item.symbol === symbol);\n\n const positionQty = getQtyFromPositions(positions, symbol);\n const positionNotional = markPriceDecimal.mul(positionQty).toNumber();\n\n const buyOrdersQty = position!.pending_long_qty;\n const sellOrdersQty = position!.pending_short_qty;\n\n const ordersNotional = markPriceDecimal\n .mul(new Decimal(buyOrdersQty).add(sellOrdersQty))\n .toNumber();\n\n const IMR_Factor = IMR_Factors[symbol];\n\n if (!IMR_Factor) {\n console.warn(\"IMR_Factor is not found:\", symbol);\n return acc;\n }\n\n const imr = IMR({\n maxLeverage: maxLeverage({\n symbolLeverage: position!.leverage,\n accountLeverage: inputs.maxLeverage,\n }),\n IMR_Factor,\n baseIMR: symbolInfo[symbol](\"base_imr\", 0),\n positionNotional,\n ordersNotional,\n });\n\n const positionQtyWithOrders = positionQtyWithOrders_by_symbol({\n positionQty,\n buyOrdersQty,\n sellOrdersQty,\n });\n\n const positionNotionalWithOrders = positionNotionalWithOrder_by_symbol({\n markPrice: markPrices[symbol] || 0,\n positionQtyWithOrders,\n });\n\n return acc.add(positionNotionalWithOrders.mul(imr));\n }, zero)\n .toNumber();\n}\n\nexport type MaxQtyInputs = {\n symbol: string;\n\n // Maximum quantity limit for opening a single position, /v1/public/info.base_max\n baseMaxQty: number;\n /**\n * Total collateral of the user (denominated in USDC), can be calculated from totalCollateral.\n * @see totalCollateral\n */\n totalCollateral: number;\n maxLeverage: number;\n baseIMR: number;\n /**\n * @see otherIMs\n */\n otherIMs: number;\n markPrice: number;\n // Quantity of open positions\n positionQty: number;\n // Quantity of long orders\n buyOrdersQty: number;\n // Quantity of short orders\n sellOrdersQty: number;\n\n IMR_Factor: number;\n\n takerFeeRate: number;\n};\n\n/**\n * Maximum order quantity.\n */\nexport function maxQty(\n side: OrderSide,\n inputs: MaxQtyInputs,\n options?: ResultOptions,\n): number {\n if (side === OrderSide.BUY) {\n return maxQtyByLong(inputs);\n }\n return maxQtyByShort(inputs);\n}\n\nexport function maxQtyByLong(\n inputs: Omit<MaxQtyInputs, \"side\">,\n options?: ResultOptions,\n): number {\n try {\n const {\n baseMaxQty,\n totalCollateral,\n otherIMs,\n maxLeverage,\n baseIMR,\n markPrice,\n IMR_Factor,\n positionQty,\n buyOrdersQty,\n takerFeeRate,\n } = inputs;\n\n if (totalCollateral === 0) {\n return 0;\n }\n\n const totalCollateralDecimal = new Decimal(totalCollateral);\n\n const factor_1 = totalCollateralDecimal\n .sub(otherIMs)\n .div(\n new Decimal(takerFeeRate)\n .mul(2)\n .mul(0.0001)\n .add(Math.max(1 / maxLeverage, baseIMR)),\n )\n .div(markPrice)\n .mul(0.995)\n .sub(new Decimal(positionQty).add(buyOrdersQty))\n .toNumber();\n\n if (positionQty === 0 && buyOrdersQty === 0) {\n return Math.min(baseMaxQty, factor_1);\n }\n\n const factor_2 = totalCollateralDecimal\n .sub(otherIMs)\n .div(IMR_Factor)\n .toPower(1 / 1.8)\n .div(markPrice)\n .sub(\n new Decimal(positionQty).add(buyOrdersQty),\n // .abs()\n // .div(new Decimal(takerFeeRate).mul(2).mul(0.0001).add(1))\n )\n .div(new Decimal(takerFeeRate).mul(2).mul(0.0001).add(1))\n .mul(0.995)\n .toNumber();\n\n return Math.min(baseMaxQty, factor_1, factor_2);\n } catch (error) {\n return 0;\n }\n}\n\nexport function maxQtyByShort(\n inputs: Omit<MaxQtyInputs, \"side\">,\n options?: ResultOptions,\n): number {\n try {\n const {\n baseMaxQty,\n totalCollateral,\n otherIMs,\n maxLeverage,\n baseIMR,\n markPrice,\n IMR_Factor,\n positionQty,\n buyOrdersQty,\n sellOrdersQty,\n takerFeeRate,\n } = inputs;\n\n const totalCollateralDecimal = new Decimal(totalCollateral);\n\n const factor_1 = totalCollateralDecimal\n .sub(otherIMs)\n .div(\n new Decimal(takerFeeRate)\n .mul(2)\n .mul(0.0001)\n .add(Math.max(1 / maxLeverage, baseIMR)),\n )\n .div(markPrice)\n .mul(0.995)\n // .add(new Decimal(positionQty).add(sellOrdersQty))\n .add(positionQty)\n .sub(Math.abs(sellOrdersQty))\n .toNumber();\n\n if (positionQty === 0 && sellOrdersQty === 0) {\n return Math.min(baseMaxQty, factor_1);\n }\n\n const factor_2 = totalCollateralDecimal\n .sub(otherIMs)\n .div(IMR_Factor)\n .toPower(1 / 1.8)\n .div(markPrice)\n // .add(\n // new Decimal(positionQty)\n // .add(sellOrdersQty)\n // // .abs()\n // )\n .add(positionQty)\n .sub(sellOrdersQty)\n .div(new Decimal(takerFeeRate).mul(2).mul(0.0001).add(1))\n .mul(0.995)\n .toNumber();\n\n return Math.min(baseMaxQty, factor_1, factor_2);\n } catch (error) {\n return 0;\n }\n}\n\nexport type TotalMarginRatioInputs = {\n totalCollateral: number;\n markPrices: { [key: string]: number };\n positions: API.Position[];\n};\n/**\n * total margin ratio\n */\nexport function totalMarginRatio(\n inputs: TotalMarginRatioInputs,\n dp?: number,\n): number {\n const { totalCollateral, markPrices, positions } = inputs;\n\n if (totalCollateral === 0) {\n return 0;\n }\n\n const totalCollateralDecimal = new Decimal(totalCollateral);\n\n const totalPositionNotional = positions.reduce((acc, cur) => {\n const markPrice = markPrices[cur.symbol] || 0;\n return acc.add(new Decimal(cur.position_qty).mul(markPrice).abs());\n }, zero);\n\n if (totalPositionNotional.eq(zero)) {\n return 0;\n }\n\n return totalCollateralDecimal.div(totalPositionNotional).toNumber();\n}\n\nexport type TotalUnrealizedROIInputs = {\n totalUnrealizedPnL: number;\n totalValue: number;\n};\n\n/**\n * totalUnrealizedROI\n */\nexport function totalUnrealizedROI(inputs: TotalUnrealizedROIInputs) {\n const { totalUnrealizedPnL, totalValue } = inputs;\n\n return new Decimal(totalUnrealizedPnL)\n .div(totalValue - totalUnrealizedPnL)\n .toNumber();\n}\n\n/**\n * current account leverage\n */\nexport function currentLeverage(totalMarginRatio: number) {\n if (totalMarginRatio === 0) {\n return 0;\n }\n return 1 / totalMarginRatio;\n}\n\nexport type AvailableBalanceInputs = {\n USDCHolding: number;\n unsettlementPnL: number;\n};\nexport function availableBalance(inputs: AvailableBalanceInputs) {\n const { USDCHolding, unsettlementPnL } = inputs;\n\n return new Decimal(USDCHolding).add(unsettlementPnL).toNumber();\n}\n\nexport type AccountMMRInputs = {\n // Total Maintenance Margin of all positions of the user (USDC)\n positionsMMR: number;\n /**\n * Notional sum of all positions,\n * positions.totalNotional()\n */\n positionsNotional: number;\n};\n\n/**\n * total maintenance margin ratio\n * @param inputs AccountMMRInputs\n * @returns number|null\n */\nexport function MMR(inputs: AccountMMRInputs): number | null {\n // If the user does not have any positions, return null\n if (inputs.positionsNotional === 0) {\n return null;\n }\n if (inputs.positionsMMR === 0) {\n return null;\n }\n return new Decimal(inputs.positionsMMR)\n .div(inputs.positionsNotional)\n .toNumber();\n}\n\nexport const collateralRatio = (params: {\n baseWeight: number;\n discountFactor: number | null;\n collateralQty: number;\n collateralCap: number;\n indexPrice: number;\n}) => {\n const {\n baseWeight,\n discountFactor,\n collateralQty,\n collateralCap,\n indexPrice,\n } = params;\n\n // if collateralCap is -1, it means the collateral is unlimited\n const cap = collateralCap === -1 ? collateralQty : collateralCap;\n\n const K = new Decimal(1.2);\n const DCF = new Decimal(discountFactor || 0);\n const qty = new Decimal(Math.min(collateralQty, cap));\n\n const notionalAbs = qty.mul(indexPrice).abs();\n const dynamicWeight = DCF.mul(notionalAbs).toPower(IMRFactorPower);\n const result = K.div(new Decimal(1).add(dynamicWeight));\n\n return result.lt(baseWeight) ? result : new Decimal(baseWeight);\n};\n\n/** collateral_value_i = min(collateral_qty_i , collateral_cap_i) * weight_i * index_price_i */\nexport const collateralContribution = (params: {\n collateralQty: number;\n collateralCap: number;\n collateralRatio: number;\n indexPrice: number;\n}) => {\n const { collateralQty, collateralCap, collateralRatio, indexPrice } = params;\n\n // if collateralCap is -1, it means the collateral is unlimited\n const cap = collateralCap === -1 ? collateralQty : collateralCap;\n\n return new Decimal(Math.min(collateralQty, cap))\n .mul(collateralRatio)\n .mul(indexPrice)\n .toNumber();\n};\n\nexport const LTV = (params: {\n usdcBalance: number;\n upnl: number;\n assets: Array<{ qty: number; indexPrice: number; weight: number }>;\n}) => {\n const { usdcBalance, upnl, assets } = params;\n\n const usdcLoss = new Decimal(Math.min(usdcBalance, 0)).abs();\n const upnlLoss = new Decimal(Math.min(upnl, 0)).abs();\n const numerator = usdcLoss.add(upnlLoss);\n\n const collateralSum = assets.reduce<Decimal>((acc, asset) => {\n return acc.add(\n new Decimal(Math.max(asset.qty, 0))\n .mul(new Decimal(asset.indexPrice))\n .mul(new Decimal(asset.weight)),\n );\n }, zero);\n\n const denominator = collateralSum.add(new Decimal(Math.max(upnl, 0)));\n\n if (numerator.isZero() || denominator.isZero()) {\n return 0;\n }\n\n return numerator.div(denominator).toNumber();\n};\n\nexport const maxWithdrawalUSDC = (inputs: {\n USDCBalance: number;\n freeCollateral: Decimal;\n upnl: number;\n}) => {\n const { USDCBalance, freeCollateral, upnl } = inputs;\n const value = Math.min(\n new Decimal(USDCBalance).toNumber(),\n new Decimal(freeCollateral).sub(Math.max(upnl, 0)).toNumber(),\n );\n return Math.max(0, value);\n};\n\nexport const maxWithdrawalOtherCollateral = (inputs: {\n USDCBalance: number;\n collateralQty: number;\n freeCollateral: Decimal;\n indexPrice: number;\n weight: Decimal;\n}) => {\n const { USDCBalance, collateralQty, freeCollateral, indexPrice, weight } =\n inputs;\n const usdcBalance = new Decimal(USDCBalance);\n const denominator = usdcBalance.isNegative()\n ? new Decimal(indexPrice).mul(weight).mul(new Decimal(1).add(0.001))\n : new Decimal(indexPrice).mul(weight);\n if (denominator.isZero()) {\n return zero;\n }\n const qty = new Decimal(collateralQty);\n const maxQtyByValue = new Decimal(freeCollateral).div(denominator);\n return maxQtyByValue.lt(qty) ? maxQtyByValue : qty;\n};\n\nexport const calcMinimumReceived = (inputs: {\n amount: number;\n slippage: number;\n}) => {\n const { amount, slippage } = inputs;\n const slippageRatio = new Decimal(slippage).div(100);\n return new Decimal(amount)\n .mul(new Decimal(1).minus(slippageRatio))\n .toNumber();\n};\n\nexport const maxLeverage = (inputs: {\n symbolLeverage?: number;\n accountLeverage: number;\n}) => {\n const { symbolLeverage, accountLeverage } = inputs;\n\n return Math.min(symbolLeverage ?? accountLeverage, accountLeverage);\n};\n","import { API as orderUtils } from \"@orderly.network/types\";\nimport { Decimal, zero } from \"@orderly.network/utils\";\nimport { notional } from \"./positions\";\n\n/**\n * Maximum price when placing an order\n */\nexport function maxPrice(markprice: number, range: number) {\n return markprice * (1 + range);\n}\n\n/**\n * Minimum price when placing an order\n */\nexport function minPrice(markprice: number, range: number) {\n return markprice * (1 - range);\n}\n\n/**\n * Scope price when placing an order\n * @returns number\n */\nexport function scopePrice(\n price: number,\n scope: number,\n side: \"BUY\" | \"SELL\",\n): number {\n if (side === \"BUY\") {\n return price * (1 - scope);\n }\n return price * (1 + scope);\n}\n\n/**\n * Calculate the order fee\n */\nexport function orderFee(inputs: {\n /**\n * Order quantity\n */\n qty: number;\n price: number;\n futuresTakeFeeRate: number;\n}): number {\n return new Decimal(inputs.qty)\n .mul(inputs.price)\n .mul(inputs.futuresTakeFeeRate)\n .toNumber();\n}\n\nexport type EstimatedLiquidationPriceInputs = {\n totalCollateral: number;\n markPrice: number;\n baseMMR: number;\n baseIMR: number;\n IMR_Factor: number;\n orderFee: number;\n positions: Pick<\n orderUtils.PositionExt,\n \"position_qty\" | \"mark_price\" | \"symbol\" | \"mmr\"\n >[];\n newOrder: {\n symbol: string;\n qty: number;\n price: number;\n };\n};\n\n/**\n * Estimated liquidation price\n * @param inputs\n * @returns\n */\nexport function estLiqPrice(inputs: EstimatedLiquidationPriceInputs): number {\n const {\n positions,\n newOrder,\n totalCollateral,\n markPrice,\n baseIMR,\n baseMMR,\n orderFee,\n IMR_Factor,\n } = inputs;\n // opened positions for the symbol\n let currentPosition:\n | Pick<\n orderUtils.PositionExt,\n \"position_qty\" | \"mark_price\" | \"symbol\" | \"mmr\"\n >\n | undefined = undefined;\n\n let newTotalMM = zero;\n\n const hasPosition =\n positions.filter((item) => item.position_qty > 0).length > 0;\n\n const basePrice = hasPosition ? markPrice : newOrder.price;\n\n const newOrderNotional = new Decimal(newOrder.qty).mul(newOrder.price);\n\n for (let index = 0; index < positions.length; index++) {\n const position = positions[index];\n let notional = new Decimal(position.position_qty).mul(position.mark_price);\n if (newOrder.symbol === position.symbol) {\n currentPosition = position;\n notional = notional.add(newOrderNotional);\n }\n\n newTotalMM = newTotalMM.add(notional.abs().mul(position.mmr));\n }\n\n // if no position\n if (!currentPosition) {\n newTotalMM = newTotalMM.add(newOrderNotional.mul(baseMMR));\n }\n\n const newMMR = Math.max(\n baseMMR,\n new Decimal(baseMMR)\n .div(baseIMR)\n .mul(IMR_Factor)\n .mul(\n newOrderNotional\n .add(\n !!currentPosition\n ? new Decimal(currentPosition.position_qty).mul(\n currentPosition.mark_price,\n )\n : zero,\n )\n .abs(),\n )\n .toPower(4 / 5)\n .toNumber(),\n );\n\n // console.log(\"new MMR\", newMMR, newTotalMM.toNumber());\n\n const newQty = new Decimal(newOrder.qty).add(\n currentPosition?.position_qty ?? 0,\n );\n\n if (newQty.eq(0)) {\n return 0;\n }\n\n const denominator = newQty.abs().mul(newMMR).sub(newQty);\n\n if (denominator.eq(zero)) {\n return 0;\n }\n\n const price = new Decimal(basePrice)\n .add(\n new Decimal(totalCollateral)\n .sub(newTotalMM)\n .sub(orderFee)\n .div(denominator),\n )\n .toNumber();\n\n return Math.max(0, price);\n}\n\nexport type EstimatedLeverageInputs = {\n totalCollateral: number;\n positions: Pick<\n orderUtils.PositionExt,\n \"position_qty\" | \"mark_price\" | \"symbol\"\n >[];\n newOrder: {\n symbol: string;\n qty: number;\n price: number;\n };\n};\n\n/**\n * Estimated leverage\n * @param inputs EstimtedLeverageInputs\n * @returns number\n */\nexport function estLeverage(inputs: EstimatedLeverageInputs): number | null {\n const { totalCollateral, positions, newOrder } = inputs;\n if (totalCollateral <= 0) {\n return null;\n }\n let hasPosition = false;\n let sumPositionNotional = positions.reduce((acc, cur) => {\n let count = new Decimal(cur.position_qty).mul(cur.mark_price);\n // acc = acc.add(\n // new Decimal(cur.position_qty).mul(cur.mark_price)\n // // .abs()\n // );\n\n if (cur.symbol === newOrder.symbol) {\n hasPosition = true;\n // acc = acc.add(new Decimal(newOrder.qty).mul(newOrder.price));\n count = count.add(new Decimal(newOrder.qty).mul(newOrder.price));\n }\n\n return acc.add(count.abs());\n }, zero);\n\n if (!hasPosition) {\n sumPositionNotional = sumPositionNotional.add(\n new Decimal(newOrder.qty).mul(newOrder.price).abs(),\n );\n }\n\n if (sumPositionNotional.eq(zero)) {\n return null;\n }\n\n const totalMarginRatio = new Decimal(totalCollateral).div(\n sumPositionNotional,\n );\n\n return new Decimal(1)\n .div(totalMarginRatio)\n .toDecimalPlaces(2, Decimal.ROUND_HALF_EVEN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Leverage","totalCollateral","otherIMs","totalUnrealizedPnL","totalValue","totalMarginRatio","MMR","collateralRatio","freeCollateral","Decimal","zero","totalCollateral","orderFee","notional","totalMarginRatio"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orderly.network/perp",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.7.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -21,11 +21,11 @@
|
|
|
21
21
|
"tsup": "^7.1.0",
|
|
22
22
|
"typedoc": "^0.26.5",
|
|
23
23
|
"typescript": "^5.1.6",
|
|
24
|
-
"tsconfig": "0.
|
|
24
|
+
"tsconfig": "0.10.0"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@orderly.network/
|
|
28
|
-
"@orderly.network/
|
|
27
|
+
"@orderly.network/utils": "2.7.0",
|
|
28
|
+
"@orderly.network/types": "2.7.0"
|
|
29
29
|
},
|
|
30
30
|
"publishConfig": {
|
|
31
31
|
"access": "public"
|