@orderly.network/perp 4.8.9-alpha.0 → 4.8.10-alpha.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 +12 -12
- package/dist/index.d.ts +12 -12
- package/dist/index.js +246 -105
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +232 -91
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -31,9 +31,9 @@ module.exports = __toCommonJS(src_exports);
|
|
|
31
31
|
// src/version.ts
|
|
32
32
|
if (typeof window !== "undefined") {
|
|
33
33
|
window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};
|
|
34
|
-
window.__ORDERLY_VERSION__["@orderly.network/perp"] = "4.8.
|
|
34
|
+
window.__ORDERLY_VERSION__["@orderly.network/perp"] = "4.8.10-alpha.0";
|
|
35
35
|
}
|
|
36
|
-
var version_default = "4.8.
|
|
36
|
+
var version_default = "4.8.10-alpha.0";
|
|
37
37
|
|
|
38
38
|
// src/positions.ts
|
|
39
39
|
var positions_exports = {};
|
|
@@ -56,14 +56,34 @@ __export(positions_exports, {
|
|
|
56
56
|
unrealizedPnLROI: () => unrealizedPnLROI,
|
|
57
57
|
unsettlementPnL: () => unsettlementPnL
|
|
58
58
|
});
|
|
59
|
-
var
|
|
59
|
+
var import_utils2 = require("@orderly.network/utils");
|
|
60
60
|
|
|
61
61
|
// src/constants.ts
|
|
62
62
|
var IMRFactorPower = 4 / 5;
|
|
63
63
|
|
|
64
|
+
// src/utils.ts
|
|
65
|
+
var import_utils = require("@orderly.network/utils");
|
|
66
|
+
var DMax = (...values) => {
|
|
67
|
+
if (values.length === 0) {
|
|
68
|
+
throw new Error("DMax requires at least one argument");
|
|
69
|
+
}
|
|
70
|
+
const decimals = values.map(
|
|
71
|
+
(val) => val instanceof import_utils.Decimal ? val : new import_utils.Decimal(val)
|
|
72
|
+
);
|
|
73
|
+
let max = decimals[0];
|
|
74
|
+
for (let i = 1; i < decimals.length; i++) {
|
|
75
|
+
if (decimals[i].gte(max)) {
|
|
76
|
+
max = decimals[i];
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return max;
|
|
80
|
+
};
|
|
81
|
+
|
|
64
82
|
// src/positions.ts
|
|
83
|
+
var MaxIterates = 30;
|
|
84
|
+
var CONVERGENCE_THRESHOLD = 1e-4;
|
|
65
85
|
function notional(qty, mark_price) {
|
|
66
|
-
return new
|
|
86
|
+
return new import_utils2.Decimal(qty).mul(mark_price).abs().toNumber();
|
|
67
87
|
}
|
|
68
88
|
function totalNotional(positions) {
|
|
69
89
|
return positions.reduce((acc, cur) => {
|
|
@@ -71,13 +91,13 @@ function totalNotional(positions) {
|
|
|
71
91
|
}, 0);
|
|
72
92
|
}
|
|
73
93
|
function unrealizedPnL(inputs) {
|
|
74
|
-
return new
|
|
94
|
+
return new import_utils2.Decimal(inputs.qty).mul(inputs.markPrice - inputs.openPrice).toNumber();
|
|
75
95
|
}
|
|
76
96
|
function unrealizedPnLROI(inputs) {
|
|
77
97
|
const { openPrice, IMR: IMR2 } = inputs;
|
|
78
98
|
if (inputs.unrealizedPnL === 0 || inputs.positionQty === 0 || openPrice === 0 || IMR2 === 0)
|
|
79
99
|
return 0;
|
|
80
|
-
return new
|
|
100
|
+
return new import_utils2.Decimal(inputs.unrealizedPnL).div(new import_utils2.Decimal(Math.abs(inputs.positionQty)).mul(openPrice).mul(IMR2)).toNumber();
|
|
81
101
|
}
|
|
82
102
|
function totalUnrealizedPnL(positions) {
|
|
83
103
|
return positions.reduce((acc, cur) => {
|
|
@@ -88,26 +108,147 @@ function totalUnrealizedPnL(positions) {
|
|
|
88
108
|
});
|
|
89
109
|
}, 0);
|
|
90
110
|
}
|
|
91
|
-
|
|
92
|
-
|
|
111
|
+
var mmForOtherSymbols = (positions) => {
|
|
112
|
+
return positions.reduce((acc, cur) => {
|
|
113
|
+
return acc.add(
|
|
114
|
+
new import_utils2.Decimal(cur.position_qty).abs().mul(cur.mark_price).mul(cur.mmr)
|
|
115
|
+
);
|
|
116
|
+
}, import_utils2.zero);
|
|
117
|
+
};
|
|
118
|
+
var calculateLiqPrice = (markPrice, positionQty, MMR3, totalCollateral2, positions) => {
|
|
119
|
+
const decimalMarkPrice = new import_utils2.Decimal(markPrice);
|
|
120
|
+
const absQty = new import_utils2.Decimal(positionQty).abs();
|
|
121
|
+
const denominator = absQty.mul(MMR3).sub(positionQty);
|
|
122
|
+
const liqPrice2 = new import_utils2.Decimal(totalCollateral2).sub(absQty.mul(decimalMarkPrice).mul(MMR3)).sub(mmForOtherSymbols(positions)).div(denominator).add(decimalMarkPrice);
|
|
123
|
+
return DMax(liqPrice2, import_utils2.zero);
|
|
124
|
+
};
|
|
125
|
+
var compareCollateralWithMM = (inputs) => {
|
|
126
|
+
return (price) => {
|
|
127
|
+
const {
|
|
128
|
+
totalCollateral: totalCollateral2,
|
|
129
|
+
positionQty,
|
|
130
|
+
markPrice,
|
|
131
|
+
baseMMR,
|
|
132
|
+
baseIMR,
|
|
133
|
+
IMRFactor,
|
|
134
|
+
positions
|
|
135
|
+
} = inputs;
|
|
136
|
+
const decimalPositionQty = new import_utils2.Decimal(positionQty);
|
|
137
|
+
const collateral = new import_utils2.Decimal(totalCollateral2).sub(decimalPositionQty.mul(markPrice)).add(decimalPositionQty.mul(price));
|
|
138
|
+
const mm = decimalPositionQty.abs().mul(price).mul(
|
|
139
|
+
Math.max(
|
|
140
|
+
baseMMR,
|
|
141
|
+
new import_utils2.Decimal(baseMMR).div(baseIMR).mul(IMRFactor).mul(decimalPositionQty.mul(price).abs().toPower(IMRFactorPower)).toNumber()
|
|
142
|
+
)
|
|
143
|
+
).add(mmForOtherSymbols(positions));
|
|
144
|
+
return collateral.gte(mm);
|
|
145
|
+
};
|
|
146
|
+
};
|
|
147
|
+
var liqPrice = (inputs) => {
|
|
148
|
+
const {
|
|
149
|
+
positionQty,
|
|
150
|
+
markPrice,
|
|
151
|
+
totalCollateral: totalCollateral2,
|
|
152
|
+
positions,
|
|
153
|
+
MMR: MMR3,
|
|
154
|
+
baseMMR,
|
|
155
|
+
baseIMR,
|
|
156
|
+
IMRFactor,
|
|
157
|
+
symbol
|
|
158
|
+
} = inputs;
|
|
93
159
|
if (positionQty === 0 || totalCollateral2 === 0) {
|
|
94
160
|
return null;
|
|
95
161
|
}
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
162
|
+
const isLONG = positionQty > 0;
|
|
163
|
+
const otherPositions = positions.filter((item) => item.symbol !== symbol);
|
|
164
|
+
if (isLONG) {
|
|
165
|
+
let liqPriceLeft = calculateLiqPrice(
|
|
166
|
+
markPrice,
|
|
167
|
+
positionQty,
|
|
168
|
+
baseMMR,
|
|
169
|
+
totalCollateral2,
|
|
170
|
+
otherPositions
|
|
99
171
|
);
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
172
|
+
let liqPriceRight = calculateLiqPrice(
|
|
173
|
+
markPrice,
|
|
174
|
+
positionQty,
|
|
175
|
+
MMR3,
|
|
176
|
+
totalCollateral2,
|
|
177
|
+
otherPositions
|
|
178
|
+
);
|
|
179
|
+
const compareCollateralWithMMFunc = compareCollateralWithMM({
|
|
180
|
+
totalCollateral: totalCollateral2,
|
|
181
|
+
positionQty,
|
|
182
|
+
markPrice,
|
|
183
|
+
baseIMR,
|
|
184
|
+
baseMMR,
|
|
185
|
+
IMRFactor,
|
|
186
|
+
positions: otherPositions
|
|
187
|
+
});
|
|
188
|
+
for (let i = 0; i < MaxIterates; i++) {
|
|
189
|
+
if (liqPriceLeft.gte(liqPriceRight)) {
|
|
190
|
+
return liqPriceRight.toNumber();
|
|
191
|
+
}
|
|
192
|
+
const mid = new import_utils2.Decimal(liqPriceLeft).add(liqPriceRight).div(2);
|
|
193
|
+
if (compareCollateralWithMMFunc(mid)) {
|
|
194
|
+
liqPriceRight = mid;
|
|
195
|
+
} else {
|
|
196
|
+
liqPriceLeft = mid;
|
|
197
|
+
}
|
|
198
|
+
if (liqPriceRight.sub(liqPriceLeft).div(liqPriceLeft.add(liqPriceRight)).mul(2).lte(CONVERGENCE_THRESHOLD)) {
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return liqPriceRight.toNumber();
|
|
203
|
+
} else {
|
|
204
|
+
let liqPriceRight = calculateLiqPrice(
|
|
205
|
+
markPrice,
|
|
206
|
+
positionQty,
|
|
207
|
+
MMR3,
|
|
208
|
+
totalCollateral2,
|
|
209
|
+
otherPositions
|
|
210
|
+
);
|
|
211
|
+
let liqPriceLeft = calculateLiqPrice(
|
|
212
|
+
markPrice,
|
|
213
|
+
positionQty,
|
|
214
|
+
Math.max(
|
|
215
|
+
baseIMR,
|
|
216
|
+
new import_utils2.Decimal(baseMMR).div(baseIMR).mul(IMRFactor).mul(
|
|
217
|
+
new import_utils2.Decimal(positionQty).mul(liqPriceRight).abs().toPower(IMRFactorPower)
|
|
218
|
+
).toNumber()
|
|
219
|
+
),
|
|
220
|
+
totalCollateral2,
|
|
221
|
+
otherPositions
|
|
222
|
+
);
|
|
223
|
+
const compareCollateralWithMMFunc = compareCollateralWithMM({
|
|
224
|
+
totalCollateral: totalCollateral2,
|
|
225
|
+
positionQty,
|
|
226
|
+
markPrice,
|
|
227
|
+
baseMMR,
|
|
228
|
+
baseIMR,
|
|
229
|
+
IMRFactor,
|
|
230
|
+
positions: otherPositions
|
|
231
|
+
});
|
|
232
|
+
for (let i = 0; i < MaxIterates; i++) {
|
|
233
|
+
if (liqPriceLeft.gte(liqPriceRight)) {
|
|
234
|
+
return liqPriceLeft.toNumber();
|
|
235
|
+
}
|
|
236
|
+
const mid = liqPriceLeft.add(liqPriceRight).div(2);
|
|
237
|
+
if (compareCollateralWithMMFunc(mid)) {
|
|
238
|
+
liqPriceLeft = mid;
|
|
239
|
+
} else {
|
|
240
|
+
liqPriceRight = mid;
|
|
241
|
+
}
|
|
242
|
+
if (liqPriceRight.sub(liqPriceLeft).div(liqPriceLeft.add(liqPriceRight)).mul(2).lte(CONVERGENCE_THRESHOLD)) {
|
|
243
|
+
break;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return liqPriceLeft.toNumber();
|
|
247
|
+
}
|
|
248
|
+
};
|
|
108
249
|
function maintenanceMargin(inputs) {
|
|
109
250
|
const { positionQty, markPrice, MMR: MMR3 } = inputs;
|
|
110
|
-
return new
|
|
251
|
+
return new import_utils2.Decimal(positionQty).mul(markPrice).mul(MMR3).abs().toNumber();
|
|
111
252
|
}
|
|
112
253
|
function unsettlementPnL(inputs) {
|
|
113
254
|
const {
|
|
@@ -117,8 +258,8 @@ function unsettlementPnL(inputs) {
|
|
|
117
258
|
sumUnitaryFunding,
|
|
118
259
|
lastSumUnitaryFunding
|
|
119
260
|
} = inputs;
|
|
120
|
-
const qty = new
|
|
121
|
-
return qty.mul(markPrice).sub(costPosition).sub(qty.mul(new
|
|
261
|
+
const qty = new import_utils2.Decimal(positionQty);
|
|
262
|
+
return qty.mul(markPrice).sub(costPosition).sub(qty.mul(new import_utils2.Decimal(sumUnitaryFunding).sub(lastSumUnitaryFunding))).toNumber();
|
|
122
263
|
}
|
|
123
264
|
function totalUnsettlementPnL(positions) {
|
|
124
265
|
if (!Array.isArray(positions) || positions.length === 0) {
|
|
@@ -144,31 +285,31 @@ function MMR(inputs) {
|
|
|
144
285
|
} = inputs;
|
|
145
286
|
return Math.max(
|
|
146
287
|
baseMMR,
|
|
147
|
-
new
|
|
288
|
+
new import_utils2.Decimal(baseMMR).div(baseIMR).mul(IMRFactor).mul(Math.pow(Math.abs(positionNotional), IMR_factor_power)).toNumber()
|
|
148
289
|
);
|
|
149
290
|
}
|
|
150
291
|
function estPnLForTP(inputs) {
|
|
151
|
-
return new
|
|
292
|
+
return new import_utils2.Decimal(inputs.positionQty).mul(new import_utils2.Decimal(inputs.price).sub(inputs.entryPrice)).toNumber();
|
|
152
293
|
}
|
|
153
294
|
function estPriceForTP(inputs) {
|
|
154
|
-
return new
|
|
295
|
+
return new import_utils2.Decimal(inputs.pnl).div(inputs.positionQty).add(inputs.entryPrice).toNumber();
|
|
155
296
|
}
|
|
156
297
|
function estOffsetForTP(inputs) {
|
|
157
|
-
return new
|
|
298
|
+
return new import_utils2.Decimal(inputs.price).div(inputs.entryPrice).toNumber();
|
|
158
299
|
}
|
|
159
300
|
function estPriceFromOffsetForTP(inputs) {
|
|
160
|
-
return new
|
|
301
|
+
return new import_utils2.Decimal(inputs.offset).add(inputs.entryPrice).toNumber();
|
|
161
302
|
}
|
|
162
303
|
function estPnLForSL(inputs) {
|
|
163
304
|
return 0;
|
|
164
305
|
}
|
|
165
306
|
function maxPositionNotional(inputs) {
|
|
166
307
|
const { leverage, IMRFactor } = inputs;
|
|
167
|
-
return new
|
|
308
|
+
return new import_utils2.Decimal(1).div(new import_utils2.Decimal(leverage).mul(IMRFactor)).pow(1 / 0.8).toNumber();
|
|
168
309
|
}
|
|
169
310
|
function maxPositionLeverage(inputs) {
|
|
170
311
|
const { IMRFactor, notional: notional2 } = inputs;
|
|
171
|
-
return new
|
|
312
|
+
return new import_utils2.Decimal(1).div(new import_utils2.Decimal(IMRFactor).mul(new import_utils2.Decimal(notional2).pow(0.8))).toNumber();
|
|
172
313
|
}
|
|
173
314
|
|
|
174
315
|
// src/account.ts
|
|
@@ -208,35 +349,35 @@ __export(account_exports, {
|
|
|
208
349
|
totalValue: () => totalValue
|
|
209
350
|
});
|
|
210
351
|
var import_types = require("@orderly.network/types");
|
|
211
|
-
var
|
|
352
|
+
var import_utils4 = require("@orderly.network/utils");
|
|
212
353
|
function totalValue(inputs) {
|
|
213
354
|
const { totalUnsettlementPnL: totalUnsettlementPnL2, USDCHolding, nonUSDCHolding } = inputs;
|
|
214
355
|
const nonUSDCHoldingValue = nonUSDCHolding.reduce((acc, cur) => {
|
|
215
|
-
return new
|
|
216
|
-
},
|
|
356
|
+
return new import_utils4.Decimal(cur.holding).mul(cur.indexPrice).add(acc);
|
|
357
|
+
}, import_utils4.zero);
|
|
217
358
|
return nonUSDCHoldingValue.add(USDCHolding).add(totalUnsettlementPnL2);
|
|
218
359
|
}
|
|
219
360
|
function freeCollateral(inputs) {
|
|
220
361
|
const value = inputs.totalCollateral.sub(inputs.totalInitialMarginWithOrders);
|
|
221
|
-
return value.isNegative() ?
|
|
362
|
+
return value.isNegative() ? import_utils4.zero : value;
|
|
222
363
|
}
|
|
223
364
|
function totalCollateral(inputs) {
|
|
224
365
|
const { USDCHolding, nonUSDCHolding, unsettlementPnL: unsettlementPnL2 } = inputs;
|
|
225
366
|
const nonUSDCHoldingValue = nonUSDCHolding.reduce((acc, cur) => {
|
|
226
367
|
const finalHolding = Math.min(cur.holding, cur.collateralCap);
|
|
227
|
-
const value = new
|
|
368
|
+
const value = new import_utils4.Decimal(finalHolding).mul(cur.collateralRatio).mul(cur.indexPrice);
|
|
228
369
|
return acc.add(value);
|
|
229
|
-
},
|
|
230
|
-
return new
|
|
370
|
+
}, import_utils4.zero);
|
|
371
|
+
return new import_utils4.Decimal(USDCHolding).add(nonUSDCHoldingValue).add(unsettlementPnL2);
|
|
231
372
|
}
|
|
232
373
|
function initialMarginWithOrder() {
|
|
233
374
|
}
|
|
234
375
|
function positionNotionalWithOrder_by_symbol(inputs) {
|
|
235
|
-
return new
|
|
376
|
+
return new import_utils4.Decimal(inputs.markPrice).mul(inputs.positionQtyWithOrders);
|
|
236
377
|
}
|
|
237
378
|
function positionQtyWithOrders_by_symbol(inputs) {
|
|
238
379
|
const { positionQty, buyOrdersQty, sellOrdersQty } = inputs;
|
|
239
|
-
const positionQtyDecimal = new
|
|
380
|
+
const positionQtyDecimal = new import_utils4.Decimal(positionQty);
|
|
240
381
|
const qty = Math.max(
|
|
241
382
|
positionQtyDecimal.add(buyOrdersQty).abs().toNumber(),
|
|
242
383
|
positionQtyDecimal.sub(sellOrdersQty).abs().toNumber()
|
|
@@ -255,8 +396,8 @@ function IMR(inputs) {
|
|
|
255
396
|
return Math.max(
|
|
256
397
|
1 / maxLeverage2,
|
|
257
398
|
baseIMR,
|
|
258
|
-
new
|
|
259
|
-
new
|
|
399
|
+
new import_utils4.Decimal(IMR_Factor).mul(
|
|
400
|
+
new import_utils4.Decimal(positionNotional).add(orderNotional).abs().toPower(IMR_factor_power)
|
|
260
401
|
).toNumber()
|
|
261
402
|
);
|
|
262
403
|
}
|
|
@@ -288,8 +429,8 @@ function getPositonsAndOrdersNotionalBySymbol(inputs) {
|
|
|
288
429
|
const positionQty = getQtyFromPositions(positions, symbol);
|
|
289
430
|
const buyOrdersQty = getQtyFromOrdersBySide(orders, symbol, import_types.OrderSide.BUY);
|
|
290
431
|
const sellOrdersQty = getQtyFromOrdersBySide(orders, symbol, import_types.OrderSide.SELL);
|
|
291
|
-
const markPriceDecimal = new
|
|
292
|
-
return markPriceDecimal.mul(positionQty).add(markPriceDecimal.mul(new
|
|
432
|
+
const markPriceDecimal = new import_utils4.Decimal(markPrice);
|
|
433
|
+
return markPriceDecimal.mul(positionQty).add(markPriceDecimal.mul(new import_utils4.Decimal(buyOrdersQty).add(sellOrdersQty))).abs().toNumber();
|
|
293
434
|
}
|
|
294
435
|
function totalInitialMarginWithOrders(inputs) {
|
|
295
436
|
const {
|
|
@@ -320,10 +461,10 @@ function totalInitialMarginWithOrders(inputs) {
|
|
|
320
461
|
markPrice,
|
|
321
462
|
positionQtyWithOrders
|
|
322
463
|
});
|
|
323
|
-
const markPriceDecimal = new
|
|
464
|
+
const markPriceDecimal = new import_utils4.Decimal(markPrice);
|
|
324
465
|
const imr = IMR({
|
|
325
466
|
positionNotional: markPriceDecimal.mul(positionQty).toNumber(),
|
|
326
|
-
ordersNotional: markPriceDecimal.mul(new
|
|
467
|
+
ordersNotional: markPriceDecimal.mul(new import_utils4.Decimal(buyOrdersQty).add(sellOrdersQty)).toNumber(),
|
|
327
468
|
maxLeverage: maxLeverage2,
|
|
328
469
|
IMR_Factor: IMR_Factors[symbol],
|
|
329
470
|
baseIMR: symbolInfo[symbol]("base_imr", 0)
|
|
@@ -352,10 +493,10 @@ function totalInitialMarginWithQty(inputs) {
|
|
|
352
493
|
markPrice,
|
|
353
494
|
positionQtyWithOrders
|
|
354
495
|
});
|
|
355
|
-
const markPriceDecimal = new
|
|
496
|
+
const markPriceDecimal = new import_utils4.Decimal(markPrice);
|
|
356
497
|
const imr = IMR({
|
|
357
498
|
positionNotional: markPriceDecimal.mul(positionQty).toNumber(),
|
|
358
|
-
ordersNotional: markPriceDecimal.mul(new
|
|
499
|
+
ordersNotional: markPriceDecimal.mul(new import_utils4.Decimal(buyOrdersQty).add(sellOrdersQty)).toNumber(),
|
|
359
500
|
maxLeverage: maxLeverage({
|
|
360
501
|
symbolLeverage: (_a = position == null ? void 0 : position.leverage) != null ? _a : inputs.maxLeverage,
|
|
361
502
|
accountLeverage: inputs.maxLeverage
|
|
@@ -402,13 +543,13 @@ function otherIMs(inputs) {
|
|
|
402
543
|
console.warn("markPrices[%s] is undefined", symbol);
|
|
403
544
|
return acc;
|
|
404
545
|
}
|
|
405
|
-
const markPriceDecimal = new
|
|
546
|
+
const markPriceDecimal = new import_utils4.Decimal(markPrices[symbol] || 0);
|
|
406
547
|
const position = positions.find((item) => item.symbol === symbol);
|
|
407
548
|
const positionQty = getQtyFromPositions(positions, symbol);
|
|
408
549
|
const positionNotional = markPriceDecimal.mul(positionQty).toNumber();
|
|
409
550
|
const buyOrdersQty = position.pending_long_qty;
|
|
410
551
|
const sellOrdersQty = position.pending_short_qty;
|
|
411
|
-
const ordersNotional = markPriceDecimal.mul(new
|
|
552
|
+
const ordersNotional = markPriceDecimal.mul(new import_utils4.Decimal(buyOrdersQty).add(sellOrdersQty)).toNumber();
|
|
412
553
|
const IMR_Factor = IMR_Factors[symbol];
|
|
413
554
|
if (typeof IMR_Factor === "undefined") {
|
|
414
555
|
console.warn("IMR_Factor is not found:", symbol);
|
|
@@ -434,7 +575,7 @@ function otherIMs(inputs) {
|
|
|
434
575
|
positionQtyWithOrders
|
|
435
576
|
});
|
|
436
577
|
return acc.add(positionNotionalWithOrders.mul(imr));
|
|
437
|
-
},
|
|
578
|
+
}, import_utils4.zero).toNumber();
|
|
438
579
|
}
|
|
439
580
|
function maxQty(side, inputs, options) {
|
|
440
581
|
if (side === import_types.OrderSide.BUY) {
|
|
@@ -459,10 +600,10 @@ function maxQtyByLong(inputs, options) {
|
|
|
459
600
|
if (totalCollateral2 === 0) {
|
|
460
601
|
return 0;
|
|
461
602
|
}
|
|
462
|
-
const totalCollateralDecimal = new
|
|
603
|
+
const totalCollateralDecimal = new import_utils4.Decimal(totalCollateral2);
|
|
463
604
|
const factor_1 = totalCollateralDecimal.sub(otherIMs2).div(
|
|
464
|
-
new
|
|
465
|
-
).div(markPrice).mul(0.995).sub(new
|
|
605
|
+
new import_utils4.Decimal(takerFeeRate).mul(2).mul(1e-4).add(Math.max(1 / maxLeverage2, baseIMR))
|
|
606
|
+
).div(markPrice).mul(0.995).sub(new import_utils4.Decimal(positionQty).add(buyOrdersQty)).toNumber();
|
|
466
607
|
if (positionQty === 0 && buyOrdersQty === 0) {
|
|
467
608
|
return Math.min(baseMaxQty, factor_1);
|
|
468
609
|
}
|
|
@@ -470,10 +611,10 @@ function maxQtyByLong(inputs, options) {
|
|
|
470
611
|
return Math.min(baseMaxQty, factor_1);
|
|
471
612
|
}
|
|
472
613
|
const factor_2 = totalCollateralDecimal.sub(otherIMs2).div(IMR_Factor).toPower(1 / 1.8).div(markPrice).sub(
|
|
473
|
-
new
|
|
614
|
+
new import_utils4.Decimal(positionQty).add(buyOrdersQty)
|
|
474
615
|
// .abs()
|
|
475
616
|
// .div(new Decimal(takerFeeRate).mul(2).mul(0.0001).add(1))
|
|
476
|
-
).div(new
|
|
617
|
+
).div(new import_utils4.Decimal(takerFeeRate).mul(2).mul(1e-4).add(1)).mul(0.995).toNumber();
|
|
477
618
|
return Math.min(baseMaxQty, factor_1, factor_2);
|
|
478
619
|
} catch (error) {
|
|
479
620
|
return 0;
|
|
@@ -494,9 +635,9 @@ function maxQtyByShort(inputs, options) {
|
|
|
494
635
|
sellOrdersQty,
|
|
495
636
|
takerFeeRate
|
|
496
637
|
} = inputs;
|
|
497
|
-
const totalCollateralDecimal = new
|
|
638
|
+
const totalCollateralDecimal = new import_utils4.Decimal(totalCollateral2);
|
|
498
639
|
const factor_1 = totalCollateralDecimal.sub(otherIMs2).div(
|
|
499
|
-
new
|
|
640
|
+
new import_utils4.Decimal(takerFeeRate).mul(2).mul(1e-4).add(Math.max(1 / maxLeverage2, baseIMR))
|
|
500
641
|
).div(markPrice).mul(0.995).add(positionQty).sub(Math.abs(sellOrdersQty)).toNumber();
|
|
501
642
|
if (positionQty === 0 && sellOrdersQty === 0) {
|
|
502
643
|
return Math.min(baseMaxQty, factor_1);
|
|
@@ -504,7 +645,7 @@ function maxQtyByShort(inputs, options) {
|
|
|
504
645
|
if (IMR_Factor === 0) {
|
|
505
646
|
return Math.min(baseMaxQty, factor_1);
|
|
506
647
|
}
|
|
507
|
-
const factor_2 = totalCollateralDecimal.sub(otherIMs2).div(IMR_Factor).toPower(1 / 1.8).div(markPrice).add(positionQty).sub(sellOrdersQty).div(new
|
|
648
|
+
const factor_2 = totalCollateralDecimal.sub(otherIMs2).div(IMR_Factor).toPower(1 / 1.8).div(markPrice).add(positionQty).sub(sellOrdersQty).div(new import_utils4.Decimal(takerFeeRate).mul(2).mul(1e-4).add(1)).mul(0.995).toNumber();
|
|
508
649
|
return Math.min(baseMaxQty, factor_1, factor_2);
|
|
509
650
|
} catch (error) {
|
|
510
651
|
return 0;
|
|
@@ -515,19 +656,19 @@ function totalMarginRatio(inputs, dp) {
|
|
|
515
656
|
if (totalCollateral2 === 0) {
|
|
516
657
|
return 0;
|
|
517
658
|
}
|
|
518
|
-
const totalCollateralDecimal = new
|
|
659
|
+
const totalCollateralDecimal = new import_utils4.Decimal(totalCollateral2);
|
|
519
660
|
const totalPositionNotional = positions.reduce((acc, cur) => {
|
|
520
661
|
const markPrice = markPrices[cur.symbol] || 0;
|
|
521
|
-
return acc.add(new
|
|
522
|
-
},
|
|
523
|
-
if (totalPositionNotional.eq(
|
|
662
|
+
return acc.add(new import_utils4.Decimal(cur.position_qty).mul(markPrice).abs());
|
|
663
|
+
}, import_utils4.zero);
|
|
664
|
+
if (totalPositionNotional.eq(import_utils4.zero)) {
|
|
524
665
|
return 0;
|
|
525
666
|
}
|
|
526
667
|
return totalCollateralDecimal.div(totalPositionNotional).toNumber();
|
|
527
668
|
}
|
|
528
669
|
function totalUnrealizedROI(inputs) {
|
|
529
670
|
const { totalUnrealizedPnL: totalUnrealizedPnL2, totalValue: totalValue2 } = inputs;
|
|
530
|
-
return new
|
|
671
|
+
return new import_utils4.Decimal(totalUnrealizedPnL2).div(totalValue2 - totalUnrealizedPnL2).toNumber();
|
|
531
672
|
}
|
|
532
673
|
function currentLeverage(totalMarginRatio2) {
|
|
533
674
|
if (totalMarginRatio2 === 0) {
|
|
@@ -537,7 +678,7 @@ function currentLeverage(totalMarginRatio2) {
|
|
|
537
678
|
}
|
|
538
679
|
function availableBalance(inputs) {
|
|
539
680
|
const { USDCHolding, unsettlementPnL: unsettlementPnL2 } = inputs;
|
|
540
|
-
return new
|
|
681
|
+
return new import_utils4.Decimal(USDCHolding).add(unsettlementPnL2).toNumber();
|
|
541
682
|
}
|
|
542
683
|
function MMR2(inputs) {
|
|
543
684
|
if (inputs.positionsNotional === 0) {
|
|
@@ -546,7 +687,7 @@ function MMR2(inputs) {
|
|
|
546
687
|
if (inputs.positionsMMR === 0) {
|
|
547
688
|
return null;
|
|
548
689
|
}
|
|
549
|
-
return new
|
|
690
|
+
return new import_utils4.Decimal(inputs.positionsMMR).div(inputs.positionsNotional).toNumber();
|
|
550
691
|
}
|
|
551
692
|
var collateralRatio = (params) => {
|
|
552
693
|
const {
|
|
@@ -557,30 +698,30 @@ var collateralRatio = (params) => {
|
|
|
557
698
|
indexPrice
|
|
558
699
|
} = params;
|
|
559
700
|
const cap = collateralCap === -1 ? collateralQty : collateralCap;
|
|
560
|
-
const K = new
|
|
561
|
-
const DCF = new
|
|
562
|
-
const qty = new
|
|
701
|
+
const K = new import_utils4.Decimal(1.2);
|
|
702
|
+
const DCF = new import_utils4.Decimal(discountFactor || 0);
|
|
703
|
+
const qty = new import_utils4.Decimal(Math.min(collateralQty, cap));
|
|
563
704
|
const notionalAbs = qty.mul(indexPrice).abs();
|
|
564
705
|
const dynamicWeight = DCF.mul(notionalAbs.toPower(IMRFactorPower));
|
|
565
|
-
const result = K.div(new
|
|
566
|
-
return result.lt(baseWeight) ? result : new
|
|
706
|
+
const result = K.div(new import_utils4.Decimal(1).add(dynamicWeight));
|
|
707
|
+
return result.lt(baseWeight) ? result : new import_utils4.Decimal(baseWeight);
|
|
567
708
|
};
|
|
568
709
|
var collateralContribution = (params) => {
|
|
569
710
|
const { collateralQty, collateralCap, collateralRatio: collateralRatio2, indexPrice } = params;
|
|
570
711
|
const cap = collateralCap === -1 ? collateralQty : collateralCap;
|
|
571
|
-
return new
|
|
712
|
+
return new import_utils4.Decimal(Math.min(collateralQty, cap)).mul(collateralRatio2).mul(indexPrice).toNumber();
|
|
572
713
|
};
|
|
573
714
|
var LTV = (params) => {
|
|
574
715
|
const { usdcBalance, upnl, assets } = params;
|
|
575
|
-
const usdcLoss = new
|
|
576
|
-
const upnlLoss = new
|
|
716
|
+
const usdcLoss = new import_utils4.Decimal(Math.min(usdcBalance, 0)).abs();
|
|
717
|
+
const upnlLoss = new import_utils4.Decimal(Math.min(upnl, 0)).abs();
|
|
577
718
|
const numerator = usdcLoss.add(upnlLoss);
|
|
578
719
|
const collateralSum = assets.reduce((acc, asset) => {
|
|
579
720
|
return acc.add(
|
|
580
|
-
new
|
|
721
|
+
new import_utils4.Decimal(Math.max(asset.qty, 0)).mul(new import_utils4.Decimal(asset.indexPrice)).mul(new import_utils4.Decimal(asset.weight))
|
|
581
722
|
);
|
|
582
|
-
},
|
|
583
|
-
const denominator = collateralSum.add(new
|
|
723
|
+
}, import_utils4.zero);
|
|
724
|
+
const denominator = collateralSum.add(new import_utils4.Decimal(Math.max(upnl, 0)));
|
|
584
725
|
if (numerator.isZero() || denominator.isZero()) {
|
|
585
726
|
return 0;
|
|
586
727
|
}
|
|
@@ -589,26 +730,26 @@ var LTV = (params) => {
|
|
|
589
730
|
var maxWithdrawalUSDC = (inputs) => {
|
|
590
731
|
const { USDCBalance, freeCollateral: freeCollateral2, upnl } = inputs;
|
|
591
732
|
const value = Math.min(
|
|
592
|
-
new
|
|
593
|
-
new
|
|
733
|
+
new import_utils4.Decimal(USDCBalance).toNumber(),
|
|
734
|
+
new import_utils4.Decimal(freeCollateral2).sub(Math.max(upnl, 0)).toNumber()
|
|
594
735
|
);
|
|
595
736
|
return Math.max(0, value);
|
|
596
737
|
};
|
|
597
738
|
var maxWithdrawalOtherCollateral = (inputs) => {
|
|
598
739
|
const { USDCBalance, collateralQty, freeCollateral: freeCollateral2, indexPrice, weight } = inputs;
|
|
599
|
-
const usdcBalance = new
|
|
600
|
-
const denominator = usdcBalance.isNegative() ? new
|
|
740
|
+
const usdcBalance = new import_utils4.Decimal(USDCBalance);
|
|
741
|
+
const denominator = usdcBalance.isNegative() ? new import_utils4.Decimal(indexPrice).mul(weight).mul(new import_utils4.Decimal(1).add(2e-3)) : new import_utils4.Decimal(indexPrice).mul(weight);
|
|
601
742
|
if (denominator.isZero()) {
|
|
602
|
-
return
|
|
743
|
+
return import_utils4.zero;
|
|
603
744
|
}
|
|
604
|
-
const qty = new
|
|
605
|
-
const maxQtyByValue = new
|
|
745
|
+
const qty = new import_utils4.Decimal(collateralQty);
|
|
746
|
+
const maxQtyByValue = new import_utils4.Decimal(freeCollateral2).div(denominator);
|
|
606
747
|
return maxQtyByValue.lt(qty) ? maxQtyByValue : qty;
|
|
607
748
|
};
|
|
608
749
|
var calcMinimumReceived = (inputs) => {
|
|
609
750
|
const { amount, slippage } = inputs;
|
|
610
|
-
const slippageRatio = new
|
|
611
|
-
return new
|
|
751
|
+
const slippageRatio = new import_utils4.Decimal(slippage).div(100);
|
|
752
|
+
return new import_utils4.Decimal(amount).mul(new import_utils4.Decimal(1).minus(slippageRatio)).toNumber();
|
|
612
753
|
};
|
|
613
754
|
var maxLeverage = (inputs) => {
|
|
614
755
|
const { symbolLeverage, accountLeverage } = inputs;
|
|
@@ -626,7 +767,7 @@ __export(order_exports, {
|
|
|
626
767
|
scopePrice: () => scopePrice,
|
|
627
768
|
tpslROI: () => tpslROI
|
|
628
769
|
});
|
|
629
|
-
var
|
|
770
|
+
var import_utils5 = require("@orderly.network/utils");
|
|
630
771
|
function maxPrice(markprice, range) {
|
|
631
772
|
return markprice * (1 + range);
|
|
632
773
|
}
|
|
@@ -640,7 +781,7 @@ function scopePrice(price, scope, side) {
|
|
|
640
781
|
return price * (1 + scope);
|
|
641
782
|
}
|
|
642
783
|
function orderFee(inputs) {
|
|
643
|
-
return new
|
|
784
|
+
return new import_utils5.Decimal(inputs.qty).mul(inputs.price).mul(inputs.futuresTakeFeeRate).toNumber();
|
|
644
785
|
}
|
|
645
786
|
function estLiqPrice(inputs) {
|
|
646
787
|
var _a;
|
|
@@ -655,13 +796,13 @@ function estLiqPrice(inputs) {
|
|
|
655
796
|
IMR_Factor
|
|
656
797
|
} = inputs;
|
|
657
798
|
let currentPosition = void 0;
|
|
658
|
-
let newTotalMM =
|
|
799
|
+
let newTotalMM = import_utils5.zero;
|
|
659
800
|
const hasPosition = positions.filter((item) => item.position_qty > 0).length > 0;
|
|
660
801
|
const basePrice = hasPosition ? markPrice : newOrder.price;
|
|
661
|
-
const newOrderNotional = new
|
|
802
|
+
const newOrderNotional = new import_utils5.Decimal(newOrder.qty).mul(newOrder.price);
|
|
662
803
|
for (let index = 0; index < positions.length; index++) {
|
|
663
804
|
const position = positions[index];
|
|
664
|
-
let notional2 = new
|
|
805
|
+
let notional2 = new import_utils5.Decimal(position.position_qty).mul(position.mark_price);
|
|
665
806
|
if (newOrder.symbol === position.symbol) {
|
|
666
807
|
currentPosition = position;
|
|
667
808
|
notional2 = notional2.add(newOrderNotional);
|
|
@@ -673,26 +814,26 @@ function estLiqPrice(inputs) {
|
|
|
673
814
|
}
|
|
674
815
|
const newMMR = Math.max(
|
|
675
816
|
baseMMR,
|
|
676
|
-
new
|
|
817
|
+
new import_utils5.Decimal(baseMMR).div(baseIMR).mul(IMR_Factor).mul(
|
|
677
818
|
newOrderNotional.add(
|
|
678
|
-
!!currentPosition ? new
|
|
819
|
+
!!currentPosition ? new import_utils5.Decimal(currentPosition.position_qty).mul(
|
|
679
820
|
currentPosition.mark_price
|
|
680
|
-
) :
|
|
821
|
+
) : import_utils5.zero
|
|
681
822
|
).abs()
|
|
682
823
|
).toPower(4 / 5).toNumber()
|
|
683
824
|
);
|
|
684
|
-
const newQty = new
|
|
825
|
+
const newQty = new import_utils5.Decimal(newOrder.qty).add(
|
|
685
826
|
(_a = currentPosition == null ? void 0 : currentPosition.position_qty) != null ? _a : 0
|
|
686
827
|
);
|
|
687
828
|
if (newQty.eq(0)) {
|
|
688
829
|
return 0;
|
|
689
830
|
}
|
|
690
831
|
const denominator = newQty.abs().mul(newMMR).sub(newQty);
|
|
691
|
-
if (denominator.eq(
|
|
832
|
+
if (denominator.eq(import_utils5.zero)) {
|
|
692
833
|
return 0;
|
|
693
834
|
}
|
|
694
|
-
const price = new
|
|
695
|
-
new
|
|
835
|
+
const price = new import_utils5.Decimal(basePrice).add(
|
|
836
|
+
new import_utils5.Decimal(totalCollateral2).sub(newTotalMM).sub(orderFee2).div(denominator)
|
|
696
837
|
).toNumber();
|
|
697
838
|
return Math.max(0, price);
|
|
698
839
|
}
|
|
@@ -703,35 +844,35 @@ function estLeverage(inputs) {
|
|
|
703
844
|
}
|
|
704
845
|
let hasPosition = false;
|
|
705
846
|
let sumPositionNotional = positions.reduce((acc, cur) => {
|
|
706
|
-
let count = new
|
|
847
|
+
let count = new import_utils5.Decimal(cur.position_qty).mul(cur.mark_price);
|
|
707
848
|
if (cur.symbol === newOrder.symbol) {
|
|
708
849
|
hasPosition = true;
|
|
709
|
-
count = count.add(new
|
|
850
|
+
count = count.add(new import_utils5.Decimal(newOrder.qty).mul(newOrder.price));
|
|
710
851
|
}
|
|
711
852
|
return acc.add(count.abs());
|
|
712
|
-
},
|
|
853
|
+
}, import_utils5.zero);
|
|
713
854
|
if (!hasPosition) {
|
|
714
855
|
sumPositionNotional = sumPositionNotional.add(
|
|
715
|
-
new
|
|
856
|
+
new import_utils5.Decimal(newOrder.qty).mul(newOrder.price).abs()
|
|
716
857
|
);
|
|
717
858
|
}
|
|
718
|
-
if (sumPositionNotional.eq(
|
|
859
|
+
if (sumPositionNotional.eq(import_utils5.zero)) {
|
|
719
860
|
return null;
|
|
720
861
|
}
|
|
721
|
-
const totalMarginRatio2 = new
|
|
862
|
+
const totalMarginRatio2 = new import_utils5.Decimal(totalCollateral2).div(
|
|
722
863
|
sumPositionNotional
|
|
723
864
|
);
|
|
724
|
-
return new
|
|
865
|
+
return new import_utils5.Decimal(1).div(totalMarginRatio2).toDecimalPlaces(2, import_utils5.Decimal.ROUND_HALF_EVEN).toNumber();
|
|
725
866
|
}
|
|
726
867
|
function tpslROI(inputs) {
|
|
727
|
-
const direction = (0,
|
|
868
|
+
const direction = (0, import_utils5.getTPSLDirection)({
|
|
728
869
|
side: inputs.side,
|
|
729
870
|
type: inputs.type,
|
|
730
871
|
closePrice: inputs.closePrice,
|
|
731
872
|
orderPrice: inputs.orderPrice
|
|
732
873
|
});
|
|
733
874
|
const { closePrice, orderPrice, leverage } = inputs;
|
|
734
|
-
return new
|
|
875
|
+
return new import_utils5.Decimal(closePrice).minus(orderPrice).div(orderPrice).mul(leverage).abs().mul(direction).toNumber();
|
|
735
876
|
}
|
|
736
877
|
// Annotate the CommonJS export names for ESM import in node:
|
|
737
878
|
0 && (module.exports = {
|