@orderly.network/hooks 3.0.0-beta.3 → 3.0.0-beta.5

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
@@ -50,9 +50,9 @@ function useMarketCategoriesConfig() {
50
50
  // src/version.ts
51
51
  if (typeof window !== "undefined") {
52
52
  window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};
53
- window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "3.0.0-beta.3";
53
+ window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "3.0.0-beta.5";
54
54
  }
55
- var version_default = "3.0.0-beta.3";
55
+ var version_default = "3.0.0-beta.5";
56
56
  var fetcher = (url, init2 = {}, queryOptions) => get(url, init2, queryOptions?.formatter);
57
57
  var noCacheConfig = {
58
58
  dedupingInterval: 0,
@@ -581,6 +581,20 @@ var useAudioPlayer = (src, options = {}) => {
581
581
  const el = getOrderFilledAudio();
582
582
  el.volume = Math.max(0, Math.min(1, volume));
583
583
  }, [volume]);
584
+ useEffect(() => {
585
+ const handleVisibilityChange = () => {
586
+ if (document.visibilityState === "visible") {
587
+ const el = getOrderFilledAudio();
588
+ el.pause();
589
+ el.removeAttribute("src");
590
+ el.load();
591
+ }
592
+ };
593
+ document.addEventListener("visibilitychange", handleVisibilityChange);
594
+ return () => {
595
+ document.removeEventListener("visibilitychange", handleVisibilityChange);
596
+ };
597
+ }, []);
584
598
  const play = useCallback(() => {
585
599
  const currentSrc = srcRef.current;
586
600
  const currentEnabled = enabledRef.current;
@@ -1290,7 +1304,7 @@ function checkTPSLOrderTypeIsMarket(key, values) {
1290
1304
  }
1291
1305
  function tpslCalculateHelper(key, inputs, options = {}) {
1292
1306
  const { symbol } = options;
1293
- 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") {
1307
+ 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") {
1294
1308
  return {
1295
1309
  [key]: inputs.value
1296
1310
  };
@@ -1298,6 +1312,7 @@ function tpslCalculateHelper(key, inputs, options = {}) {
1298
1312
  const orderType = key.startsWith("tp_") ? AlgoOrderType.TAKE_PROFIT : AlgoOrderType.STOP_LOSS;
1299
1313
  const keyPrefix = key.slice(0, 3);
1300
1314
  const qty = Number(key === "quantity" ? inputs.value : inputs.qty);
1315
+ const markPrice = inputs.markPrice ?? inputs.entryPrice;
1301
1316
  if (qty === 0 && (key === "tp_pnl" || key === "sl_pnl" || key === "tp_trigger_price" || key === "sl_trigger_price")) {
1302
1317
  return {
1303
1318
  // [`${keyPrefix}trigger_price`]: "",
@@ -1308,7 +1323,7 @@ function tpslCalculateHelper(key, inputs, options = {}) {
1308
1323
  [key]: inputs.value
1309
1324
  };
1310
1325
  }
1311
- let trigger_price, offset, offset_percentage, pnl, order_price, tpsl_order_type = inputs.values[`${keyPrefix}order_type`] ?? OrderType.MARKET;
1326
+ 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;
1312
1327
  const entryPrice = new Decimal(inputs.entryPrice).todp(options.symbol?.quote_dp ?? 2, Decimal.ROUND_UP).toNumber();
1313
1328
  switch (key) {
1314
1329
  case "tp_trigger_price":
@@ -1320,6 +1335,8 @@ function tpslCalculateHelper(key, inputs, options = {}) {
1320
1335
  [`${keyPrefix}trigger_price`]: trigger_price,
1321
1336
  [`${keyPrefix}offset`]: "",
1322
1337
  [`${keyPrefix}offset_percentage`]: "",
1338
+ [`${keyPrefix}offset_from_mark`]: "",
1339
+ [`${keyPrefix}offset_percentage_from_mark`]: "",
1323
1340
  [`${keyPrefix}pnl`]: "",
1324
1341
  [`${keyPrefix}ROI`]: ""
1325
1342
  };
@@ -1329,31 +1346,6 @@ function tpslCalculateHelper(key, inputs, options = {}) {
1329
1346
  }
1330
1347
  break;
1331
1348
  }
1332
- // case "tp_enable":
1333
- // case "sl_enable": {
1334
- // return {
1335
- // [`${keyPrefix}enable`]: inputs.value,
1336
- // [`${keyPrefix}order_type`]: OrderType.MARKET,
1337
- // [`${keyPrefix}trigger_price`]: "",
1338
- // [`${keyPrefix}order_price`]: "",
1339
- // [`${keyPrefix}offset`]: "",
1340
- // [`${keyPrefix}offset_percentage`]: "",
1341
- // [`${keyPrefix}pnl`]: "",
1342
- // [`${keyPrefix}ROI`]: "",
1343
- // };
1344
- // }
1345
- // case 'tp_pnl':{
1346
- // if (inputs.values.tp_order_type !== OrderType.MARKET) {
1347
- // pnl = inputs.value;
1348
- // trigger_price = pnlToPrice({
1349
- // qty,
1350
- // pnl: Number(inputs.value),
1351
- // entryPrice,
1352
- // orderSide: inputs.orderSide,
1353
- // orderType,
1354
- // })
1355
- // }
1356
- // }
1357
1349
  case "tp_pnl":
1358
1350
  case "sl_pnl": {
1359
1351
  pnl = inputs.value;
@@ -1400,6 +1392,30 @@ function tpslCalculateHelper(key, inputs, options = {}) {
1400
1392
  }
1401
1393
  break;
1402
1394
  }
1395
+ case "tp_offset_from_mark":
1396
+ case "sl_offset_from_mark": {
1397
+ offset_from_mark = inputs.value;
1398
+ if (!checkTPSLOrderTypeIsMarket(key, inputs.values)) {
1399
+ order_price = offsetToPrice({
1400
+ qty,
1401
+ offset: Number(inputs.value),
1402
+ entryPrice: markPrice,
1403
+ orderSide: inputs.orderSide,
1404
+ orderType: key === "tp_offset_from_mark" ? AlgoOrderType.TAKE_PROFIT : AlgoOrderType.STOP_LOSS
1405
+ });
1406
+ trigger_price = inputs.values[`${keyPrefix}trigger_price`] ?? order_price;
1407
+ } else {
1408
+ trigger_price = offsetToPrice({
1409
+ qty,
1410
+ offset: Number(inputs.value),
1411
+ // entryPrice,
1412
+ entryPrice: markPrice,
1413
+ orderSide: inputs.orderSide,
1414
+ orderType: key === "tp_offset_from_mark" ? AlgoOrderType.TAKE_PROFIT : AlgoOrderType.STOP_LOSS
1415
+ });
1416
+ }
1417
+ break;
1418
+ }
1403
1419
  case "tp_order_price":
1404
1420
  case "sl_order_price": {
1405
1421
  order_price = inputs.value;
@@ -1440,12 +1456,38 @@ function tpslCalculateHelper(key, inputs, options = {}) {
1440
1456
  }
1441
1457
  break;
1442
1458
  }
1459
+ case "tp_offset_percentage_from_mark":
1460
+ case "sl_offset_percentage_from_mark": {
1461
+ offset_percentage_from_mark = inputs.value;
1462
+ const markBase = Number(markPrice);
1463
+ if (!checkTPSLOrderTypeIsMarket(key, inputs.values)) {
1464
+ order_price = offsetPercentageToPrice({
1465
+ qty,
1466
+ percentage: Number(`${inputs.value}`.replace(/\.0{0,2}$/, "")),
1467
+ entryPrice: markBase,
1468
+ orderSide: inputs.orderSide,
1469
+ orderType
1470
+ });
1471
+ trigger_price = inputs.values[`${keyPrefix}trigger_price`] ?? order_price;
1472
+ } else {
1473
+ trigger_price = offsetPercentageToPrice({
1474
+ qty,
1475
+ percentage: Number(`${inputs.value}`.replace(/\.0{0,2}$/, "")),
1476
+ entryPrice: markBase,
1477
+ orderSide: inputs.orderSide,
1478
+ orderType
1479
+ });
1480
+ }
1481
+ break;
1482
+ }
1443
1483
  }
1444
1484
  if (!trigger_price && checkTPSLOrderTypeIsMarket(key, inputs.values)) {
1445
1485
  return {
1446
1486
  [`${keyPrefix}trigger_price`]: "",
1447
1487
  [`${keyPrefix}offset`]: "",
1448
1488
  [`${keyPrefix}offset_percentage`]: "",
1489
+ [`${keyPrefix}offset_from_mark`]: "",
1490
+ [`${keyPrefix}offset_percentage_from_mark`]: "",
1449
1491
  [`${keyPrefix}pnl`]: "",
1450
1492
  [`${keyPrefix}ROI`]: "",
1451
1493
  [key]: inputs.value
@@ -1490,6 +1532,8 @@ function tpslCalculateHelper(key, inputs, options = {}) {
1490
1532
  [`${keyPrefix}order_price`]: order_price ? todpIfNeed(order_price, symbol?.quote_dp ?? 2) : "",
1491
1533
  [`${keyPrefix}offset`]: offset ?? "",
1492
1534
  [`${keyPrefix}offset_percentage`]: offset_percentage ?? "",
1535
+ [`${keyPrefix}offset_from_mark`]: offset_from_mark ?? inputs.values[`${keyPrefix}offset_from_mark`] ?? "",
1536
+ [`${keyPrefix}offset_percentage_from_mark`]: offset_percentage_from_mark ?? inputs.values[`${keyPrefix}offset_percentage_from_mark`] ?? "",
1493
1537
  [`${keyPrefix}pnl`]: pnl ?? ""
1494
1538
  // [`${keyPrefix}ROI`]: calcROI({
1495
1539
  // pnl: Number(pnl ?? 0),
@@ -1677,6 +1721,7 @@ function tpslInputHandle(inputs) {
1677
1721
  // order price or mark price
1678
1722
  qty: values.side === OrderSide.BUY ? Number(values.order_quantity) : Number(values.order_quantity) * -1,
1679
1723
  orderSide: values.side,
1724
+ markPrice,
1680
1725
  // values: newValues,
1681
1726
  values
1682
1727
  },
@@ -1712,6 +1757,10 @@ var getCalculateHandler = (fieldName) => {
1712
1757
  case "sl_offset":
1713
1758
  case "tp_offset_percentage":
1714
1759
  case "sl_offset_percentage":
1760
+ case "tp_offset_from_mark":
1761
+ case "sl_offset_from_mark":
1762
+ case "tp_offset_percentage_from_mark":
1763
+ case "sl_offset_percentage_from_mark":
1715
1764
  case "tp_order_price":
1716
1765
  case "tp_order_type":
1717
1766
  case "sl_order_type":
@@ -3136,7 +3185,9 @@ var DEFAULT_SYMBOL_DEPTHS = {
3136
3185
  };
3137
3186
  var DEFAULT_SYMBOL_DISPLAY_NAMES = {
3138
3187
  PERP_XAU_USDC: "XAU (Gold) ",
3139
- PERP_XAG_USDC: "XAG (Silver)"
3188
+ PERP_XAG_USDC: "XAG (Silver)",
3189
+ PERP_CL_USDC: "CL (WTI Crude Oil)",
3190
+ PERP_BZ_USDC: "BZ (Brent Crude Oil)"
3140
3191
  };
3141
3192
  var ProxyConfigStore = class {
3142
3193
  constructor(_originConfigStore) {
@@ -5124,6 +5175,11 @@ var MarkPriceCalculator = class extends BaseCalculator {
5124
5175
 
5125
5176
  // src/orderly/calculator/calculatorContext.ts
5126
5177
  var CalculatorContext = class _CalculatorContext {
5178
+ // holding: API.Holding[];
5179
+ // portfolio — dynamic getter to always read the latest output from PortfolioCalculator
5180
+ get portfolio() {
5181
+ return this.output["portfolio"] || useAppStore.getState().portfolio;
5182
+ }
5127
5183
  static get instance() {
5128
5184
  return this._instance;
5129
5185
  }
@@ -5142,7 +5198,6 @@ var CalculatorContext = class _CalculatorContext {
5142
5198
  update(scope, data) {
5143
5199
  this.setCtxData();
5144
5200
  this.markPrices = scope === "markPrice" /* MARK_PRICE */ ? data : this.output[MarketCalculatorName];
5145
- this.portfolio = this.output["portfolio"] || useAppStore.getState().portfolio;
5146
5201
  return this;
5147
5202
  }
5148
5203
  setCtxData() {
@@ -5160,7 +5215,6 @@ var CalculatorContext = class _CalculatorContext {
5160
5215
  clearCache() {
5161
5216
  this.output = {};
5162
5217
  this.accountInfo = void 0;
5163
- this.portfolio = void 0;
5164
5218
  }
5165
5219
  deleteByName(name) {
5166
5220
  delete this.output[name];
@@ -11026,7 +11080,11 @@ var tpslFields = [
11026
11080
  "tp_offset",
11027
11081
  "sl_offset",
11028
11082
  "tp_offset_percentage",
11029
- "sl_offset_percentage"
11083
+ "sl_offset_percentage",
11084
+ "tp_offset_from_mark",
11085
+ "sl_offset_from_mark",
11086
+ "tp_offset_percentage_from_mark",
11087
+ "sl_offset_percentage_from_mark"
11030
11088
  ];
11031
11089
  var isBracketOrder = (order) => {
11032
11090
  return !!order.tp_trigger_price || !!order.sl_trigger_price;
@@ -11242,6 +11300,8 @@ var useTaskProfitAndStopLossInternal = (position, options) => {
11242
11300
  entryPrice: position.average_open_price,
11243
11301
  qty: side === OrderSide.BUY ? Number(prev.quantity) : -Number(prev.quantity),
11244
11302
  orderSide: side,
11303
+ markPrice: markPrice ?? position.average_open_price,
11304
+ // use mark price as the default value
11245
11305
  values: prev
11246
11306
  },
11247
11307
  {
@@ -18622,10 +18682,14 @@ var useOrderEntryNextInternal = (symbol, options = {}) => {
18622
18682
  tp_pnl: "",
18623
18683
  tp_offset: "",
18624
18684
  tp_offset_percentage: "",
18685
+ tp_offset_from_mark: "",
18686
+ tp_offset_percentage_from_mark: "",
18625
18687
  sl_trigger_price: "",
18626
18688
  sl_pnl: "",
18627
18689
  sl_offset: "",
18628
- sl_offset_percentage: ""
18690
+ sl_offset_percentage: "",
18691
+ sl_offset_from_mark: "",
18692
+ sl_offset_percentage_from_mark: ""
18629
18693
  }));
18630
18694
  },
18631
18695
  hasTP_SL: () => {
@@ -18716,6 +18780,42 @@ var useOrderEntryNextInternal = (symbol, options = {}) => {
18716
18780
  symbolInfo2
18717
18781
  );
18718
18782
  }
18783
+ if (newValues.tp_offset_from_mark !== void 0 && newValues.tp_offset_from_mark !== "") {
18784
+ newValues = calculate2(
18785
+ newValues,
18786
+ "tp_offset_from_mark",
18787
+ newValues.tp_offset_from_mark,
18788
+ markPrice,
18789
+ symbolInfo2
18790
+ );
18791
+ }
18792
+ if (newValues.tp_offset_percentage_from_mark !== void 0 && newValues.tp_offset_percentage_from_mark !== "") {
18793
+ newValues = calculate2(
18794
+ newValues,
18795
+ "tp_offset_percentage_from_mark",
18796
+ newValues.tp_offset_percentage_from_mark,
18797
+ markPrice,
18798
+ symbolInfo2
18799
+ );
18800
+ }
18801
+ if (newValues.sl_offset_from_mark !== void 0 && newValues.sl_offset_from_mark !== "") {
18802
+ newValues = calculate2(
18803
+ newValues,
18804
+ "sl_offset_from_mark",
18805
+ newValues.sl_offset_from_mark,
18806
+ markPrice,
18807
+ symbolInfo2
18808
+ );
18809
+ }
18810
+ if (newValues.sl_offset_percentage_from_mark !== void 0 && newValues.sl_offset_percentage_from_mark !== "") {
18811
+ newValues = calculate2(
18812
+ newValues,
18813
+ "sl_offset_percentage_from_mark",
18814
+ newValues.sl_offset_percentage_from_mark,
18815
+ markPrice,
18816
+ symbolInfo2
18817
+ );
18818
+ }
18719
18819
  } else {
18720
18820
  if (typeof newValues.tp_trigger_price !== "undefined") {
18721
18821
  newValues = calculate2(
@@ -18735,6 +18835,42 @@ var useOrderEntryNextInternal = (symbol, options = {}) => {
18735
18835
  symbolInfo2
18736
18836
  );
18737
18837
  }
18838
+ if (newValues.tp_offset_from_mark !== void 0 && newValues.tp_offset_from_mark !== "") {
18839
+ newValues = calculate2(
18840
+ newValues,
18841
+ "tp_offset_from_mark",
18842
+ newValues.tp_offset_from_mark,
18843
+ markPrice,
18844
+ symbolInfo2
18845
+ );
18846
+ }
18847
+ if (newValues.tp_offset_percentage_from_mark !== void 0 && newValues.tp_offset_percentage_from_mark !== "") {
18848
+ newValues = calculate2(
18849
+ newValues,
18850
+ "tp_offset_percentage_from_mark",
18851
+ newValues.tp_offset_percentage_from_mark,
18852
+ markPrice,
18853
+ symbolInfo2
18854
+ );
18855
+ }
18856
+ if (newValues.sl_offset_from_mark !== void 0 && newValues.sl_offset_from_mark !== "") {
18857
+ newValues = calculate2(
18858
+ newValues,
18859
+ "sl_offset_from_mark",
18860
+ newValues.sl_offset_from_mark,
18861
+ markPrice,
18862
+ symbolInfo2
18863
+ );
18864
+ }
18865
+ if (newValues.sl_offset_percentage_from_mark !== void 0 && newValues.sl_offset_percentage_from_mark !== "") {
18866
+ newValues = calculate2(
18867
+ newValues,
18868
+ "sl_offset_percentage_from_mark",
18869
+ newValues.sl_offset_percentage_from_mark,
18870
+ markPrice,
18871
+ symbolInfo2
18872
+ );
18873
+ }
18738
18874
  }
18739
18875
  return newValues;
18740
18876
  };
@@ -19714,6 +19850,7 @@ var useTpslPriceChecker = (params) => {
19714
19850
  slPrice,
19715
19851
  liqPrice,
19716
19852
  side,
19853
+ markPrice,
19717
19854
  currentPosition,
19718
19855
  orderQuantity
19719
19856
  } = params;
@@ -19769,6 +19906,17 @@ var useTpslPriceChecker = (params) => {
19769
19906
  if (slPrice === void 0 || liqPrice === void 0 || side === void 0 || liqPrice === null) {
19770
19907
  return null;
19771
19908
  }
19909
+ if (markPrice != null && Number.isFinite(markPrice) && markPrice > 0) {
19910
+ if (side === OrderSide.BUY && liqPrice > markPrice) {
19911
+ return null;
19912
+ }
19913
+ if (side === OrderSide.SELL && liqPrice < markPrice) {
19914
+ return null;
19915
+ }
19916
+ }
19917
+ if (typeof liqPrice === "number" && (!Number.isFinite(liqPrice) || liqPrice <= 0)) {
19918
+ return null;
19919
+ }
19772
19920
  let slPriceDecimal;
19773
19921
  let liqPriceDecimal;
19774
19922
  try {
@@ -19795,6 +19943,7 @@ var useTpslPriceChecker = (params) => {
19795
19943
  slPrice,
19796
19944
  liqPrice,
19797
19945
  side,
19946
+ markPrice,
19798
19947
  warning_threshold,
19799
19948
  currentPosition,
19800
19949
  orderQuantity