dexbot 1.6.12 → 1.6.14

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.
Files changed (213) hide show
  1. package/CHANGELOG.md +65 -1
  2. package/README.md +2 -2
  3. package/analysis/README.md +9 -11
  4. package/analysis/ama_fitting/README.md +5 -3
  5. package/claw/README.md +1 -0
  6. package/claw/docs/AI_BOT_LIBRARY_API.md +3 -1
  7. package/claw/docs/DEXBOT2_TUNING_CHEAT_SHEET.md +4 -4
  8. package/claw/modules/claw_bridge.ts +0 -3
  9. package/claw/modules/claw_catalog.ts +8 -4
  10. package/claw/modules/claw_skill_md.ts +1 -0
  11. package/claw/modules/dexbot_profiles.ts +59 -15
  12. package/claw/modules/types.ts +2 -1
  13. package/claw/package.json +3 -3
  14. package/claw/runtimes/openclaw-plugin/openclaw.plugin.json +1 -1
  15. package/claw/runtimes/openclaw-plugin/package.json +1 -1
  16. package/claw/skills/margin-trading/SKILL.md +1 -1
  17. package/claw/skills/margin-trading/references/position-management.md +1 -1
  18. package/claw/tsconfig.json +2 -2
  19. package/dist/analysis/ama_fitting/analyze_lambda_vs_slow.js +1 -14
  20. package/dist/analysis/ama_fitting/fetch_lp_candles.js +51 -41
  21. package/dist/analysis/ama_fitting/generate_unified_comparison_chart.d.ts +1 -24
  22. package/dist/analysis/ama_fitting/generate_unified_comparison_chart.js +36 -18
  23. package/dist/analysis/ama_fitting/optimizer_high_resolution.js +18 -66
  24. package/dist/analysis/analyze_args.d.ts +12 -0
  25. package/dist/analysis/analyze_args.js +20 -0
  26. package/dist/analysis/bot_fitting/backtest_ama_sweep.d.ts +1 -16
  27. package/dist/analysis/bot_fitting/backtest_ama_sweep.js +20 -101
  28. package/dist/analysis/bot_fitting/backtest_bot_fitting.js +26 -92
  29. package/dist/analysis/bot_fitting/backtest_lookback_drawdown.js +19 -34
  30. package/dist/analysis/bot_fitting/shared_utils.d.ts +71 -3
  31. package/dist/analysis/bot_fitting/shared_utils.js +124 -8
  32. package/dist/analysis/chart_ui.d.ts +30 -1
  33. package/dist/analysis/chart_ui.js +37 -1
  34. package/dist/analysis/fills_cache.d.ts +3 -23
  35. package/dist/analysis/fills_cache.js +3 -84
  36. package/dist/analysis/fills_source.d.ts +31 -1
  37. package/dist/analysis/fills_source.js +69 -1
  38. package/dist/analysis/grid_correction_check.d.ts +1 -23
  39. package/dist/analysis/grid_correction_check.js +15 -58
  40. package/dist/analysis/math_utils.d.ts +85 -1
  41. package/dist/analysis/math_utils.js +116 -1
  42. package/dist/analysis/price_sources.d.ts +22 -1
  43. package/dist/analysis/price_sources.js +23 -0
  44. package/dist/analysis/resolve_source.js +31 -10
  45. package/dist/analysis/trade_profitability.d.ts +6 -0
  46. package/dist/analysis/trade_profitability.js +23 -97
  47. package/dist/analysis/tradingview/analyze_tradingview.d.ts +1 -17
  48. package/dist/analysis/tradingview/analyze_tradingview.js +1 -1
  49. package/dist/analysis/tradingview/tradingview_uplot_chart_generator.js +27 -40
  50. package/dist/analysis/trend_detection/backtest_ama_slope_huber.d.ts +1 -101
  51. package/dist/analysis/trend_detection/backtest_ama_slope_huber.js +22 -80
  52. package/dist/analysis/trend_detection/dynamic_weight_chart_generator.js +11 -37
  53. package/dist/analysis/trend_detection/volatility_chart_generator.js +9 -10
  54. package/dist/analysis/worker_pool.d.ts +38 -0
  55. package/dist/analysis/worker_pool.js +67 -0
  56. package/dist/dexbot.js +67 -48
  57. package/dist/market_adapter/ama_signal_runner.js +21 -54
  58. package/dist/market_adapter/candle_utils.d.ts +7 -1
  59. package/dist/market_adapter/candle_utils.js +11 -1
  60. package/dist/market_adapter/cli_args.d.ts +22 -0
  61. package/dist/market_adapter/cli_args.js +30 -0
  62. package/dist/market_adapter/core/config_normalizers.js +11 -6
  63. package/dist/market_adapter/core/kibana_candles.d.ts +3 -1
  64. package/dist/market_adapter/core/kibana_candles.js +3 -2
  65. package/dist/market_adapter/core/market_adapter_service.d.ts +85 -85
  66. package/dist/market_adapter/core/market_adapter_service.js +93 -158
  67. package/dist/market_adapter/core/signals/hurst_analyzer.d.ts +1 -6
  68. package/dist/market_adapter/core/signals/hurst_analyzer.js +1 -1
  69. package/dist/market_adapter/core/signals/permutation_entropy_analyzer.d.ts +1 -11
  70. package/dist/market_adapter/core/signals/permutation_entropy_analyzer.js +1 -1
  71. package/dist/market_adapter/inputs/fetch_book_data.js +0 -1
  72. package/dist/market_adapter/inputs/fetch_cex_synthetic_data.js +63 -107
  73. package/dist/market_adapter/inputs/fetch_lp_data.d.ts +8 -1
  74. package/dist/market_adapter/inputs/fetch_lp_data.js +25 -7
  75. package/dist/market_adapter/inputs/kibana_feed_source.js +4 -26
  76. package/dist/market_adapter/inputs/window_cache.d.ts +4 -72
  77. package/dist/market_adapter/inputs/window_cache.js +52 -177
  78. package/dist/market_adapter/interval_utils.d.ts +11 -1
  79. package/dist/market_adapter/interval_utils.js +16 -1
  80. package/dist/market_adapter/lp_chart_runner.d.ts +3 -0
  81. package/dist/market_adapter/lp_chart_runner.js +37 -19
  82. package/dist/market_adapter/market_adapter.js +27 -72
  83. package/dist/market_adapter/utils/data_discovery.d.ts +45 -1
  84. package/dist/market_adapter/utils/data_discovery.js +220 -27
  85. package/dist/market_adapter/utils/month_shards.d.ts +84 -0
  86. package/dist/market_adapter/utils/month_shards.js +237 -0
  87. package/dist/modules/account_bots.d.ts +8 -6
  88. package/dist/modules/account_bots.js +112 -131
  89. package/dist/modules/account_orders.d.ts +49 -4
  90. package/dist/modules/account_orders.js +114 -49
  91. package/dist/modules/bitshares-native/chain_client.d.ts +1 -1
  92. package/dist/modules/bitshares-native/chain_client.js +14 -13
  93. package/dist/modules/bitshares-native/crypto/ecc_core.d.ts +1 -2
  94. package/dist/modules/bitshares-native/crypto/ecc_core.js +1 -1
  95. package/dist/modules/bitshares-native/resolvers.js +22 -25
  96. package/dist/modules/bitshares-native/serial/chain_constants.d.ts +1 -2
  97. package/dist/modules/bitshares-native/serial/chain_constants.js +1 -2
  98. package/dist/modules/bitshares-native/serial/types.js +22 -50
  99. package/dist/modules/bitshares-native/signing_client.d.ts +1 -1
  100. package/dist/modules/bot_defaults.d.ts +18 -3
  101. package/dist/modules/bot_defaults.js +34 -8
  102. package/dist/modules/bot_settings.d.ts +5 -1
  103. package/dist/modules/bot_settings.js +55 -21
  104. package/dist/modules/broadcast_failure.d.ts +2 -1
  105. package/dist/modules/chain_orders.d.ts +1 -1
  106. package/dist/modules/chain_orders.js +51 -96
  107. package/dist/modules/cli_start_onboarding.d.ts +2 -1
  108. package/dist/modules/config.js +6 -1
  109. package/dist/modules/constants.d.ts +14 -6
  110. package/dist/modules/constants.js +32 -6
  111. package/dist/modules/cr_planner.d.ts +1 -1
  112. package/dist/modules/cr_planner.js +1 -5
  113. package/dist/modules/credit_pricing.d.ts +2 -1
  114. package/dist/modules/credit_pricing.js +36 -40
  115. package/dist/modules/credit_runtime.d.ts +7 -0
  116. package/dist/modules/credit_runtime.js +45 -73
  117. package/dist/modules/daemon_node_health.d.ts +4 -3
  118. package/dist/modules/dexbot_class.d.ts +33 -17
  119. package/dist/modules/dexbot_class.js +113 -61
  120. package/dist/modules/dexbot_cow_runtime.d.ts +62 -181
  121. package/dist/modules/dexbot_cow_runtime.js +380 -69
  122. package/dist/modules/dexbot_credential_client.d.ts +1 -23
  123. package/dist/modules/dexbot_credential_client.js +2 -3
  124. package/dist/modules/dexbot_fill_runtime.d.ts +2 -3
  125. package/dist/modules/dexbot_fill_runtime.js +17 -7
  126. package/dist/modules/dexbot_maintenance_runtime.d.ts +36 -81
  127. package/dist/modules/dexbot_maintenance_runtime.js +137 -133
  128. package/dist/modules/dexbot_startup_runtime.d.ts +0 -1
  129. package/dist/modules/dexbot_startup_runtime.js +25 -14
  130. package/dist/modules/dexbot_state_recovery.js +16 -0
  131. package/dist/modules/general_settings.d.ts +1 -1
  132. package/dist/modules/graceful_shutdown.js +17 -11
  133. package/dist/modules/key_store.d.ts +0 -5
  134. package/dist/modules/key_store.js +1 -2
  135. package/dist/modules/lazy_shim.d.ts +11 -0
  136. package/dist/modules/lazy_shim.js +16 -0
  137. package/dist/modules/node_connect_policy.d.ts +3 -2
  138. package/dist/modules/node_failure_ledger.d.ts +6 -5
  139. package/dist/modules/order/accounting.js +11 -11
  140. package/dist/modules/order/export.js +2 -2
  141. package/dist/modules/order/format.d.ts +26 -3
  142. package/dist/modules/order/format.js +33 -3
  143. package/dist/modules/order/genesis_policy.d.ts +4 -4
  144. package/dist/modules/order/genesis_policy.js +6 -3
  145. package/dist/modules/order/grid.d.ts +26 -12
  146. package/dist/modules/order/grid.js +468 -80
  147. package/dist/modules/order/grid_reconcile.d.ts +1 -1
  148. package/dist/modules/order/grid_reconcile.js +5 -5
  149. package/dist/modules/order/grid_reconcile_internal.js +48 -40
  150. package/dist/modules/order/index.d.ts +35 -8
  151. package/dist/modules/order/logger.d.ts +1 -8
  152. package/dist/modules/order/logger.js +2 -11
  153. package/dist/modules/order/manager.d.ts +18 -15
  154. package/dist/modules/order/manager.js +91 -74
  155. package/dist/modules/order/strategy.d.ts +2 -2
  156. package/dist/modules/order/strategy.js +51 -21
  157. package/dist/modules/order/sync_engine.js +4 -0
  158. package/dist/modules/order/utils/math.d.ts +3 -3
  159. package/dist/modules/order/utils/math.js +38 -27
  160. package/dist/modules/order/utils/order.d.ts +58 -58
  161. package/dist/modules/order/utils/order.js +158 -137
  162. package/dist/modules/order/utils/system.d.ts +202 -35
  163. package/dist/modules/order/utils/system.js +409 -31
  164. package/dist/modules/order/utils/validate.js +28 -6
  165. package/dist/modules/order/utils/withPoolRef.d.ts +2 -1
  166. package/dist/modules/paths.js +32 -34
  167. package/dist/modules/runtime_settings.d.ts +3 -5
  168. package/dist/modules/runtime_settings.js +3 -3
  169. package/dist/modules/settings_merge.d.ts +2 -2
  170. package/dist/modules/socket_json_client.d.ts +2 -1
  171. package/dist/modules/types.d.ts +89 -30
  172. package/dist/modules/utils/errors.js +1 -3
  173. package/dist/modules/version_notice.d.ts +11 -10
  174. package/dist/pm2.d.ts +0 -1
  175. package/dist/pm2.js +56 -35
  176. package/dist/scripts/analyze-git.js +2 -15
  177. package/dist/scripts/analyze-orders.d.ts +1 -1
  178. package/dist/scripts/analyze-orders.js +4 -4
  179. package/dist/scripts/chart_command.js +2 -2
  180. package/dist/scripts/check_any.d.ts +4 -0
  181. package/dist/scripts/check_any.js +53 -21
  182. package/dist/scripts/diagnose-pool-history.js +1 -6
  183. package/dist/scripts/divergence-calc.js +6 -6
  184. package/dist/scripts/run-tests.js +0 -1
  185. package/dist/scripts/sync-version.js +0 -10
  186. package/dist/scripts/validate_bots.d.ts +1 -1
  187. package/dist/scripts/validate_bots.js +3 -3
  188. package/dist/unlock.js +7 -2
  189. package/docs/BITSHARES_ONBOARDING.md +8 -12
  190. package/docs/COPY_ON_WRITE_MASTER_PLAN.md +2 -3
  191. package/docs/COW_INVARIANTS.md +6 -6
  192. package/docs/DEXBOT_COMPARISON.md +7 -7
  193. package/docs/EVOLUTION.md +10 -8
  194. package/docs/FUND_MOVEMENT_AND_ACCOUNTING.md +8 -6
  195. package/docs/GRID_PRICE_INVARIANT.md +3 -3
  196. package/docs/GRID_RECALCULATION.md +4 -1
  197. package/docs/GRID_RECONCILE.md +4 -4
  198. package/docs/LIFECYCLE.md +11 -11
  199. package/docs/LOGGING.md +14 -0
  200. package/docs/MPA_CREDIT_USAGE.md +1 -1
  201. package/docs/ORDER_ENGINE_POST_1.0_RETROSPECTIVE.md +12 -4
  202. package/docs/README.md +3 -3
  203. package/docs/WORKFLOW.md +1 -0
  204. package/docs/architecture.md +25 -16
  205. package/docs/developer_guide.md +7 -2
  206. package/market_adapter/README.md +6 -4
  207. package/package.json +6 -9
  208. package/scripts/README.md +43 -38
  209. package/tests/README.md +26 -11
  210. package/dist/scripts/diagnose-kibana-candles.d.ts +0 -3
  211. package/dist/scripts/diagnose-kibana-candles.js +0 -82
  212. package/scripts/git-viewer.sh +0 -489
  213. package/scripts/postinstall.js +0 -16
