@orderly.network/perp 1.0.24-alpha.8 → 1.0.24

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 CHANGED
@@ -8,7 +8,7 @@ declare global {
8
8
  };
9
9
  }
10
10
  }
11
- declare const _default: "1.0.24-alpha.8";
11
+ declare const _default: "1.0.24";
12
12
 
13
13
  /**
14
14
  * Calculates the notional value of a single position.
@@ -24,7 +24,7 @@ declare function notional(qty: number, mark_price: number): number;
24
24
  * @link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Notional
25
25
  */
26
26
  declare function totalNotional(positions: API.Position[]): number;
27
- type UnrealPnLInputs = {
27
+ declare type UnrealPnLInputs = {
28
28
  markPrice: number;
29
29
  openPrice: number;
30
30
  qty: number;
@@ -35,7 +35,7 @@ type UnrealPnLInputs = {
35
35
  * @returns The unrealized profit or loss of the position.
36
36
  */
37
37
  declare function unrealizedPnL(inputs: UnrealPnLInputs): number;
38
- type UnrealPnLROIInputs = {
38
+ declare type UnrealPnLROIInputs = {
39
39
  positionQty: number;
40
40
  openPrice: number;
41
41
  IMR: number;
@@ -53,7 +53,7 @@ declare function unrealizedPnLROI(inputs: UnrealPnLROIInputs): number;
53
53
  * @returns The total unrealized profit or loss of all positions.
54
54
  */
55
55
  declare function totalUnrealizedPnL(positions: API.Position[]): number;
56
- type LiqPriceInputs = {
56
+ declare type LiqPriceInputs = {
57
57
  markPrice: number;
58
58
  totalCollateral: number;
59
59
  positionQty: number;
@@ -66,7 +66,7 @@ type LiqPriceInputs = {
66
66
  * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Position-Liq.-Price}
67
67
  */
68
68
  declare function liqPrice(inputs: LiqPriceInputs): number;
69
- type MMInputs = {
69
+ declare type MMInputs = {
70
70
  positionQty: number;
71
71
  markPrice: number;
72
72
  MMR: number;
@@ -77,7 +77,7 @@ type MMInputs = {
77
77
  * @returns The maintenance margin of the position.
78
78
  */
79
79
  declare function maintenanceMargin(inputs: MMInputs): number;
80
- type UnsettlementPnLInputs = {
80
+ declare type UnsettlementPnLInputs = {
81
81
  positionQty: number;
82
82
  markPrice: number;
83
83
  costPosition: number;
@@ -91,7 +91,7 @@ type UnsettlementPnLInputs = {
91
91
  * @link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Unsettlement-PNL-%5BinlineExtension%5D
92
92
  */
93
93
  declare function unsettlementPnL(inputs: UnsettlementPnLInputs): number;
94
- type TotalUnsettlementPnLInputs = {
94
+ declare type TotalUnsettlementPnLInputs = {
95
95
  positions: (API.Position & {
96
96
  sum_unitary_funding: number;
97
97
  })[];
@@ -156,10 +156,10 @@ declare namespace positions {
156
156
  };
157
157
  }
158
158
 
159
- type ResultOptions = {
159
+ declare type ResultOptions = {
160
160
  dp: number;
161
161
  };
162
- type TotalValueInputs = {
162
+ declare type TotalValueInputs = {
163
163
  totalUnsettlementPnL: number;
164
164
  USDCHolding: number;
165
165
  nonUSDCHolding: {
@@ -178,7 +178,7 @@ declare function totalValue(inputs: TotalValueInputs): Decimal;
178
178
  *
179
179
  * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Free-collateral}
180
180
  */
181
- type FreeCollateralInputs = {
181
+ declare type FreeCollateralInputs = {
182
182
  totalCollateral: Decimal;
183
183
  totalInitialMarginWithOrders: number;
184
184
  };
@@ -186,7 +186,7 @@ type FreeCollateralInputs = {
186
186
  * 计算可用保证金
187
187
  */
188
188
  declare function freeCollateral(inputs: FreeCollateralInputs): Decimal;
189
- type TotalCollateralValueInputs = {
189
+ declare type TotalCollateralValueInputs = {
190
190
  USDCHolding: number;
191
191
  nonUSDCHolding: {
192
192
  holding: number;
@@ -201,7 +201,7 @@ type TotalCollateralValueInputs = {
201
201
  */
202
202
  declare function totalCollateral(inputs: TotalCollateralValueInputs): Decimal;
203
203
  declare function initialMarginWithOrder(): void;
204
- type PositionNotionalWithOrderInputs = {
204
+ declare type PositionNotionalWithOrderInputs = {
205
205
  markPrice: number;
206
206
  positionQtyWithOrders: number;
207
207
  };
@@ -209,7 +209,7 @@ type PositionNotionalWithOrderInputs = {
209
209
  * 單一 Symbol position / orders notional 加總
210
210
  */
211
211
  declare function positionNotionalWithOrder_by_symbol(inputs: PositionNotionalWithOrderInputs): Decimal;
212
- type PositionQtyWithOrderInputs = {
212
+ declare type PositionQtyWithOrderInputs = {
213
213
  positionQty: number;
214
214
  buyOrdersQty: number;
215
215
  sellOrdersQty: number;
@@ -218,7 +218,7 @@ type PositionQtyWithOrderInputs = {
218
218
  * 單一 Symbol position / orders qty 加總
219
219
  */
220
220
  declare function positionQtyWithOrders_by_symbol(inputs: PositionQtyWithOrderInputs): number;
221
- type IMRInputs = {
221
+ declare type IMRInputs = {
222
222
  maxLeverage: number;
223
223
  baseIMR: number;
224
224
  IMR_Factor: number;
@@ -247,7 +247,7 @@ declare function getPositonsAndOrdersNotionalBySymbol(inputs: {
247
247
  symbol: string;
248
248
  markPrice: number;
249
249
  }): number;
250
- type TotalInitialMarginWithOrdersInputs = {
250
+ declare type TotalInitialMarginWithOrdersInputs = {
251
251
  positions: API.Position[];
252
252
  orders: API.Order[];
253
253
  markPrices: {
@@ -275,7 +275,7 @@ declare function groupOrdersBySymbol(orders: API.Order[]): {
275
275
  * @returns An array of unique symbols.
276
276
  */
277
277
  declare function extractSymbols(positions: Pick<API.Position, "symbol">[], orders: Pick<API.Order, "symbol">[]): string[];
278
- type OtherIMsInputs = {
278
+ declare type OtherIMsInputs = {
279
279
  positions: API.Position[];
280
280
  orders: API.Order[];
281
281
  markPrices: {
@@ -291,7 +291,7 @@ type OtherIMsInputs = {
291
291
  * 除当前symbol外的其他symbol已占用的总保证金
292
292
  */
293
293
  declare function otherIMs(inputs: OtherIMsInputs): number;
294
- type MaxQtyInputs = {
294
+ declare type MaxQtyInputs = {
295
295
  symbol: string;
296
296
  baseMaxQty: number;
297
297
  /**
@@ -319,7 +319,7 @@ type MaxQtyInputs = {
319
319
  declare function maxQty(side: OrderSide, inputs: MaxQtyInputs, options?: ResultOptions): number;
320
320
  declare function maxQtyByLong(inputs: Omit<MaxQtyInputs, "side">): number;
321
321
  declare function maxQtyByShort(inputs: Omit<MaxQtyInputs, "side">): number;
322
- type TotalMarginRatioInputs = {
322
+ declare type TotalMarginRatioInputs = {
323
323
  totalCollateral: number;
324
324
  markPrices: {
325
325
  [key: string]: number;
@@ -330,7 +330,7 @@ type TotalMarginRatioInputs = {
330
330
  * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Margin-Ratio}
331
331
  */
332
332
  declare function totalMarginRatio(inputs: TotalMarginRatioInputs, dp?: number): number;
333
- type TotalUnrealizedROIInputs = {
333
+ declare type TotalUnrealizedROIInputs = {
334
334
  totalUnrealizedPnL: number;
335
335
  totalValue: number;
336
336
  };
@@ -343,7 +343,7 @@ declare function totalUnrealizedROI(inputs: TotalUnrealizedROIInputs): number;
343
343
  * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Current-account-leverage}
344
344
  */
345
345
  declare function currentLeverage(totalMarginRatio: number): number;
346
- type AvailableBalanceInputs = {
346
+ declare type AvailableBalanceInputs = {
347
347
  USDCHolding: number;
348
348
  unsettlementPnL: number;
349
349
  };
package/dist/index.d.ts CHANGED
@@ -8,7 +8,7 @@ declare global {
8
8
  };
9
9
  }
10
10
  }
11
- declare const _default: "1.0.24-alpha.8";
11
+ declare const _default: "1.0.24";
12
12
 
13
13
  /**
14
14
  * Calculates the notional value of a single position.
@@ -24,7 +24,7 @@ declare function notional(qty: number, mark_price: number): number;
24
24
  * @link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Notional
25
25
  */
26
26
  declare function totalNotional(positions: API.Position[]): number;
27
- type UnrealPnLInputs = {
27
+ declare type UnrealPnLInputs = {
28
28
  markPrice: number;
29
29
  openPrice: number;
30
30
  qty: number;
@@ -35,7 +35,7 @@ type UnrealPnLInputs = {
35
35
  * @returns The unrealized profit or loss of the position.
36
36
  */
37
37
  declare function unrealizedPnL(inputs: UnrealPnLInputs): number;
38
- type UnrealPnLROIInputs = {
38
+ declare type UnrealPnLROIInputs = {
39
39
  positionQty: number;
40
40
  openPrice: number;
41
41
  IMR: number;
@@ -53,7 +53,7 @@ declare function unrealizedPnLROI(inputs: UnrealPnLROIInputs): number;
53
53
  * @returns The total unrealized profit or loss of all positions.
54
54
  */
55
55
  declare function totalUnrealizedPnL(positions: API.Position[]): number;
56
- type LiqPriceInputs = {
56
+ declare type LiqPriceInputs = {
57
57
  markPrice: number;
58
58
  totalCollateral: number;
59
59
  positionQty: number;
@@ -66,7 +66,7 @@ type LiqPriceInputs = {
66
66
  * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Position-Liq.-Price}
67
67
  */
68
68
  declare function liqPrice(inputs: LiqPriceInputs): number;
69
- type MMInputs = {
69
+ declare type MMInputs = {
70
70
  positionQty: number;
71
71
  markPrice: number;
72
72
  MMR: number;
@@ -77,7 +77,7 @@ type MMInputs = {
77
77
  * @returns The maintenance margin of the position.
78
78
  */
79
79
  declare function maintenanceMargin(inputs: MMInputs): number;
80
- type UnsettlementPnLInputs = {
80
+ declare type UnsettlementPnLInputs = {
81
81
  positionQty: number;
82
82
  markPrice: number;
83
83
  costPosition: number;
@@ -91,7 +91,7 @@ type UnsettlementPnLInputs = {
91
91
  * @link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Unsettlement-PNL-%5BinlineExtension%5D
92
92
  */
93
93
  declare function unsettlementPnL(inputs: UnsettlementPnLInputs): number;
94
- type TotalUnsettlementPnLInputs = {
94
+ declare type TotalUnsettlementPnLInputs = {
95
95
  positions: (API.Position & {
96
96
  sum_unitary_funding: number;
97
97
  })[];
@@ -156,10 +156,10 @@ declare namespace positions {
156
156
  };
157
157
  }
158
158
 
159
- type ResultOptions = {
159
+ declare type ResultOptions = {
160
160
  dp: number;
161
161
  };
162
- type TotalValueInputs = {
162
+ declare type TotalValueInputs = {
163
163
  totalUnsettlementPnL: number;
164
164
  USDCHolding: number;
165
165
  nonUSDCHolding: {
@@ -178,7 +178,7 @@ declare function totalValue(inputs: TotalValueInputs): Decimal;
178
178
  *
179
179
  * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Free-collateral}
180
180
  */
181
- type FreeCollateralInputs = {
181
+ declare type FreeCollateralInputs = {
182
182
  totalCollateral: Decimal;
183
183
  totalInitialMarginWithOrders: number;
184
184
  };
@@ -186,7 +186,7 @@ type FreeCollateralInputs = {
186
186
  * 计算可用保证金
187
187
  */
188
188
  declare function freeCollateral(inputs: FreeCollateralInputs): Decimal;
189
- type TotalCollateralValueInputs = {
189
+ declare type TotalCollateralValueInputs = {
190
190
  USDCHolding: number;
191
191
  nonUSDCHolding: {
192
192
  holding: number;
@@ -201,7 +201,7 @@ type TotalCollateralValueInputs = {
201
201
  */
202
202
  declare function totalCollateral(inputs: TotalCollateralValueInputs): Decimal;
203
203
  declare function initialMarginWithOrder(): void;
204
- type PositionNotionalWithOrderInputs = {
204
+ declare type PositionNotionalWithOrderInputs = {
205
205
  markPrice: number;
206
206
  positionQtyWithOrders: number;
207
207
  };
@@ -209,7 +209,7 @@ type PositionNotionalWithOrderInputs = {
209
209
  * 單一 Symbol position / orders notional 加總
210
210
  */
211
211
  declare function positionNotionalWithOrder_by_symbol(inputs: PositionNotionalWithOrderInputs): Decimal;
212
- type PositionQtyWithOrderInputs = {
212
+ declare type PositionQtyWithOrderInputs = {
213
213
  positionQty: number;
214
214
  buyOrdersQty: number;
215
215
  sellOrdersQty: number;
@@ -218,7 +218,7 @@ type PositionQtyWithOrderInputs = {
218
218
  * 單一 Symbol position / orders qty 加總
219
219
  */
220
220
  declare function positionQtyWithOrders_by_symbol(inputs: PositionQtyWithOrderInputs): number;
221
- type IMRInputs = {
221
+ declare type IMRInputs = {
222
222
  maxLeverage: number;
223
223
  baseIMR: number;
224
224
  IMR_Factor: number;
@@ -247,7 +247,7 @@ declare function getPositonsAndOrdersNotionalBySymbol(inputs: {
247
247
  symbol: string;
248
248
  markPrice: number;
249
249
  }): number;
250
- type TotalInitialMarginWithOrdersInputs = {
250
+ declare type TotalInitialMarginWithOrdersInputs = {
251
251
  positions: API.Position[];
252
252
  orders: API.Order[];
253
253
  markPrices: {
@@ -275,7 +275,7 @@ declare function groupOrdersBySymbol(orders: API.Order[]): {
275
275
  * @returns An array of unique symbols.
276
276
  */
277
277
  declare function extractSymbols(positions: Pick<API.Position, "symbol">[], orders: Pick<API.Order, "symbol">[]): string[];
278
- type OtherIMsInputs = {
278
+ declare type OtherIMsInputs = {
279
279
  positions: API.Position[];
280
280
  orders: API.Order[];
281
281
  markPrices: {
@@ -291,7 +291,7 @@ type OtherIMsInputs = {
291
291
  * 除当前symbol外的其他symbol已占用的总保证金
292
292
  */
293
293
  declare function otherIMs(inputs: OtherIMsInputs): number;
294
- type MaxQtyInputs = {
294
+ declare type MaxQtyInputs = {
295
295
  symbol: string;
296
296
  baseMaxQty: number;
297
297
  /**
@@ -319,7 +319,7 @@ type MaxQtyInputs = {
319
319
  declare function maxQty(side: OrderSide, inputs: MaxQtyInputs, options?: ResultOptions): number;
320
320
  declare function maxQtyByLong(inputs: Omit<MaxQtyInputs, "side">): number;
321
321
  declare function maxQtyByShort(inputs: Omit<MaxQtyInputs, "side">): number;
322
- type TotalMarginRatioInputs = {
322
+ declare type TotalMarginRatioInputs = {
323
323
  totalCollateral: number;
324
324
  markPrices: {
325
325
  [key: string]: number;
@@ -330,7 +330,7 @@ type TotalMarginRatioInputs = {
330
330
  * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Margin-Ratio}
331
331
  */
332
332
  declare function totalMarginRatio(inputs: TotalMarginRatioInputs, dp?: number): number;
333
- type TotalUnrealizedROIInputs = {
333
+ declare type TotalUnrealizedROIInputs = {
334
334
  totalUnrealizedPnL: number;
335
335
  totalValue: number;
336
336
  };
@@ -343,7 +343,7 @@ declare function totalUnrealizedROI(inputs: TotalUnrealizedROIInputs): number;
343
343
  * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Current-account-leverage}
344
344
  */
345
345
  declare function currentLeverage(totalMarginRatio: number): number;
346
- type AvailableBalanceInputs = {
346
+ declare type AvailableBalanceInputs = {
347
347
  USDCHolding: number;
348
348
  unsettlementPnL: number;
349
349
  };
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"] = "1.0.24-alpha.8";
34
+ window.__ORDERLY_VERSION__["@orderly.network/perp"] = "1.0.24";
35
35
  }
36
- var version_default = "1.0.24-alpha.8";
36
+ var version_default = "1.0.24";
37
37
 
38
38
  // src/positions.ts
39
39
  var positions_exports = {};
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","\ndeclare 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\"] = \"1.0.24-alpha.8\";\n};\n\nexport default \"1.0.24-alpha.8\";\n","import { API } from \"@orderly.network/types\";\nimport { Decimal } 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 * @link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Notional\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(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 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 * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Position-Liq.-Price}\n */\nexport function liqPrice(inputs: LiqPriceInputs): number {\n const { markPrice, totalCollateral, positionQty, MMR } = inputs;\n const totalNotional = notional(positionQty, markPrice);\n\n if (positionQty === 0) {\n return 0;\n }\n\n return Math.max(\n new Decimal(markPrice)\n .add(\n new Decimal(totalCollateral)\n .sub(new Decimal(totalNotional).mul(MMR))\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 * @link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Unsettlement-PNL-%5BinlineExtension%5D\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 * @link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Unsettlement-PNL-%5BinlineExtension%5D\n */\nexport function totalUnsettlementPnL(\n positions: (API.Position & {\n sum_unitary_funding: number;\n })[]\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\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: {\n baseMMR: number;\n baseIMR: number;\n IMRFactor: number;\n positionNotional: number;\n IMR_factor_power: number;\n}): 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.abs(positionNotional))\n .toPower(IMR_factor_power)\n .toNumber()\n );\n}\n","/**\n * The power of the IMR factor.\n * @constant\n * @default\n */\nexport const IMRFactorPower = 4 / 5;\n","import { Decimal, zero } from \"@orderly.network/utils\";\nimport { IMRFactorPower } from \"./constants\";\nimport {\n API,\n OrderSide,\n OrderType,\n type WSMessage,\n} from \"@orderly.network/types\";\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 markPrice: number;\n //保證金替代率 暂时默认0\n discount: number;\n }[];\n};\n/**\n * 用戶總資產價值 (USDC計價),包含無法作為保證金的資產\n * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Value}\n */\nexport function totalValue(inputs: TotalValueInputs): Decimal {\n const { totalUnsettlementPnL, USDCHolding, nonUSDCHolding } = inputs;\n\n const nonUSDCHoldingValue = nonUSDCHolding.reduce((acc, cur) => {\n return new Decimal(cur.holding).mul(cur.markPrice).add(acc);\n }, zero);\n\n return nonUSDCHoldingValue.add(USDCHolding).add(totalUnsettlementPnL);\n}\n\n/**\n * 用戶帳戶當前可用保證金的價值總和 (USDC計價)\n *\n * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Free-collateral}\n */\nexport type FreeCollateralInputs = {\n // 总保证金\n totalCollateral: Decimal;\n // 总初始保证金\n totalInitialMarginWithOrders: number;\n};\n/**\n * 计算可用保证金\n */\nexport function freeCollateral(inputs: FreeCollateralInputs): Decimal {\n return inputs.totalCollateral.sub(inputs.totalInitialMarginWithOrders);\n}\n\nexport type TotalCollateralValueInputs = {\n // USDC 的 holding 數量\n USDCHolding: number;\n nonUSDCHolding: {\n holding: number;\n markPrice: number;\n //保證金替代率 暂时默认0\n discount: number;\n }[];\n // 未结算盈亏\n unsettlementPnL: number;\n};\n/**\n * 计算总保证金\n * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-collateral-%5BinlineExtension%5D}\n */\nexport function totalCollateral(inputs: TotalCollateralValueInputs): Decimal {\n const { USDCHolding, nonUSDCHolding } = inputs;\n const nonUSDCHoldingValue = nonUSDCHolding.reduce((acc, cur) => {\n return (\n acc +\n new Decimal(cur.holding).mul(cur.markPrice).mul(cur.discount).toNumber()\n );\n }, 0);\n\n return new Decimal(USDCHolding)\n .add(nonUSDCHoldingValue)\n .add(inputs.unsettlementPnL);\n}\n\nexport function initialMarginWithOrder() {}\n\nexport type PositionNotionalWithOrderInputs = {\n markPrice: number;\n positionQtyWithOrders: number;\n};\n/**\n * 單一 Symbol position / orders notional 加總\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 // 单个Symbol的所有买单合计\n buyOrdersQty: number;\n // 单个Symbol的所有卖单合计\n sellOrdersQty: number;\n};\n/**\n * 單一 Symbol position / orders qty 加總\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 * 單一 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 * 从仓位列表中获取指定symbol的仓位数量\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 * 从订单列表中获取指定symbol的看多,看空订单数量,\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 * 计算用戶已使用初始保證金加總 ( 包含 position / 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\n/**\n * 把订单按照symbol分组, 因为一个symbol可以有多个挂单\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 // 传入除当前symbol外的其他symbol的仓位列表\n positions: API.Position[];\n // 传入除当前symbol外的其他symbol的订单列表\n orders: API.Order[];\n\n markPrices: { [key: string]: number };\n maxLeverage: number;\n symbolInfo: any;\n IMR_Factors: { [key: string]: number };\n};\n/**\n * 除当前symbol外的其他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 = extractSymbols(positions, orders);\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 positionQty = getQtyFromPositions(positions, symbol);\n const positionNotional = markPriceDecimal.mul(positionQty).toNumber();\n\n const buyOrdersQty = getQtyFromOrdersBySide(\n orders,\n symbol,\n OrderSide.BUY\n );\n const sellOrdersQty = getQtyFromOrdersBySide(\n orders,\n symbol,\n OrderSide.SELL\n );\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 //單次開倉最大 Qty 限制, /v1/public/info.base_max\n baseMaxQty: number;\n /**\n * 用户保证金总额(USDC 计价), 可以由 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 // 已开仓数量\n positionQty: number;\n // 已挂多单数量\n buyOrdersQty: number;\n // 已挂空单数量\n sellOrdersQty: number;\n\n IMR_Factor: number;\n\n takerFeeRate: number;\n};\n\n/**\n * 最大可下单数量\n * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Max-Order-QTY}\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(inputs: Omit<MaxQtyInputs, \"side\">): 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)\n .add(buyOrdersQty)\n // .abs()\n .div(new Decimal(takerFeeRate).mul(2).mul(0.0001).add(1))\n )\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(inputs: Omit<MaxQtyInputs, \"side\">): 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\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 .add(\n new Decimal(positionQty)\n .add(sellOrdersQty)\n // .abs()\n .div(new Decimal(takerFeeRate).mul(2).mul(0.0001).add(1))\n )\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 * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#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 * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Unrealized-ROI}\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 * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#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","/**\n * 下单时的最高价格\n * @see https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Max-price\n */\nexport function maxPrice(markprice: number, range: number) {\n return markprice * (1 + range);\n}\n\n/**\n * 下单时的最低价格\n * @see https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Min-price\n */\nexport function minPrice(markprice: number, range: number) {\n return markprice * (1 - range);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACQA,IAAG,OAAO,WAAW,aAAa;AAC9B,SAAO,sBAAsB,OAAO,uBAAuB,CAAC;AAC5D,SAAO,oBAAoB,uBAAuB,IAAI;AAC1D;AAEA,IAAO,kBAAQ;;;ACbf;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,mBAAwB;;;ACIjB,IAAM,iBAAiB,IAAI;;;ADK3B,SAAS,SAAS,KAAa,YAA4B;A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ateral","MMR","totalNotional","import_utils","totalUnsettlementPnL","totalCollateral","otherIMs","totalUnrealizedPnL","totalValue","totalMarginRatio","unsettlementPnL"]}
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","\ndeclare 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\"] = \"1.0.24\";\n};\n\nexport default \"1.0.24\";\n","import { API } from \"@orderly.network/types\";\nimport { Decimal } 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 * @link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Notional\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(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 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 * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Position-Liq.-Price}\n */\nexport function liqPrice(inputs: LiqPriceInputs): number {\n const { markPrice, totalCollateral, positionQty, MMR } = inputs;\n const totalNotional = notional(positionQty, markPrice);\n\n if (positionQty === 0) {\n return 0;\n }\n\n return Math.max(\n new Decimal(markPrice)\n .add(\n new Decimal(totalCollateral)\n .sub(new Decimal(totalNotional).mul(MMR))\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 * @link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Unsettlement-PNL-%5BinlineExtension%5D\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 * @link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Unsettlement-PNL-%5BinlineExtension%5D\n */\nexport function totalUnsettlementPnL(\n positions: (API.Position & {\n sum_unitary_funding: number;\n })[]\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\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: {\n baseMMR: number;\n baseIMR: number;\n IMRFactor: number;\n positionNotional: number;\n IMR_factor_power: number;\n}): 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.abs(positionNotional))\n .toPower(IMR_factor_power)\n .toNumber()\n );\n}\n","/**\n * The power of the IMR factor.\n * @constant\n * @default\n */\nexport const IMRFactorPower = 4 / 5;\n","import { Decimal, zero } from \"@orderly.network/utils\";\nimport { IMRFactorPower } from \"./constants\";\nimport {\n API,\n OrderSide,\n OrderType,\n type WSMessage,\n} from \"@orderly.network/types\";\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 markPrice: number;\n //保證金替代率 暂时默认0\n discount: number;\n }[];\n};\n/**\n * 用戶總資產價值 (USDC計價),包含無法作為保證金的資產\n * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Value}\n */\nexport function totalValue(inputs: TotalValueInputs): Decimal {\n const { totalUnsettlementPnL, USDCHolding, nonUSDCHolding } = inputs;\n\n const nonUSDCHoldingValue = nonUSDCHolding.reduce((acc, cur) => {\n return new Decimal(cur.holding).mul(cur.markPrice).add(acc);\n }, zero);\n\n return nonUSDCHoldingValue.add(USDCHolding).add(totalUnsettlementPnL);\n}\n\n/**\n * 用戶帳戶當前可用保證金的價值總和 (USDC計價)\n *\n * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Free-collateral}\n */\nexport type FreeCollateralInputs = {\n // 总保证金\n totalCollateral: Decimal;\n // 总初始保证金\n totalInitialMarginWithOrders: number;\n};\n/**\n * 计算可用保证金\n */\nexport function freeCollateral(inputs: FreeCollateralInputs): Decimal {\n return inputs.totalCollateral.sub(inputs.totalInitialMarginWithOrders);\n}\n\nexport type TotalCollateralValueInputs = {\n // USDC 的 holding 數量\n USDCHolding: number;\n nonUSDCHolding: {\n holding: number;\n markPrice: number;\n //保證金替代率 暂时默认0\n discount: number;\n }[];\n // 未结算盈亏\n unsettlementPnL: number;\n};\n/**\n * 计算总保证金\n * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-collateral-%5BinlineExtension%5D}\n */\nexport function totalCollateral(inputs: TotalCollateralValueInputs): Decimal {\n const { USDCHolding, nonUSDCHolding } = inputs;\n const nonUSDCHoldingValue = nonUSDCHolding.reduce((acc, cur) => {\n return (\n acc +\n new Decimal(cur.holding).mul(cur.markPrice).mul(cur.discount).toNumber()\n );\n }, 0);\n\n return new Decimal(USDCHolding)\n .add(nonUSDCHoldingValue)\n .add(inputs.unsettlementPnL);\n}\n\nexport function initialMarginWithOrder() {}\n\nexport type PositionNotionalWithOrderInputs = {\n markPrice: number;\n positionQtyWithOrders: number;\n};\n/**\n * 單一 Symbol position / orders notional 加總\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 // 单个Symbol的所有买单合计\n buyOrdersQty: number;\n // 单个Symbol的所有卖单合计\n sellOrdersQty: number;\n};\n/**\n * 單一 Symbol position / orders qty 加總\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 * 單一 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 * 从仓位列表中获取指定symbol的仓位数量\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 * 从订单列表中获取指定symbol的看多,看空订单数量,\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 * 计算用戶已使用初始保證金加總 ( 包含 position / 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\n/**\n * 把订单按照symbol分组, 因为一个symbol可以有多个挂单\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 // 传入除当前symbol外的其他symbol的仓位列表\n positions: API.Position[];\n // 传入除当前symbol外的其他symbol的订单列表\n orders: API.Order[];\n\n markPrices: { [key: string]: number };\n maxLeverage: number;\n symbolInfo: any;\n IMR_Factors: { [key: string]: number };\n};\n/**\n * 除当前symbol外的其他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 = extractSymbols(positions, orders);\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 positionQty = getQtyFromPositions(positions, symbol);\n const positionNotional = markPriceDecimal.mul(positionQty).toNumber();\n\n const buyOrdersQty = getQtyFromOrdersBySide(\n orders,\n symbol,\n OrderSide.BUY\n );\n const sellOrdersQty = getQtyFromOrdersBySide(\n orders,\n symbol,\n OrderSide.SELL\n );\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 //單次開倉最大 Qty 限制, /v1/public/info.base_max\n baseMaxQty: number;\n /**\n * 用户保证金总额(USDC 计价), 可以由 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 // 已开仓数量\n positionQty: number;\n // 已挂多单数量\n buyOrdersQty: number;\n // 已挂空单数量\n sellOrdersQty: number;\n\n IMR_Factor: number;\n\n takerFeeRate: number;\n};\n\n/**\n * 最大可下单数量\n * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Max-Order-QTY}\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(inputs: Omit<MaxQtyInputs, \"side\">): 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)\n .add(buyOrdersQty)\n // .abs()\n .div(new Decimal(takerFeeRate).mul(2).mul(0.0001).add(1))\n )\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(inputs: Omit<MaxQtyInputs, \"side\">): 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\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 .add(\n new Decimal(positionQty)\n .add(sellOrdersQty)\n // .abs()\n .div(new Decimal(takerFeeRate).mul(2).mul(0.0001).add(1))\n )\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 * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#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 * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Unrealized-ROI}\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 * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#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","/**\n * 下单时的最高价格\n * @see https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Max-price\n */\nexport function maxPrice(markprice: number, range: number) {\n return markprice * (1 + range);\n}\n\n/**\n * 下单时的最低价格\n * @see https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Min-price\n */\nexport function minPrice(markprice: number, range: number) {\n return markprice * (1 - range);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACQA,IAAG,OAAO,WAAW,aAAa;AAC9B,SAAO,sBAAsB,OAAO,uBAAuB,CAAC;AAC5D,SAAO,oBAAoB,uBAAuB,IAAI;AAC1D;AAEA,IAAO,kBAAQ;;;ACbf;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,mBAAwB;;;ACIjB,IAAM,iBAAiB,IAAI;;;ADK3B,SAAS,SAAS,KAAa,YAA4B;AAChE,SAAO,IAAI,qBAAQ,GAAG,EAAE,IAAI,UAAU,EAAE,IAAI,EAAE,SAAS;AACzD;AAQO,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,qBAAQ,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,qBAAQ,OAAO,aAAa,EACpC,IAAI,IAAI,qBAAQ,OAAO,WAAW,EAAE,IAAI,SAAS,EAAE,IAAIA,IAAG,CAAC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AAqB,EAAE,SAAS;AACpE;AAWO,SAAS,mBAAmB,QAAkC;AACnE,QAAM,EAAE,oBAAAE,qBAAoB,YAAAC,YAAW,IAAI;AAE3C,SAAO,IAAI,sBAAQD,mBAAkB,EAClC,IAAIC,cAAaD,mBAAkB,EACnC,SAAS;AACd;AAKO,SAAS,gBAAgBE,mBAA0B;AACxD,MAAIA,sBAAqB,GAAG;AAC1B,WAAO;AAAA,EACT;AACA,SAAO,IAAIA;AACb;AAMO,SAAS,iBAAiB,QAAgC;AAC/D,QAAM,EAAE,aAAa,iBAAAC,iBAAgB,IAAI;AAEzC,SAAO,IAAI,sBAAQ,WAAW,EAAE,IAAIA,gBAAe,EAAE,SAAS;AAChE;;;AC/oBA;AAAA;AAAA;AAAA;AAAA;AAIO,SAAS,SAAS,WAAmB,OAAe;AACzD,SAAO,aAAa,IAAI;AAC1B;AAMO,SAAS,SAAS,WAAmB,OAAe;AACzD,SAAO,aAAa,IAAI;AAC1B;","names":["IMR","totalCollateral","MMR","totalNotional","import_utils","totalUnsettlementPnL","totalCollateral","otherIMs","totalUnrealizedPnL","totalValue","totalMarginRatio","unsettlementPnL"]}
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"] = "1.0.24-alpha.8";
10
+ window.__ORDERLY_VERSION__["@orderly.network/perp"] = "1.0.24";
11
11
  }
12
- var version_default = "1.0.24-alpha.8";
12
+ var version_default = "1.0.24";
13
13
 
14
14
  // src/positions.ts
15
15
  var positions_exports = {};
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/version.ts","../src/positions.ts","../src/constants.ts","../src/account.ts","../src/order.ts"],"sourcesContent":["\ndeclare 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\"] = \"1.0.24-alpha.8\";\n};\n\nexport default \"1.0.24-alpha.8\";\n","import { API } from \"@orderly.network/types\";\nimport { Decimal } 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 * @link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Notional\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(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 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 * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Position-Liq.-Price}\n */\nexport function liqPrice(inputs: LiqPriceInputs): number {\n const { markPrice, totalCollateral, positionQty, MMR } = inputs;\n const totalNotional = notional(positionQty, markPrice);\n\n if (positionQty === 0) {\n return 0;\n }\n\n return Math.max(\n new Decimal(markPrice)\n .add(\n new Decimal(totalCollateral)\n .sub(new Decimal(totalNotional).mul(MMR))\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 * @link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Unsettlement-PNL-%5BinlineExtension%5D\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 * @link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Unsettlement-PNL-%5BinlineExtension%5D\n */\nexport function totalUnsettlementPnL(\n positions: (API.Position & {\n sum_unitary_funding: number;\n })[]\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\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: {\n baseMMR: number;\n baseIMR: number;\n IMRFactor: number;\n positionNotional: number;\n IMR_factor_power: number;\n}): 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.abs(positionNotional))\n .toPower(IMR_factor_power)\n .toNumber()\n );\n}\n","/**\n * The power of the IMR factor.\n * @constant\n * @default\n */\nexport const IMRFactorPower = 4 / 5;\n","import { Decimal, zero } from \"@orderly.network/utils\";\nimport { IMRFactorPower } from \"./constants\";\nimport {\n API,\n OrderSide,\n OrderType,\n type WSMessage,\n} from \"@orderly.network/types\";\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 markPrice: number;\n //保證金替代率 暂时默认0\n discount: number;\n }[];\n};\n/**\n * 用戶總資產價值 (USDC計價),包含無法作為保證金的資產\n * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Value}\n */\nexport function totalValue(inputs: TotalValueInputs): Decimal {\n const { totalUnsettlementPnL, USDCHolding, nonUSDCHolding } = inputs;\n\n const nonUSDCHoldingValue = nonUSDCHolding.reduce((acc, cur) => {\n return new Decimal(cur.holding).mul(cur.markPrice).add(acc);\n }, zero);\n\n return nonUSDCHoldingValue.add(USDCHolding).add(totalUnsettlementPnL);\n}\n\n/**\n * 用戶帳戶當前可用保證金的價值總和 (USDC計價)\n *\n * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Free-collateral}\n */\nexport type FreeCollateralInputs = {\n // 总保证金\n totalCollateral: Decimal;\n // 总初始保证金\n totalInitialMarginWithOrders: number;\n};\n/**\n * 计算可用保证金\n */\nexport function freeCollateral(inputs: FreeCollateralInputs): Decimal {\n return inputs.totalCollateral.sub(inputs.totalInitialMarginWithOrders);\n}\n\nexport type TotalCollateralValueInputs = {\n // USDC 的 holding 數量\n USDCHolding: number;\n nonUSDCHolding: {\n holding: number;\n markPrice: number;\n //保證金替代率 暂时默认0\n discount: number;\n }[];\n // 未结算盈亏\n unsettlementPnL: number;\n};\n/**\n * 计算总保证金\n * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-collateral-%5BinlineExtension%5D}\n */\nexport function totalCollateral(inputs: TotalCollateralValueInputs): Decimal {\n const { USDCHolding, nonUSDCHolding } = inputs;\n const nonUSDCHoldingValue = nonUSDCHolding.reduce((acc, cur) => {\n return (\n acc +\n new Decimal(cur.holding).mul(cur.markPrice).mul(cur.discount).toNumber()\n );\n }, 0);\n\n return new Decimal(USDCHolding)\n .add(nonUSDCHoldingValue)\n .add(inputs.unsettlementPnL);\n}\n\nexport function initialMarginWithOrder() {}\n\nexport type PositionNotionalWithOrderInputs = {\n markPrice: number;\n positionQtyWithOrders: number;\n};\n/**\n * 單一 Symbol position / orders notional 加總\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 // 单个Symbol的所有买单合计\n buyOrdersQty: number;\n // 单个Symbol的所有卖单合计\n sellOrdersQty: number;\n};\n/**\n * 單一 Symbol position / orders qty 加總\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 * 單一 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 * 从仓位列表中获取指定symbol的仓位数量\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 * 从订单列表中获取指定symbol的看多,看空订单数量,\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 * 计算用戶已使用初始保證金加總 ( 包含 position / 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\n/**\n * 把订单按照symbol分组, 因为一个symbol可以有多个挂单\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 // 传入除当前symbol外的其他symbol的仓位列表\n positions: API.Position[];\n // 传入除当前symbol外的其他symbol的订单列表\n orders: API.Order[];\n\n markPrices: { [key: string]: number };\n maxLeverage: number;\n symbolInfo: any;\n IMR_Factors: { [key: string]: number };\n};\n/**\n * 除当前symbol外的其他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 = extractSymbols(positions, orders);\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 positionQty = getQtyFromPositions(positions, symbol);\n const positionNotional = markPriceDecimal.mul(positionQty).toNumber();\n\n const buyOrdersQty = getQtyFromOrdersBySide(\n orders,\n symbol,\n OrderSide.BUY\n );\n const sellOrdersQty = getQtyFromOrdersBySide(\n orders,\n symbol,\n OrderSide.SELL\n );\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 //單次開倉最大 Qty 限制, /v1/public/info.base_max\n baseMaxQty: number;\n /**\n * 用户保证金总额(USDC 计价), 可以由 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 // 已开仓数量\n positionQty: number;\n // 已挂多单数量\n buyOrdersQty: number;\n // 已挂空单数量\n sellOrdersQty: number;\n\n IMR_Factor: number;\n\n takerFeeRate: number;\n};\n\n/**\n * 最大可下单数量\n * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Max-Order-QTY}\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(inputs: Omit<MaxQtyInputs, \"side\">): 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)\n .add(buyOrdersQty)\n // .abs()\n .div(new Decimal(takerFeeRate).mul(2).mul(0.0001).add(1))\n )\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(inputs: Omit<MaxQtyInputs, \"side\">): 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\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 .add(\n new Decimal(positionQty)\n .add(sellOrdersQty)\n // .abs()\n .div(new Decimal(takerFeeRate).mul(2).mul(0.0001).add(1))\n )\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 * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#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 * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Unrealized-ROI}\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 * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#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","/**\n * 下单时的最高价格\n * @see https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Max-price\n */\nexport function maxPrice(markprice: number, range: number) {\n return markprice * (1 + range);\n}\n\n/**\n * 下单时的最低价格\n * @see https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Min-price\n */\nexport function minPrice(markprice: number, range: number) {\n return markprice * (1 - range);\n}\n"],"mappings":";;;;;;;AAQA,IAAG,OAAO,WAAW,aAAa;AAC9B,SAAO,sBAAsB,OAAO,uBAAuB,CAAC;AAC5D,SAAO,oBAAoB,uBAAuB,IAA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AAS;AAChE;;;AC/oBA;AAAA;AAAA;AAAA;AAAA;AAIO,SAAS,SAAS,WAAmB,OAAe;AACzD,SAAO,aAAa,IAAI;AAC1B;AAMO,SAAS,SAAS,WAAmB,OAAe;AACzD,SAAO,aAAa,IAAI;AAC1B;","names":["IMR","totalCollateral","MMR","totalNotional","Decimal","totalUnsettlementPnL","Decimal","totalCollateral","otherIMs","totalUnrealizedPnL","totalValue","totalMarginRatio","unsettlementPnL"]}
1
+ {"version":3,"sources":["../src/version.ts","../src/positions.ts","../src/constants.ts","../src/account.ts","../src/order.ts"],"sourcesContent":["\ndeclare 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\"] = \"1.0.24\";\n};\n\nexport default \"1.0.24\";\n","import { API } from \"@orderly.network/types\";\nimport { Decimal } 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 * @link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Notional\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(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 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 * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Position-Liq.-Price}\n */\nexport function liqPrice(inputs: LiqPriceInputs): number {\n const { markPrice, totalCollateral, positionQty, MMR } = inputs;\n const totalNotional = notional(positionQty, markPrice);\n\n if (positionQty === 0) {\n return 0;\n }\n\n return Math.max(\n new Decimal(markPrice)\n .add(\n new Decimal(totalCollateral)\n .sub(new Decimal(totalNotional).mul(MMR))\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 * @link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Unsettlement-PNL-%5BinlineExtension%5D\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 * @link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Unsettlement-PNL-%5BinlineExtension%5D\n */\nexport function totalUnsettlementPnL(\n positions: (API.Position & {\n sum_unitary_funding: number;\n })[]\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\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: {\n baseMMR: number;\n baseIMR: number;\n IMRFactor: number;\n positionNotional: number;\n IMR_factor_power: number;\n}): 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.abs(positionNotional))\n .toPower(IMR_factor_power)\n .toNumber()\n );\n}\n","/**\n * The power of the IMR factor.\n * @constant\n * @default\n */\nexport const IMRFactorPower = 4 / 5;\n","import { Decimal, zero } from \"@orderly.network/utils\";\nimport { IMRFactorPower } from \"./constants\";\nimport {\n API,\n OrderSide,\n OrderType,\n type WSMessage,\n} from \"@orderly.network/types\";\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 markPrice: number;\n //保證金替代率 暂时默认0\n discount: number;\n }[];\n};\n/**\n * 用戶總資產價值 (USDC計價),包含無法作為保證金的資產\n * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Value}\n */\nexport function totalValue(inputs: TotalValueInputs): Decimal {\n const { totalUnsettlementPnL, USDCHolding, nonUSDCHolding } = inputs;\n\n const nonUSDCHoldingValue = nonUSDCHolding.reduce((acc, cur) => {\n return new Decimal(cur.holding).mul(cur.markPrice).add(acc);\n }, zero);\n\n return nonUSDCHoldingValue.add(USDCHolding).add(totalUnsettlementPnL);\n}\n\n/**\n * 用戶帳戶當前可用保證金的價值總和 (USDC計價)\n *\n * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Free-collateral}\n */\nexport type FreeCollateralInputs = {\n // 总保证金\n totalCollateral: Decimal;\n // 总初始保证金\n totalInitialMarginWithOrders: number;\n};\n/**\n * 计算可用保证金\n */\nexport function freeCollateral(inputs: FreeCollateralInputs): Decimal {\n return inputs.totalCollateral.sub(inputs.totalInitialMarginWithOrders);\n}\n\nexport type TotalCollateralValueInputs = {\n // USDC 的 holding 數量\n USDCHolding: number;\n nonUSDCHolding: {\n holding: number;\n markPrice: number;\n //保證金替代率 暂时默认0\n discount: number;\n }[];\n // 未结算盈亏\n unsettlementPnL: number;\n};\n/**\n * 计算总保证金\n * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-collateral-%5BinlineExtension%5D}\n */\nexport function totalCollateral(inputs: TotalCollateralValueInputs): Decimal {\n const { USDCHolding, nonUSDCHolding } = inputs;\n const nonUSDCHoldingValue = nonUSDCHolding.reduce((acc, cur) => {\n return (\n acc +\n new Decimal(cur.holding).mul(cur.markPrice).mul(cur.discount).toNumber()\n );\n }, 0);\n\n return new Decimal(USDCHolding)\n .add(nonUSDCHoldingValue)\n .add(inputs.unsettlementPnL);\n}\n\nexport function initialMarginWithOrder() {}\n\nexport type PositionNotionalWithOrderInputs = {\n markPrice: number;\n positionQtyWithOrders: number;\n};\n/**\n * 單一 Symbol position / orders notional 加總\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 // 单个Symbol的所有买单合计\n buyOrdersQty: number;\n // 单个Symbol的所有卖单合计\n sellOrdersQty: number;\n};\n/**\n * 單一 Symbol position / orders qty 加總\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 * 單一 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 * 从仓位列表中获取指定symbol的仓位数量\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 * 从订单列表中获取指定symbol的看多,看空订单数量,\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 * 计算用戶已使用初始保證金加總 ( 包含 position / 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\n/**\n * 把订单按照symbol分组, 因为一个symbol可以有多个挂单\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 // 传入除当前symbol外的其他symbol的仓位列表\n positions: API.Position[];\n // 传入除当前symbol外的其他symbol的订单列表\n orders: API.Order[];\n\n markPrices: { [key: string]: number };\n maxLeverage: number;\n symbolInfo: any;\n IMR_Factors: { [key: string]: number };\n};\n/**\n * 除当前symbol外的其他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 = extractSymbols(positions, orders);\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 positionQty = getQtyFromPositions(positions, symbol);\n const positionNotional = markPriceDecimal.mul(positionQty).toNumber();\n\n const buyOrdersQty = getQtyFromOrdersBySide(\n orders,\n symbol,\n OrderSide.BUY\n );\n const sellOrdersQty = getQtyFromOrdersBySide(\n orders,\n symbol,\n OrderSide.SELL\n );\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 //單次開倉最大 Qty 限制, /v1/public/info.base_max\n baseMaxQty: number;\n /**\n * 用户保证金总额(USDC 计价), 可以由 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 // 已开仓数量\n positionQty: number;\n // 已挂多单数量\n buyOrdersQty: number;\n // 已挂空单数量\n sellOrdersQty: number;\n\n IMR_Factor: number;\n\n takerFeeRate: number;\n};\n\n/**\n * 最大可下单数量\n * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Max-Order-QTY}\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(inputs: Omit<MaxQtyInputs, \"side\">): 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)\n .add(buyOrdersQty)\n // .abs()\n .div(new Decimal(takerFeeRate).mul(2).mul(0.0001).add(1))\n )\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(inputs: Omit<MaxQtyInputs, \"side\">): 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\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 .add(\n new Decimal(positionQty)\n .add(sellOrdersQty)\n // .abs()\n .div(new Decimal(takerFeeRate).mul(2).mul(0.0001).add(1))\n )\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 * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#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 * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Unrealized-ROI}\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 * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#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","/**\n * 下单时的最高价格\n * @see https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Max-price\n */\nexport function maxPrice(markprice: number, range: number) {\n return markprice * (1 + range);\n}\n\n/**\n * 下单时的最低价格\n * @see https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Min-price\n */\nexport function minPrice(markprice: number, range: number) {\n return markprice * (1 - range);\n}\n"],"mappings":";;;;;;;AAQA,IAAG,OAAO,WAAW,aAAa;AAC9B,SAAO,sBAAsB,OAAO,uBAAuB,CAAC;AAC5D,SAAO,oBAAoB,uBAAuB,IAAI;AAC1D;AAEA,IAAO,kBAAQ;;;ACbf;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,SAAS,eAAe;;;ACIjB,IAAM,iBAAiB,IAAI;;;ADK3B,SAAS,SAAS,KAAa,YAA4B;AAChE,SAAO,IAAI,QAAQ,GAAG,EAAE,IAAI,UAAU,EAAE,IAAI,EAAE,SAAS;AACzD;AAQO,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,OAAO,WAAW,EAAE,IAAI,SAAS,EAAE,IAAIA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AAI,qBAAqB,EAAE,SAAS;AACpE;AAWO,SAAS,mBAAmB,QAAkC;AACnE,QAAM,EAAE,oBAAAG,qBAAoB,YAAAC,YAAW,IAAI;AAE3C,SAAO,IAAIJ,SAAQG,mBAAkB,EAClC,IAAIC,cAAaD,mBAAkB,EACnC,SAAS;AACd;AAKO,SAAS,gBAAgBE,mBAA0B;AACxD,MAAIA,sBAAqB,GAAG;AAC1B,WAAO;AAAA,EACT;AACA,SAAO,IAAIA;AACb;AAMO,SAAS,iBAAiB,QAAgC;AAC/D,QAAM,EAAE,aAAa,iBAAAC,iBAAgB,IAAI;AAEzC,SAAO,IAAIN,SAAQ,WAAW,EAAE,IAAIM,gBAAe,EAAE,SAAS;AAChE;;;AC/oBA;AAAA;AAAA;AAAA;AAAA;AAIO,SAAS,SAAS,WAAmB,OAAe;AACzD,SAAO,aAAa,IAAI;AAC1B;AAMO,SAAS,SAAS,WAAmB,OAAe;AACzD,SAAO,aAAa,IAAI;AAC1B;","names":["IMR","totalCollateral","MMR","totalNotional","Decimal","totalUnsettlementPnL","Decimal","totalCollateral","otherIMs","totalUnrealizedPnL","totalValue","totalMarginRatio","unsettlementPnL"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orderly.network/perp",
3
- "version": "1.0.24-alpha.8",
3
+ "version": "1.0.24",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -8,7 +8,7 @@
8
8
  "author": "leo",
9
9
  "license": "ISC",
10
10
  "peerDependencies": {
11
- "@orderly.network/types": "0.1.24-alpha.8"
11
+ "@orderly.network/types": "0.1.24"
12
12
  },
13
13
  "files": [
14
14
  "dist"
@@ -26,8 +26,8 @@
26
26
  "tsconfig": "0.0.82"
27
27
  },
28
28
  "dependencies": {
29
- "@orderly.network/utils": "0.0.75",
30
- "@orderly.network/types": "0.1.24-alpha.8"
29
+ "@orderly.network/utils": "0.0.76",
30
+ "@orderly.network/types": "0.1.24"
31
31
  },
32
32
  "publishConfig": {
33
33
  "access": "public"