@orderly.network/perp 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,425 @@
1
+ import { API, OrderSide } from '@orderly.network/types';
2
+ import { Decimal } from '@orderly.network/utils';
3
+
4
+ /**
5
+ * 单个仓位价值
6
+ * @param qty 数量
7
+ * @param mark_price 价格
8
+ */
9
+ declare function notional(qty: number, mark_price: number): number;
10
+ /**
11
+ * 所有仓位价值
12
+ * @param positions
13
+ * @returns
14
+ *
15
+ * @link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Notional
16
+ */
17
+ declare function totalNotional(positions: API.Position[]): number;
18
+ type UnrealPnLInputs = {
19
+ markPrice: number;
20
+ openPrice: number;
21
+ qty: number;
22
+ };
23
+ /**
24
+ * 单个仓位未实现盈亏
25
+ * @param qty 数量
26
+ * @param price 价格
27
+ */
28
+ declare function unrealizedPnL(inputs: UnrealPnLInputs): number;
29
+ type UnrealPnLROIInputs = {
30
+ positionQty: number;
31
+ openPrice: number;
32
+ IMR: number;
33
+ unrealizedPnL: number;
34
+ };
35
+ declare function unrealizedPnLROI(inputs: UnrealPnLROIInputs): number;
36
+ /**
37
+ * 所有仓位未实现盈亏
38
+ * @param inputs
39
+ */
40
+ declare function totalUnrealizedPnL(positions: API.Position[]): number;
41
+ type LiqPriceInputs = {
42
+ markPrice: number;
43
+ totalCollateral: number;
44
+ positionQty: number;
45
+ MMR: number;
46
+ };
47
+ /**
48
+ * 单个仓位强平价格
49
+ *
50
+ * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Position-Liq.-Price}
51
+ */
52
+ declare function liqPrice(inputs: LiqPriceInputs): number;
53
+ type MMInputs = {
54
+ positionQty: number;
55
+ markPrice: number;
56
+ MMR: number;
57
+ };
58
+ declare function maintenanceMargin(inputs: MMInputs): number;
59
+ type UnsettlementPnLInputs = {
60
+ positionQty: number;
61
+ markPrice: number;
62
+ costPosition: number;
63
+ sumUnitaryFunding: number;
64
+ lastSumUnitaryFunding: number;
65
+ };
66
+ /**
67
+ * 计算每个仓位未结算 PnL
68
+ * @link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Unsettlement-PNL-%5BinlineExtension%5D
69
+ */
70
+ declare function unsettlementPnL(inputs: UnsettlementPnLInputs): number;
71
+ type TotalUnsettlementPnLInputs = {
72
+ positions: (API.Position & {
73
+ sum_unitary_funding: number;
74
+ })[];
75
+ sumUnitaryFunding: number;
76
+ };
77
+ /**
78
+ * 计算所有仓位未结算 PnL
79
+ * @param inputs
80
+ * @returns
81
+ * @link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Unsettlement-PNL-%5BinlineExtension%5D
82
+ */
83
+ declare function totalUnsettlementPnL(positions: (API.Position & {
84
+ sum_unitary_funding: number;
85
+ })[]): number;
86
+ /**
87
+ * 计算仓位强平价格
88
+ * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Position-Liq.-Price}
89
+ */
90
+ declare function MMR(inputs: {
91
+ baseMMR: number;
92
+ baseIMR: number;
93
+ IMRFactor: number;
94
+ positionNotional: number;
95
+ IMR_factor_power: number;
96
+ }): number;
97
+
98
+ type positions_LiqPriceInputs = LiqPriceInputs;
99
+ type positions_MMInputs = MMInputs;
100
+ declare const positions_MMR: typeof MMR;
101
+ type positions_TotalUnsettlementPnLInputs = TotalUnsettlementPnLInputs;
102
+ type positions_UnrealPnLInputs = UnrealPnLInputs;
103
+ type positions_UnrealPnLROIInputs = UnrealPnLROIInputs;
104
+ type positions_UnsettlementPnLInputs = UnsettlementPnLInputs;
105
+ declare const positions_liqPrice: typeof liqPrice;
106
+ declare const positions_maintenanceMargin: typeof maintenanceMargin;
107
+ declare const positions_notional: typeof notional;
108
+ declare const positions_totalNotional: typeof totalNotional;
109
+ declare const positions_totalUnrealizedPnL: typeof totalUnrealizedPnL;
110
+ declare const positions_totalUnsettlementPnL: typeof totalUnsettlementPnL;
111
+ declare const positions_unrealizedPnL: typeof unrealizedPnL;
112
+ declare const positions_unrealizedPnLROI: typeof unrealizedPnLROI;
113
+ declare const positions_unsettlementPnL: typeof unsettlementPnL;
114
+ declare namespace positions {
115
+ export {
116
+ positions_LiqPriceInputs as LiqPriceInputs,
117
+ positions_MMInputs as MMInputs,
118
+ positions_MMR as MMR,
119
+ positions_TotalUnsettlementPnLInputs as TotalUnsettlementPnLInputs,
120
+ positions_UnrealPnLInputs as UnrealPnLInputs,
121
+ positions_UnrealPnLROIInputs as UnrealPnLROIInputs,
122
+ positions_UnsettlementPnLInputs as UnsettlementPnLInputs,
123
+ positions_liqPrice as liqPrice,
124
+ positions_maintenanceMargin as maintenanceMargin,
125
+ positions_notional as notional,
126
+ positions_totalNotional as totalNotional,
127
+ positions_totalUnrealizedPnL as totalUnrealizedPnL,
128
+ positions_totalUnsettlementPnL as totalUnsettlementPnL,
129
+ positions_unrealizedPnL as unrealizedPnL,
130
+ positions_unrealizedPnLROI as unrealizedPnLROI,
131
+ positions_unsettlementPnL as unsettlementPnL,
132
+ };
133
+ }
134
+
135
+ type ResultOptions = {
136
+ dp: number;
137
+ };
138
+ type TotalValueInputs = {
139
+ totalUnsettlementPnL: number;
140
+ USDCHolding: number;
141
+ nonUSDCHolding: {
142
+ holding: number;
143
+ markPrice: number;
144
+ discount: number;
145
+ }[];
146
+ };
147
+ /**
148
+ * 用戶總資產價值 (USDC計價),包含無法作為保證金的資產
149
+ * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Value}
150
+ */
151
+ declare function totalValue(inputs: TotalValueInputs): Decimal;
152
+ /**
153
+ * 用戶帳戶當前可用保證金的價值總和 (USDC計價)
154
+ *
155
+ * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Free-collateral}
156
+ */
157
+ type FreeCollateralInputs = {
158
+ totalCollateral: Decimal;
159
+ totalInitialMarginWithOrders: number;
160
+ };
161
+ /**
162
+ * 计算可用保证金
163
+ */
164
+ declare function freeCollateral(inputs: FreeCollateralInputs): Decimal;
165
+ type TotalCollateralValueInputs = {
166
+ USDCHolding: number;
167
+ nonUSDCHolding: {
168
+ holding: number;
169
+ markPrice: number;
170
+ discount: number;
171
+ }[];
172
+ unsettlementPnL: number;
173
+ };
174
+ /**
175
+ * 计算总保证金
176
+ * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-collateral-%5BinlineExtension%5D}
177
+ */
178
+ declare function totalCollateral(inputs: TotalCollateralValueInputs): Decimal;
179
+ declare function initialMarginWithOrder(): void;
180
+ type PositionNotionalWithOrderInputs = {
181
+ markPrice: number;
182
+ positionQtyWithOrders: number;
183
+ };
184
+ /**
185
+ * 單一 Symbol position / orders notional 加總
186
+ */
187
+ declare function positionNotionalWithOrder_by_symbol(inputs: PositionNotionalWithOrderInputs): Decimal;
188
+ type PositionQtyWithOrderInputs = {
189
+ positionQty: number;
190
+ buyOrdersQty: number;
191
+ sellOrdersQty: number;
192
+ };
193
+ /**
194
+ * 單一 Symbol position / orders qty 加總
195
+ */
196
+ declare function positionQtyWithOrders_by_symbol(inputs: PositionQtyWithOrderInputs): number;
197
+ type IMRInputs = {
198
+ maxLeverage: number;
199
+ baseIMR: number;
200
+ IMR_Factor: number;
201
+ positionNotional: number;
202
+ ordersNotional: number;
203
+ IMR_factor_power?: number;
204
+ };
205
+ /**
206
+ * 單一 Symbol 初始保證金率
207
+ * Max(1 / Max Account Leverage, Base IMR i, IMR Factor i * Abs(Position Notional i + Order Notional i)^(4/5))
208
+ */
209
+ declare function IMR(inputs: IMRInputs): number;
210
+ declare function buyOrdersFilter_by_symbol(orders: API.Order[], symbol: string): API.Order[];
211
+ declare function sellOrdersFilter_by_symbol(orders: API.Order[], symbol: string): API.Order[];
212
+ /**
213
+ * 从仓位列表中获取指定symbol的仓位数量
214
+ */
215
+ declare function getQtyFromPositions(positions: API.Position[], symbol: string): number;
216
+ /**
217
+ * 从订单列表中获取指定symbol的看多,看空订单数量,
218
+ */
219
+ declare function getQtyFromOrdersBySide(orders: API.Order[], symbol: string, side: OrderSide): number;
220
+ declare function getPositonsAndOrdersNotionalBySymbol(inputs: {
221
+ positions: API.Position[];
222
+ orders: API.Order[];
223
+ symbol: string;
224
+ markPrice: number;
225
+ }): number;
226
+ type TotalInitialMarginWithOrdersInputs = {
227
+ positions: API.Position[];
228
+ orders: API.Order[];
229
+ markPrices: {
230
+ [key: string]: number;
231
+ };
232
+ symbolInfo: any;
233
+ IMR_Factors: {
234
+ [key: string]: number;
235
+ };
236
+ } & Pick<IMRInputs, "maxLeverage">;
237
+ /**
238
+ * 计算用戶已使用初始保證金加總 ( 包含 position / orders )
239
+ */
240
+ declare function totalInitialMarginWithOrders(inputs: TotalInitialMarginWithOrdersInputs): number;
241
+ /**
242
+ * 把订单按照symbol分组, 因为一个symbol可以有多个挂单
243
+ */
244
+ declare function groupOrdersBySymbol(orders: API.Order[]): {
245
+ [key: string]: API.Order[];
246
+ };
247
+ /**
248
+ * Extracts all unique symbols from positions and orders.
249
+ * @param positions - An array of position objects.
250
+ * @param orders - An array of order objects.
251
+ * @returns An array of unique symbols.
252
+ */
253
+ declare function extractSymbols(positions: Pick<API.Position, "symbol">[], orders: Pick<API.Order, "symbol">[]): string[];
254
+ type OtherIMsInputs = {
255
+ positions: API.Position[];
256
+ orders: API.Order[];
257
+ markPrices: {
258
+ [key: string]: number;
259
+ };
260
+ maxLeverage: number;
261
+ symbolInfo: any;
262
+ IMR_Factors: {
263
+ [key: string]: number;
264
+ };
265
+ };
266
+ /**
267
+ * 除当前symbol外的其他symbol已占用的总保证金
268
+ */
269
+ declare function otherIMs(inputs: OtherIMsInputs): number;
270
+ type MaxQtyInputs = {
271
+ symbol: string;
272
+ baseMaxQty: number;
273
+ /**
274
+ * 用户保证金总额(USDC 计价), 可以由 totalCollateral 计算得出
275
+ * @see totalCollateral
276
+ */
277
+ totalCollateral: number;
278
+ maxLeverage: number;
279
+ baseIMR: number;
280
+ /**
281
+ * @see otherIMs
282
+ */
283
+ otherIMs: number;
284
+ markPrice: number;
285
+ positionQty: number;
286
+ buyOrdersQty: number;
287
+ sellOrdersQty: number;
288
+ IMR_Factor: number;
289
+ takerFeeRate: number;
290
+ };
291
+ /**
292
+ * 最大可下单数量
293
+ * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Max-Order-QTY}
294
+ */
295
+ declare function maxQty(side: OrderSide, inputs: MaxQtyInputs, options?: ResultOptions): number;
296
+ declare function maxQtyByLong(inputs: Omit<MaxQtyInputs, "side">): number;
297
+ declare function maxQtyByShort(inputs: Omit<MaxQtyInputs, "side">): number;
298
+ type TotalMarginRatioInputs = {
299
+ totalCollateral: number;
300
+ markPrices: {
301
+ [key: string]: number;
302
+ };
303
+ positions: API.Position[];
304
+ };
305
+ /**
306
+ * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Margin-Ratio}
307
+ */
308
+ declare function totalMarginRatio(inputs: TotalMarginRatioInputs, dp?: number): number;
309
+ type TotalUnrealizedROIInputs = {
310
+ totalUnrealizedPnL: number;
311
+ totalValue: number;
312
+ };
313
+ /**
314
+ * totalUnrealizedROI
315
+ * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Total-Unrealized-ROI}
316
+ */
317
+ declare function totalUnrealizedROI(inputs: TotalUnrealizedROIInputs): number;
318
+ /**
319
+ * @see {@link https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Current-account-leverage}
320
+ */
321
+ declare function currentLeverage(totalMarginRatio: number): number;
322
+ type AvailableBalanceInputs = {
323
+ USDCHolding: number;
324
+ unsettlementPnL: number;
325
+ };
326
+ declare function availableBalance(inputs: AvailableBalanceInputs): number;
327
+
328
+ type account_AvailableBalanceInputs = AvailableBalanceInputs;
329
+ type account_FreeCollateralInputs = FreeCollateralInputs;
330
+ declare const account_IMR: typeof IMR;
331
+ type account_IMRInputs = IMRInputs;
332
+ type account_MaxQtyInputs = MaxQtyInputs;
333
+ type account_OtherIMsInputs = OtherIMsInputs;
334
+ type account_PositionNotionalWithOrderInputs = PositionNotionalWithOrderInputs;
335
+ type account_PositionQtyWithOrderInputs = PositionQtyWithOrderInputs;
336
+ type account_ResultOptions = ResultOptions;
337
+ type account_TotalCollateralValueInputs = TotalCollateralValueInputs;
338
+ type account_TotalInitialMarginWithOrdersInputs = TotalInitialMarginWithOrdersInputs;
339
+ type account_TotalMarginRatioInputs = TotalMarginRatioInputs;
340
+ type account_TotalUnrealizedROIInputs = TotalUnrealizedROIInputs;
341
+ type account_TotalValueInputs = TotalValueInputs;
342
+ declare const account_availableBalance: typeof availableBalance;
343
+ declare const account_buyOrdersFilter_by_symbol: typeof buyOrdersFilter_by_symbol;
344
+ declare const account_currentLeverage: typeof currentLeverage;
345
+ declare const account_extractSymbols: typeof extractSymbols;
346
+ declare const account_freeCollateral: typeof freeCollateral;
347
+ declare const account_getPositonsAndOrdersNotionalBySymbol: typeof getPositonsAndOrdersNotionalBySymbol;
348
+ declare const account_getQtyFromOrdersBySide: typeof getQtyFromOrdersBySide;
349
+ declare const account_getQtyFromPositions: typeof getQtyFromPositions;
350
+ declare const account_groupOrdersBySymbol: typeof groupOrdersBySymbol;
351
+ declare const account_initialMarginWithOrder: typeof initialMarginWithOrder;
352
+ declare const account_maxQty: typeof maxQty;
353
+ declare const account_maxQtyByLong: typeof maxQtyByLong;
354
+ declare const account_maxQtyByShort: typeof maxQtyByShort;
355
+ declare const account_otherIMs: typeof otherIMs;
356
+ declare const account_positionNotionalWithOrder_by_symbol: typeof positionNotionalWithOrder_by_symbol;
357
+ declare const account_positionQtyWithOrders_by_symbol: typeof positionQtyWithOrders_by_symbol;
358
+ declare const account_sellOrdersFilter_by_symbol: typeof sellOrdersFilter_by_symbol;
359
+ declare const account_totalCollateral: typeof totalCollateral;
360
+ declare const account_totalInitialMarginWithOrders: typeof totalInitialMarginWithOrders;
361
+ declare const account_totalMarginRatio: typeof totalMarginRatio;
362
+ declare const account_totalUnrealizedROI: typeof totalUnrealizedROI;
363
+ declare const account_totalValue: typeof totalValue;
364
+ declare namespace account {
365
+ export {
366
+ account_AvailableBalanceInputs as AvailableBalanceInputs,
367
+ account_FreeCollateralInputs as FreeCollateralInputs,
368
+ account_IMR as IMR,
369
+ account_IMRInputs as IMRInputs,
370
+ account_MaxQtyInputs as MaxQtyInputs,
371
+ account_OtherIMsInputs as OtherIMsInputs,
372
+ account_PositionNotionalWithOrderInputs as PositionNotionalWithOrderInputs,
373
+ account_PositionQtyWithOrderInputs as PositionQtyWithOrderInputs,
374
+ account_ResultOptions as ResultOptions,
375
+ account_TotalCollateralValueInputs as TotalCollateralValueInputs,
376
+ account_TotalInitialMarginWithOrdersInputs as TotalInitialMarginWithOrdersInputs,
377
+ account_TotalMarginRatioInputs as TotalMarginRatioInputs,
378
+ account_TotalUnrealizedROIInputs as TotalUnrealizedROIInputs,
379
+ account_TotalValueInputs as TotalValueInputs,
380
+ account_availableBalance as availableBalance,
381
+ account_buyOrdersFilter_by_symbol as buyOrdersFilter_by_symbol,
382
+ account_currentLeverage as currentLeverage,
383
+ account_extractSymbols as extractSymbols,
384
+ account_freeCollateral as freeCollateral,
385
+ account_getPositonsAndOrdersNotionalBySymbol as getPositonsAndOrdersNotionalBySymbol,
386
+ account_getQtyFromOrdersBySide as getQtyFromOrdersBySide,
387
+ account_getQtyFromPositions as getQtyFromPositions,
388
+ account_groupOrdersBySymbol as groupOrdersBySymbol,
389
+ account_initialMarginWithOrder as initialMarginWithOrder,
390
+ account_maxQty as maxQty,
391
+ account_maxQtyByLong as maxQtyByLong,
392
+ account_maxQtyByShort as maxQtyByShort,
393
+ account_otherIMs as otherIMs,
394
+ account_positionNotionalWithOrder_by_symbol as positionNotionalWithOrder_by_symbol,
395
+ account_positionQtyWithOrders_by_symbol as positionQtyWithOrders_by_symbol,
396
+ account_sellOrdersFilter_by_symbol as sellOrdersFilter_by_symbol,
397
+ account_totalCollateral as totalCollateral,
398
+ account_totalInitialMarginWithOrders as totalInitialMarginWithOrders,
399
+ account_totalMarginRatio as totalMarginRatio,
400
+ account_totalUnrealizedROI as totalUnrealizedROI,
401
+ account_totalValue as totalValue,
402
+ };
403
+ }
404
+
405
+ /**
406
+ * 下单时的最高价格
407
+ * @see https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Max-price
408
+ */
409
+ declare function maxPrice(markprice: number, range: number): number;
410
+ /**
411
+ * 下单时的最低价格
412
+ * @see https://wootraders.atlassian.net/wiki/spaces/WOOFI/pages/346030144/v2#Min-price
413
+ */
414
+ declare function minPrice(markprice: number, range: number): number;
415
+
416
+ declare const order_maxPrice: typeof maxPrice;
417
+ declare const order_minPrice: typeof minPrice;
418
+ declare namespace order {
419
+ export {
420
+ order_maxPrice as maxPrice,
421
+ order_minPrice as minPrice,
422
+ };
423
+ }
424
+
425
+ export { account, order, order as orderUtils, positions };