@@ -30,22 +30,6 @@ declare function parseArgs(): {
30
30
  quiet: boolean;
31
31
  listBots: boolean;
32
32
  };
33
- declare function loadJsonMeta(filePath: string | null | undefined): import("../math_utils.js").CandleFile | {
34
- meta: null;
35
- candles: null;
36
- };
37
- declare function inferTitle(meta: {
38
- pool?: unknown;
39
- intervalSeconds?: unknown;
40
- assetA?: {
41
- symbol?: unknown;
42
- id?: unknown;
43
- };
44
- assetB?: {
45
- symbol?: unknown;
46
- id?: unknown;
47
- };
48
- } | null | undefined, fallback: string): string;
49
33
  declare function main(): Promise<void>;
50
- export { main, parseArgs, loadJsonMeta, inferTitle };
34
+ export { main, parseArgs };
51
35
  //# sourceMappingURL=analyze_tradingview.d.ts.map
@@ -324,5 +324,5 @@ async function main() {
324
324
  }
325
325
  }
326
326
  main().catch((err) => { console.error(err); process.exit(1); });
327
- export { main, parseArgs, loadJsonMeta, inferTitle };
327
+ export { main, parseArgs };
328
328
  //# sourceMappingURL=analyze_tradingview.js.map
@@ -3,7 +3,7 @@ import fs from 'node:fs';
3
3
  import { MARKET_ADAPTER } from '../../modules/constants.js';
