@orderly.network/hooks 2.12.2-alpha.0 → 2.12.3-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.mjs CHANGED
@@ -49,9 +49,9 @@ function useMarketCategoriesConfig() {
49
49
  // src/version.ts
50
50
  if (typeof window !== "undefined") {
51
51
  window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};
52
- window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "2.12.2-alpha.0";
52
+ window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "2.12.3-alpha.0";
53
53
  }
54
- var version_default = "2.12.2-alpha.0";
54
+ var version_default = "2.12.3-alpha.0";
55
55
  var fetcher = (url, init2 = {}, queryOptions) => get(url, init2, queryOptions?.formatter);
56
56
  var noCacheConfig = {
57
57
  dedupingInterval: 0,
@@ -573,6 +573,20 @@ var useAudioPlayer = (src, options = {}) => {
573
573
  const el = getOrderFilledAudio();
574
574
  el.volume = Math.max(0, Math.min(1, volume));
575
575
  }, [volume]);
576
+ useEffect(() => {
577
+ const handleVisibilityChange = () => {
578
+ if (document.visibilityState === "visible") {
579
+ const el = getOrderFilledAudio();
580
+ el.pause();
581
+ el.removeAttribute("src");
582
+ el.load();
583
+ }
584
+ };
585
+ document.addEventListener("visibilitychange", handleVisibilityChange);
586
+ return () => {
587
+ document.removeEventListener("visibilitychange", handleVisibilityChange);
588
+ };
589
+ }, []);
576
590
  const play = useCallback(() => {
577
591
  const currentSrc = srcRef.current;
578
592
  const currentEnabled = enabledRef.current;
@@ -1282,7 +1296,7 @@ function checkTPSLOrderTypeIsMarket(key, values) {
1282
1296
  }
1283
1297
  function tpslCalculateHelper(key, inputs, options = {}) {
1284
1298
  const { symbol } = options;
1285
- if (key !== "quantity" && key !== "tp_trigger_price" && key !== "sl_trigger_price" && key !== "tp_pnl" && key !== "sl_pnl" && key !== "tp_offset" && key !== "sl_offset" && key !== "tp_offset_percentage" && key !== "sl_offset_percentage" && key !== "tp_order_price" && key !== "sl_order_price" && key !== "tp_order_type" && key !== "sl_order_type") {
1299
+ if (key !== "quantity" && key !== "tp_trigger_price" && key !== "sl_trigger_price" && key !== "tp_pnl" && key !== "sl_pnl" && key !== "tp_offset" && key !== "sl_offset" && key !== "tp_offset_percentage" && key !== "sl_offset_percentage" && key !== "tp_offset_from_mark" && key !== "sl_offset_from_mark" && key !== "tp_offset_percentage_from_mark" && key !== "sl_offset_percentage_from_mark" && key !== "tp_order_price" && key !== "sl_order_price" && key !== "tp_order_type" && key !== "sl_order_type") {
1286
1300
  return {
1287
1301
  [key]: inputs.value
1288
1302
  };
@@ -1290,6 +1304,7 @@ function tpslCalculateHelper(key, inputs, options = {}) {
1290
1304
  const orderType = key.startsWith("tp_") ? AlgoOrderType.TAKE_PROFIT : AlgoOrderType.STOP_LOSS;
1291
1305
  const keyPrefix = key.slice(0, 3);
1292
1306
  const qty = Number(key === "quantity" ? inputs.value : inputs.qty);
1307
+ const markPrice = inputs.markPrice ?? inputs.entryPrice;
1293
1308
  if (qty === 0 && (key === "tp_pnl" || key === "sl_pnl" || key === "tp_trigger_price" || key === "sl_trigger_price")) {
1294
1309
  return {
1295
1310
  // [`${keyPrefix}trigger_price`]: "",
@@ -1300,7 +1315,7 @@ function tpslCalculateHelper(key, inputs, options = {}) {
1300
1315
  [key]: inputs.value
1301
1316
  };
1302
1317
  }
1303
- let trigger_price, offset, offset_percentage, pnl, order_price, tpsl_order_type = inputs.values[`${keyPrefix}order_type`] ?? OrderType.MARKET;
1318
+ let trigger_price, offset, offset_percentage, offset_from_mark, offset_percentage_from_mark, pnl, order_price, tpsl_order_type = inputs.values[`${keyPrefix}order_type`] ?? OrderType.MARKET;
1304
1319
  const entryPrice = new Decimal(inputs.entryPrice).todp(options.symbol?.quote_dp ?? 2, Decimal.ROUND_UP).toNumber();
1305
1320
  switch (key) {
1306
1321
  case "tp_trigger_price":
@@ -1312,6 +1327,8 @@ function tpslCalculateHelper(key, inputs, options = {}) {
1312
1327
  [`${keyPrefix}trigger_price`]: trigger_price,
1313
1328
  [`${keyPrefix}offset`]: "",
1314
1329
  [`${keyPrefix}offset_percentage`]: "",
1330
+ [`${keyPrefix}offset_from_mark`]: "",
1331
+ [`${keyPrefix}offset_percentage_from_mark`]: "",
1315
1332
  [`${keyPrefix}pnl`]: "",
1316
1333
  [`${keyPrefix}ROI`]: ""
1317
1334
  };
@@ -1321,31 +1338,6 @@ function tpslCalculateHelper(key, inputs, options = {}) {
1321
1338
  }
1322
1339
  break;
1323
1340
  }
1324
- // case "tp_enable":
1325
- // case "sl_enable": {
1326
- // return {
1327
- // [`${keyPrefix}enable`]: inputs.value,
1328
- // [`${keyPrefix}order_type`]: OrderType.MARKET,
1329
- // [`${keyPrefix}trigger_price`]: "",
1330
- // [`${keyPrefix}order_price`]: "",
1331
- // [`${keyPrefix}offset`]: "",
1332
- // [`${keyPrefix}offset_percentage`]: "",
1333
- // [`${keyPrefix}pnl`]: "",
1334
- // [`${keyPrefix}ROI`]: "",
1335
- // };
1336
- // }
1337
- // case 'tp_pnl':{
1338
- // if (inputs.values.tp_order_type !== OrderType.MARKET) {
1339
- // pnl = inputs.value;
1340
- // trigger_price = pnlToPrice({
1341
- // qty,
1342
- // pnl: Number(inputs.value),
1343
- // entryPrice,
1344
- // orderSide: inputs.orderSide,
1345
- // orderType,
1346
- // })
1347
- // }
1348
- // }
1349
1341
  case "tp_pnl":
1350
1342
  case "sl_pnl": {
1351
1343
  pnl = inputs.value;
@@ -1392,6 +1384,30 @@ function tpslCalculateHelper(key, inputs, options = {}) {
1392
1384
  }
1393
1385
  break;
1394
1386
  }
1387
+ case "tp_offset_from_mark":
1388
+ case "sl_offset_from_mark": {
1389
+ offset_from_mark = inputs.value;
1390
+ if (!checkTPSLOrderTypeIsMarket(key, inputs.values)) {
1391
+ order_price = offsetToPrice({
1392
+ qty,
1393
+ offset: Number(inputs.value),
1394
+ entryPrice: markPrice,
1395
+ orderSide: inputs.orderSide,
1396
+ orderType: key === "tp_offset_from_mark" ? AlgoOrderType.TAKE_PROFIT : AlgoOrderType.STOP_LOSS
1397
+ });
1398
+ trigger_price = inputs.values[`${keyPrefix}trigger_price`] ?? order_price;
1399
+ } else {
1400
+ trigger_price = offsetToPrice({
1401
+ qty,
1402
+ offset: Number(inputs.value),
1403
+ // entryPrice,
1404
+ entryPrice: markPrice,
1405
+ orderSide: inputs.orderSide,
1406
+ orderType: key === "tp_offset_from_mark" ? AlgoOrderType.TAKE_PROFIT : AlgoOrderType.STOP_LOSS
1407
+ });
1408
+ }
1409
+ break;
1410
+ }
1395
1411
  case "tp_order_price":
1396
1412
  case "sl_order_price": {
1397
1413
  order_price = inputs.value;
@@ -1432,12 +1448,38 @@ function tpslCalculateHelper(key, inputs, options = {}) {
1432
1448
  }
1433
1449
  break;
1434
1450
  }
1451
+ case "tp_offset_percentage_from_mark":
1452
+ case "sl_offset_percentage_from_mark": {
1453
+ offset_percentage_from_mark = inputs.value;
1454
+ const markBase = Number(markPrice);
1455
+ if (!checkTPSLOrderTypeIsMarket(key, inputs.values)) {
1456
+ order_price = offsetPercentageToPrice({
1457
+ qty,
1458
+ percentage: Number(`${inputs.value}`.replace(/\.0{0,2}$/, "")),
1459
+ entryPrice: markBase,
1460
+ orderSide: inputs.orderSide,
1461
+ orderType
1462
+ });
1463
+ trigger_price = inputs.values[`${keyPrefix}trigger_price`] ?? order_price;
1464
+ } else {
1465
+ trigger_price = offsetPercentageToPrice({
1466
+ qty,
1467
+ percentage: Number(`${inputs.value}`.replace(/\.0{0,2}$/, "")),
1468
+ entryPrice: markBase,
1469
+ orderSide: inputs.orderSide,
1470
+ orderType
1471
+ });
1472
+ }
1473
+ break;
1474
+ }
1435
1475
  }
1436
1476
  if (!trigger_price && checkTPSLOrderTypeIsMarket(key, inputs.values)) {
1437
1477
  return {
1438
1478
  [`${keyPrefix}trigger_price`]: "",
1439
1479
  [`${keyPrefix}offset`]: "",
1440
1480
  [`${keyPrefix}offset_percentage`]: "",
1481
+ [`${keyPrefix}offset_from_mark`]: "",
1482
+ [`${keyPrefix}offset_percentage_from_mark`]: "",
1441
1483
  [`${keyPrefix}pnl`]: "",
1442
1484
  [`${keyPrefix}ROI`]: "",
1443
1485
  [key]: inputs.value
@@ -1482,6 +1524,8 @@ function tpslCalculateHelper(key, inputs, options = {}) {
1482
1524
  [`${keyPrefix}order_price`]: order_price ? todpIfNeed(order_price, symbol?.quote_dp ?? 2) : "",
1483
1525
  [`${keyPrefix}offset`]: offset ?? "",
1484
1526
  [`${keyPrefix}offset_percentage`]: offset_percentage ?? "",
1527
+ [`${keyPrefix}offset_from_mark`]: offset_from_mark ?? inputs.values[`${keyPrefix}offset_from_mark`] ?? "",
1528
+ [`${keyPrefix}offset_percentage_from_mark`]: offset_percentage_from_mark ?? inputs.values[`${keyPrefix}offset_percentage_from_mark`] ?? "",
1485
1529
  [`${keyPrefix}pnl`]: pnl ?? ""
1486
1530
  // [`${keyPrefix}ROI`]: calcROI({
1487
1531
  // pnl: Number(pnl ?? 0),
@@ -1669,6 +1713,7 @@ function tpslInputHandle(inputs) {
1669
1713
  // order price or mark price
1670
1714
  qty: values.side === OrderSide.BUY ? Number(values.order_quantity) : Number(values.order_quantity) * -1,
1671
1715
  orderSide: values.side,
1716
+ markPrice,
1672
1717
  // values: newValues,
1673
1718
  values
1674
1719
  },
@@ -1704,6 +1749,10 @@ var getCalculateHandler = (fieldName) => {
1704
1749
  case "sl_offset":
1705
1750
  case "tp_offset_percentage":
1706
1751
  case "sl_offset_percentage":
1752
+ case "tp_offset_from_mark":
1753
+ case "sl_offset_from_mark":
1754
+ case "tp_offset_percentage_from_mark":
1755
+ case "sl_offset_percentage_from_mark":
1707
1756
  case "tp_order_price":
1708
1757
  case "tp_order_type":
1709
1758
  case "sl_order_type":
@@ -5118,6 +5167,11 @@ var MarkPriceCalculator = class extends BaseCalculator {
5118
5167
 
5119
5168
  // src/orderly/calculator/calculatorContext.ts
5120
5169
  var CalculatorContext = class _CalculatorContext {
5170
+ // holding: API.Holding[];
5171
+ // portfolio — dynamic getter to always read the latest output from PortfolioCalculator
5172
+ get portfolio() {
5173
+ return this.output["portfolio"] || useAppStore.getState().portfolio;
5174
+ }
5121
5175
  static get instance() {
5122
5176
  return this._instance;
5123
5177
  }
@@ -5136,7 +5190,6 @@ var CalculatorContext = class _CalculatorContext {
5136
5190
  update(scope, data) {
5137
5191
  this.setCtxData();
5138
5192
  this.markPrices = scope === "markPrice" /* MARK_PRICE */ ? data : this.output[MarketCalculatorName];
5139
- this.portfolio = this.output["portfolio"] || useAppStore.getState().portfolio;
5140
5193
  return this;
5141
5194
  }
5142
5195
  setCtxData() {
@@ -5154,7 +5207,6 @@ var CalculatorContext = class _CalculatorContext {
5154
5207
  clearCache() {
5155
5208
  this.output = {};
5156
5209
  this.accountInfo = void 0;
5157
- this.portfolio = void 0;
5158
5210
  }
5159
5211
  deleteByName(name) {
5160
5212
  delete this.output[name];
@@ -11020,7 +11072,11 @@ var tpslFields = [
11020
11072
  "tp_offset",
11021
11073
  "sl_offset",
11022
11074
  "tp_offset_percentage",
11023
- "sl_offset_percentage"
11075
+ "sl_offset_percentage",
11076
+ "tp_offset_from_mark",
11077
+ "sl_offset_from_mark",
11078
+ "tp_offset_percentage_from_mark",
11079
+ "sl_offset_percentage_from_mark"
11024
11080
  ];
11025
11081
  var isBracketOrder = (order) => {
11026
11082
  return !!order.tp_trigger_price || !!order.sl_trigger_price;
@@ -11236,6 +11292,8 @@ var useTaskProfitAndStopLossInternal = (position, options) => {
11236
11292
  entryPrice: position.average_open_price,
11237
11293
  qty: side === OrderSide.BUY ? Number(prev.quantity) : -Number(prev.quantity),
11238
11294
  orderSide: side,
11295
+ markPrice: markPrice ?? position.average_open_price,
11296
+ // use mark price as the default value
11239
11297
  values: prev
11240
11298
  },
11241
11299
  {
@@ -18616,10 +18674,14 @@ var useOrderEntryNextInternal = (symbol, options = {}) => {
18616
18674
  tp_pnl: "",
18617
18675
  tp_offset: "",
18618
18676
  tp_offset_percentage: "",
18677
+ tp_offset_from_mark: "",
18678
+ tp_offset_percentage_from_mark: "",
18619
18679
  sl_trigger_price: "",
18620
18680
  sl_pnl: "",
18621
18681
  sl_offset: "",
18622
- sl_offset_percentage: ""
18682
+ sl_offset_percentage: "",
18683
+ sl_offset_from_mark: "",
18684
+ sl_offset_percentage_from_mark: ""
18623
18685
  }));
18624
18686
  },
18625
18687
  hasTP_SL: () => {
@@ -18710,6 +18772,42 @@ var useOrderEntryNextInternal = (symbol, options = {}) => {
18710
18772
  symbolInfo2
18711
18773
  );
18712
18774
  }
18775
+ if (newValues.tp_offset_from_mark !== void 0 && newValues.tp_offset_from_mark !== "") {
18776
+ newValues = calculate2(
18777
+ newValues,
18778
+ "tp_offset_from_mark",
18779
+ newValues.tp_offset_from_mark,
18780
+ markPrice,
18781
+ symbolInfo2
18782
+ );
18783
+ }
18784
+ if (newValues.tp_offset_percentage_from_mark !== void 0 && newValues.tp_offset_percentage_from_mark !== "") {
18785
+ newValues = calculate2(
18786
+ newValues,
18787
+ "tp_offset_percentage_from_mark",
18788
+ newValues.tp_offset_percentage_from_mark,
18789
+ markPrice,
18790
+ symbolInfo2
18791
+ );
18792
+ }
18793
+ if (newValues.sl_offset_from_mark !== void 0 && newValues.sl_offset_from_mark !== "") {
18794
+ newValues = calculate2(
18795
+ newValues,
18796
+ "sl_offset_from_mark",
18797
+ newValues.sl_offset_from_mark,
18798
+ markPrice,
18799
+ symbolInfo2
18800
+ );
18801
+ }
18802
+ if (newValues.sl_offset_percentage_from_mark !== void 0 && newValues.sl_offset_percentage_from_mark !== "") {
18803
+ newValues = calculate2(
18804
+ newValues,
18805
+ "sl_offset_percentage_from_mark",
18806
+ newValues.sl_offset_percentage_from_mark,
18807
+ markPrice,
18808
+ symbolInfo2
18809
+ );
18810
+ }
18713
18811
  } else {
18714
18812
  if (typeof newValues.tp_trigger_price !== "undefined") {
18715
18813
  newValues = calculate2(
@@ -18729,6 +18827,42 @@ var useOrderEntryNextInternal = (symbol, options = {}) => {
18729
18827
  symbolInfo2
18730
18828
  );
18731
18829
  }
18830
+ if (newValues.tp_offset_from_mark !== void 0 && newValues.tp_offset_from_mark !== "") {
18831
+ newValues = calculate2(
18832
+ newValues,
18833
+ "tp_offset_from_mark",
18834
+ newValues.tp_offset_from_mark,
18835
+ markPrice,
18836
+ symbolInfo2
18837
+ );
18838
+ }
18839
+ if (newValues.tp_offset_percentage_from_mark !== void 0 && newValues.tp_offset_percentage_from_mark !== "") {
18840
+ newValues = calculate2(
18841
+ newValues,
18842
+ "tp_offset_percentage_from_mark",
18843
+ newValues.tp_offset_percentage_from_mark,
18844
+ markPrice,
18845
+ symbolInfo2
18846
+ );
18847
+ }
18848
+ if (newValues.sl_offset_from_mark !== void 0 && newValues.sl_offset_from_mark !== "") {
18849
+ newValues = calculate2(
18850
+ newValues,
18851
+ "sl_offset_from_mark",
18852
+ newValues.sl_offset_from_mark,
18853
+ markPrice,
18854
+ symbolInfo2
18855
+ );
18856
+ }
18857
+ if (newValues.sl_offset_percentage_from_mark !== void 0 && newValues.sl_offset_percentage_from_mark !== "") {
18858
+ newValues = calculate2(
18859
+ newValues,
18860
+ "sl_offset_percentage_from_mark",
18861
+ newValues.sl_offset_percentage_from_mark,
18862
+ markPrice,
18863
+ symbolInfo2
18864
+ );
18865
+ }
18732
18866
  }
18733
18867
  return newValues;
18734
18868
  };
@@ -19746,6 +19880,9 @@ var useTpslPriceChecker = (params) => {
19746
19880
  return null;
19747
19881
  }
19748
19882
  }
19883
+ if (typeof liqPrice === "number" && (!Number.isFinite(liqPrice) || liqPrice <= 0)) {
19884
+ return null;
19885
+ }
19749
19886
  let slPriceDecimal;
19750
19887
  let liqPriceDecimal;
19751
19888
  try {