@orderly.network/perp 4.10.2 → 4.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +1496 -225
- package/dist/index.d.ts +1496 -225
- package/dist/index.js +638 -255
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +622 -220
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { API, OrderSide } from '@orderly.network/types';
|
|
1
|
+
import { API, OrderSide, OrderType } from '@orderly.network/types';
|
|
2
2
|
import { Decimal } from '@orderly.network/utils';
|
|
3
3
|
|
|
4
4
|
declare global {
|
|
@@ -8,7 +8,7 @@ declare global {
|
|
|
8
8
|
};
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
|
-
declare const _default: "4.
|
|
11
|
+
declare const _default: "4.11.0";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Calculates the notional value of a single position.
|
|
@@ -18,41 +18,213 @@ declare const _default: "4.10.2";
|
|
|
18
18
|
*/
|
|
19
19
|
declare function notional(qty: number, mark_price: number): number;
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
21
|
+
* @formulaId totalNotional
|
|
22
|
+
* @name Total Notional
|
|
23
|
+
* @formula Total Notional = sum ( abs(position_qty_i * mark_price_i) )
|
|
24
|
+
* @description
|
|
25
|
+
* ## Term Definitions
|
|
26
|
+
*
|
|
27
|
+
* - **Total Notional**: Sum of current position notional values
|
|
28
|
+
* - **position_qty_i**: Single symbol position quantity
|
|
29
|
+
* - **mark_price_i**: Single symbol mark price
|
|
30
|
+
*
|
|
31
|
+
* ## Example
|
|
32
|
+
*
|
|
33
|
+
* **Total Notional** = 10112.43
|
|
34
|
+
*
|
|
35
|
+
* **abs(BTC position notional)** = 5197.2
|
|
36
|
+
*
|
|
37
|
+
* **abs(ETH position notional)** = 4915.23
|
|
38
|
+
*
|
|
22
39
|
* @param positions The array of positions.
|
|
23
40
|
* @returns The total notional value of all positions.
|
|
24
41
|
*/
|
|
25
42
|
declare function totalNotional(positions: API.Position[]): number;
|
|
43
|
+
|
|
44
|
+
/** @deprecated Use inline type or the new input type instead */
|
|
26
45
|
type UnrealPnLInputs = {
|
|
27
46
|
markPrice: number;
|
|
28
47
|
openPrice: number;
|
|
29
48
|
qty: number;
|
|
30
49
|
};
|
|
31
50
|
/**
|
|
32
|
-
*
|
|
51
|
+
* @formulaId unrealizedPnL
|
|
52
|
+
* @description Calculates the unrealized profit or loss of a single position.
|
|
53
|
+
* This formula applies to both cross margin and isolated margin positions.
|
|
54
|
+
* @formula Unrealized PnL = position_qty * (mark_price - entry_price)
|
|
33
55
|
* @param inputs The inputs for calculating the unrealized profit or loss.
|
|
34
|
-
* @returns The unrealized profit or loss of the position.
|
|
56
|
+
* @returns The unrealized profit or loss of the position (in USDC).
|
|
35
57
|
*/
|
|
36
|
-
declare function unrealizedPnL(inputs:
|
|
37
|
-
|
|
38
|
-
|
|
58
|
+
declare function unrealizedPnL(inputs: {
|
|
59
|
+
/** symbol mark price */
|
|
60
|
+
markPrice: number;
|
|
61
|
+
/** symbol open price (entry price) */
|
|
39
62
|
openPrice: number;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
};
|
|
63
|
+
/** symbol quantity (position quantity, positive for long, negative for short) */
|
|
64
|
+
qty: number;
|
|
65
|
+
}): number;
|
|
43
66
|
/**
|
|
44
|
-
*
|
|
67
|
+
* @formulaId unrealizedPnLROI
|
|
68
|
+
* @name Position unrealized ROI
|
|
69
|
+
* @formula Position unrealized ROI = Position unrealized PNL / ( IMR_i * abs(position_qty_i * entry_price_i) ) * 100%, IMR_i = Max(1 / Symbol Leverage i, Base IMR i, IMR Factor i * Abs(Position Notional i)^(4/5))
|
|
70
|
+
* @description
|
|
71
|
+
* ## Term Definitions
|
|
72
|
+
*
|
|
73
|
+
* - **Position unrealized ROI**: Single symbol unrealized return on investment
|
|
74
|
+
* - **Position unrealized PNL**: Single symbol unrealized profit and loss
|
|
75
|
+
* - **IMR_i**: Single symbol initial margin rate
|
|
76
|
+
* - **Symbol Leverage_i**: Current leverage for symbol i under current margin mode
|
|
77
|
+
* - **Base IMR_i**: Single symbol base initial margin rate
|
|
78
|
+
* - **IMR Factor_i**: Single symbol IMR calculation factor, from v1/client/info
|
|
79
|
+
* - **Position Notional_i**: Single symbol position notional sum
|
|
80
|
+
* - **position_qty_i**: Single symbol position quantity
|
|
81
|
+
* - **entry_price_i**: Single symbol entry price (avg. open price)
|
|
82
|
+
*
|
|
83
|
+
* ## Example
|
|
84
|
+
*
|
|
85
|
+
* **Position unrealized ROI** = Position unrealized PNL / (IMR_i * abs(position_qty_i * entry_price_i)) * 100% = 216.69 / (0.1 * abs(-3 * 1710.64)) * 100% = 42.22%
|
|
86
|
+
*
|
|
87
|
+
* **ETH IMR_i** = Max(1/10, 0.1, 0.0000003754 * abs(-4915.23)^(4/5)) = Max(0.1, 0.1, 0.000337077174) = 0.1
|
|
88
|
+
*
|
|
89
|
+
* - Symbol Leverage_i = 10
|
|
90
|
+
* - ETH Base IMR_i = 0.1
|
|
91
|
+
* - ETH IMR Factor_i = 0.0000003754
|
|
92
|
+
* - ETH position_qty_i = -3
|
|
93
|
+
* - ETH entry_price_i = 1710.64
|
|
94
|
+
* - ETH mark_price_i = 1638.41
|
|
95
|
+
* - ETH Position Notional = -3 * 1638.41 = -4915.23
|
|
96
|
+
* - ETH Position unrealized PNL = 216.69
|
|
97
|
+
*
|
|
45
98
|
* @param inputs The inputs for calculating the ROI.
|
|
46
99
|
* @returns The ROI of the position's unrealized profit or loss.
|
|
47
100
|
*/
|
|
48
|
-
declare function unrealizedPnLROI(inputs:
|
|
101
|
+
declare function unrealizedPnLROI(inputs: {
|
|
102
|
+
positionQty: number;
|
|
103
|
+
openPrice: number;
|
|
104
|
+
IMR: number;
|
|
105
|
+
unrealizedPnL: number;
|
|
106
|
+
}): number;
|
|
49
107
|
/**
|
|
50
|
-
*
|
|
108
|
+
* @formulaId totalUnrealizedPnl
|
|
109
|
+
* @name Total Unrealized PNL
|
|
110
|
+
* @formula Total Unrealized PNL = sum ( unrealized_pnl_i ), unrealized_pnl_i = position_qty_i * ( mark_price_i - entry_price _i )
|
|
111
|
+
* @description
|
|
112
|
+
* ## Term Definitions
|
|
113
|
+
*
|
|
114
|
+
* - **Total Unrealized PNL**: Sum of all current unrealized profit and loss for user's positions
|
|
115
|
+
* - **unrealized_pnl_i**: Current unrealized profit and loss for a single symbol
|
|
116
|
+
* - **position_qty_i**: Single symbol position quantity
|
|
117
|
+
* - **mark_price_i**: Single symbol mark price
|
|
118
|
+
* - **entry_price_i**: Single symbol entry price (avg. open price)
|
|
119
|
+
*
|
|
120
|
+
* ## Example
|
|
121
|
+
*
|
|
122
|
+
* ```
|
|
123
|
+
* BTC-PERP unrealized_pnl_i = 0.2 * (25986.2 - 26067) = -16.16
|
|
124
|
+
* ETH-PERP unrealized_pnl_i = -3 * (1638.41 - 1710.64) = 216.69
|
|
125
|
+
*
|
|
126
|
+
* Total Unrealized PNL = -16.16 + 216.69 = 200.53
|
|
127
|
+
* ```
|
|
51
128
|
* @param positions The array of positions.
|
|
52
129
|
* @returns The total unrealized profit or loss of all positions.
|
|
53
130
|
*/
|
|
54
131
|
declare function totalUnrealizedPnL(positions: API.Position[]): number;
|
|
55
|
-
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @formulaId liqPrice
|
|
135
|
+
* @name Position Liquidation Price
|
|
136
|
+
* @description
|
|
137
|
+
*
|
|
138
|
+
* ## Define:
|
|
139
|
+
*
|
|
140
|
+
* ### (1) calculate_liq_price function
|
|
141
|
+
*
|
|
142
|
+
* ```
|
|
143
|
+
* calculate_liq_price( mark_price, position_qty, mmr )
|
|
144
|
+
* ```
|
|
145
|
+
*
|
|
146
|
+
* If `position_qty >= 0` AND if `abs(position_qty) * mmr - position_qty >= 0`:
|
|
147
|
+
*
|
|
148
|
+
* Return `mark_price`
|
|
149
|
+
*
|
|
150
|
+
* Else:
|
|
151
|
+
*
|
|
152
|
+
* Return `max( mark_price + [ total_collateral_value - abs(position_qty) * mark_price * mmr - mm_for_other_symbols ] / [ abs(position_qty) * mmr - position_qty ], 0 )`
|
|
153
|
+
*
|
|
154
|
+
* Where `total_collateral_value` and `mm_for_other_symbols` are constants.
|
|
155
|
+
*
|
|
156
|
+
* - **total_collateral_value**
|
|
157
|
+
* - **mm_for_other_symbols** = `sum_i ( abs(position_qty_i) * mark_price_i * mmr_i )` for i != current symbol
|
|
158
|
+
*
|
|
159
|
+
* ### (2) compare_collateral_w_mm function
|
|
160
|
+
*
|
|
161
|
+
* ```
|
|
162
|
+
* compare_collateral_w_mm( price ) = collateral >= mm
|
|
163
|
+
* ```
|
|
164
|
+
*
|
|
165
|
+
* Where:
|
|
166
|
+
* - **collateral** = `total_collateral_value - position_qty_i * mark_price + position_qty_i * price`
|
|
167
|
+
* - **mm** = `abs(position_qty_i) * price * Max(Base MMR i, (Base MMR i / Base IMR i) * IMR Factor i * Abs(position_qty_i * price)^(4/5)) + mm_for_other_symbols`
|
|
168
|
+
*
|
|
169
|
+
* ## Given:
|
|
170
|
+
*
|
|
171
|
+
* Position liquidation price for symbol i with:
|
|
172
|
+
* - current mark price = `mark_price_i`
|
|
173
|
+
* - current position qty = `position_qty_i`
|
|
174
|
+
* - current mmr = `mmr_i = Max(Base MMR i, (Base MMR i / Base IMR i) * IMR Factor i * Abs(Position Notional i)^(4/5))`
|
|
175
|
+
* - symbol base mmr = `base_mmr_i`
|
|
176
|
+
*
|
|
177
|
+
* ## For LONG position
|
|
178
|
+
*
|
|
179
|
+
* ```
|
|
180
|
+
* liq_price_left = calculate_liq_price( mark_price_i, position_qty_i, base_mmr_i )
|
|
181
|
+
* liq_price_right = calculate_liq_price( mark_price_i, position_qty_i, mmr_i )
|
|
182
|
+
*
|
|
183
|
+
* ITERATE 30 times:
|
|
184
|
+
* if liq_price_left >= liq_price_right:
|
|
185
|
+
* return liq_price_right
|
|
186
|
+
*
|
|
187
|
+
* mid = ( liq_price_left + liq_price_right ) / 2
|
|
188
|
+
*
|
|
189
|
+
* if compare_collateral_w_mm( mid ):
|
|
190
|
+
* liq_price_right = mid
|
|
191
|
+
* else:
|
|
192
|
+
* liq_price_left = mid
|
|
193
|
+
*
|
|
194
|
+
* if (liq_price_right - liq_price_left) / (liq_price_left + liq_price_right) * 2 <= 0.0001:
|
|
195
|
+
* break
|
|
196
|
+
*
|
|
197
|
+
* return liq_price_right
|
|
198
|
+
* ```
|
|
199
|
+
*
|
|
200
|
+
* ## For SHORT position
|
|
201
|
+
*
|
|
202
|
+
* ```
|
|
203
|
+
* liq_price_right = calculate_liq_price( mark_price_i, position_qty_i, mmr_i )
|
|
204
|
+
* liq_price_left = calculate_liq_price( mark_price_i, position_qty_i,
|
|
205
|
+
* Max(Base MMR i, (Base MMR i / Base IMR i) * IMR Factor i * Abs(position_qty_i * liq_price_right)^(4/5))
|
|
206
|
+
* )
|
|
207
|
+
*
|
|
208
|
+
* ITERATE 30 times:
|
|
209
|
+
* if liq_price_left >= liq_price_right:
|
|
210
|
+
* return liq_price_left
|
|
211
|
+
*
|
|
212
|
+
* mid = ( liq_price_left + liq_price_right ) / 2
|
|
213
|
+
*
|
|
214
|
+
* if compare_collateral_w_mm( mid ):
|
|
215
|
+
* liq_price_left = mid
|
|
216
|
+
* else:
|
|
217
|
+
* liq_price_right = mid
|
|
218
|
+
*
|
|
219
|
+
* if (liq_price_right - liq_price_left) / (liq_price_left + liq_price_right) * 2 <= 0.0001:
|
|
220
|
+
* break
|
|
221
|
+
*
|
|
222
|
+
* return liq_price_left
|
|
223
|
+
* ```
|
|
224
|
+
*
|
|
225
|
+
* @returns The liquidation price of the position.
|
|
226
|
+
*/
|
|
227
|
+
declare function liqPrice(inputs: {
|
|
56
228
|
markPrice: number;
|
|
57
229
|
symbol: string;
|
|
58
230
|
totalCollateral: number;
|
|
@@ -63,58 +235,154 @@ declare const liqPrice: (inputs: {
|
|
|
63
235
|
baseIMR: number;
|
|
64
236
|
IMRFactor: number;
|
|
65
237
|
costPosition: number;
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
positionQty: number;
|
|
69
|
-
markPrice: number;
|
|
70
|
-
MMR: number;
|
|
71
|
-
};
|
|
238
|
+
}): number | null;
|
|
239
|
+
|
|
72
240
|
/**
|
|
73
|
-
*
|
|
241
|
+
* @formulaId maintenanceMargin
|
|
242
|
+
* @name Position maintenance margin
|
|
243
|
+
* @formula Position maintenance margin = abs (position_qty_i * mark_price_i * MMR_i )
|
|
244
|
+
* @description
|
|
245
|
+
* ## Term Definitions
|
|
246
|
+
*
|
|
247
|
+
* - **Position maintenance margin**: Single symbol maintenance margin
|
|
248
|
+
* - **MMR_i**: Single symbol maintenance margin rate
|
|
249
|
+
* - **Base MMR_i**: Single symbol base maintenance margin rate
|
|
250
|
+
* - **Base IMR_i**: Single symbol base initial margin rate
|
|
251
|
+
* - **IMR Factor_i**: Single symbol IMR calculation factor, from v1/client/info
|
|
252
|
+
* - **Position Notional_i**: Single symbol position notional sum
|
|
253
|
+
* - **position_qty_i**: Single symbol position quantity
|
|
254
|
+
* - **mark_price_i**: Single symbol mark price
|
|
255
|
+
*
|
|
256
|
+
* ## MMR Formula
|
|
257
|
+
*
|
|
258
|
+
* MMR_i = Max(Base MMR_i, (Base MMR_i / Base IMR_i) * IMR Factor_i * Abs(Position Notional_i)^(4/5))
|
|
259
|
+
*
|
|
260
|
+
* ## Example
|
|
261
|
+
*
|
|
262
|
+
* **BTC Position maintenance margin** = abs(position_qty_i * mark_price_i * MMR_i) = abs(0.2 * 25986.2 * 0.05) = 259.86
|
|
263
|
+
*
|
|
264
|
+
* **BTC MMR_i** = Max(0.05, (0.05 / 0.1) * 0.0000002512 * 5197.2^(4/5)) = Max(0.05, 0.000117924809) = 0.05
|
|
265
|
+
*
|
|
266
|
+
* - BTC Base MMR_i = 0.05
|
|
267
|
+
* - BTC Base IMR_i = 0.1
|
|
268
|
+
* - BTC IMR Factor_i = 0.0000002512
|
|
269
|
+
* - Abs(BTC Position Notional_i) = 5197.2
|
|
270
|
+
* - position_qty_i = 0.2
|
|
271
|
+
* - mark_price_i = 25986.2
|
|
272
|
+
*
|
|
74
273
|
* @param inputs The inputs for calculating the maintenance margin.
|
|
75
274
|
* @returns The maintenance margin of the position.
|
|
76
275
|
*/
|
|
77
|
-
declare function maintenanceMargin(inputs:
|
|
78
|
-
type UnsettlementPnLInputs = {
|
|
276
|
+
declare function maintenanceMargin(inputs: {
|
|
79
277
|
positionQty: number;
|
|
80
278
|
markPrice: number;
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
279
|
+
MMR: number;
|
|
280
|
+
}): number;
|
|
281
|
+
|
|
282
|
+
/** @deprecated Use inline type or the new input type instead */
|
|
283
|
+
type LiqPriceInputs = {
|
|
284
|
+
markPrice: number;
|
|
285
|
+
totalCollateral: number;
|
|
286
|
+
positionQty: number;
|
|
287
|
+
positions: Pick<API.PositionExt, "position_qty" | "mark_price" | "mmr">[];
|
|
288
|
+
MMR: number;
|
|
84
289
|
};
|
|
85
290
|
/**
|
|
86
|
-
*
|
|
291
|
+
* @formulaId unsettlementPnl
|
|
292
|
+
* @name Position Unrealized PNL
|
|
293
|
+
* @formula Position Unrealized PNL = position_qty_i * (mark_price_i - entry_price_i)
|
|
294
|
+
* @description
|
|
295
|
+
* ## Term Definitions
|
|
296
|
+
*
|
|
297
|
+
* - **Position Unrealized PNL**: Single symbol unrealized profit and loss
|
|
298
|
+
* - **position_qty_i**: Single symbol position quantity
|
|
299
|
+
* - **mark_price_i**: Single symbol mark price
|
|
300
|
+
* - **entry_price_i**: Single symbol entry price (avg. open price)
|
|
301
|
+
*
|
|
302
|
+
* ## Example
|
|
303
|
+
*
|
|
304
|
+
* **ETH Position Unrealized PNL** = position_qty_i * (mark_price_i - entry_price_i) = -3 * (1638.41 - 1710.64) = 216.69
|
|
305
|
+
*
|
|
306
|
+
* - ETH position_qty_i = -3
|
|
307
|
+
* - ETH mark_price_i = 1638.41
|
|
308
|
+
* - ETH entry_price_i = 1710.64
|
|
309
|
+
*
|
|
87
310
|
* @param inputs The inputs for calculating the unrealized profit or loss.
|
|
88
311
|
* @returns The unrealized profit or loss of each position.
|
|
89
312
|
*/
|
|
90
|
-
declare function unsettlementPnL(inputs:
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
})[];
|
|
313
|
+
declare function unsettlementPnL(inputs: {
|
|
314
|
+
positionQty: number;
|
|
315
|
+
markPrice: number;
|
|
316
|
+
costPosition: number;
|
|
95
317
|
sumUnitaryFunding: number;
|
|
96
|
-
|
|
318
|
+
lastSumUnitaryFunding: number;
|
|
319
|
+
}): number;
|
|
97
320
|
/**
|
|
98
|
-
*
|
|
321
|
+
* @formulaId totalUnsettlementPnL
|
|
322
|
+
* @name Total Unsettlement PNL
|
|
323
|
+
* @formula Unsettlement PNL = position_qty_i * mark_price_i - cost_position_i - position_qty_i * (sum_unitary_funding_i - last_sum_unitary_funding_i)
|
|
324
|
+
* @description
|
|
325
|
+
* ## Term Definitions
|
|
326
|
+
*
|
|
327
|
+
* - **total unsettlement PNL**: Sum of user account's unsettled PNL
|
|
328
|
+
* - **mark_price_i**: Single symbol mark price
|
|
329
|
+
* - **position_qty_i**: Single symbol position quantity
|
|
330
|
+
* - **cost_position_i**: Single symbol notional snapshot from last settlement, `/v1/position`
|
|
331
|
+
* - **sum_unitary_funding_i**: Single symbol current cumulative unit funding fee, `/v1/public/funding_rate`
|
|
332
|
+
* - **last_sum_unitary_funding_i**: Single symbol cumulative unit funding fee from last settlement, `/v1/position`
|
|
333
|
+
*
|
|
334
|
+
* ## Example
|
|
335
|
+
*
|
|
336
|
+
* **BTC-PERP Unsettlement PNL** = 0.2 * 25986.2 - 5197.2 - 0.2 * (-1585.92 + 1583.92) = 0.44
|
|
337
|
+
*
|
|
338
|
+
* **ETH-PERP Unsettlement PNL** = -3 * 1638.41 + 4902.45 + 3 * (-52.728 + 50.728) = -18.78
|
|
339
|
+
*
|
|
340
|
+
* **Total Unsettlement PNL** = 0.44 - 18.78 = -18.34
|
|
341
|
+
*
|
|
99
342
|
* @param positions The array of positions.
|
|
100
343
|
* @returns The total unrealized profit or loss of all positions.
|
|
101
344
|
*/
|
|
102
345
|
declare function totalUnsettlementPnL(positions: (API.Position & {
|
|
103
346
|
sum_unitary_funding: number;
|
|
104
347
|
})[]): number;
|
|
105
|
-
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* @formulaId MMR
|
|
351
|
+
* @name Position Maintenance Margin Rate
|
|
352
|
+
* @formula MMR_i = Max(Base MMR_i, (Base MMR_i / Base IMR_i) * IMR Factor_i * Abs(Position Notional_i)^(4/5))
|
|
353
|
+
* @description
|
|
354
|
+
* ## Term Definitions
|
|
355
|
+
*
|
|
356
|
+
* - **MMR_i**: Single symbol maintenance margin rate
|
|
357
|
+
* - **Base MMR_i**: Single symbol base maintenance margin rate
|
|
358
|
+
* - **Base IMR_i**: Single symbol base initial margin rate
|
|
359
|
+
* - **IMR Factor_i**: Single symbol IMR calculation factor, from v1/client/info
|
|
360
|
+
* - **Position Notional_i**: Single symbol position notional sum
|
|
361
|
+
* - **position_qty_i**: Single symbol position quantity
|
|
362
|
+
* - **mark_price_i**: Single symbol mark price
|
|
363
|
+
*
|
|
364
|
+
* ## Example
|
|
365
|
+
*
|
|
366
|
+
* **BTC MMR_i** = Max(0.05, (0.05 / 0.1) * 0.0000002512 * 5197.2^(4/5)) = Max(0.05, 0.000117924809) = 0.05
|
|
367
|
+
*
|
|
368
|
+
* - BTC Base MMR_i = 0.05
|
|
369
|
+
* - BTC Base IMR_i = 0.1
|
|
370
|
+
* - BTC IMR Factor_i = 0.0000002512
|
|
371
|
+
* - Abs(BTC Position Notional_i) = 5197.2
|
|
372
|
+
* - position_qty_i = 0.2
|
|
373
|
+
* - mark_price_i = 25986.2
|
|
374
|
+
*
|
|
375
|
+
* @param inputs The inputs for calculating the MMR.
|
|
376
|
+
* @returns The MMR of the position.
|
|
377
|
+
*/
|
|
378
|
+
declare function MMR$1(inputs: {
|
|
106
379
|
baseMMR: number;
|
|
107
380
|
baseIMR: number;
|
|
108
381
|
IMRFactor: number;
|
|
109
382
|
positionNotional: number;
|
|
110
383
|
IMR_factor_power: number;
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
* Calculates the maintenance margin requirement (MMR) of a position.
|
|
114
|
-
* @param inputs The inputs for calculating the MMR.
|
|
115
|
-
* @returns The MMR of the position.
|
|
116
|
-
*/
|
|
117
|
-
declare function MMR$1(inputs: MMRInputs): number;
|
|
384
|
+
}): number;
|
|
385
|
+
|
|
118
386
|
/**
|
|
119
387
|
* Calculates the profit or loss for take profit.
|
|
120
388
|
* @returns The profit or loss for take profit.
|
|
@@ -155,9 +423,11 @@ declare function estPnLForSL(inputs: {
|
|
|
155
423
|
positionQty: number;
|
|
156
424
|
entryPrice: number;
|
|
157
425
|
}): number;
|
|
426
|
+
|
|
158
427
|
/**
|
|
159
|
-
*
|
|
160
|
-
*
|
|
428
|
+
* @formulaId maxPositionNotional
|
|
429
|
+
* @description calculate the max position notional
|
|
430
|
+
* @formula max_notional = ( (1/ (leverage * imr_factor) ) ^ (1/0.8)
|
|
161
431
|
*/
|
|
162
432
|
declare function maxPositionNotional(inputs: {
|
|
163
433
|
/** symbol leverage */
|
|
@@ -172,35 +442,139 @@ declare function maxPositionLeverage(inputs: {
|
|
|
172
442
|
notional: number;
|
|
173
443
|
}): number;
|
|
174
444
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
445
|
+
/**
|
|
446
|
+
* @formulaId liquidationPriceIsolated
|
|
447
|
+
* @name Liquidation Price for Isolated Margin Position
|
|
448
|
+
* @formula liquidation_price = (isolated_position_margin' - cost_position' - funding_adjustment) / (abs(position_qty') * MMR' - position_qty')
|
|
449
|
+
* funding_adjustment = position_qty' * (sum_unitary_funding - last_sum_unitary_funding)
|
|
450
|
+
* position_qty' = position_qty + order_side * order_qty
|
|
451
|
+
* MMR' = max(base_MMR, (base_MMR / base_IMR) * IMR_factor * abs(position_qty' * reference_price)^(4/5))
|
|
452
|
+
* @description
|
|
453
|
+
*
|
|
454
|
+
* ## Definition
|
|
455
|
+
*
|
|
456
|
+
* **liquidation_price**: Price at which the isolated margin position will be liquidated
|
|
457
|
+
*
|
|
458
|
+
* **isolated_position_margin'**: Isolated position margin after order execution (if applicable)
|
|
459
|
+
*
|
|
460
|
+
* **cost_position'**: Position cost after order execution (if applicable)
|
|
461
|
+
*
|
|
462
|
+
* **funding_adjustment**: Adjustment for funding fees
|
|
463
|
+
*
|
|
464
|
+
* **position_qty'**: Position quantity after order execution
|
|
465
|
+
*
|
|
466
|
+
* **MMR'**: Maintenance margin rate after order execution
|
|
467
|
+
*
|
|
468
|
+
* ## Scenarios
|
|
469
|
+
*
|
|
470
|
+
* ### 1. No Order (order_qty = 0)
|
|
471
|
+
* - `isolated_position_margin' = isolated_position_margin`
|
|
472
|
+
* - `cost_position' = cost_position`
|
|
473
|
+
*
|
|
474
|
+
* ### 2. Open/Add Position (position_qty = 0 or order_side = sign(position_qty))
|
|
475
|
+
* - `isolated_position_margin' = isolated_position_margin + order_qty * reference_price / leverage`
|
|
476
|
+
* - `cost_position' = cost_position + order_side * order_qty * reference_price`
|
|
477
|
+
*
|
|
478
|
+
* ### 3. Close/Reduce Position (order_side ≠ sign(position_qty) and sign(position_qty') = sign(position_qty))
|
|
479
|
+
* - `isolated_position_margin' = isolated_position_margin * position_qty' / position_qty`
|
|
480
|
+
* - `cost_position' = cost_position + order_side * order_qty * reference_price`
|
|
481
|
+
*
|
|
482
|
+
* ### 4. Flip Position (order_side ≠ sign(position_qty) and sign(position_qty') ≠ sign(position_qty))
|
|
483
|
+
* - `isolated_position_margin' = abs(position_qty') * reference_price / leverage`
|
|
484
|
+
* - `cost_position' = position_qty' * reference_price`
|
|
485
|
+
*
|
|
486
|
+
* ## Example
|
|
487
|
+
*
|
|
488
|
+
* ```
|
|
489
|
+
* isolated_position_margin = 2000
|
|
490
|
+
* cost_position = 100000
|
|
491
|
+
* position_qty = 2 (long)
|
|
492
|
+
* sum_unitary_funding = 0.001
|
|
493
|
+
* last_sum_unitary_funding = 0.0008
|
|
494
|
+
* base_MMR = 0.025
|
|
495
|
+
* base_IMR = 0.04
|
|
496
|
+
* IMR_factor = 0.0000001
|
|
497
|
+
* reference_price = 50000
|
|
498
|
+
* liquidation_price = (2000 - 100000 - 0.0004) / (2 * 0.025 - 2) ≈ 50256.41
|
|
499
|
+
* ```
|
|
500
|
+
*
|
|
501
|
+
* @param inputs Input parameters for calculating liquidation price
|
|
502
|
+
* @returns Liquidation price (in USDC) or null if invalid
|
|
503
|
+
*/
|
|
504
|
+
declare function liquidationPriceIsolated(inputs: {
|
|
505
|
+
/**
|
|
506
|
+
* @description Current isolated position margin
|
|
507
|
+
*/
|
|
508
|
+
isolatedPositionMargin: number;
|
|
509
|
+
/**
|
|
510
|
+
* @description Current position cost
|
|
511
|
+
*/
|
|
512
|
+
costPosition: number;
|
|
513
|
+
/**
|
|
514
|
+
* @description Current position quantity (positive for long, negative for short)
|
|
515
|
+
*/
|
|
516
|
+
positionQty: number;
|
|
517
|
+
/**
|
|
518
|
+
* @description Current cumulative unitary funding
|
|
519
|
+
*/
|
|
520
|
+
sumUnitaryFunding: number;
|
|
521
|
+
/**
|
|
522
|
+
* @description Last cumulative unitary funding (at last settlement)
|
|
523
|
+
*/
|
|
524
|
+
lastSumUnitaryFunding: number;
|
|
525
|
+
/**
|
|
526
|
+
* @description Base maintenance margin rate
|
|
527
|
+
*/
|
|
528
|
+
baseMMR: number;
|
|
529
|
+
/**
|
|
530
|
+
* @description Base initial margin rate
|
|
531
|
+
*/
|
|
532
|
+
baseIMR: number;
|
|
533
|
+
/**
|
|
534
|
+
* @description IMR calculation factor
|
|
535
|
+
*/
|
|
536
|
+
IMRFactor: number;
|
|
537
|
+
/**
|
|
538
|
+
* @description Reference price (mark price for current position, or order price for estimated liquidation)
|
|
539
|
+
*/
|
|
540
|
+
referencePrice?: number;
|
|
541
|
+
/**
|
|
542
|
+
* @description Order side (BUY = +1, SELL = -1) for calculating estimated liquidation after order execution
|
|
543
|
+
*/
|
|
544
|
+
orderSide?: "BUY" | "SELL";
|
|
545
|
+
/**
|
|
546
|
+
* @description Order quantity for calculating estimated liquidation after order execution
|
|
547
|
+
*/
|
|
548
|
+
orderQty?: number;
|
|
549
|
+
/**
|
|
550
|
+
* @description Leverage for the position
|
|
551
|
+
*/
|
|
552
|
+
leverage: number;
|
|
553
|
+
}): number | null;
|
|
554
|
+
|
|
555
|
+
type index$1_LiqPriceInputs = LiqPriceInputs;
|
|
556
|
+
type index$1_UnrealPnLInputs = UnrealPnLInputs;
|
|
557
|
+
declare const index$1_estOffsetForTP: typeof estOffsetForTP;
|
|
558
|
+
declare const index$1_estPnLForSL: typeof estPnLForSL;
|
|
559
|
+
declare const index$1_estPnLForTP: typeof estPnLForTP;
|
|
560
|
+
declare const index$1_estPriceForTP: typeof estPriceForTP;
|
|
561
|
+
declare const index$1_estPriceFromOffsetForTP: typeof estPriceFromOffsetForTP;
|
|
562
|
+
declare const index$1_liqPrice: typeof liqPrice;
|
|
563
|
+
declare const index$1_liquidationPriceIsolated: typeof liquidationPriceIsolated;
|
|
564
|
+
declare const index$1_maintenanceMargin: typeof maintenanceMargin;
|
|
565
|
+
declare const index$1_maxPositionLeverage: typeof maxPositionLeverage;
|
|
566
|
+
declare const index$1_maxPositionNotional: typeof maxPositionNotional;
|
|
567
|
+
declare const index$1_notional: typeof notional;
|
|
568
|
+
declare const index$1_totalNotional: typeof totalNotional;
|
|
569
|
+
declare const index$1_totalUnrealizedPnL: typeof totalUnrealizedPnL;
|
|
570
|
+
declare const index$1_totalUnsettlementPnL: typeof totalUnsettlementPnL;
|
|
571
|
+
declare const index$1_unrealizedPnL: typeof unrealizedPnL;
|
|
572
|
+
declare const index$1_unrealizedPnLROI: typeof unrealizedPnLROI;
|
|
573
|
+
declare const index$1_unsettlementPnL: typeof unsettlementPnL;
|
|
574
|
+
declare namespace index$1 {
|
|
575
|
+
export { type index$1_LiqPriceInputs as LiqPriceInputs, MMR$1 as MMR, type index$1_UnrealPnLInputs as UnrealPnLInputs, index$1_estOffsetForTP as estOffsetForTP, index$1_estPnLForSL as estPnLForSL, index$1_estPnLForTP as estPnLForTP, index$1_estPriceForTP as estPriceForTP, index$1_estPriceFromOffsetForTP as estPriceFromOffsetForTP, index$1_liqPrice as liqPrice, index$1_liquidationPriceIsolated as liquidationPriceIsolated, index$1_maintenanceMargin as maintenanceMargin, index$1_maxPositionLeverage as maxPositionLeverage, index$1_maxPositionNotional as maxPositionNotional, index$1_notional as notional, index$1_totalNotional as totalNotional, index$1_totalUnrealizedPnL as totalUnrealizedPnL, index$1_totalUnsettlementPnL as totalUnsettlementPnL, index$1_unrealizedPnL as unrealizedPnL, index$1_unrealizedPnLROI as unrealizedPnLROI, index$1_unsettlementPnL as unsettlementPnL };
|
|
199
576
|
}
|
|
200
577
|
|
|
201
|
-
type ResultOptions = {
|
|
202
|
-
dp: number;
|
|
203
|
-
};
|
|
204
578
|
type TotalValueInputs = {
|
|
205
579
|
totalUnsettlementPnL: number;
|
|
206
580
|
USDCHolding: number;
|
|
@@ -210,55 +584,260 @@ type TotalValueInputs = {
|
|
|
210
584
|
}[];
|
|
211
585
|
};
|
|
212
586
|
/**
|
|
213
|
-
*
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
*
|
|
587
|
+
* @formulaId totalValue
|
|
588
|
+
* @name Total Value
|
|
589
|
+
* @formula Total Value = total_holding + total_isolated_position_margin + total_unsettled_PNL, total_holding = usdc balance.holding + SUM(non-usdc balance.holding * mark price)
|
|
590
|
+
* @description
|
|
591
|
+
*
|
|
592
|
+
* ## Definition
|
|
593
|
+
*
|
|
594
|
+
* **Total Value** = User's total asset value (denominated in USDC), including assets that cannot be used as collateral
|
|
595
|
+
*
|
|
596
|
+
* **Total holding** = Sum of all holding quantities in the user's account
|
|
597
|
+
*
|
|
598
|
+
* **usdc balance.holding** = USDC holding quantity
|
|
599
|
+
*
|
|
600
|
+
* **non-usdc balance.holding * mark price** = Value of non-USDC asset holdings (denominated in USDC)
|
|
601
|
+
*
|
|
602
|
+
* **total_isolated_position_margin** = Sum of all isolated margin position margins
|
|
603
|
+
*
|
|
604
|
+
* **holding**: Asset quantity held by the user, from `/v1/client/holding` or v2 Websocket API | Balance
|
|
605
|
+
*
|
|
606
|
+
* **mark price**: Current price of the asset, from v2 Websocket API | Balance
|
|
607
|
+
*
|
|
608
|
+
* **total unsettlement PNL** = Sum of user's account unsettled PNL (including both cross and isolated margin positions)
|
|
609
|
+
*
|
|
610
|
+
* ## Example
|
|
611
|
+
*
|
|
612
|
+
* ```
|
|
613
|
+
* total_holding = 2000 + 1000 * 1.001 = 3001
|
|
614
|
+
* total_isolated_position_margin = 500
|
|
615
|
+
* total_unsettled_PNL = -18.34
|
|
616
|
+
* Total Value = 3001 + 500 - 18.34 = 3482.66
|
|
617
|
+
* ```
|
|
218
618
|
*/
|
|
619
|
+
declare function totalValue(inputs: {
|
|
620
|
+
/**
|
|
621
|
+
* @description Total unsettled PNL of user account (including both cross and isolated margin positions)
|
|
622
|
+
*/
|
|
623
|
+
totalUnsettlementPnL: number;
|
|
624
|
+
/**
|
|
625
|
+
* @description USDC holding quantity
|
|
626
|
+
*/
|
|
627
|
+
USDCHolding: number;
|
|
628
|
+
/**
|
|
629
|
+
* @description Non-USDC holdings with their index prices
|
|
630
|
+
*/
|
|
631
|
+
nonUSDCHolding: {
|
|
632
|
+
holding: number;
|
|
633
|
+
indexPrice: number;
|
|
634
|
+
}[];
|
|
635
|
+
/**
|
|
636
|
+
* @description Total isolated position margin (sum of all isolated margin positions). Pass 0 if no isolated margin positions exist.
|
|
637
|
+
* @default 0
|
|
638
|
+
*/
|
|
639
|
+
totalIsolatedPositionMargin?: number;
|
|
640
|
+
}): Decimal;
|
|
641
|
+
|
|
219
642
|
type FreeCollateralInputs = {
|
|
220
643
|
totalCollateral: Decimal;
|
|
221
644
|
totalInitialMarginWithOrders: number;
|
|
222
645
|
};
|
|
223
646
|
/**
|
|
224
|
-
*
|
|
647
|
+
* @formulaId freeCollateral
|
|
648
|
+
* @name Free Collateral
|
|
649
|
+
* @formula Free Collateral = Total_collateral_value - total_initial_margin_with_orders
|
|
650
|
+
* Total_collateral_value = (usdc balance.holding + usdc balance.pending_short_qty - usdc balance.isolated_order_frozen) + SUM(non-usdc balance.holding * mark price * discount) + total_cross_unsettled_PNL
|
|
651
|
+
* total_initial_margin_with_orders = sum ( cross_position_notional_with_orders_i * cross_IMR_i (with_orders))
|
|
652
|
+
* IMR_i (with_orders) = Max(1 / Symbol Leverage i, Base IMR i, IMR Factor i * Abs(Position Notional i + Order Notional i)^(4/5))
|
|
653
|
+
* position_notional_with_orders_i = abs( mark_price_i * position_qty_with_orders_i)
|
|
654
|
+
* position_qty_with_orders_i = max[ abs(position_qty_i + sum_position_qty_buy_orders_i), abs(position_qty_i - sum_position_qty_sell_orders_i)]
|
|
655
|
+
* @description
|
|
656
|
+
*
|
|
657
|
+
* ## Definition
|
|
658
|
+
*
|
|
659
|
+
* **Free collateral**: Total value of available margin in the user's account (denominated in USDC). For isolated margin mode, this only considers cross margin positions and orders.
|
|
660
|
+
*
|
|
661
|
+
* **Total_collateral_value**: Total value of collateral assets in the user's account (denominated in USDC). For isolated margin mode, includes cross margin unsettled PNL but excludes isolated order frozen amounts.
|
|
662
|
+
* Use `totalCollateral` function with optional parameters (`usdcBalancePendingShortQty`, `usdcBalanceIsolatedOrderFrozen`, `totalCrossUnsettledPnL`) to calculate this value.
|
|
663
|
+
*
|
|
664
|
+
* **total_initial_margin_with_orders**: Total initial margin used by cross margin positions and orders (isolated margin positions are excluded).
|
|
665
|
+
* Use `totalInitialMarginWithQty` function with `orders` parameter to calculate this value. The function automatically filters to cross margin only.
|
|
666
|
+
*
|
|
667
|
+
* **usdc balance.pending_short_qty**: USDC balance frozen for pending short orders
|
|
668
|
+
*
|
|
669
|
+
* **usdc balance.isolated_order_frozen**: USDC balance frozen for isolated margin orders
|
|
670
|
+
*
|
|
671
|
+
* **total_cross_unsettled_PNL**: Total unsettled PNL from cross margin positions only
|
|
672
|
+
*
|
|
673
|
+
* **initial_margin_i with order**: Initial margin for symbol i (considering both positions and orders)
|
|
674
|
+
*
|
|
675
|
+
* **IMR_i (with_orders)**: Initial margin rate for a single symbol (considering both position and order notional)
|
|
676
|
+
*
|
|
677
|
+
* **Symbol Leverage i**: Leverage for symbol i under current margin mode (cross/isolated). Use `position.leverage` when position exists; otherwise resolve by symbol + mode leverage source.
|
|
678
|
+
*
|
|
679
|
+
* **Base IMR i**: Base initial margin rate for a single symbol, from `/v1/public/info`
|
|
680
|
+
*
|
|
681
|
+
* **IMR Factor i**: IMR calculation factor for a single symbol, from `v1/client/info`
|
|
682
|
+
*
|
|
683
|
+
* **Position Notional i**: Sum of position notional for a single symbol
|
|
684
|
+
*
|
|
685
|
+
* **Order Notional i**: Sum of order notional for a single symbol
|
|
686
|
+
*
|
|
687
|
+
* **position_notional_with_orders_i**: Sum of position and order notional for a single symbol
|
|
688
|
+
*
|
|
689
|
+
* **mark_price_i**: Mark price for a single symbol
|
|
690
|
+
*
|
|
691
|
+
* **position_qty_with_orders_i**: Sum of position and order quantity for a single symbol
|
|
692
|
+
*
|
|
693
|
+
* **position_qty_i**: Position quantity for a single symbol
|
|
694
|
+
*
|
|
695
|
+
* **sum_position_qty_buy_orders_i**: Sum of long order quantity for a single symbol [algo orders should be ignored]
|
|
696
|
+
*
|
|
697
|
+
* **sum_position_qty_sell_orders_i**: Sum of short order quantity for a single symbol [algo orders should be ignored]
|
|
698
|
+
*
|
|
699
|
+
* ## Example
|
|
700
|
+
*
|
|
701
|
+
* ```
|
|
702
|
+
* Total_collateral_value = (2000 + 100 - 200) + 1 * 2000 * 0.9 + 50 = 2050
|
|
703
|
+
* total_initial_margin_with_orders = 1500
|
|
704
|
+
* Free Collateral = 2050 - 1500 = 550
|
|
705
|
+
* ```
|
|
225
706
|
*/
|
|
226
|
-
declare function freeCollateral(inputs:
|
|
227
|
-
|
|
228
|
-
|
|
707
|
+
declare function freeCollateral(inputs: {
|
|
708
|
+
/**
|
|
709
|
+
* @description Total collateral value
|
|
710
|
+
*/
|
|
711
|
+
totalCollateral: Decimal;
|
|
712
|
+
/**
|
|
713
|
+
* @description Total initial margin with orders (for cross margin positions only)
|
|
714
|
+
*/
|
|
715
|
+
totalInitialMarginWithOrders: number;
|
|
716
|
+
}): Decimal;
|
|
717
|
+
|
|
718
|
+
type FreeCollateralUSDCOnlyInputs = {
|
|
719
|
+
/**
|
|
720
|
+
* Free collateral (total_collateral_value - total_initial_margin_with_orders).
|
|
721
|
+
* @see freeCollateral
|
|
722
|
+
*/
|
|
723
|
+
freeCollateral: Decimal;
|
|
724
|
+
/**
|
|
725
|
+
* Non-USDC token holdings; same structure as in totalCollateral.
|
|
726
|
+
* Each item contributes (capped_holding × index_price × discount) to the sum to subtract.
|
|
727
|
+
*/
|
|
229
728
|
nonUSDCHolding: {
|
|
230
729
|
holding: number;
|
|
231
730
|
indexPrice: number;
|
|
232
731
|
collateralCap: number;
|
|
233
732
|
collateralRatio: Decimal;
|
|
234
733
|
}[];
|
|
235
|
-
unsettlementPnL: number;
|
|
236
|
-
};
|
|
237
|
-
/**
|
|
238
|
-
* Calculate total collateral.
|
|
239
|
-
*/
|
|
240
|
-
declare function totalCollateral(inputs: TotalCollateralValueInputs): Decimal;
|
|
241
|
-
declare function initialMarginWithOrder(): void;
|
|
242
|
-
type PositionNotionalWithOrderInputs = {
|
|
243
|
-
markPrice: number;
|
|
244
|
-
positionQtyWithOrders: number;
|
|
245
734
|
};
|
|
246
735
|
/**
|
|
247
|
-
*
|
|
736
|
+
* @formulaId freeCollateralUSDCOnly
|
|
737
|
+
* @name Free Collateral (USDC Only)
|
|
738
|
+
* @formula Free Collateral USDC Only = max(0, free_collateral - SUM(non_usdc_token.holding × mark_price × discount))
|
|
739
|
+
* @description
|
|
740
|
+
*
|
|
741
|
+
* ## Definition
|
|
742
|
+
*
|
|
743
|
+
* **Free Collateral (USDC Only)**: Part of free collateral that is backed only by USDC (and unsettled PNL), i.e. excluding the value of non-USDC collateral.
|
|
744
|
+
*
|
|
745
|
+
* **free_collateral**: From freeCollateral (total_collateral_value - total_initial_margin_with_orders).
|
|
746
|
+
*
|
|
747
|
+
* **non_usdc_token.holding × mark_price × discount**: Same as in totalCollateral — value of each non-USDC asset (capped by collateralCap), using indexPrice as mark price and collateralRatio as discount.
|
|
748
|
+
*
|
|
749
|
+
* ## Example
|
|
750
|
+
*
|
|
751
|
+
* ```
|
|
752
|
+
* free_collateral = 550
|
|
753
|
+
* SUM(non_usdc.holding × mark_price × discount) = 1 * 2000 * 0.9 = 1800
|
|
754
|
+
* Free Collateral USDC Only = max(0, 550 - 1800) = 0
|
|
755
|
+
* ```
|
|
248
756
|
*/
|
|
249
|
-
declare function
|
|
250
|
-
|
|
251
|
-
positionQty: number;
|
|
252
|
-
buyOrdersQty: number;
|
|
253
|
-
sellOrdersQty: number;
|
|
254
|
-
};
|
|
757
|
+
declare function freeCollateralUSDCOnly(inputs: FreeCollateralUSDCOnlyInputs): Decimal;
|
|
758
|
+
|
|
255
759
|
/**
|
|
256
|
-
*
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
760
|
+
* @formulaId totalCollateral
|
|
761
|
+
* @name Total Collateral
|
|
762
|
+
* @formula Total collateral = usdc balance.holding + SUM(non-usdc balance.holding * mark price * discount) + total unsettlement PNL
|
|
763
|
+
* @description
|
|
764
|
+
*
|
|
765
|
+
* ## Definition
|
|
766
|
+
*
|
|
767
|
+
* **discount**: Collateral substitution rate
|
|
768
|
+
*
|
|
769
|
+
* **Total collateral**: Total value of collateral assets in the user's account (denominated in USDC)
|
|
770
|
+
*
|
|
771
|
+
* **usdc balance.holding**: USDC holding quantity
|
|
772
|
+
*
|
|
773
|
+
* **non-usdc balance.holding * mark price**: Value of non-USDC asset holdings (denominated in USDC)
|
|
774
|
+
*
|
|
775
|
+
* **holding**: Asset quantity held by the user, from `/v1/client/holding` or v2 Websocket API | Balance
|
|
776
|
+
*
|
|
777
|
+
* **mark price**: Current price of the asset, from v2 Websocket API | Balance
|
|
778
|
+
*
|
|
779
|
+
* **total unsettlement PNL**: Sum of user's account unsettled PNL
|
|
780
|
+
*
|
|
781
|
+
* ## Example
|
|
782
|
+
*
|
|
783
|
+
* ```
|
|
784
|
+
* Total collateral = 2000 + 1000 * 1.001 * 0 - 18.34 = 1981.66
|
|
785
|
+
* total unsettlement PNL = -18.34
|
|
786
|
+
* ```
|
|
787
|
+
*/
|
|
788
|
+
declare function totalCollateral(inputs: {
|
|
789
|
+
USDCHolding: number;
|
|
790
|
+
nonUSDCHolding: {
|
|
791
|
+
holding: number;
|
|
792
|
+
indexPrice: number;
|
|
793
|
+
collateralCap: number;
|
|
794
|
+
collateralRatio: Decimal;
|
|
795
|
+
}[];
|
|
796
|
+
/**
|
|
797
|
+
* Sum of user's account unsettled PNL
|
|
798
|
+
*/
|
|
799
|
+
unsettlementPnL: number;
|
|
800
|
+
/**
|
|
801
|
+
* @description USDC balance frozen for pending short orders (for freeCollateral calculation)
|
|
802
|
+
* @default 0
|
|
803
|
+
*/
|
|
804
|
+
usdcBalancePendingShortQty?: number;
|
|
805
|
+
/**
|
|
806
|
+
* @description USDC balance frozen for isolated margin orders (for freeCollateral calculation)
|
|
807
|
+
* @default 0
|
|
808
|
+
*/
|
|
809
|
+
usdcBalanceIsolatedOrderFrozen?: number;
|
|
810
|
+
/**
|
|
811
|
+
* @description Total cross margin unsettled PNL (for freeCollateral calculation). If provided, this will be used instead of unsettlementPnL.
|
|
812
|
+
*/
|
|
813
|
+
totalCrossUnsettledPnL?: number;
|
|
814
|
+
}): Decimal;
|
|
815
|
+
|
|
816
|
+
/**
|
|
817
|
+
* Sum of notional value for a symbol's position and orders.
|
|
818
|
+
*/
|
|
819
|
+
declare function positionNotionalWithOrder_by_symbol(inputs: {
|
|
820
|
+
markPrice: number;
|
|
821
|
+
positionQtyWithOrders: number;
|
|
822
|
+
}): Decimal;
|
|
823
|
+
/**
|
|
824
|
+
* Sum of position quantity and orders quantity for a symbol.
|
|
825
|
+
*/
|
|
826
|
+
declare function positionQtyWithOrders_by_symbol(inputs: {
|
|
827
|
+
positionQty: number;
|
|
828
|
+
buyOrdersQty: number;
|
|
829
|
+
sellOrdersQty: number;
|
|
830
|
+
}): number;
|
|
831
|
+
|
|
832
|
+
/**
|
|
833
|
+
* @formulaId imr
|
|
834
|
+
* @description
|
|
835
|
+
* Initial margin rate for a symbol.
|
|
836
|
+
* Max(1 / Symbol Leverage i, Base IMR i, IMR Factor i * Abs(Position Notional i + Order Notional i)^(4/5))
|
|
837
|
+
*/
|
|
838
|
+
declare function IMR(inputs: {
|
|
839
|
+
/**
|
|
840
|
+
* effective symbol leverage (resolved by symbol + margin mode)
|
|
262
841
|
*/
|
|
263
842
|
maxLeverage: number;
|
|
264
843
|
baseIMR: number;
|
|
@@ -266,14 +845,11 @@ type IMRInputs = {
|
|
|
266
845
|
positionNotional: number;
|
|
267
846
|
ordersNotional: number;
|
|
268
847
|
IMR_factor_power?: number;
|
|
269
|
-
};
|
|
270
|
-
|
|
271
|
-
* Initial margin rate for a symbol.
|
|
272
|
-
* Max(1 / Max Account Leverage, Base IMR i, IMR Factor i * Abs(Position Notional i + Order Notional i)^(4/5))
|
|
273
|
-
*/
|
|
274
|
-
declare function IMR(inputs: IMRInputs): number;
|
|
848
|
+
}): number;
|
|
849
|
+
|
|
275
850
|
declare function buyOrdersFilter_by_symbol(orders: API.Order[], symbol: string): API.Order[];
|
|
276
851
|
declare function sellOrdersFilter_by_symbol(orders: API.Order[], symbol: string): API.Order[];
|
|
852
|
+
|
|
277
853
|
/**
|
|
278
854
|
* Get the quantity of a specified symbol from the list of positions.
|
|
279
855
|
*/
|
|
@@ -288,24 +864,21 @@ declare function getPositonsAndOrdersNotionalBySymbol(inputs: {
|
|
|
288
864
|
symbol: string;
|
|
289
865
|
markPrice: number;
|
|
290
866
|
}): number;
|
|
291
|
-
|
|
292
|
-
positions: API.Position[];
|
|
293
|
-
orders: API.Order[];
|
|
294
|
-
markPrices: {
|
|
295
|
-
[key: string]: number;
|
|
296
|
-
};
|
|
297
|
-
symbolInfo: any;
|
|
298
|
-
IMR_Factors: {
|
|
299
|
-
[key: string]: number;
|
|
300
|
-
};
|
|
301
|
-
} & Pick<IMRInputs, "maxLeverage">;
|
|
867
|
+
|
|
302
868
|
/**
|
|
303
|
-
*
|
|
304
|
-
*
|
|
869
|
+
* Calculate total initial margin with orders for cross margin positions.
|
|
870
|
+
* Formula: total_initial_margin_with_orders = sum(cross_position_notional_with_orders_i * cross_IMR_i)
|
|
871
|
+
*
|
|
872
|
+
* @param positions - All positions (will be filtered to cross margin only)
|
|
873
|
+
* @param orders - All orders (will be filtered to cross margin only)
|
|
874
|
+
* @param markPrices - Mark prices by symbol
|
|
875
|
+
* @param symbolInfo - Symbol info accessor
|
|
876
|
+
* @param IMR_Factors - IMR factors by symbol
|
|
877
|
+
* @param maxLeverageBySymbol - Symbol leverage map (symbol + margin mode)
|
|
305
878
|
*/
|
|
306
|
-
declare function totalInitialMarginWithOrders(inputs: TotalInitialMarginWithOrdersInputs): number;
|
|
307
879
|
declare function totalInitialMarginWithQty(inputs: {
|
|
308
880
|
positions: API.Position[];
|
|
881
|
+
orders: API.Order[];
|
|
309
882
|
markPrices: {
|
|
310
883
|
[key: string]: number;
|
|
311
884
|
};
|
|
@@ -313,11 +886,9 @@ declare function totalInitialMarginWithQty(inputs: {
|
|
|
313
886
|
IMR_Factors: {
|
|
314
887
|
[key: string]: number;
|
|
315
888
|
};
|
|
316
|
-
|
|
317
|
-
* account max leverage
|
|
318
|
-
*/
|
|
319
|
-
maxLeverage: number;
|
|
889
|
+
maxLeverageBySymbol?: Record<string, number>;
|
|
320
890
|
}): number;
|
|
891
|
+
|
|
321
892
|
/**
|
|
322
893
|
* Group orders by symbol, as a symbol can have multiple orders.
|
|
323
894
|
*/
|
|
@@ -331,26 +902,29 @@ declare function groupOrdersBySymbol(orders: API.Order[]): {
|
|
|
331
902
|
* @returns An array of unique symbols.
|
|
332
903
|
*/
|
|
333
904
|
declare function extractSymbols(positions: Pick<API.Position, "symbol">[], orders: Pick<API.Order, "symbol">[]): string[];
|
|
334
|
-
|
|
905
|
+
|
|
906
|
+
/**
|
|
907
|
+
* Total margin used by other symbols (except the current symbol).
|
|
908
|
+
*/
|
|
909
|
+
declare function otherIMs(inputs: {
|
|
335
910
|
positions: API.Position[];
|
|
336
911
|
markPrices: {
|
|
337
912
|
[key: string]: number;
|
|
338
913
|
};
|
|
339
|
-
/**
|
|
340
|
-
* account max leverage
|
|
341
|
-
*/
|
|
342
|
-
maxLeverage: number;
|
|
343
914
|
symbolInfo: any;
|
|
344
915
|
IMR_Factors: {
|
|
345
916
|
[key: string]: number;
|
|
346
917
|
};
|
|
918
|
+
}): number;
|
|
919
|
+
|
|
920
|
+
type ResultOptions = {
|
|
921
|
+
dp: number;
|
|
347
922
|
};
|
|
348
|
-
/**
|
|
349
|
-
* Total margin used by other symbols (except the current symbol).
|
|
350
|
-
*/
|
|
351
|
-
declare function otherIMs(inputs: OtherIMsInputs): number;
|
|
352
923
|
type MaxQtyInputs = {
|
|
353
924
|
symbol: string;
|
|
925
|
+
/**
|
|
926
|
+
* @description Maximum quantity limit for opening a single position, /v1/public/info.base_max
|
|
927
|
+
*/
|
|
354
928
|
baseMaxQty: number;
|
|
355
929
|
/**
|
|
356
930
|
* Total collateral of the user (denominated in USDC), can be calculated from totalCollateral.
|
|
@@ -371,53 +945,396 @@ type MaxQtyInputs = {
|
|
|
371
945
|
takerFeeRate: number;
|
|
372
946
|
};
|
|
373
947
|
/**
|
|
374
|
-
*
|
|
948
|
+
* @formulaId maxQty
|
|
949
|
+
* @name Max Order QTY
|
|
950
|
+
* @description
|
|
951
|
+
* ## Max Long Quantity Formula
|
|
952
|
+
*
|
|
953
|
+
* ```
|
|
954
|
+
* max long qty = MIN (
|
|
955
|
+
* base max,
|
|
956
|
+
* (((Total_collateral_value - Other_IMs) / (Max(1 / Symbol Leverage i, Base IMR i) + 2 * futures_take_fee_rate * 0.0001) / mark_price_i) * 0.995 - position_qty_this_symbol - sum_buy_order_qty_this_symbol),
|
|
957
|
+
* ((((Total_collateral_value - Other_IMs) / IMR Factor i)^(1/1.8)) / mark_price_i - position_qty_this_symbol - sum_buy_order_qty_this_symbol) / (1 + 2 * futures_take_fee_rate * 0.0001) * 0.995
|
|
958
|
+
* )
|
|
959
|
+
* ```
|
|
960
|
+
*
|
|
961
|
+
* ## Max Short Quantity Formula
|
|
962
|
+
*
|
|
963
|
+
* ```
|
|
964
|
+
* max short qty = MIN (
|
|
965
|
+
* base max,
|
|
966
|
+
* (((Total_collateral_value - Other_IMs) / (Max(1 / Symbol Leverage i, Base IMR i) + 2 * futures_take_fee_rate * 0.0001) / mark_price_i) * 0.995 + position_qty_this_symbol - sum_sell_order_qty_this_symbol),
|
|
967
|
+
* ((((Total_collateral_value - Other_IMs) / IMR Factor i)^(1/1.8)) / mark_price_i + position_qty_this_symbol - sum_sell_order_qty_this_symbol) / (1 + 2 * futures_take_fee_rate * 0.0001) * 0.995
|
|
968
|
+
* )
|
|
969
|
+
* ```
|
|
970
|
+
*
|
|
971
|
+
* ## Reduce Only Mode
|
|
972
|
+
*
|
|
973
|
+
* When reduce only is enabled:
|
|
974
|
+
* - If `position_qty_i > 0`: max long qty = 0, max short qty = abs(position_qty_i)
|
|
975
|
+
* - If `position_qty_i < 0`: max long qty = abs(position_qty_i), max short qty = 0
|
|
976
|
+
* - If `position_qty_i = 0`: max long qty = 0, max short qty = 0
|
|
977
|
+
*
|
|
978
|
+
* ## Variable Definitions
|
|
979
|
+
*
|
|
980
|
+
* | Variable | Description | API Reference |
|
|
981
|
+
* |----------|-------------|---------------|
|
|
982
|
+
* | `max long qty` | Maximum long quantity for current symbol | |
|
|
983
|
+
* | `max short qty` | Maximum short quantity for current symbol | |
|
|
984
|
+
* | `base_max` | Maximum quantity limit for opening a single position | `/v1/public/info.base_max` |
|
|
985
|
+
* | `Total_collateral_value` | Total value of collateral assets in user account (USDC denominated) | |
|
|
986
|
+
* | `Other_IMs` | Initial margin occupied by all other symbols excluding current symbol | |
|
|
987
|
+
* | `IMR_i (with_orders)` | Initial margin rate for a single symbol (considering both position/orders notional) | |
|
|
988
|
+
* | `Symbol Leverage i` | Leverage for symbol i under current margin mode | `position.leverage` or symbol+mode leverage source |
|
|
989
|
+
* | `Base IMR i` | Base initial margin rate for a single symbol | `/v1/public/info` |
|
|
990
|
+
* | `IMR Factor i` | IMR calculation factor for a single symbol | `v1/client/info` |
|
|
991
|
+
* | `Position Notional i` | Sum of position notional for a single symbol | |
|
|
992
|
+
* | `Order Notional i` | Sum of order notional for a single symbol | |
|
|
993
|
+
* | `position_notional_with_orders_i` | Sum of position/orders notional for a single symbol | |
|
|
994
|
+
* | `mark_price_i` | Mark price for a single symbol | |
|
|
995
|
+
* | `position_qty_with_orders_i` | Sum of position/orders quantity for a single symbol | |
|
|
996
|
+
* | `position_qty_i` | Position quantity for a single symbol | |
|
|
997
|
+
* | `sum_position_qty_buy_orders_i` | Sum of long order quantity for a single symbol [algo orders ignored] | |
|
|
998
|
+
* | `sum_position_qty_sell_orders_i` | Sum of short order quantity for a single symbol [algo orders ignored] | |
|
|
999
|
+
* | `futures_take_fee_rate` | User's futures taker fee rate | `GET /v1/client/info` |
|
|
1000
|
+
*
|
|
1001
|
+
* ## Calculation Details
|
|
1002
|
+
*
|
|
1003
|
+
* ```
|
|
1004
|
+
* Other_IMs = sum(position_notional_with_orders_i * IMR_i (with_orders)) // excluding current symbol
|
|
1005
|
+
*
|
|
1006
|
+
* IMR_i (with_orders) = Max(1 / Symbol Leverage i, Base IMR i, IMR Factor i * Abs(Position Notional i + Order Notional i)^(4/5))
|
|
1007
|
+
*
|
|
1008
|
+
* position_notional_with_orders_i = abs(mark_price_i * position_qty_with_orders_i)
|
|
1009
|
+
*
|
|
1010
|
+
* position_qty_with_orders_i = max[abs(position_qty_i + sum_position_qty_buy_orders_i), abs(position_qty_i - sum_position_qty_sell_orders_i)]
|
|
1011
|
+
* ```
|
|
1012
|
+
*
|
|
1013
|
+
* ## Example Calculation
|
|
1014
|
+
*
|
|
1015
|
+
* **Given:**
|
|
1016
|
+
* - `futures_take_fee_rate = 8`
|
|
1017
|
+
* - `BTC base max = 20`
|
|
1018
|
+
* - `Total_collateral_value = 1981.66`
|
|
1019
|
+
* - `Other_IMs = ETH Initial Margin = 4915.23 * 0.1 = 491.523`
|
|
1020
|
+
* - `BTC mark_price_i = 25986.2`
|
|
1021
|
+
* - `BTC position_qty_this_symbol = 0.2`
|
|
1022
|
+
* - `sum_buy_order_qty_this_symbol = 0.3`
|
|
1023
|
+
* - `sum_sell_order_qty_this_symbol = -0.5`
|
|
1024
|
+
*
|
|
1025
|
+
* **Max Long Quantity:**
|
|
1026
|
+
* ```
|
|
1027
|
+
* max long qty = MIN(
|
|
1028
|
+
* 20 BTC,
|
|
1029
|
+
* ((1981.66 - 491.523) / (0.1 + 2 * 8 * 0.0001) / 25986.2 * 0.995 - 0.2 - 0.3) = 0.0615815026 BTC,
|
|
1030
|
+
* ((((1981.66 - 491.523) / 0.0000002512)^(1/1.8)) / 25986.2 - 0.2 - 0.3) / (1 + 2 * 8 * 0.0001) * 0.995 = 9.78216039 BTC
|
|
1031
|
+
* ) = 0.0615815026 BTC
|
|
1032
|
+
* ```
|
|
1033
|
+
*
|
|
1034
|
+
* **Max Short Quantity:**
|
|
1035
|
+
* ```
|
|
1036
|
+
* max short qty = MIN(
|
|
1037
|
+
* 20 BTC,
|
|
1038
|
+
* ((1981.66 - 491.523) / (0.1 + 2 * 8 * 0.0001) / 25986.2 * 0.995 + 0.2 - 0.5) = 0.261581503 BTC,
|
|
1039
|
+
* ((((1981.66 - 491.523) / 0.0000002512)^(1/1.8)) / 25986.2 + 0.2 - 0.5) / (1 + 2 * 8 * 0.0001) * 0.995 = 9.98084249726 BTC
|
|
1040
|
+
* ) = 0.261581503 BTC
|
|
1041
|
+
* ```
|
|
1042
|
+
*
|
|
1043
|
+
* ## Additional Examples
|
|
1044
|
+
*
|
|
1045
|
+
* **Base max qty calculation:**
|
|
1046
|
+
* ```
|
|
1047
|
+
* Base max qty = (1981.66 - 491.523) / (0.1 + 2 * 8 * 0.0001) / 25986.2 * 0.995 = 0.561581503 BTC
|
|
1048
|
+
* ```
|
|
1049
|
+
*
|
|
1050
|
+
* **Different position scenarios:**
|
|
1051
|
+
*
|
|
1052
|
+
* 1. **Short position -0.3 BTC:**
|
|
1053
|
+
* - max long qty = 0.561581503 - (-0.3) = 0.861581503
|
|
1054
|
+
* - max short qty = 0.561581503 + (-0.3) = 0.261581503
|
|
1055
|
+
*
|
|
1056
|
+
* 2. **Short position -0.3 BTC + sell orders 0.1:**
|
|
1057
|
+
* - max long qty = 0.561581503 - (-0.3) = 0.861581503
|
|
1058
|
+
* - max short qty = 0.561581503 + (-0.3) - 0.1 = 0.161581503
|
|
1059
|
+
*
|
|
1060
|
+
* 3. **Long position 0.3 BTC + buy orders 0.2 + sell orders 0.1:**
|
|
1061
|
+
* - max long qty = 0.561581503 - 0.3 - 0.2 = 0.061581503
|
|
1062
|
+
* - max short qty = 0.561581503 + 0.3 - 0.1 = 0.761581503
|
|
1063
|
+
*
|
|
1064
|
+
* ## Special Case: Insufficient Collateral
|
|
1065
|
+
*
|
|
1066
|
+
* When `totalCollatValue <= newTotalIM`:
|
|
1067
|
+
*
|
|
1068
|
+
* ```
|
|
1069
|
+
* newOrderSize_iter = ITERATE() return max(0, newOrderSize_iter * 99.5% + others)
|
|
1070
|
+
* ```
|
|
1071
|
+
*
|
|
1072
|
+
* **ITERATE() Algorithm:**
|
|
1073
|
+
* ```
|
|
1074
|
+
* ITERATE() {
|
|
1075
|
+
* iteratorLeverage = min(1 / Symbol Leverage i, Base IMR i)
|
|
1076
|
+
* iteratorStep = 2
|
|
1077
|
+
*
|
|
1078
|
+
* // First iteration (30 times)
|
|
1079
|
+
* for (i = 0; i < 30; i++) {
|
|
1080
|
+
* iteratorLeverage = max(0, iteratorLeverage - iteratorStep)
|
|
1081
|
+
* newOrderSize1 = (adjustedCollateral - othersIM) * iteratorLeverage / markPrice
|
|
1082
|
+
* calculate afterTradeIM
|
|
1083
|
+
* if (adjustedCollateral >= afterTradeIM) break
|
|
1084
|
+
* }
|
|
1085
|
+
*
|
|
1086
|
+
* leftLeverage = iteratorLeverage
|
|
1087
|
+
* rightLeverage = min(symbolLeverage_i, leftLeverage + iteratorStep)
|
|
1088
|
+
*
|
|
1089
|
+
* // Binary search (30 times)
|
|
1090
|
+
* for (i = 0; i < 30; i++) {
|
|
1091
|
+
* midLeverage = (leftLeverage + rightLeverage) / 2
|
|
1092
|
+
* newOrderSize2 = (adjustedCollateral - othersIM) * midLeverage / markPrice
|
|
1093
|
+
* calculate afterTradeIM
|
|
1094
|
+
* precision = (adjustedCollateral - afterTradeIM) / adjustedCollateral
|
|
1095
|
+
*
|
|
1096
|
+
* if (adjustedCollateral > afterTradeIM) {
|
|
1097
|
+
* leftLeverage = midLeverage
|
|
1098
|
+
* if (0 <= precision <= 0.5%) break
|
|
1099
|
+
* } else {
|
|
1100
|
+
* rightLeverage = midLeverage
|
|
1101
|
+
* }
|
|
1102
|
+
* }
|
|
1103
|
+
*
|
|
1104
|
+
* return newOrderSize2
|
|
1105
|
+
* }
|
|
1106
|
+
* ```
|
|
375
1107
|
*/
|
|
376
1108
|
declare function maxQty(side: OrderSide, inputs: MaxQtyInputs, options?: ResultOptions): number;
|
|
377
1109
|
declare function maxQtyByLong(inputs: Omit<MaxQtyInputs, "side">, options?: ResultOptions): number;
|
|
378
1110
|
declare function maxQtyByShort(inputs: Omit<MaxQtyInputs, "side">, options?: ResultOptions): number;
|
|
379
|
-
|
|
1111
|
+
|
|
1112
|
+
/**
|
|
1113
|
+
* @formulaId maxQtyIsolated
|
|
1114
|
+
* @name Maximum Tradeable Quantity for Isolated Margin
|
|
1115
|
+
* @formula max_notional = min((1 / leverage / imr_factor)^(5/4), symbol_max_notional)
|
|
1116
|
+
* @description
|
|
1117
|
+
*
|
|
1118
|
+
* ## Definition
|
|
1119
|
+
*
|
|
1120
|
+
* **maxQtyIsolated**: Maximum tradeable quantity for isolated margin positions
|
|
1121
|
+
*
|
|
1122
|
+
* This function calculates the maximum quantity that can be traded for an isolated margin position,
|
|
1123
|
+
* considering available balance, leverage, position limits, and pending orders.
|
|
1124
|
+
*
|
|
1125
|
+
* ## Business Rules
|
|
1126
|
+
*
|
|
1127
|
+
* ### For BUY Orders:
|
|
1128
|
+
* - If `reduce_only == False` and `position_qty >= 0` (long or no position): Use simplified formula
|
|
1129
|
+
* - If `reduce_only == False` and `position_qty < 0` (short position): Use binary search iteration
|
|
1130
|
+
* - If `reduce_only == True`: Return `MAX(0, -position_qty)` (can only reduce short position)
|
|
1131
|
+
*
|
|
1132
|
+
* ### For SELL Orders:
|
|
1133
|
+
* - If `reduce_only == False` and `position_qty <= 0` (short or no position): Use simplified formula
|
|
1134
|
+
* - If `reduce_only == False` and `position_qty > 0` (long position): Use binary search iteration
|
|
1135
|
+
* - If `reduce_only == True`: Return `MAX(0, position_qty)` (can only reduce long position)
|
|
1136
|
+
*
|
|
1137
|
+
* ### Binary Search Algorithm:
|
|
1138
|
+
* - Used for reverse position scenarios (e.g., buying when holding short position)
|
|
1139
|
+
* - Maximum 30 iterations
|
|
1140
|
+
* - Searches for maximum quantity that satisfies: `iso_order_frozen <= available_balance` and `open_notional <= max_notional`
|
|
1141
|
+
*
|
|
1142
|
+
* ## Example
|
|
1143
|
+
*
|
|
1144
|
+
* ```
|
|
1145
|
+
* order_side = BUY
|
|
1146
|
+
* reduce_only = False
|
|
1147
|
+
* position_qty = 5 (long)
|
|
1148
|
+
* available_balance = 1000 USDC
|
|
1149
|
+
* leverage = 25
|
|
1150
|
+
* mark_price = 100000 USDC
|
|
1151
|
+
* current_order_reference_price = 99900 USDC
|
|
1152
|
+
* max_notional = 10059467.44 USDC
|
|
1153
|
+
* pending_long_notional = 299200 USDC
|
|
1154
|
+
* max_qty = MIN(1000 / 99900 * 25, (10059467.44 - 100000 * 5 - 299200) / 99900) = 0.25 BTC
|
|
1155
|
+
* ```
|
|
1156
|
+
*
|
|
1157
|
+
* @param inputs Input parameters for calculating maximum tradeable quantity
|
|
1158
|
+
* @returns Maximum tradeable quantity
|
|
1159
|
+
*/
|
|
1160
|
+
declare function maxQtyForIsolatedMargin(inputs: {
|
|
1161
|
+
/**
|
|
1162
|
+
* @description Trading symbol
|
|
1163
|
+
*/
|
|
1164
|
+
symbol: string;
|
|
1165
|
+
/**
|
|
1166
|
+
* @description Order side (BUY or SELL)
|
|
1167
|
+
*/
|
|
1168
|
+
orderSide: OrderSide;
|
|
1169
|
+
/**
|
|
1170
|
+
* @description Current order reference price
|
|
1171
|
+
*/
|
|
1172
|
+
currentOrderReferencePrice: number;
|
|
1173
|
+
/**
|
|
1174
|
+
* @description Available balance (USDC)
|
|
1175
|
+
*/
|
|
1176
|
+
availableBalance: number;
|
|
1177
|
+
/**
|
|
1178
|
+
* @description Leverage for the trading pair
|
|
1179
|
+
*/
|
|
1180
|
+
leverage: number;
|
|
1181
|
+
/**
|
|
1182
|
+
* @description Base initial margin rate
|
|
1183
|
+
*/
|
|
1184
|
+
baseIMR: number;
|
|
1185
|
+
/**
|
|
1186
|
+
* @description IMR calculation factor
|
|
1187
|
+
*/
|
|
1188
|
+
IMR_Factor: number;
|
|
1189
|
+
/**
|
|
1190
|
+
* @description Mark price
|
|
1191
|
+
*/
|
|
1192
|
+
markPrice: number;
|
|
1193
|
+
/**
|
|
1194
|
+
* @description Current position quantity (positive for long, negative for short)
|
|
1195
|
+
*/
|
|
1196
|
+
positionQty: number;
|
|
1197
|
+
/**
|
|
1198
|
+
* @description Pending long orders (excluding current order)
|
|
1199
|
+
*/
|
|
1200
|
+
pendingLongOrders: Array<{
|
|
1201
|
+
referencePrice: number;
|
|
1202
|
+
quantity: number;
|
|
1203
|
+
}>;
|
|
1204
|
+
/**
|
|
1205
|
+
* @description Pending sell orders (excluding current order)
|
|
1206
|
+
*/
|
|
1207
|
+
pendingSellOrders: Array<{
|
|
1208
|
+
referencePrice: number;
|
|
1209
|
+
quantity: number;
|
|
1210
|
+
}>;
|
|
1211
|
+
/**
|
|
1212
|
+
* @description Already frozen margin for long orders
|
|
1213
|
+
*/
|
|
1214
|
+
isoOrderFrozenLong: number;
|
|
1215
|
+
/**
|
|
1216
|
+
* @description Already frozen margin for short orders
|
|
1217
|
+
*/
|
|
1218
|
+
isoOrderFrozenShort: number;
|
|
1219
|
+
/**
|
|
1220
|
+
* @description Maximum notional value for the symbol
|
|
1221
|
+
*/
|
|
1222
|
+
symbolMaxNotional: number;
|
|
1223
|
+
/**
|
|
1224
|
+
* @description Precision threshold for binary search (default: 1)
|
|
1225
|
+
*/
|
|
1226
|
+
epsilon?: number;
|
|
1227
|
+
}): number;
|
|
1228
|
+
|
|
1229
|
+
/**
|
|
1230
|
+
* total margin ratio
|
|
1231
|
+
*/
|
|
1232
|
+
declare function totalMarginRatio(inputs: {
|
|
380
1233
|
totalCollateral: number;
|
|
381
1234
|
markPrices: {
|
|
382
1235
|
[key: string]: number;
|
|
383
1236
|
};
|
|
384
1237
|
positions: API.Position[];
|
|
385
|
-
};
|
|
1238
|
+
}, dp?: number): number;
|
|
1239
|
+
|
|
386
1240
|
/**
|
|
387
|
-
*
|
|
1241
|
+
* @formulaId totalUnrealizedROI
|
|
1242
|
+
* @name Total Unrealized ROI
|
|
1243
|
+
* @formula Total Unrealized ROI = Total Unrealized PNL / ( Total Value - Total Unrealized PNL ) * 100%
|
|
1244
|
+
* @description
|
|
1245
|
+
*
|
|
1246
|
+
* ## Definition
|
|
1247
|
+
*
|
|
1248
|
+
* **Total Unrealized PNL** = Sum of unrealized profit and loss for all current positions of the user
|
|
1249
|
+
*
|
|
1250
|
+
* **Total Value** = User's total asset value (denominated in USDC), including assets that cannot be used as collateral
|
|
1251
|
+
*
|
|
1252
|
+
* ## Example
|
|
1253
|
+
*
|
|
1254
|
+
* ```
|
|
1255
|
+
* Total Unrealized ROI = 200.53 / ( 2982.66 - 200.53 ) * 100% = 7.21%
|
|
1256
|
+
* Total Unrealized PNL = 200.53
|
|
1257
|
+
* Total Value = 2982.66
|
|
1258
|
+
* ```
|
|
388
1259
|
*/
|
|
389
|
-
declare function
|
|
390
|
-
type TotalUnrealizedROIInputs = {
|
|
1260
|
+
declare function totalUnrealizedROI(inputs: {
|
|
391
1261
|
totalUnrealizedPnL: number;
|
|
392
1262
|
totalValue: number;
|
|
393
|
-
};
|
|
394
|
-
|
|
395
|
-
* totalUnrealizedROI
|
|
396
|
-
*/
|
|
397
|
-
declare function totalUnrealizedROI(inputs: TotalUnrealizedROIInputs): number;
|
|
1263
|
+
}): number;
|
|
1264
|
+
|
|
398
1265
|
/**
|
|
399
1266
|
* current account leverage
|
|
400
1267
|
*/
|
|
401
1268
|
declare function currentLeverage(totalMarginRatio: number): number;
|
|
402
|
-
|
|
1269
|
+
|
|
1270
|
+
declare function availableBalance(inputs: {
|
|
403
1271
|
USDCHolding: number;
|
|
404
1272
|
unsettlementPnL: number;
|
|
405
|
-
};
|
|
406
|
-
|
|
407
|
-
|
|
1273
|
+
}): number;
|
|
1274
|
+
/**
|
|
1275
|
+
* @formulaId availableBalanceForIsolatedMargin
|
|
1276
|
+
* @name Available Balance for Isolated Margin
|
|
1277
|
+
* @formula availableBalanceForIsolatedMargin = max(0, min(USDC_balance, free_collateral - max(total_cross_unsettled_pnl, 0)))
|
|
1278
|
+
* @description
|
|
1279
|
+
*
|
|
1280
|
+
* ## Definition
|
|
1281
|
+
*
|
|
1282
|
+
* max(0, min(USDC_balance, free_collateral - max(total_cross_unsettled_pnl, 0))), where
|
|
1283
|
+
*
|
|
1284
|
+
* **USDC_balance** = User's USDC balance
|
|
1285
|
+
*
|
|
1286
|
+
* **free_collateral** = Available collateral in the user's account (for cross margin trading)
|
|
1287
|
+
*
|
|
1288
|
+
* **total_cross_unsettled_pnl** = sum( unsettled_PNL_i ) across all cross margin positions
|
|
1289
|
+
*/
|
|
1290
|
+
declare function availableBalanceForIsolatedMargin(inputs: {
|
|
1291
|
+
USDCHolding: number;
|
|
1292
|
+
totalCrossUnsettledPnL: number;
|
|
1293
|
+
freeCollateral: number;
|
|
1294
|
+
}): number;
|
|
1295
|
+
|
|
1296
|
+
/**
|
|
1297
|
+
* @formulaId mmr
|
|
1298
|
+
* @name Total Maintenance Margin Ratio
|
|
1299
|
+
* @formula Total Maintenance Margin Ratio = sum(Position maintenance margin) / total_position_notional * 100%, total_position_notional = sum(abs(position_qty_i * mark_price_i))
|
|
1300
|
+
* @description
|
|
1301
|
+
*
|
|
1302
|
+
* ## Definition
|
|
1303
|
+
*
|
|
1304
|
+
* **Total Maintenance Margin Ratio** = User's account maintenance margin ratio
|
|
1305
|
+
*
|
|
1306
|
+
* **sum(Position maintenance margin)** = Total maintenance margin of all user positions (denominated in USDC)
|
|
1307
|
+
*
|
|
1308
|
+
* **total_position_notional** = Sum of notional value of current positions
|
|
1309
|
+
*
|
|
1310
|
+
* **position_qty_i** = Position quantity for a single symbol
|
|
1311
|
+
*
|
|
1312
|
+
* **mark_price_i** = Mark price for a single symbol
|
|
1313
|
+
*
|
|
1314
|
+
* ## Example
|
|
1315
|
+
*
|
|
1316
|
+
* ```
|
|
1317
|
+
* Total Margin Ratio = 505.61 / 10112.43 * 100% = 4.99988628%
|
|
1318
|
+
* total_position_notional = 10112.43
|
|
1319
|
+
* abs(BTC position notional) = 5197.2
|
|
1320
|
+
* abs(ETH position notional) = 4915.23
|
|
1321
|
+
* sum(Position maintenance margin) = 505.61
|
|
1322
|
+
* BTC position MM = 259.86
|
|
1323
|
+
* ETH position MM = 245.75
|
|
1324
|
+
* ```
|
|
1325
|
+
*
|
|
1326
|
+
* @param inputs AccountMMRInputs
|
|
1327
|
+
* @returns number|null
|
|
1328
|
+
*/
|
|
1329
|
+
declare function MMR(inputs: {
|
|
408
1330
|
positionsMMR: number;
|
|
409
1331
|
/**
|
|
410
1332
|
* Notional sum of all positions,
|
|
411
1333
|
* positions.totalNotional()
|
|
412
1334
|
*/
|
|
413
1335
|
positionsNotional: number;
|
|
414
|
-
};
|
|
415
|
-
|
|
416
|
-
* total maintenance margin ratio
|
|
417
|
-
* @param inputs AccountMMRInputs
|
|
418
|
-
* @returns number|null
|
|
419
|
-
*/
|
|
420
|
-
declare function MMR(inputs: AccountMMRInputs): number | null;
|
|
1336
|
+
}): number | null;
|
|
1337
|
+
|
|
421
1338
|
declare const collateralRatio: (params: {
|
|
422
1339
|
baseWeight: number;
|
|
423
1340
|
discountFactor: number | null;
|
|
@@ -432,6 +1349,7 @@ declare const collateralContribution: (params: {
|
|
|
432
1349
|
collateralRatio: number;
|
|
433
1350
|
indexPrice: number;
|
|
434
1351
|
}) => number;
|
|
1352
|
+
|
|
435
1353
|
declare const LTV: (params: {
|
|
436
1354
|
usdcBalance: number;
|
|
437
1355
|
upnl: number;
|
|
@@ -441,6 +1359,7 @@ declare const LTV: (params: {
|
|
|
441
1359
|
weight: number;
|
|
442
1360
|
}>;
|
|
443
1361
|
}) => number;
|
|
1362
|
+
|
|
444
1363
|
/**
|
|
445
1364
|
* max(0, min(USDC_balance, free_collateral - max(upnl, 0)))
|
|
446
1365
|
*/
|
|
@@ -466,64 +1385,179 @@ declare const calcMinimumReceived: (inputs: {
|
|
|
466
1385
|
amount: number;
|
|
467
1386
|
slippage: number;
|
|
468
1387
|
}) => number;
|
|
1388
|
+
|
|
469
1389
|
/**
|
|
470
|
-
* @
|
|
1390
|
+
* @formulaId maxAdd
|
|
1391
|
+
* @name Maximum Margin Addition for Isolated Position
|
|
1392
|
+
* @formula max_add = max(0, min(USDC_balance, free_collateral - max(total_cross_unsettled_pnl, 0)))
|
|
1393
|
+
* @description
|
|
1394
|
+
*
|
|
1395
|
+
* ## Definition
|
|
1396
|
+
*
|
|
1397
|
+
* **max_add**: Maximum amount of margin that can be added to an isolated margin position
|
|
1398
|
+
*
|
|
1399
|
+
* **USDC_balance**: User's USDC balance
|
|
1400
|
+
*
|
|
1401
|
+
* **free_collateral**: Available collateral in the user's account (for cross margin trading)
|
|
1402
|
+
*
|
|
1403
|
+
* **total_cross_unsettled_pnl**: Total unsettled PNL from cross margin positions only
|
|
1404
|
+
*
|
|
1405
|
+
* ## Business Rules
|
|
1406
|
+
*
|
|
1407
|
+
* - Maximum add amount cannot exceed available USDC balance
|
|
1408
|
+
* - Maximum add amount cannot exceed free collateral minus cross margin unrealized profit
|
|
1409
|
+
* - Cross margin unrealized profit reduces available funds for adding isolated margin
|
|
1410
|
+
*
|
|
1411
|
+
* ## Example
|
|
1412
|
+
*
|
|
1413
|
+
* ```
|
|
1414
|
+
* USDC_balance = 500
|
|
1415
|
+
* free_collateral = 300
|
|
1416
|
+
* total_cross_unsettled_pnl = 100 (profit)
|
|
1417
|
+
* max_add = max(0, min(500, 300 - max(100, 0))) = max(0, min(500, 200)) = 200
|
|
1418
|
+
* ```
|
|
1419
|
+
*
|
|
1420
|
+
* @param inputs Input parameters for calculating maximum margin addition
|
|
1421
|
+
* @returns Maximum margin that can be added (in USDC)
|
|
471
1422
|
*/
|
|
472
|
-
declare
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
1423
|
+
declare function maxAdd(inputs: {
|
|
1424
|
+
/**
|
|
1425
|
+
* @description USDC balance
|
|
1426
|
+
*/
|
|
1427
|
+
USDCHolding: number;
|
|
1428
|
+
/**
|
|
1429
|
+
* @description Free collateral (available for cross margin trading)
|
|
1430
|
+
*/
|
|
1431
|
+
freeCollateral: number;
|
|
1432
|
+
/**
|
|
1433
|
+
* @description Total cross margin unsettled PNL
|
|
1434
|
+
*/
|
|
1435
|
+
totalCrossUnsettledPnL: number;
|
|
1436
|
+
}): number;
|
|
1437
|
+
/**
|
|
1438
|
+
* @formulaId maxReduce
|
|
1439
|
+
* @name Maximum Margin Reduction for Isolated Position
|
|
1440
|
+
* @formula max_reduce = max(0, isolated_position_margin - position_notional * imr + min(0, position_unsettled_pnl))
|
|
1441
|
+
* @description
|
|
1442
|
+
*
|
|
1443
|
+
* ## Definition
|
|
1444
|
+
*
|
|
1445
|
+
* **max_reduce**: Maximum amount of margin that can be reduced from an isolated margin position
|
|
1446
|
+
*
|
|
1447
|
+
* **isolated_position_margin**: Current margin allocated to the isolated position
|
|
1448
|
+
*
|
|
1449
|
+
* **position_notional**: Notional value of the isolated position
|
|
1450
|
+
*
|
|
1451
|
+
* **imr**: Initial margin rate for the isolated position
|
|
1452
|
+
*
|
|
1453
|
+
* **position_unsettled_pnl**: Unrealized PNL of the isolated position
|
|
1454
|
+
*
|
|
1455
|
+
* ## Business Rules
|
|
1456
|
+
*
|
|
1457
|
+
* - Maximum reduce amount cannot exceed current isolated position margin
|
|
1458
|
+
* - Unrealized losses increase the maximum reducible amount
|
|
1459
|
+
* - Position notional and IMR determine the minimum required margin
|
|
1460
|
+
*
|
|
1461
|
+
* ## Example
|
|
1462
|
+
*
|
|
1463
|
+
* ```
|
|
1464
|
+
* isolated_position_margin = 1000
|
|
1465
|
+
* position_notional = 5000
|
|
1466
|
+
* imr = 0.02
|
|
1467
|
+
* position_unsettled_pnl = -100 (loss)
|
|
1468
|
+
* max_reduce = max(0, 1000 - 5000 * 0.02 + min(0, -100)) = max(0, 1000 - 100 - 100) = 800
|
|
1469
|
+
* ```
|
|
1470
|
+
*
|
|
1471
|
+
* @param inputs Input parameters for calculating maximum margin reduction
|
|
1472
|
+
* @returns Maximum margin that can be reduced (in USDC)
|
|
1473
|
+
*/
|
|
1474
|
+
declare function maxReduce(inputs: {
|
|
1475
|
+
/**
|
|
1476
|
+
* @description Current margin allocated to the isolated position
|
|
1477
|
+
*/
|
|
1478
|
+
isolatedPositionMargin: number;
|
|
1479
|
+
/**
|
|
1480
|
+
* @description Notional value of the isolated position
|
|
1481
|
+
*/
|
|
1482
|
+
positionNotional: number;
|
|
1483
|
+
/**
|
|
1484
|
+
* @description Initial margin rate for the isolated position
|
|
1485
|
+
*/
|
|
1486
|
+
imr: number;
|
|
1487
|
+
/**
|
|
1488
|
+
* @description Unrealized PNL of the isolated position
|
|
1489
|
+
*/
|
|
1490
|
+
positionUnsettledPnL: number;
|
|
1491
|
+
}): number;
|
|
476
1492
|
|
|
477
|
-
type
|
|
478
|
-
type
|
|
479
|
-
|
|
480
|
-
declare const
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
type
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
declare const
|
|
495
|
-
declare const
|
|
496
|
-
declare const
|
|
497
|
-
declare const
|
|
498
|
-
declare const
|
|
499
|
-
declare const
|
|
500
|
-
declare const
|
|
501
|
-
declare const
|
|
502
|
-
declare const
|
|
503
|
-
declare const
|
|
504
|
-
declare const
|
|
505
|
-
declare const
|
|
506
|
-
declare const
|
|
507
|
-
declare const
|
|
508
|
-
declare const
|
|
509
|
-
declare const
|
|
510
|
-
declare const
|
|
511
|
-
declare const
|
|
512
|
-
declare const
|
|
513
|
-
declare const
|
|
514
|
-
declare const
|
|
515
|
-
declare const
|
|
516
|
-
declare
|
|
517
|
-
|
|
518
|
-
declare const account_totalInitialMarginWithOrders: typeof totalInitialMarginWithOrders;
|
|
519
|
-
declare const account_totalInitialMarginWithQty: typeof totalInitialMarginWithQty;
|
|
520
|
-
declare const account_totalMarginRatio: typeof totalMarginRatio;
|
|
521
|
-
declare const account_totalUnrealizedROI: typeof totalUnrealizedROI;
|
|
522
|
-
declare const account_totalValue: typeof totalValue;
|
|
523
|
-
declare namespace account {
|
|
524
|
-
export { type account_AccountMMRInputs as AccountMMRInputs, type account_AvailableBalanceInputs as AvailableBalanceInputs, type account_FreeCollateralInputs as FreeCollateralInputs, account_IMR as IMR, type account_IMRInputs as IMRInputs, account_LTV as LTV, account_MMR as MMR, type account_MaxQtyInputs as MaxQtyInputs, type account_OtherIMsInputs as OtherIMsInputs, type account_PositionNotionalWithOrderInputs as PositionNotionalWithOrderInputs, type account_PositionQtyWithOrderInputs as PositionQtyWithOrderInputs, type account_ResultOptions as ResultOptions, type account_TotalCollateralValueInputs as TotalCollateralValueInputs, type account_TotalInitialMarginWithOrdersInputs as TotalInitialMarginWithOrdersInputs, type account_TotalMarginRatioInputs as TotalMarginRatioInputs, type account_TotalUnrealizedROIInputs as TotalUnrealizedROIInputs, type account_TotalValueInputs as TotalValueInputs, account_availableBalance as availableBalance, account_buyOrdersFilter_by_symbol as buyOrdersFilter_by_symbol, account_calcMinimumReceived as calcMinimumReceived, account_collateralContribution as collateralContribution, account_collateralRatio as collateralRatio, account_currentLeverage as currentLeverage, account_extractSymbols as extractSymbols, account_freeCollateral as freeCollateral, account_getPositonsAndOrdersNotionalBySymbol as getPositonsAndOrdersNotionalBySymbol, account_getQtyFromOrdersBySide as getQtyFromOrdersBySide, account_getQtyFromPositions as getQtyFromPositions, account_groupOrdersBySymbol as groupOrdersBySymbol, account_initialMarginWithOrder as initialMarginWithOrder, account_maxLeverage as maxLeverage, account_maxQty as maxQty, account_maxQtyByLong as maxQtyByLong, account_maxQtyByShort as maxQtyByShort, account_maxWithdrawalOtherCollateral as maxWithdrawalOtherCollateral, account_maxWithdrawalUSDC as maxWithdrawalUSDC, account_otherIMs as otherIMs, account_positionNotionalWithOrder_by_symbol as positionNotionalWithOrder_by_symbol, account_positionQtyWithOrders_by_symbol as positionQtyWithOrders_by_symbol, account_sellOrdersFilter_by_symbol as sellOrdersFilter_by_symbol, account_totalCollateral as totalCollateral, account_totalInitialMarginWithOrders as totalInitialMarginWithOrders, account_totalInitialMarginWithQty as totalInitialMarginWithQty, account_totalMarginRatio as totalMarginRatio, account_totalUnrealizedROI as totalUnrealizedROI, account_totalValue as totalValue };
|
|
1493
|
+
type index_FreeCollateralInputs = FreeCollateralInputs;
|
|
1494
|
+
type index_FreeCollateralUSDCOnlyInputs = FreeCollateralUSDCOnlyInputs;
|
|
1495
|
+
declare const index_IMR: typeof IMR;
|
|
1496
|
+
declare const index_LTV: typeof LTV;
|
|
1497
|
+
declare const index_MMR: typeof MMR;
|
|
1498
|
+
type index_MaxQtyInputs = MaxQtyInputs;
|
|
1499
|
+
type index_ResultOptions = ResultOptions;
|
|
1500
|
+
type index_TotalValueInputs = TotalValueInputs;
|
|
1501
|
+
declare const index_availableBalance: typeof availableBalance;
|
|
1502
|
+
declare const index_availableBalanceForIsolatedMargin: typeof availableBalanceForIsolatedMargin;
|
|
1503
|
+
declare const index_buyOrdersFilter_by_symbol: typeof buyOrdersFilter_by_symbol;
|
|
1504
|
+
declare const index_calcMinimumReceived: typeof calcMinimumReceived;
|
|
1505
|
+
declare const index_collateralContribution: typeof collateralContribution;
|
|
1506
|
+
declare const index_collateralRatio: typeof collateralRatio;
|
|
1507
|
+
declare const index_currentLeverage: typeof currentLeverage;
|
|
1508
|
+
declare const index_extractSymbols: typeof extractSymbols;
|
|
1509
|
+
declare const index_freeCollateral: typeof freeCollateral;
|
|
1510
|
+
declare const index_freeCollateralUSDCOnly: typeof freeCollateralUSDCOnly;
|
|
1511
|
+
declare const index_getPositonsAndOrdersNotionalBySymbol: typeof getPositonsAndOrdersNotionalBySymbol;
|
|
1512
|
+
declare const index_getQtyFromOrdersBySide: typeof getQtyFromOrdersBySide;
|
|
1513
|
+
declare const index_getQtyFromPositions: typeof getQtyFromPositions;
|
|
1514
|
+
declare const index_groupOrdersBySymbol: typeof groupOrdersBySymbol;
|
|
1515
|
+
declare const index_maxAdd: typeof maxAdd;
|
|
1516
|
+
declare const index_maxQty: typeof maxQty;
|
|
1517
|
+
declare const index_maxQtyByLong: typeof maxQtyByLong;
|
|
1518
|
+
declare const index_maxQtyByShort: typeof maxQtyByShort;
|
|
1519
|
+
declare const index_maxQtyForIsolatedMargin: typeof maxQtyForIsolatedMargin;
|
|
1520
|
+
declare const index_maxReduce: typeof maxReduce;
|
|
1521
|
+
declare const index_maxWithdrawalOtherCollateral: typeof maxWithdrawalOtherCollateral;
|
|
1522
|
+
declare const index_maxWithdrawalUSDC: typeof maxWithdrawalUSDC;
|
|
1523
|
+
declare const index_otherIMs: typeof otherIMs;
|
|
1524
|
+
declare const index_positionNotionalWithOrder_by_symbol: typeof positionNotionalWithOrder_by_symbol;
|
|
1525
|
+
declare const index_positionQtyWithOrders_by_symbol: typeof positionQtyWithOrders_by_symbol;
|
|
1526
|
+
declare const index_sellOrdersFilter_by_symbol: typeof sellOrdersFilter_by_symbol;
|
|
1527
|
+
declare const index_totalCollateral: typeof totalCollateral;
|
|
1528
|
+
declare const index_totalInitialMarginWithQty: typeof totalInitialMarginWithQty;
|
|
1529
|
+
declare const index_totalMarginRatio: typeof totalMarginRatio;
|
|
1530
|
+
declare const index_totalUnrealizedROI: typeof totalUnrealizedROI;
|
|
1531
|
+
declare const index_totalValue: typeof totalValue;
|
|
1532
|
+
declare namespace index {
|
|
1533
|
+
export { type index_FreeCollateralInputs as FreeCollateralInputs, type index_FreeCollateralUSDCOnlyInputs as FreeCollateralUSDCOnlyInputs, index_IMR as IMR, index_LTV as LTV, index_MMR as MMR, type index_MaxQtyInputs as MaxQtyInputs, type index_ResultOptions as ResultOptions, type index_TotalValueInputs as TotalValueInputs, index_availableBalance as availableBalance, index_availableBalanceForIsolatedMargin as availableBalanceForIsolatedMargin, index_buyOrdersFilter_by_symbol as buyOrdersFilter_by_symbol, index_calcMinimumReceived as calcMinimumReceived, index_collateralContribution as collateralContribution, index_collateralRatio as collateralRatio, index_currentLeverage as currentLeverage, index_extractSymbols as extractSymbols, index_freeCollateral as freeCollateral, index_freeCollateralUSDCOnly as freeCollateralUSDCOnly, index_getPositonsAndOrdersNotionalBySymbol as getPositonsAndOrdersNotionalBySymbol, index_getQtyFromOrdersBySide as getQtyFromOrdersBySide, index_getQtyFromPositions as getQtyFromPositions, index_groupOrdersBySymbol as groupOrdersBySymbol, index_maxAdd as maxAdd, index_maxQty as maxQty, index_maxQtyByLong as maxQtyByLong, index_maxQtyByShort as maxQtyByShort, index_maxQtyForIsolatedMargin as maxQtyForIsolatedMargin, index_maxReduce as maxReduce, index_maxWithdrawalOtherCollateral as maxWithdrawalOtherCollateral, index_maxWithdrawalUSDC as maxWithdrawalUSDC, index_otherIMs as otherIMs, index_positionNotionalWithOrder_by_symbol as positionNotionalWithOrder_by_symbol, index_positionQtyWithOrders_by_symbol as positionQtyWithOrders_by_symbol, index_sellOrdersFilter_by_symbol as sellOrdersFilter_by_symbol, index_totalCollateral as totalCollateral, index_totalInitialMarginWithQty as totalInitialMarginWithQty, index_totalMarginRatio as totalMarginRatio, index_totalUnrealizedROI as totalUnrealizedROI, index_totalValue as totalValue };
|
|
525
1534
|
}
|
|
526
1535
|
|
|
1536
|
+
/** @deprecated Use inline type or the new input type instead */
|
|
1537
|
+
type EstimatedLiquidationPriceInputs = {
|
|
1538
|
+
totalCollateral: number;
|
|
1539
|
+
markPrice: number;
|
|
1540
|
+
baseMMR: number;
|
|
1541
|
+
baseIMR: number;
|
|
1542
|
+
IMR_Factor: number;
|
|
1543
|
+
orderFee: number;
|
|
1544
|
+
positions: Pick<API.PositionExt, "position_qty" | "mark_price" | "symbol" | "mmr">[];
|
|
1545
|
+
newOrder: {
|
|
1546
|
+
symbol: string;
|
|
1547
|
+
qty: number;
|
|
1548
|
+
price: number;
|
|
1549
|
+
};
|
|
1550
|
+
};
|
|
1551
|
+
/** @deprecated Use inline type or the new input type instead */
|
|
1552
|
+
type EstimatedLeverageInputs = {
|
|
1553
|
+
totalCollateral: number;
|
|
1554
|
+
positions: Pick<API.PositionExt, "position_qty" | "mark_price" | "symbol" | "mmr">[];
|
|
1555
|
+
newOrder: {
|
|
1556
|
+
symbol: string;
|
|
1557
|
+
qty: number;
|
|
1558
|
+
price: number;
|
|
1559
|
+
};
|
|
1560
|
+
};
|
|
527
1561
|
/**
|
|
528
1562
|
* Maximum price when placing an order
|
|
529
1563
|
*/
|
|
@@ -548,41 +1582,276 @@ declare function orderFee(inputs: {
|
|
|
548
1582
|
price: number;
|
|
549
1583
|
futuresTakeFeeRate: number;
|
|
550
1584
|
}): number;
|
|
551
|
-
|
|
552
|
-
|
|
1585
|
+
/**
|
|
1586
|
+
* Calculate reference price for a **new order** based on business rules.
|
|
1587
|
+
*
|
|
1588
|
+
* The reference price is used by risk / margin formulas as the effective
|
|
1589
|
+
* execution price when the user is preparing a new order.
|
|
1590
|
+
*
|
|
1591
|
+
* Business rules (simplified):
|
|
1592
|
+
*
|
|
1593
|
+
* - LIMIT
|
|
1594
|
+
* - BUY: `reference = limit_price`
|
|
1595
|
+
* - SELL: `reference = max(limit_price, Bid1)`
|
|
1596
|
+
* - If `limit_price` is not provided: same as MARKET
|
|
1597
|
+
*
|
|
1598
|
+
* - MARKET
|
|
1599
|
+
* - BUY: `reference = Ask1`
|
|
1600
|
+
* - SELL: `reference = Bid1`
|
|
1601
|
+
*
|
|
1602
|
+
* - STOP MARKET
|
|
1603
|
+
* - If `stop_price` provided: `reference = stop_price`
|
|
1604
|
+
* - Else: same as MARKET
|
|
1605
|
+
*
|
|
1606
|
+
* - STOP LIMIT
|
|
1607
|
+
* - If `limit_price` provided: `reference = limit_price`
|
|
1608
|
+
* - Else: same as MARKET
|
|
1609
|
+
*
|
|
1610
|
+
* - TRAILING STOP
|
|
1611
|
+
* - If `trigger_price` provided: `reference = trigger_price`
|
|
1612
|
+
* - Else: same as MARKET
|
|
1613
|
+
*
|
|
1614
|
+
* - BBO (LIMIT with ASK / BID as `orderTypeExt`)
|
|
1615
|
+
* - BUY + ASK => `reference = Ask1`
|
|
1616
|
+
* - SELL + BID => `reference = Bid1`
|
|
1617
|
+
* - BUY + BID => `reference = Bid1`
|
|
1618
|
+
* - SELL + ASK => `reference = Ask1`
|
|
1619
|
+
*
|
|
1620
|
+
* @param order Lightweight description of the new order
|
|
1621
|
+
* @param askPrice Ask1 price from orderbook
|
|
1622
|
+
* @param bidPrice Bid1 price from orderbook
|
|
1623
|
+
* @returns Reference price or null if it cannot be determined
|
|
1624
|
+
*/
|
|
1625
|
+
declare function getOrderReferencePrice(order: {
|
|
1626
|
+
/**
|
|
1627
|
+
* @description Order type (e.g. LIMIT, MARKET, STOP_LIMIT, STOP_MARKET, TRAILING_STOP, ASK, BID)
|
|
1628
|
+
*/
|
|
1629
|
+
orderType: OrderType;
|
|
1630
|
+
/**
|
|
1631
|
+
* @description Extended order type for BBO orders (ASK / BID as LIMIT extensions)
|
|
1632
|
+
*/
|
|
1633
|
+
orderTypeExt?: OrderType;
|
|
1634
|
+
/**
|
|
1635
|
+
* @description Order side (BUY or SELL)
|
|
1636
|
+
*/
|
|
1637
|
+
side: OrderSide;
|
|
1638
|
+
/**
|
|
1639
|
+
* @description User input LIMIT price (for LIMIT / STOP_LIMIT orders)
|
|
1640
|
+
*/
|
|
1641
|
+
limitPrice?: number;
|
|
1642
|
+
/**
|
|
1643
|
+
* @description Trigger price (for STOP_MARKET / STOP_LIMIT / TRAILING_STOP orders)
|
|
1644
|
+
*/
|
|
1645
|
+
triggerPrice?: number;
|
|
1646
|
+
}, askPrice: number, bidPrice: number): number | null;
|
|
1647
|
+
/**
|
|
1648
|
+
* @formulaId estLiqPriceIsolated
|
|
1649
|
+
* @name Est. Position liq. Price (Isolated Margin)
|
|
1650
|
+
* @description
|
|
1651
|
+
* Estimate the liquidation price for an isolated-margin position after placing a new order.
|
|
1652
|
+
*
|
|
1653
|
+
* The underlying formula is:
|
|
1654
|
+
*
|
|
1655
|
+
* \[
|
|
1656
|
+
* liquidation\_price =
|
|
1657
|
+
* \frac{
|
|
1658
|
+
* isolated\_position\_margin' - cost\_position' - funding\_adjustment
|
|
1659
|
+
* }{
|
|
1660
|
+
* |position\_qty'| \cdot MMR' - position\_qty'
|
|
1661
|
+
* }
|
|
1662
|
+
* \]
|
|
1663
|
+
*
|
|
1664
|
+
* Where:
|
|
1665
|
+
* - `position_qty' = positionQty + orderSide * orderQty`
|
|
1666
|
+
* - `funding_adjustment = position_qty' * (sumUnitaryFunding - lastSumUnitaryFunding)`
|
|
1667
|
+
* - `MMR' = max(baseMMR, (baseMMR / baseIMR) * IMR_Factor * abs(position_qty' * reference_price)^(4/5))`
|
|
1668
|
+
*
|
|
1669
|
+
* Notes:
|
|
1670
|
+
* - This function only considers a **single isolated position** (no cross-margin collateral or other symbols).
|
|
1671
|
+
* - `newOrder.qty > 0` is treated as a BUY, `newOrder.qty < 0` as a SELL.
|
|
1672
|
+
*
|
|
1673
|
+
* @param inputs Estimation inputs for isolated-margin liquidation price
|
|
1674
|
+
* @returns Estimated liquidation price (in quote currency), or 0 when invalid / no position
|
|
1675
|
+
*/
|
|
1676
|
+
declare function estLiqPriceIsolated(inputs: {
|
|
1677
|
+
/**
|
|
1678
|
+
* @description Current isolated margin of the position
|
|
1679
|
+
*/
|
|
1680
|
+
isolatedPositionMargin: number;
|
|
1681
|
+
/**
|
|
1682
|
+
* @description Current position cost (qty * average entry price)
|
|
1683
|
+
*/
|
|
1684
|
+
costPosition: number;
|
|
1685
|
+
/**
|
|
1686
|
+
* @description Current position quantity (positive for long, negative for short)
|
|
1687
|
+
*/
|
|
1688
|
+
positionQty: number;
|
|
1689
|
+
/**
|
|
1690
|
+
* @description Current cumulative unitary funding
|
|
1691
|
+
*/
|
|
1692
|
+
sumUnitaryFunding: number;
|
|
1693
|
+
/**
|
|
1694
|
+
* @description Last cumulative unitary funding at the last settlement
|
|
1695
|
+
*/
|
|
1696
|
+
lastSumUnitaryFunding: number;
|
|
1697
|
+
/**
|
|
1698
|
+
* @description Current mark price of the symbol
|
|
1699
|
+
*/
|
|
553
1700
|
markPrice: number;
|
|
1701
|
+
/**
|
|
1702
|
+
* @description Base maintenance margin rate
|
|
1703
|
+
*/
|
|
554
1704
|
baseMMR: number;
|
|
1705
|
+
/**
|
|
1706
|
+
* @description Base initial margin rate
|
|
1707
|
+
*/
|
|
555
1708
|
baseIMR: number;
|
|
1709
|
+
/**
|
|
1710
|
+
* @description IMR calculation factor
|
|
1711
|
+
*/
|
|
556
1712
|
IMR_Factor: number;
|
|
557
|
-
|
|
558
|
-
|
|
1713
|
+
/**
|
|
1714
|
+
* @description Leverage for this isolated position
|
|
1715
|
+
*/
|
|
1716
|
+
leverage: number;
|
|
1717
|
+
/**
|
|
1718
|
+
* @description New order information used for estimation
|
|
1719
|
+
*/
|
|
559
1720
|
newOrder: {
|
|
1721
|
+
/**
|
|
1722
|
+
* @description Symbol of the order (kept for interface consistency)
|
|
1723
|
+
*/
|
|
560
1724
|
symbol: string;
|
|
1725
|
+
/**
|
|
1726
|
+
* @description Order quantity (positive for BUY, negative for SELL)
|
|
1727
|
+
*/
|
|
561
1728
|
qty: number;
|
|
1729
|
+
/**
|
|
1730
|
+
* @description Order price (reference price when opening / adding / flipping)
|
|
1731
|
+
*/
|
|
562
1732
|
price: number;
|
|
563
1733
|
};
|
|
564
|
-
};
|
|
1734
|
+
}): number;
|
|
565
1735
|
/**
|
|
566
|
-
*
|
|
1736
|
+
* @formulaId estLiqPrice
|
|
1737
|
+
* @name Est. Position liq. Price
|
|
1738
|
+
* @description
|
|
1739
|
+
*
|
|
1740
|
+
* ## When user has positions:
|
|
1741
|
+
*
|
|
1742
|
+
* ```
|
|
1743
|
+
* Est. liq. Position Price = max(mark_price_i + (total_collateral_value - new_total_MM - order_fee) / (abs(position_qty_i + new_order_qty_i) * new_MMRi - (position_qty_i + new_order_qty_i)), 0)
|
|
1744
|
+
* ```
|
|
1745
|
+
*
|
|
1746
|
+
* ## When user has no positions:
|
|
1747
|
+
*
|
|
1748
|
+
* ```
|
|
1749
|
+
* Est. liq. Position Price = max(order_price_i + (total_collateral_value - new_total_MM - order_fee) / (abs(position_qty_i + new_order_qty_i) * new_MMRi - (position_qty_i + new_order_qty_i)), 0)
|
|
1750
|
+
* ```
|
|
1751
|
+
*
|
|
1752
|
+
* ## Formula Components:
|
|
1753
|
+
*
|
|
1754
|
+
* - `order_fee = new_order_qty_i * order_price_i * futures_take_fee_rate`
|
|
1755
|
+
* - `new_total_MM = sum(abs(position_qty_i * mark_price_i + new_order_qty_i * order_price_i)) * MMRi)`
|
|
1756
|
+
* - `new_MMRi = Max(Base_MMR_i, (Base_MMR_i / Base_IMR_i) * IMR_Factor_i * Abs(position_qty_i * mark_price_i + new_order_qty_i * limit_price_i)^(4/5))`
|
|
1757
|
+
*
|
|
1758
|
+
* ## Order Price Determination:
|
|
1759
|
+
*
|
|
1760
|
+
* ### Market Order:
|
|
1761
|
+
* - **Long order**: `order_price_i = ask0`
|
|
1762
|
+
* - **Short order**: `order_price_i = bid0`
|
|
1763
|
+
*
|
|
1764
|
+
* ### Limit Order:
|
|
1765
|
+
*
|
|
1766
|
+
* #### Long order:
|
|
1767
|
+
* - If `limit_price >= ask0`: `order_price_i = ask0`
|
|
1768
|
+
* - If `limit_price < ask0`: `order_price_i = limit_price`
|
|
1769
|
+
*
|
|
1770
|
+
* #### Short order:
|
|
1771
|
+
* - If `limit_price <= bid0`: `order_price_i = bid0`
|
|
1772
|
+
* - If `limit_price > bid0`: `order_price_i = limit_price`
|
|
1773
|
+
*
|
|
1774
|
+
* ## Parameter Definitions:
|
|
1775
|
+
*
|
|
1776
|
+
* | Parameter | Description |
|
|
1777
|
+
* |-----------|-------------|
|
|
1778
|
+
* | `Est. Position liq. Price` | Estimated liquidation price for the position |
|
|
1779
|
+
* | `position_qty_i` | Position quantity for a single symbol |
|
|
1780
|
+
* | `mark_price_i` | Mark price for a single symbol |
|
|
1781
|
+
* | `total_collateral_value` | Total asset value of user's account margin (USDC denominated) |
|
|
1782
|
+
* | `new_order_qty_i` | Symbol quantity when user prepares to open position (positive for long, negative for short) |
|
|
1783
|
+
* | `new_total_MM` | Sum of current position maintenance margin (including prepared order maintenance margin) |
|
|
1784
|
+
* | `new_MMR_i` | Maintenance margin rate for a single symbol (including prepared order notional consideration) |
|
|
1785
|
+
* | `Base_MMR_i` | Base maintenance margin rate for a single symbol |
|
|
1786
|
+
* | `Base_IMR_i` | Base initial margin rate for a single symbol |
|
|
1787
|
+
* | `IMR_Factor_i` | IMR calculation factor for a single symbol, from v1/client/info |
|
|
1788
|
+
* | `Position_Notional_i` | Sum of position notional for a single symbol |
|
|
1789
|
+
* | `order_fee` | Estimated order fee when user prepares to open position |
|
|
1790
|
+
* | `futures_take_fee_rate` | User's futures take fee rate, from GET /v1/client/info |
|
|
1791
|
+
* | `order_price_i` | Estimated execution price when user prepares to open position |
|
|
1792
|
+
* | `limit_price` | Price entered by user when preparing to open position |
|
|
1793
|
+
* | `ask0` | Minimum ask price from orderbook |
|
|
1794
|
+
* | `bid0` | Maximum bid price from orderbook |
|
|
1795
|
+
*
|
|
1796
|
+
* ## Examples:
|
|
1797
|
+
*
|
|
1798
|
+
* ### Market Order Example:
|
|
1799
|
+
* Long BTC qty = 0.1, mark price = 25986.2, ask0 = 26000, BTC position_qty_i = 0.2, ETH position_qty_i = -3
|
|
1800
|
+
* futures_take_fee_rate = 0.06%
|
|
1801
|
+
*
|
|
1802
|
+
* **Result**: BTC Est. Position liq. Price = 21268.7316
|
|
1803
|
+
*
|
|
1804
|
+
* ### Limit Order Example 1:
|
|
1805
|
+
* Long BTC qty = 0.1, mark price = 25986.2, ask0 = 26000, limit price = 25000, BTC position_qty_i = 0.2, ETH position_qty_i = -3
|
|
1806
|
+
*
|
|
1807
|
+
* **Result**: BTC Est. Position liq. Price = 21250.9772
|
|
1808
|
+
*
|
|
1809
|
+
* ### Limit Order Example 2:
|
|
1810
|
+
* Short BTC qty = -0.1, mark price = 25986.2, bid0 = 25900, limit price = 25000, BTC position_qty_i = 0.2, ETH position_qty_i = -3
|
|
1811
|
+
*
|
|
1812
|
+
* **Result**: BTC Est. Position liq. Price = 9102.17368
|
|
1813
|
+
*
|
|
1814
|
+
* ### No Position Example:
|
|
1815
|
+
* Long BTC qty = 0.1, mark price = 25986.2, ask0 = 26000, limit price = 25000
|
|
1816
|
+
*
|
|
1817
|
+
* **Result**: BTC Est. Position liq. Price = 5472
|
|
1818
|
+
*
|
|
567
1819
|
* @param inputs
|
|
568
1820
|
* @returns
|
|
569
1821
|
*/
|
|
570
|
-
declare function estLiqPrice(inputs:
|
|
571
|
-
type EstimatedLeverageInputs = {
|
|
1822
|
+
declare function estLiqPrice(inputs: {
|
|
572
1823
|
totalCollateral: number;
|
|
573
|
-
|
|
1824
|
+
markPrice: number;
|
|
1825
|
+
baseMMR: number;
|
|
1826
|
+
baseIMR: number;
|
|
1827
|
+
IMR_Factor: number;
|
|
1828
|
+
orderFee: number;
|
|
1829
|
+
positions: {
|
|
1830
|
+
position_qty: number;
|
|
1831
|
+
mark_price: number;
|
|
1832
|
+
symbol: string;
|
|
1833
|
+
mmr: number;
|
|
1834
|
+
}[];
|
|
574
1835
|
newOrder: {
|
|
575
1836
|
symbol: string;
|
|
576
1837
|
qty: number;
|
|
577
1838
|
price: number;
|
|
578
1839
|
};
|
|
579
|
-
};
|
|
1840
|
+
}): number;
|
|
580
1841
|
/**
|
|
581
1842
|
* Estimated leverage
|
|
582
1843
|
* @param inputs EstimtedLeverageInputs
|
|
583
1844
|
* @returns number
|
|
584
1845
|
*/
|
|
585
|
-
declare function estLeverage(inputs:
|
|
1846
|
+
declare function estLeverage(inputs: {
|
|
1847
|
+
totalCollateral: number;
|
|
1848
|
+
positions: Pick<API.PositionExt, "position_qty" | "mark_price" | "symbol">[];
|
|
1849
|
+
newOrder: {
|
|
1850
|
+
symbol: string;
|
|
1851
|
+
qty: number;
|
|
1852
|
+
price: number;
|
|
1853
|
+
};
|
|
1854
|
+
}): number | null;
|
|
586
1855
|
declare function tpslROI(inputs: {
|
|
587
1856
|
side: OrderSide;
|
|
588
1857
|
type: "tp" | "sl";
|
|
@@ -595,13 +1864,15 @@ type order_EstimatedLeverageInputs = EstimatedLeverageInputs;
|
|
|
595
1864
|
type order_EstimatedLiquidationPriceInputs = EstimatedLiquidationPriceInputs;
|
|
596
1865
|
declare const order_estLeverage: typeof estLeverage;
|
|
597
1866
|
declare const order_estLiqPrice: typeof estLiqPrice;
|
|
1867
|
+
declare const order_estLiqPriceIsolated: typeof estLiqPriceIsolated;
|
|
1868
|
+
declare const order_getOrderReferencePrice: typeof getOrderReferencePrice;
|
|
598
1869
|
declare const order_maxPrice: typeof maxPrice;
|
|
599
1870
|
declare const order_minPrice: typeof minPrice;
|
|
600
1871
|
declare const order_orderFee: typeof orderFee;
|
|
601
1872
|
declare const order_scopePrice: typeof scopePrice;
|
|
602
1873
|
declare const order_tpslROI: typeof tpslROI;
|
|
603
1874
|
declare namespace order {
|
|
604
|
-
export { type order_EstimatedLeverageInputs as EstimatedLeverageInputs, type order_EstimatedLiquidationPriceInputs as EstimatedLiquidationPriceInputs, order_estLeverage as estLeverage, order_estLiqPrice as estLiqPrice, order_maxPrice as maxPrice, order_minPrice as minPrice, order_orderFee as orderFee, order_scopePrice as scopePrice, order_tpslROI as tpslROI };
|
|
1875
|
+
export { type order_EstimatedLeverageInputs as EstimatedLeverageInputs, type order_EstimatedLiquidationPriceInputs as EstimatedLiquidationPriceInputs, order_estLeverage as estLeverage, order_estLiqPrice as estLiqPrice, order_estLiqPriceIsolated as estLiqPriceIsolated, order_getOrderReferencePrice as getOrderReferencePrice, order_maxPrice as maxPrice, order_minPrice as minPrice, order_orderFee as orderFee, order_scopePrice as scopePrice, order_tpslROI as tpslROI };
|
|
605
1876
|
}
|
|
606
1877
|
|
|
607
|
-
export { account, order, order as orderUtils, positions, _default as version };
|
|
1878
|
+
export { index as account, order, order as orderUtils, index$1 as positions, _default as version };
|