4
4
  import { escapeHtml, serializeJsonForScript, UPLOT_SHARED_SCRIPT, embedFunctionSources, uplotInlineTags } from '../chart_utils.js';
5
5
  import { cursorCSS, uplotBgCSS } from '../chart_css.js';
6
- import { zoomResetScript } from '../chart_ui.js';
6
+ import { zoomResetScript, formatTimeLabelFn } from '../chart_ui.js';
7
7
  import { normalizeCandle } from '../math_utils.js';
8
8
  import { PATHS } from '../../modules/paths.js';
9
9
  import { getStorage } from '../../modules/storage/index.js';
@@ -1004,20 +1004,7 @@ function generateHTML(data, title = 'TradingView Style Research') {
1004
1004
  const d = new Date(ts * 1000);
1005
1005
  return d.toLocaleString(undefined, { year: 'numeric', month: 'short', day: '2-digit', hour: '2-digit', minute: '2-digit' });
1006
1006
  }
1007
- function formatTimeLabel(tsSec, spanSec) {
1008
- const d = new Date(tsSec * 1000);
1009
- if (!Number.isFinite(spanSec)) spanSec = 0;
1010
- if (spanSec >= 365 * 24 * 3600 * 2) {
1011
- return String(d.getUTCFullYear());
1012
- }
1013
- if (spanSec >= 90 * 24 * 3600) {
1014
- return d.toLocaleString(undefined, { month: 'short', year: 'numeric', timeZone: 'UTC' });
1015
- }
1016
- if (spanSec >= 14 * 24 * 3600) {
1017
- return d.toLocaleString(undefined, { month: 'short', day: '2-digit', timeZone: 'UTC' });
1018
- }
1019
- return d.toLocaleString(undefined, { month: 'short', day: '2-digit', hour: '2-digit', minute: '2-digit', hour12: false, timeZone: 'UTC' });
1020
- }
1007
+ ${formatTimeLabelFn({ dateTimeSep: ', ', padDay: true })}
1021
1008
  function makeTimeAxis(showLabels) {
1022
1009
  return {
1023
1010
  show: true,
@@ -2650,8 +2637,8 @@ function generateHTML(data, title = 'TradingView Style Research') {
2650
2637
  // Spread = best buy/best sell gap.
2651
2638
  // Pair-aware: order prices invert (1/p) with the candles so B/A shows
2652
2639
  // the same levels in display units (side colors preserved).
2653
- let reserveLine = null;
2654
- let reserveLabel = null;
2640
+ let edgeLine = null;
2641
+ let edgeLabel = null;
2655
2642
  let ceilingLine = null;
2656
2643
  let ceilingLabel = null;
2657
2644
  let spreadMidLabel = null;
@@ -2683,7 +2670,7 @@ function generateHTML(data, title = 'TradingView Style Research') {
2683
2670
  return (overRect.top - rootRect.top) + (1 - frac) * overRect.height;
2684
2671
  }
2685
2672
  function hideOverlayNodes() {
2686
- [reserveLine, reserveLabel, ceilingLine, ceilingLabel, spreadMidLabel, orderBuyLabel, orderSellLabel].forEach((n) => { if (n) n.style.display = 'none'; });
2673
+ [edgeLine, edgeLabel, ceilingLine, ceilingLabel, spreadMidLabel, orderBuyLabel, orderSellLabel].forEach((n) => { if (n) n.style.display = 'none'; });
2687
2674
  orderLineDivs.forEach((d) => { d.style.display = 'none'; });
2688
2675
  orderPriceTags.forEach((t) => { t.style.display = 'none'; });
2689
2676
  }
@@ -2691,15 +2678,15 @@ function generateHTML(data, title = 'TradingView Style Research') {
2691
2678
  if (!u || !u.over || !currentCandles.length) return;
2692
2679
  if (!currentOrdersVisible || (!hasOrders && !hasBounds)) { hideOverlayNodes(); return; }
2693
2680
  try { if (getComputedStyle(u.root).position === 'static') u.root.style.position = 'relative'; } catch (e) {}
2694
- if (!reserveLine || reserveLine.parentNode !== u.root) {
2695
- if (reserveLine && reserveLine.parentNode) reserveLine.parentNode.removeChild(reserveLine);
2696
- if (reserveLabel && reserveLabel.parentNode) reserveLabel.parentNode.removeChild(reserveLabel);
2697
- reserveLine = document.createElement('div');
2698
- reserveLine.style.cssText = 'position:absolute;z-index:1;pointer-events:none;height:0;border-top:2px dashed #f97316;opacity:0.7;left:0;right:0;display:none;';
2699
- reserveLabel = document.createElement('div');
2700
- reserveLabel.style.cssText = 'position:absolute;z-index:25;pointer-events:none;font:600 10px Segoe UI, sans-serif;line-height:15px;height:15px;padding:0 5px;border-radius:3px;color:#f97316;background:rgba(30,41,59,0.9);white-space:nowrap;display:none;';
2701
- u.root.appendChild(reserveLine);
2702
- u.root.appendChild(reserveLabel);
2681
+ if (!edgeLine || edgeLine.parentNode !== u.root) {
2682
+ if (edgeLine && edgeLine.parentNode) edgeLine.parentNode.removeChild(edgeLine);
2683
+ if (edgeLabel && edgeLabel.parentNode) edgeLabel.parentNode.removeChild(edgeLabel);
2684
+ edgeLine = document.createElement('div');
2685
+ edgeLine.style.cssText = 'position:absolute;z-index:1;pointer-events:none;height:0;border-top:2px dashed #f97316;opacity:0.7;left:0;right:0;display:none;';
2686
+ edgeLabel = document.createElement('div');
2687
+ edgeLabel.style.cssText = 'position:absolute;z-index:25;pointer-events:none;font:600 10px Segoe UI, sans-serif;line-height:15px;height:15px;padding:0 5px;border-radius:3px;color:#f97316;background:rgba(30,41,59,0.9);white-space:nowrap;display:none;';
2688
+ u.root.appendChild(edgeLine);
2689
+ u.root.appendChild(edgeLabel);
2703
2690
  }
2704
2691
  // Grid bounds span the full DEXBot grid (live + virtual slots
2705
2692
  // in base units), never calculated: bottom = lowest grid buy,
@@ -2712,7 +2699,7 @@ function generateHTML(data, title = 'TradingView Style Research') {
2712
2699
  const gridLow = Number.isFinite(rawLow) && rawLow > 0 ? rawLow : null;
2713
2700
  const gridHigh = Number.isFinite(rawHigh) && rawHigh > 0 ? rawHigh : null;
2714
2701
  const inversePairs = normalizePairMode(currentPairMode) === 'inverse';
2715
- const reservePrice = inversePairs
2702
+ const edgePrice = inversePairs
2716
2703
  ? (gridHigh != null ? 1 / gridHigh : null)
2717
2704
  : gridLow;
2718
2705
  const ceilingPrice = inversePairs
@@ -2721,20 +2708,20 @@ function generateHTML(data, title = 'TradingView Style Research') {
2721
2708
  const ys = u.scales.y || {};
2722
2709
  const rootRect = u.root.getBoundingClientRect();
2723
2710
  const overRect = u.over.getBoundingClientRect();
2724
- const y = reservePrice != null ? yForPriceCached(ys, overRect, rootRect, reservePrice) : null;
2711
+ const y = edgePrice != null ? yForPriceCached(ys, overRect, rootRect, edgePrice) : null;
2725
2712
  if (y == null) {
2726
- reserveLine.style.display = 'none';
2727
- reserveLabel.style.display = 'none';
2713
+ edgeLine.style.display = 'none';
2714
+ edgeLabel.style.display = 'none';
2728
2715
  } else {
2729
- reserveLine.style.display = 'block';
2730
- reserveLine.style.top = y + 'px';
2716
+ edgeLine.style.display = 'block';
2717
+ edgeLine.style.top = y + 'px';
2731
2718
  const lastClose = currentCandles[currentCandles.length - 1].close;
2732
- const dipRaw = Number.isFinite(lastClose) && lastClose > 0 ? (lastClose - reservePrice) / lastClose * 100 : NaN;
2719
+ const dipRaw = Number.isFinite(lastClose) && lastClose > 0 ? (lastClose - edgePrice) / lastClose * 100 : NaN;
2733
2720
  const dipTxt = Number.isFinite(dipRaw) ? (dipRaw >= 0 ? '-' : '+') + Math.abs(Math.round(dipRaw)) + '%' : '-';
2734
- reserveLabel.style.display = 'block';
2735
- reserveLabel.textContent = 'reserve ' + fmtPriceLabel(reservePrice) + ' (' + dipTxt + ')';
2736
- reserveLabel.style.left = '8px';
2737
- reserveLabel.style.top = (y - 16) + 'px';
2721
+ edgeLabel.style.display = 'block';
2722
+ edgeLabel.textContent = 'reserve ' + fmtPriceLabel(edgePrice) + ' (' + dipTxt + ')';
2723
+ edgeLabel.style.left = '8px';
2724
+ edgeLabel.style.top = (y - 16) + 'px';
2738
2725
  }
2739
2726
  if (!ceilingLine || ceilingLine.parentNode !== u.root) {
2740
2727
  if (ceilingLine && ceilingLine.parentNode) ceilingLine.parentNode.removeChild(ceilingLine);
@@ -2891,8 +2878,8 @@ function generateHTML(data, title = 'TradingView Style Research') {
2891
2878
  }
2892
2879
  }
2893
2880
  function resetOverlayNodes() {
2894
- reserveLine = null;
2895
- reserveLabel = null;
2881
+ edgeLine = null;
2882
+ edgeLabel = null;
2896
2883
  ceilingLine = null;
2897
2884
  ceilingLabel = null;
2898
2885
  spreadMidLabel = null;
@@ -1,102 +1,2 @@
1
- import { type HuberScaleMode, type HuberEstimator } from './huber_scale_variants.js';
2
- type Candle = {
3
- time: number;
4
- open: number;
5
- high: number;
6
- low: number;
7
- close: number;
8
- volume: number;
9
- };
10
- type AmaDef = {
11
- name: string;
12
- er: number;
13
- fast: number;
14
- slow: number;
15
- };
16
- declare function parseArgs(): {
17
- dataPath: string;
18
- amaName: string;
19
- er: number | null;
20
- fast: number | null;
21
- slow: number | null;
22
- lookbacks: number[];
23
- priceThresholdPct: number;
24
- slopeThresholdFactor: number;
25
- slopeEnabled: boolean;
26
- slopePersistBars: number;
27
- confirmFraction: number;
28
- whipsawBars: number;
29
- scaleMode: HuberScaleMode;
30
- truthWindowBars: number | null;
31
- forwardWindowBars: number | null;
32
- outPath: string | null;
33
- };
34
- declare function loadCandles(input: string): {
35
- candles: Candle[];
36
- meta: Record<string, unknown> | null;
37
- files: number;
38
- };
39
- /**
40
- * Cross-correlation peak lag between the causal signal and a reference.
41
- * Returns the smallest non-negative k maximising corr(sig[t], ref[t-k]).
42
- * A positive lag means `sig` trails `ref` by k bars (the smoother's group delay).
43
- */
44
- declare function crossCorrelationLag(sig: (number | null)[], ref: (number | null)[], maxLag: number): {
45
- lagBars: number;
46
- corr: number | null;
47
- };
48
- /**
49
- * Median bars between a sign flip in the centred reference slope and the first
50
- * bar the causal slope confirms the new direction at or above `confirmPct`.
51
- * `misses` counts flips the causal slope never confirmed inside the window.
52
- */
53
- declare function reversalLag(sig: (number | null)[], ref: (number | null)[], maxLag: number, confirmPct: number): {
54
- lagBars: number | null;
55
- flips: number;
56
- misses: number;
57
- samples: number;
58
- };
59
- declare function analyzeLookback(closes: number[], amaValues: number[], lookbackBars: number, amaDef: AmaDef, cfg: ReturnType<typeof parseArgs>, truthArr: (number | null)[], priceTruthArr: (number | null)[], truthHalf: number, estimator?: HuberEstimator): {
60
- lookbackBars: number;
61
- huberScaleMode: HuberScaleMode;
62
- meanOutlierFractionPct: number | null;
63
- meanHuberScale: number | null;
64
- priceLagBars: number;
65
- priceLagCorr: number | null;
66
- amaLagBars: number;
67
- amaLagCorr: number | null;
68
- reversalLagBars: number | null;
69
- reversalFlips: number;
70
- reversalMisses: number;
71
- resets: number;
72
- resetsTotal: number;
73
- priceResets: number;
74
- slopeResets: number;
75
- resetsPerDay: number;
76
- avgBarsBetweenResets: number | null;
77
- medianBarsBetweenResets: number | null;
78
- p90BarsBetweenResets: number | null;
79
- minBarsBetweenResets: number | null;
80
- whipsawResets: number;
81
- whipsawPct: number;
82
- gridAgeSinceLastResetBars: number | null;
83
- meanAbsSlopePct: number;
84
- slopeStdPct: number;
85
- slopeWobblePct: number;
86
- secondDiffEnergy: number;
87
- zeroCrossPer1k: number;
88
- saturationPct: number;
89
- neutralPct: number;
90
- avgAbsRangeOffset: number;
91
- offsetActivePct: number;
92
- forwardWindowBars: number;
93
- rangeWrongWayPct: number | null;
94
- rangeTiltActivePct: number | null;
95
- meanWrongStreakBars: number | null;
96
- medianAdverseMoveWhileWrongPct: number | null;
97
- p90AdverseMoveWhileWrongPct: number | null;
98
- warmupBars: number;
99
- finiteSlopeBars: number;
100
- };
101
- export { loadCandles, crossCorrelationLag, reversalLag, analyzeLookback };
1
+ export {};
102
2
  //# sourceMappingURL=backtest_ama_slope_huber.d.ts.map
@@ -42,12 +42,13 @@ import { calculateAMA, getAmaWarmupBars } from '../../market_adapter/core/strate
42
42
  import { computeAmaSlopeClipThreshold } from '../../market_adapter/core/strategies/dynamic_weight_series.js';
43
43
  import { createHuberEstimator } from './huber_scale_variants.js';
44
44
  import { simulateGridResetSeries, GRID_RESET_BOOTSTRAP } from '../tradingview/grid_reset_sim.js';
45
- import { normalizeCandle, range } from '../math_utils.js';
45
+ import { range, median, percentile, fmtNum as fmt, loadCandleSeries } from '../math_utils.js';
46
46
  import { parseListOrRange } from '../bot_fitting/shared_utils.js';
47
+ import { printHelpHeader, helpRow } from '../analyze_args.js';
47
48
  import { getStorage } from '../../modules/storage/index.js';
48
49
  import { PATHS } from '../../modules/paths.js';
49
50
  import { MARKET_ADAPTER } from '../../modules/constants.js';
50
- const { readJSON, writeJSON } = getStorage();
51
+ const { writeJSON } = getStorage();
51
52
  const MA = MARKET_ADAPTER;
52
53
  // ─── CLI ─────────────────────────────────────────────────────────────────────
53
54
  /**
@@ -175,25 +176,21 @@ function parseArgs() {
175
176
  return out;
176
177
  }
177
178
  function printHelp() {
178
- console.log('AMA-Slope-Huber lookback backtest');
179
- console.log('');
180
- console.log('Usage: node dist/analysis/trend_detection/backtest_ama_slope_huber.js [options]');
181
- console.log('');
182
- console.log('Options:');
183
- console.log(' --data <path> LP candle file or shard directory (required)');
184
- console.log(` --ama <AMA1..AMA4> Built-in AMA preset (default: ${MA.DEFAULT_AMA_KEY})`);
185
- console.log(' --er/--fast/--slow <n> Override AMA periods (override --ama)');
186
- console.log(' --lookback <spec> Lookback sweep in bars (default: 28h,26h,...,8h; also accepts 28:8:2 or 8:28:2)');
187
- console.log(` --price-threshold <pct> Drift reset threshold (default: ${MA.AMA_DELTA_THRESHOLD_PERCENT})`);
188
- console.log(` --slope-threshold-factor <n> % of max slope for the slope reset (default: ${MA.AMA_SLOPE_DELTA_THRESHOLD_PERCENT})`);
189
- console.log(` --slope-persist <bars> Bars the slope delta must persist before resetting (default: ${MA.AMA_SLOPE_PERSIST_BARS})`);
190
- console.log(' --no-slope Disable the slope-delta reset (drift only)');
191
- console.log(' --confirm <frac> Slope confirmation size as a fraction of max slope (default: 0.25)');
192
- console.log(' --scale-mode <mode> Huber scale estimate: none (production) | df | mscale (default: none)');
193
- console.log(' --whipsaw <bars> Back-to-back reset gap counted as a whipsaw (default: 3)');
194
- console.log(' --truth-window <bars> Centred reference half-window (default: max lookback)');
195
- console.log(' --forward-window <bars> Realized forward horizon for range tilt direction (default: lookback)');
196
- console.log(' --out <path> JSON output path');
179
+ printHelpHeader('AMA-Slope-Huber lookback backtest', 'trend_detection/backtest_ama_slope_huber.js');
180
+ console.log(helpRow('--data <path>', 'LP candle file or shard directory (required)', 29));
181
+ console.log(helpRow('--ama <AMA1..AMA4>', `Built-in AMA preset (default: ${MA.DEFAULT_AMA_KEY})`, 29));
182
+ console.log(helpRow('--er/--fast/--slow <n>', 'Override AMA periods (override --ama)', 29));
183
+ console.log(helpRow('--lookback <spec>', 'Lookback sweep in bars (default: 28h,26h,...,8h; also accepts 28:8:2 or 8:28:2)', 29));
184
+ console.log(helpRow('--price-threshold <pct>', `Drift reset threshold (default: ${MA.AMA_DELTA_THRESHOLD_PERCENT})`, 29));
185
+ console.log(helpRow('--slope-threshold-factor <n>', `% of max slope for the slope reset (default: ${MA.AMA_SLOPE_DELTA_THRESHOLD_PERCENT})`, 29));
186
+ console.log(helpRow('--slope-persist <bars>', `Bars the slope delta must persist before resetting (default: ${MA.AMA_SLOPE_PERSIST_BARS})`, 29));
187
+ console.log(helpRow('--no-slope', 'Disable the slope-delta reset (drift only)', 29));
188
+ console.log(helpRow('--confirm <frac>', 'Slope confirmation size as a fraction of max slope (default: 0.25)', 29));
189
+ console.log(helpRow('--scale-mode <mode>', 'Huber scale estimate: none (production) | df | mscale (default: none)', 29));
190
+ console.log(helpRow('--whipsaw <bars>', 'Back-to-back reset gap counted as a whipsaw (default: 3)', 29));
191
+ console.log(helpRow('--truth-window <bars>', 'Centred reference half-window (default: max lookback)', 29));
192
+ console.log(helpRow('--forward-window <bars>', 'Realized forward horizon for range tilt direction (default: lookback)', 29));
193
+ console.log(helpRow('--out <path>', 'JSON output path', 29));
197
194
  }
198
195
  function resolveAma(cfg) {
199
196
  if (cfg.er != null || cfg.fast != null || cfg.slow != null) {
@@ -209,61 +206,7 @@ function resolveAma(cfg) {
209
206
  return { name: cfg.amaName, er: preset.erPeriod, fast: preset.fastPeriod, slow: preset.slowPeriod };
210
207
  }
211
208
  // ─── Data loading ────────────────────────────────────────────────────────────
212
- function loadCandles(input) {
213
- const resolved = path.resolve(input);
214
- if (!fs.existsSync(resolved))
215
- throw new Error(`Data path not found: ${resolved}`);
216
- const stat = fs.statSync(resolved);
217
- const files = stat.isDirectory()
218
- ? fs.readdirSync(resolved)
219
- .filter((name) => /\.json$/i.test(name) && !/manifest/i.test(name))
220
- .map((name) => path.join(resolved, name))
221
- .sort()
222
- : [resolved];
223
- const byTime = new Map();
224
- let meta = null;
225
- for (const file of files) {
226
- let raw = null;
227
- try {
228
- raw = readJSON(file);
229
- }
230
- catch {
231
- continue;
232
- }
233
- const arr = Array.isArray(raw?.candles) ? raw.candles : (Array.isArray(raw) ? raw : null);
234
- if (!arr || arr.length === 0)
235
- continue;
236
- if (!meta && raw?.meta)
237
- meta = raw.meta;
238
- for (const c of arr) {
239
- const n = normalizeCandle(c);
240
- if (n)
241
- byTime.set(n.time, { time: n.time, open: n.open, high: n.high, low: n.low, close: n.close, volume: n.volume });
242
- }
243
- }
244
- const candles = [...byTime.values()].sort((a, b) => a.time - b.time);
245
- if (candles.length === 0)
246
- throw new Error(`No candles found under ${resolved}`);
247
- return { candles, meta, files: files.length };
248
- }
249
- // ─── Small stats helpers ─────────────────────────────────────────────────────
250
- function median(values) {
251
- if (values.length === 0)
252
- return null;
253
- const s = values.slice().sort((a, b) => a - b);
254
- const mid = s.length >> 1;
255
- return s.length % 2 ? s[mid] : (s[mid - 1] + s[mid]) / 2;
256
- }
257
- function percentile(values, pct) {
258
- if (values.length === 0)
259
- return null;
260
- const s = values.slice().sort((a, b) => a - b);
261
- const idx = Math.min(s.length - 1, Math.max(0, Math.round((pct / 100) * (s.length - 1))));
262
- return s[idx];
263
- }
264
- function fmt(x, d = 2) {
265
- return Number.isFinite(Number(x)) ? Number(x).toFixed(d) : 'n/a';
266
- }
209
+ // File/directory shard loading lives in math_utils.loadCandleSeries.
267
210
  // ─── Lag estimators ──────────────────────────────────────────────────────────
268
211
  /**
269
212
  * Cross-correlation peak lag between the causal signal and a reference.
@@ -519,7 +462,7 @@ function analyzeLookback(closes, amaValues, lookbackBars, amaDef, cfg, truthArr,
519
462
  resetsPerDay,
520
463
  avgBarsBetweenResets: gaps.length > 0 ? gaps.reduce((a, b) => a + b, 0) / gaps.length : null,
521
464
  medianBarsBetweenResets: median(gaps),
522
- p90BarsBetweenResets: percentile(gaps, 90),
465
+ p90BarsBetweenResets: percentile(gaps, 90, { interpolation: 'nearest' }),
523
466
  minBarsBetweenResets: gaps.length > 0 ? Math.min(...gaps) : null,
524
467
  whipsawResets: whipsaws,
525
468
  whipsawPct: gaps.length > 0 ? (whipsaws / gaps.length) * 100 : 0,
@@ -540,7 +483,7 @@ function analyzeLookback(closes, amaValues, lookbackBars, amaDef, cfg, truthArr,
540
483
  rangeTiltActivePct: evaluatedBars > 0 ? (tiltActiveBars / evaluatedBars) * 100 : null,
541
484
  meanWrongStreakBars: wrongStreaks.length > 0 ? wrongStreaks.reduce((a, b) => a + b, 0) / wrongStreaks.length : null,
542
485
  medianAdverseMoveWhileWrongPct: median(adverseMoves),
543
- p90AdverseMoveWhileWrongPct: percentile(adverseMoves, 90),
486
+ p90AdverseMoveWhileWrongPct: percentile(adverseMoves, 90, { interpolation: 'nearest' }),
544
487
  // context
545
488
  warmupBars,
546
489
  finiteSlopeBars: finite,
@@ -572,7 +515,7 @@ function addCompositeScore(results) {
572
515
  function run() {
573
516
  const cfg = parseArgs();
574
517
  const amaDef = resolveAma(cfg);
575
- const { candles, meta, files } = loadCandles(cfg.dataPath);
518
+ const { candles, meta, files } = loadCandleSeries(cfg.dataPath);
576
519
  const closes = candles.map((c) => c.close);
577
520
  const n = closes.length;
578
521
  const amaValues = calculateAMA(closes, { erPeriod: amaDef.er, fastPeriod: amaDef.fast, slowPeriod: amaDef.slow });
@@ -721,5 +664,4 @@ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href)
721
664
  process.exit(1);
722
665
  }
723
666
  }
724
- export { loadCandles, crossCorrelationLag, reversalLag, analyzeLookback };
725
667
  //# sourceMappingURL=backtest_ama_slope_huber.js.map
@@ -6,7 +6,7 @@ import { computeDynamicWeightSeries, computeHuberWindowSlopePct, computeAmaSlope
6
6
  import { buildKalmanVelocitySeries, computeAbsolutePercentileThreshold, resolveKalmanVelocitySmoothingConfig, smoothKalmanVelocityPoint, } from '../../market_adapter/core/signals/kalman_velocity_smoothing.js';
7
7
  import { embedFunctionSources, escapeHtml, serializeJsonForScript, toEpochSeconds, UPLOT_SHARED_SCRIPT, uplotInlineTags } from '../chart_utils.js';
8
8
  import { sharedChartCSS } from '../chart_css.js';
9
- import { Y_AXIS_SIZE, makeCursorConfig, bindHoverStateFn, wireChartEvents, zoomResetScript, sizeChartsFn } from '../chart_ui.js';
9
+ import { Y_AXIS_SIZE, makeCursorConfig, bindHoverStateFn, wireChartEvents, zoomResetScript, sizeChartsFn, formatTimeLabelFn, refreshChartsPreservingZoomFn } from '../chart_ui.js';
10
10
  // Browser-embedded shared functions — the interactive chart runs the exact same
11
11
  // pure logic as the live market adapter service instead of a hand-copied copy.
12
12
  const EMBEDDED_SHARED_FUNCS = embedFunctionSources([
@@ -687,21 +687,15 @@ function generateHTML(data, title = 'Dynamic Weight Research') {
687
687
 
688
688
  ${UPLOT_SHARED_SCRIPT}
689
689
 
690
- function refreshChartsPreservingZoom() {
691
- const xs = outputChart?.scales?.x;
692
- const savedX = xs ? {
693
- min: Number.isFinite(xs.min) ? xs.min : xMin,
694
- max: Number.isFinite(xs.max) ? xs.max : xMax,
695
- } : null;
696
-
697
- amaChart.setData([data.dates, dynamicAmaSlopePct], false);
698
- kalmanChart.setData([data.dates, currentKalmanVelocityPct, data.kalmanDisplacementPct], false);
699
- outputChart.setData([data.dates, combinedOff, echoCombinedOff], false);
700
-
701
- if (savedX) {
702
- charts.forEach(c => c.batch(() => c.setScale('x', savedX)));
703
- }
704
- }
690
+ ${refreshChartsPreservingZoomFn({
691
+ anchor: 'outputChart?.scales?.x',
692
+ reload: [
693
+ 'amaChart.setData([data.dates, dynamicAmaSlopePct], false);',
694
+ 'kalmanChart.setData([data.dates, currentKalmanVelocityPct, data.kalmanDisplacementPct], false);',
695
+ 'outputChart.setData([data.dates, combinedOff, echoCombinedOff], false);',
696
+ ],
697
+ charts: 'charts',
698
+ })}
705
699
 
706
700
  let lastLiveIdx = data.realBarCount - 1;
707
701
 
@@ -895,27 +889,7 @@ function generateHTML(data, title = 'Dynamic Weight Research') {
895
889
 
896
890
  const cursorCfg = ${makeCursorConfig()};
897
891
 
898
- const TIME_MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
899
-
900
- function pad2(n) {
901
- return String(n).padStart(2, '0');
902
- }
903
-
904
- function formatTimeLabel(tsSec, spanSec) {
905
- const d = new Date(tsSec * 1000);
906
- if (!Number.isFinite(spanSec)) spanSec = 0;
907
-
908
- if (spanSec >= 365 * 24 * 3600 * 2) {
909
- return String(d.getUTCFullYear());
910
- }
911
- if (spanSec >= 90 * 24 * 3600) {
912
- return TIME_MONTHS[d.getUTCMonth()] + ' ' + d.getUTCFullYear();
913
- }
914
- if (spanSec >= 14 * 24 * 3600) {
915
- return TIME_MONTHS[d.getUTCMonth()] + ' ' + d.getUTCDate();
916
- }
917
- return TIME_MONTHS[d.getUTCMonth()] + ' ' + d.getUTCDate() + ' ' + pad2(d.getUTCHours()) + ':' + pad2(d.getUTCMinutes());
918
- }
892
+ ${formatTimeLabelFn()}
919
893
 
920
894
  function makeTimeAxis(showLabels = false) {
921
895
  return {
@@ -4,7 +4,7 @@ import { computeATRSeries } from '../../market_adapter/core/strategies/atr/calcu
4
4
  import { computeVolatilityShift } from '../../market_adapter/core/strategies/volatility_shift.js';
5
5
  import { embedFunctionSources, escapeHtml, serializeJsonForScript, toEpochSeconds, UPLOT_SHARED_SCRIPT, uplotInlineTags } from '../chart_utils.js';
6
6
  import { sharedChartCSS } from '../chart_css.js';
7
- import { Y_AXIS_SIZE, makeCursorConfig, bindHoverStateFn, wireChartEvents, zoomResetScript, sizeChartsFn } from '../chart_ui.js';
7
+ import { Y_AXIS_SIZE, makeCursorConfig, bindHoverStateFn, wireChartEvents, zoomResetScript, sizeChartsFn, refreshChartsPreservingZoomFn } from '../chart_ui.js';
8
8
  const EMBEDDED_SHARED_FUNCS = embedFunctionSources([computeATRSeries, computeVolatilityShift]);
9
9
  function generateHTML(data, title = 'ATR Volatility Research') {
10
10
  const results = data.allResults || [];
@@ -578,15 +578,14 @@ function generateHTML(data, title = 'ATR Volatility Research') {
578
578
  charts = [priceChart, varianceChart, shiftChart];
579
579
  ${wireChartEvents('charts', 'updateLegend', 'shiftChart.cursor.idx ?? data.realBarCount - 1')}
580
580
 
581
- function refreshChartsPreservingZoom() {
582
- const xs = shiftChart.scales.x;
583
- const savedX = xs ? { min: Number.isFinite(xs.min) ? xs.min : xMin, max: Number.isFinite(xs.max) ? xs.max : xMax } : null;
584
- varianceChart.setData([data.dates, toPlotLog(varianceSeries)], false);
585
- shiftChart.setData([data.dates, rawDeltaArr, deltaArr], false);
586
- if (savedX) {
587
- [varianceChart, shiftChart].forEach((chart) => chart.batch(() => chart.setScale('x', savedX)));
588
- }
589
- }
581
+ ${refreshChartsPreservingZoomFn({
582
+ anchor: 'shiftChart.scales.x',
583
+ reload: [
584
+ 'varianceChart.setData([data.dates, toPlotLog(varianceSeries)], false);',
585
+ 'shiftChart.setData([data.dates, rawDeltaArr, deltaArr], false);',
586
+ ],
587
+ charts: '[varianceChart, shiftChart]',
588
+ })}
590
589
 
591
590
  function applyShiftFromSliders() {
592
591
  recalcShift();
@@ -0,0 +1,38 @@
1
+ interface ModuleWorkerOptions<TResult> {
2
+ /**
3
+ * Called for each non-final worker message. The final message (the one for
4
+ * which `resolveOn` returns `done: true`) is not forwarded.
5
+ */
6
+ onProgress?: (msg: unknown) => void;
7
+ /**
8
+ * Inspect a worker message. Return `{ done: true, value }` to resolve the
9
+ * worker promise, or `{ done: false }` to keep waiting.
10
+ */
11
+ resolveOn: (msg: unknown) => {
12
+ done: true;
13
+ value: TResult;
14
+ } | {
15
+ done: false;
16
+ };
17
+ }
18
+ /**
19
+ * Run a single worker thread whose entry module is the caller itself.
20
+ *
21
+ * ESM has no `__filename`, so the worker entry is resolved from the caller's
22
+ * `import.meta.url` — the caller passes its own `import.meta.url`. The worker
23
+ * module must branch on `isMainThread` to run its handler and should let the
24
+ * thread exit naturally after `parentPort.postMessage`: an explicit
25
+ * `process.exit(0)` can race the message flush and silently drop the result
26
+ * (this was duplicated in optimizer_high_resolution and backtest_ama_sweep).
27
+ *
28
+ * Rejects on `error`, on a non-zero `exit` code, and on a clean exit that never
29
+ * posted a resolvable result (previously that case hung forever).
30
+ */
31
+ declare function runModuleWorker<TResult>(callerModuleUrl: string, workerData: unknown, { onProgress, resolveOn }: ModuleWorkerOptions<TResult>): Promise<TResult>;
32
+ /**
33
+ * Split `values` into at most `shardCount` contiguous, roughly equal chunks.
34
+ * Empty chunks are dropped. Single home for the copy in optimizer_high_resolution.
35
+ */
36
+ declare function splitIntoShards<T>(values: T[], shardCount: number): T[][];
37
+ export { runModuleWorker, splitIntoShards };
38
+ //# sourceMappingURL=worker_pool.d.ts.map
@@ -0,0 +1,67 @@
1
+ 'use strict';
2
+ import { Worker } from 'node:worker_threads';
3
+ import { fileURLToPath } from 'node:url';
4
+ /**
5
+ * Run a single worker thread whose entry module is the caller itself.
6
+ *
7
+ * ESM has no `__filename`, so the worker entry is resolved from the caller's
8
+ * `import.meta.url` — the caller passes its own `import.meta.url`. The worker
9
+ * module must branch on `isMainThread` to run its handler and should let the
10
+ * thread exit naturally after `parentPort.postMessage`: an explicit
11
+ * `process.exit(0)` can race the message flush and silently drop the result
12
+ * (this was duplicated in optimizer_high_resolution and backtest_ama_sweep).
13
+ *
14
+ * Rejects on `error`, on a non-zero `exit` code, and on a clean exit that never
15
+ * posted a resolvable result (previously that case hung forever).
16
+ */
17
+ function runModuleWorker(callerModuleUrl, workerData, { onProgress, resolveOn }) {
18
+ return new Promise((resolve, reject) => {
19
+ let settled = false;
20
+ const worker = new Worker(fileURLToPath(callerModuleUrl), { workerData });
21
+ worker.on('message', (msg) => {
22
+ const outcome = resolveOn(msg);
23
+ if (outcome.done) {
24
+ settled = true;
25
+ resolve(outcome.value);
26
+ return;
27
+ }
28
+ if (onProgress)
29
+ onProgress(msg);
30
+ });
31
+ worker.on('error', (err) => {
32
+ settled = true;
33
+ reject(err);
34
+ });
35
+ worker.on('exit', (code) => {
36
+ if (settled)
37
+ return;
38
+ if (code !== 0) {
39
+ settled = true;
40
+ reject(new Error(`Worker exited with code ${code}`));
41
+ return;
42
+ }
43
+ // Defer so a final `message` already in flight is processed first;
44
+ // only a genuinely result-less clean exit rejects.
45
+ setImmediate(() => {
46
+ if (settled)
47
+ return;
48
+ settled = true;
49
+ reject(new Error('Worker exited without posting a result'));
50
+ });
51
+ });
52
+ });
53
+ }
54
+ /**
55
+ * Split `values` into at most `shardCount` contiguous, roughly equal chunks.
56
+ * Empty chunks are dropped. Single home for the copy in optimizer_high_resolution.
57
+ */
58
+ function splitIntoShards(values, shardCount) {
59
+ const out = [];
60
+ const size = Math.ceil(values.length / shardCount);
61
+ for (let i = 0; i < values.length; i += size) {
62
+ out.push(values.slice(i, i + size));
63
+ }
64
+ return out.filter((s) => s.length > 0);
65
+ }
66
+ export { runModuleWorker, splitIntoShards };
67
+ //# sourceMappingURL=worker_pool.js.map