dexbot 1.6.12 → 1.6.13

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 (170) hide show
  1. package/CHANGELOG.md +33 -1
  2. package/analysis/README.md +9 -11
  3. package/analysis/ama_fitting/README.md +5 -3
  4. package/claw/README.md +1 -0
  5. package/claw/docs/AI_BOT_LIBRARY_API.md +2 -0
  6. package/claw/modules/claw_bridge.ts +0 -3
  7. package/claw/modules/claw_skill_md.ts +1 -0
  8. package/claw/package.json +3 -3
  9. package/claw/runtimes/openclaw-plugin/openclaw.plugin.json +1 -1
  10. package/claw/runtimes/openclaw-plugin/package.json +1 -1
  11. package/claw/tsconfig.json +2 -2
  12. package/dist/analysis/ama_fitting/analyze_lambda_vs_slow.js +1 -14
  13. package/dist/analysis/ama_fitting/fetch_lp_candles.js +51 -41
  14. package/dist/analysis/ama_fitting/generate_unified_comparison_chart.d.ts +1 -24
  15. package/dist/analysis/ama_fitting/generate_unified_comparison_chart.js +36 -18
  16. package/dist/analysis/ama_fitting/optimizer_high_resolution.js +18 -66
  17. package/dist/analysis/analyze_args.d.ts +12 -0
  18. package/dist/analysis/analyze_args.js +20 -0
  19. package/dist/analysis/bot_fitting/backtest_ama_sweep.d.ts +1 -16
  20. package/dist/analysis/bot_fitting/backtest_ama_sweep.js +20 -101
  21. package/dist/analysis/bot_fitting/backtest_bot_fitting.js +26 -92
  22. package/dist/analysis/bot_fitting/backtest_lookback_drawdown.js +19 -34
  23. package/dist/analysis/bot_fitting/shared_utils.d.ts +71 -3
  24. package/dist/analysis/bot_fitting/shared_utils.js +124 -8
  25. package/dist/analysis/chart_ui.d.ts +30 -1
  26. package/dist/analysis/chart_ui.js +37 -1
  27. package/dist/analysis/fills_cache.d.ts +3 -23
  28. package/dist/analysis/fills_cache.js +3 -84
  29. package/dist/analysis/fills_source.d.ts +31 -1
  30. package/dist/analysis/fills_source.js +69 -1
  31. package/dist/analysis/grid_correction_check.d.ts +1 -23
  32. package/dist/analysis/grid_correction_check.js +15 -58
  33. package/dist/analysis/math_utils.d.ts +85 -1
  34. package/dist/analysis/math_utils.js +116 -1
  35. package/dist/analysis/price_sources.d.ts +22 -1
  36. package/dist/analysis/price_sources.js +23 -0
  37. package/dist/analysis/resolve_source.js +31 -10
  38. package/dist/analysis/trade_profitability.d.ts +6 -0
  39. package/dist/analysis/trade_profitability.js +23 -97
  40. package/dist/analysis/tradingview/analyze_tradingview.d.ts +1 -17
  41. package/dist/analysis/tradingview/analyze_tradingview.js +1 -1
  42. package/dist/analysis/tradingview/tradingview_uplot_chart_generator.js +2 -15
  43. package/dist/analysis/trend_detection/backtest_ama_slope_huber.d.ts +1 -101
  44. package/dist/analysis/trend_detection/backtest_ama_slope_huber.js +22 -80
  45. package/dist/analysis/trend_detection/dynamic_weight_chart_generator.js +11 -37
  46. package/dist/analysis/trend_detection/volatility_chart_generator.js +9 -10
  47. package/dist/analysis/worker_pool.d.ts +38 -0
  48. package/dist/analysis/worker_pool.js +67 -0
  49. package/dist/dexbot.js +67 -48
  50. package/dist/market_adapter/ama_signal_runner.js +21 -54
  51. package/dist/market_adapter/candle_utils.d.ts +7 -1
  52. package/dist/market_adapter/candle_utils.js +11 -1
  53. package/dist/market_adapter/cli_args.d.ts +22 -0
  54. package/dist/market_adapter/cli_args.js +30 -0
  55. package/dist/market_adapter/core/config_normalizers.js +11 -6
  56. package/dist/market_adapter/core/kibana_candles.d.ts +3 -1
  57. package/dist/market_adapter/core/kibana_candles.js +3 -2
  58. package/dist/market_adapter/core/market_adapter_service.d.ts +85 -85
  59. package/dist/market_adapter/core/market_adapter_service.js +93 -158
  60. package/dist/market_adapter/core/signals/hurst_analyzer.d.ts +1 -6
  61. package/dist/market_adapter/core/signals/hurst_analyzer.js +1 -1
  62. package/dist/market_adapter/core/signals/permutation_entropy_analyzer.d.ts +1 -11
  63. package/dist/market_adapter/core/signals/permutation_entropy_analyzer.js +1 -1
  64. package/dist/market_adapter/inputs/fetch_book_data.js +0 -1
  65. package/dist/market_adapter/inputs/fetch_cex_synthetic_data.js +63 -107
  66. package/dist/market_adapter/inputs/fetch_lp_data.d.ts +8 -1
  67. package/dist/market_adapter/inputs/fetch_lp_data.js +25 -7
  68. package/dist/market_adapter/inputs/kibana_feed_source.js +4 -26
  69. package/dist/market_adapter/inputs/window_cache.d.ts +4 -72
  70. package/dist/market_adapter/inputs/window_cache.js +52 -177
  71. package/dist/market_adapter/interval_utils.d.ts +11 -1
  72. package/dist/market_adapter/interval_utils.js +16 -1
  73. package/dist/market_adapter/lp_chart_runner.d.ts +3 -0
  74. package/dist/market_adapter/lp_chart_runner.js +37 -19
  75. package/dist/market_adapter/market_adapter.js +27 -72
  76. package/dist/market_adapter/utils/data_discovery.d.ts +45 -1
  77. package/dist/market_adapter/utils/data_discovery.js +220 -27
  78. package/dist/market_adapter/utils/month_shards.d.ts +84 -0
  79. package/dist/market_adapter/utils/month_shards.js +237 -0
  80. package/dist/modules/account_bots.d.ts +1 -0
  81. package/dist/modules/account_bots.js +102 -121
  82. package/dist/modules/account_orders.d.ts +13 -0
  83. package/dist/modules/account_orders.js +38 -44
  84. package/dist/modules/bitshares-native/chain_client.d.ts +1 -1
  85. package/dist/modules/bitshares-native/chain_client.js +14 -13
  86. package/dist/modules/bitshares-native/resolvers.js +22 -25
  87. package/dist/modules/bitshares-native/serial/types.js +22 -50
  88. package/dist/modules/bitshares-native/signing_client.d.ts +1 -1
  89. package/dist/modules/bot_defaults.d.ts +2 -1
  90. package/dist/modules/bot_settings.d.ts +1 -1
  91. package/dist/modules/broadcast_failure.d.ts +2 -1
  92. package/dist/modules/chain_orders.d.ts +1 -1
  93. package/dist/modules/chain_orders.js +51 -96
  94. package/dist/modules/cli_start_onboarding.d.ts +2 -1
  95. package/dist/modules/constants.d.ts +7 -1
  96. package/dist/modules/constants.js +20 -1
  97. package/dist/modules/cr_planner.d.ts +1 -1
  98. package/dist/modules/cr_planner.js +1 -5
  99. package/dist/modules/credit_pricing.d.ts +2 -1
  100. package/dist/modules/credit_pricing.js +36 -40
  101. package/dist/modules/credit_runtime.d.ts +7 -0
  102. package/dist/modules/credit_runtime.js +45 -73
  103. package/dist/modules/daemon_node_health.d.ts +4 -3
  104. package/dist/modules/dexbot_class.d.ts +28 -17
  105. package/dist/modules/dexbot_class.js +105 -58
  106. package/dist/modules/dexbot_cow_runtime.d.ts +0 -7
  107. package/dist/modules/dexbot_cow_runtime.js +54 -28
  108. package/dist/modules/dexbot_credential_client.d.ts +0 -7
  109. package/dist/modules/dexbot_credential_client.js +1 -2
  110. package/dist/modules/dexbot_fill_runtime.d.ts +1 -2
  111. package/dist/modules/dexbot_fill_runtime.js +5 -6
  112. package/dist/modules/dexbot_maintenance_runtime.d.ts +20 -3
  113. package/dist/modules/dexbot_maintenance_runtime.js +84 -69
  114. package/dist/modules/dexbot_startup_runtime.d.ts +0 -1
  115. package/dist/modules/dexbot_startup_runtime.js +13 -14
  116. package/dist/modules/general_settings.d.ts +1 -1
  117. package/dist/modules/graceful_shutdown.js +17 -11
  118. package/dist/modules/key_store.js +0 -1
  119. package/dist/modules/lazy_shim.d.ts +11 -0
  120. package/dist/modules/lazy_shim.js +16 -0
  121. package/dist/modules/node_connect_policy.d.ts +3 -2
  122. package/dist/modules/node_failure_ledger.d.ts +6 -5
  123. package/dist/modules/order/format.d.ts +12 -3
  124. package/dist/modules/order/format.js +15 -3
  125. package/dist/modules/order/genesis_policy.d.ts +3 -3
  126. package/dist/modules/order/genesis_policy.js +5 -2
  127. package/dist/modules/order/grid.d.ts +4 -2
  128. package/dist/modules/order/grid.js +91 -35
  129. package/dist/modules/order/index.d.ts +1 -0
  130. package/dist/modules/order/logger.d.ts +1 -8
  131. package/dist/modules/order/logger.js +0 -9
  132. package/dist/modules/order/manager.d.ts +9 -13
  133. package/dist/modules/order/manager.js +33 -50
  134. package/dist/modules/order/utils/order.js +39 -35
  135. package/dist/modules/order/utils/system.d.ts +9 -29
  136. package/dist/modules/order/utils/system.js +17 -2
  137. package/dist/modules/order/utils/validate.js +2 -2
  138. package/dist/modules/order/utils/withPoolRef.d.ts +2 -1
  139. package/dist/modules/paths.js +32 -34
  140. package/dist/modules/runtime_settings.d.ts +2 -4
  141. package/dist/modules/settings_merge.d.ts +2 -2
  142. package/dist/modules/socket_json_client.d.ts +2 -1
  143. package/dist/modules/types.d.ts +42 -27
  144. package/dist/modules/utils/errors.js +1 -3
  145. package/dist/modules/version_notice.d.ts +11 -10
  146. package/dist/pm2.d.ts +0 -1
  147. package/dist/pm2.js +56 -35
  148. package/dist/scripts/analyze-git.js +2 -15
  149. package/dist/scripts/chart_command.js +2 -2
  150. package/dist/scripts/check_any.d.ts +4 -0
  151. package/dist/scripts/check_any.js +53 -21
  152. package/dist/scripts/diagnose-pool-history.js +1 -6
  153. package/dist/scripts/run-tests.js +0 -1
  154. package/dist/scripts/sync-version.js +0 -10
  155. package/docs/BITSHARES_ONBOARDING.md +8 -12
  156. package/docs/COPY_ON_WRITE_MASTER_PLAN.md +1 -1
  157. package/docs/DEXBOT_COMPARISON.md +6 -6
  158. package/docs/EVOLUTION.md +9 -8
  159. package/docs/FUND_MOVEMENT_AND_ACCOUNTING.md +1 -1
  160. package/docs/README.md +1 -1
  161. package/docs/WORKFLOW.md +1 -0
  162. package/docs/architecture.md +19 -10
  163. package/market_adapter/README.md +6 -4
  164. package/package.json +6 -9
  165. package/scripts/README.md +43 -38
  166. package/tests/README.md +25 -10
  167. package/dist/scripts/diagnose-kibana-candles.d.ts +0 -3
  168. package/dist/scripts/diagnose-kibana-candles.js +0 -82
  169. package/scripts/git-viewer.sh +0 -489
  170. package/scripts/postinstall.js +0 -16
@@ -4,8 +4,9 @@ import fs from 'node:fs';
4
4
  import path from 'node:path';
5
5
  import { pathToFileURL } from 'node:url';
6
6
  import { resolveAccountRef } from './account_resolver.js';
7
- import { BTS_ID, assetPrec as getPrec, assetSymbol, resolveAssetPrecisions, toReal, } from './fills_source.js';
7
+ import { BTS_ID, assetPrec as getPrec, assetSymbol, resolveAssetPrecisions, classifyFill, matchesAssetRef, } from './fills_source.js';
8
8
  import { writePnlReport } from './pnl_report.js';
9
+ import { percentile } from './math_utils.js';
9
10
  import { fetchFillsCached } from './fills_cache.js';
10
11
  import { toFileUrl } from './chart_utils.js';
11
12
  import { PATHS } from '../modules/paths.js';
@@ -202,101 +203,39 @@ function parseArgs(argv = process.argv.slice(2)) {
202
203
  }
203
204
  // ─── Fill Classification ─────────────────────────────────────────────────────
204
205
  /**
205
- * A fill needs strictly positive, finite amounts on both legs. A zero base
206
- * amount would produce an infinite price (quoteAmount / 0) that poisons
207
- * downstream inventory lots and PnL aggregates.
206
+ * Classify fills into trade records. Direction/base/quote resolution and the
207
+ * amount sanity check live in fills_source.classifyFill (single home, also used
208
+ * by grid_correction_check); this wrapper owns the PnL-specific market-fee
209
+ * handling, the asset/pair filters and the unknown-precision skip count.
208
210
  */
209
- function isValidFillAmounts(baseAmount, quoteAmount) {
210
- return Number.isFinite(baseAmount) && Number.isFinite(quoteAmount)
211
- && baseAmount > 0 && quoteAmount > 0;
212
- }
213
- /** Match a user-supplied pair leg against a chain asset id or its symbol. */
214
- function matchesAssetRef(ref, assetId) {
215
- const want = String(ref).trim().toUpperCase();
216
- if (!want)
217
- return false;
218
- return assetId.toUpperCase() === want || assetSymbol(assetId).toUpperCase() === want;
219
- }
220
211
  function classifyFills(fills, filterAsset, pairFilter = null) {
221
212
  const trades = [];
222
213
  const pairs = new Set();
223
214
  let skipped = 0;
224
215
  for (const f of fills) {
225
- const pAsset = f.pays.asset_id;
226
- const rAsset = f.receives.asset_id;
227
- let direction;
228
- let baseAsset;
229
- let quoteAsset;
230
- let baseAmount;
231
- let quoteAmount;
232
- let price;
233
- let marketFeeReal;
234
- let marketFeeAsset;
216
+ const c = classifyFill(f);
217
+ if (!c) {
218
+ skipped++;
219
+ continue;
220
+ }
221
+ // Market fee is always deducted from receives; an unknown fee asset
222
+ // precision makes the fee (and therefore net PnL) uncomputable.
235
223
  const feePrec = getPrec(f.fee.asset_id);
236
224
  if (feePrec === undefined || isNaN(f.fee.amount)) {
237
225
  skipped++;
238
226
  continue;
239
227
  }
240
- const feeReal = f.fee.amount / Math.pow(10, feePrec);
241
- if (pAsset === BTS_ID && rAsset !== BTS_ID) {
242
- direction = 'buy';
243
- baseAsset = rAsset;
244
- quoteAsset = BTS_ID;
245
- baseAmount = toReal(f.receives.amount, rAsset);
246
- quoteAmount = toReal(f.pays.amount, BTS_ID);
247
- if (!isValidFillAmounts(baseAmount, quoteAmount)) {
248
- skipped++;
249
- continue;
250
- }
251
- price = quoteAmount / baseAmount;
252
- marketFeeReal = feeReal;
253
- marketFeeAsset = f.fee.asset_id;
254
- }
255
- else if (rAsset === BTS_ID && pAsset !== BTS_ID) {
256
- direction = 'sell';
257
- baseAsset = pAsset;
258
- quoteAsset = BTS_ID;
259
- baseAmount = toReal(f.pays.amount, pAsset);
260
- quoteAmount = toReal(f.receives.amount, BTS_ID);
261
- if (!isValidFillAmounts(baseAmount, quoteAmount)) {
262
- skipped++;
263
- continue;
264
- }
265
- price = quoteAmount / baseAmount;
266
- marketFeeReal = feeReal;
267
- marketFeeAsset = f.fee.asset_id;
268
- }
269
- else {
270
- // Non-BTS cross-pair: use consistent ordering (lower asset ID = base)
271
- const baseForCheck = pAsset < rAsset ? pAsset : rAsset;
272
- const quoteForCheck = pAsset < rAsset ? rAsset : pAsset;
273
- // Skip if either asset precision is unknown
274
- if (getPrec(baseForCheck) === undefined || getPrec(quoteForCheck) === undefined) {
275
- skipped++;
276
- continue;
277
- }
278
- const isSell = pAsset < rAsset;
279
- direction = isSell ? 'sell' : 'buy';
280
- baseAsset = isSell ? pAsset : rAsset;
281
- quoteAsset = isSell ? rAsset : pAsset;
282
- baseAmount = toReal(isSell ? f.pays.amount : f.receives.amount, baseAsset);
283
- quoteAmount = toReal(isSell ? f.receives.amount : f.pays.amount, quoteAsset);
284
- if (!isValidFillAmounts(baseAmount, quoteAmount)) {
285
- skipped++;
286
- continue;
287
- }
288
- price = quoteAmount / baseAmount;
289
- marketFeeReal = feeReal;
290
- marketFeeAsset = f.fee.asset_id;
291
- }
228
+ const marketFeeReal = f.fee.amount / Math.pow(10, feePrec);
229
+ const marketFeeAsset = f.fee.asset_id;
230
+ const { direction, baseAsset, quoteAsset, baseAmount, quoteAmount, price } = c;
292
231
  if (filterAsset && baseAsset !== filterAsset)
293
232
  continue;
294
233
  if (pairFilter && !(matchesAssetRef(pairFilter.base, baseAsset) && matchesAssetRef(pairFilter.quote, quoteAsset)))
295
234
  continue;
296
235
  // Validate market fee asset: fee is always deducted from receives
297
236
  // (base for buys, quote for sells). Warn if unexpected.
298
- if (marketFeeReal > 0 && marketFeeAsset !== '' && marketFeeAsset !== rAsset) {
299
- console.warn(` [warn] Fill ${f.orderId}: fee asset ${marketFeeAsset} ≠ receives asset ${rAsset}. Market fee PnL may be incorrect.`);
237
+ if (marketFeeReal > 0 && marketFeeAsset !== '' && marketFeeAsset !== f.receives.asset_id) {
238
+ console.warn(` [warn] Fill ${f.orderId}: fee asset ${marketFeeAsset} ≠ receives asset ${f.receives.asset_id}. Market fee PnL may be incorrect.`);
300
239
  }
301
240
  const pairKey = `${baseAsset}:${quoteAsset}`;
302
241
  pairs.add(pairKey);
@@ -595,19 +534,6 @@ function printPnlDetail(pairs) {
595
534
  console.log('');
596
535
  }
597
536
  }
598
- function percentile(sorted, p) {
599
- const n = sorted.length;
600
- if (n === 0)
601
- return 0;
602
- if (n === 1)
603
- return sorted[0];
604
- const k = (p / 100) * (n - 1);
605
- const f = Math.floor(k);
606
- const c = Math.ceil(k);
607
- if (f === c)
608
- return sorted[f];
609
- return sorted[f] * (c - k) + sorted[c] * (k - f);
610
- }
611
537
  function computeMetrics(pair, window) {
612
538
  const pnls = pair.realizedPnls;
613
539
  const total = pnls.length;
@@ -757,7 +683,7 @@ function computeMetrics(pair, window) {
757
683
  ? fillCounts.reduce((a, b) => Math.max(a, b), 0)
758
684
  : 0;
759
685
  const sortedCounts = [...fillCounts].sort((a, b) => a - b);
760
- const fillsPerOrderMedian = sellOrdersFilled > 0 ? percentile(sortedCounts, 50) : 0;
686
+ const fillsPerOrderMedian = sellOrdersFilled > 0 ? percentile(sortedCounts, 50, { sorted: true, empty: 0 }) : 0;
761
687
  const oneShotOrderRatio = sellOrdersFilled > 0
762
688
  ? fillCounts.filter(c => c === 1).length / sellOrdersFilled
763
689
  : 0;
@@ -890,15 +816,15 @@ function computeMetrics(pair, window) {
890
816
  }
891
817
  // Payoff distribution stats
892
818
  const pnlPcts = [...pnls.map(r => r.pnlPct)].sort((a, b) => a - b);
893
- const medianPnlPct = percentile(pnlPcts, 50);
894
- const p25PnlPct = percentile(pnlPcts, 25);
895
- const p75PnlPct = percentile(pnlPcts, 75);
819
+ const medianPnlPct = percentile(pnlPcts, 50, { sorted: true, empty: 0 });
820
+ const p25PnlPct = percentile(pnlPcts, 25, { sorted: true, empty: 0 });
821
+ const p75PnlPct = percentile(pnlPcts, 75, { sorted: true, empty: 0 });
896
822
  const absAvgLoss = Math.abs(avgLoss);
897
823
  let rValues = [];
898
824
  if (absAvgLoss > 0) {
899
825
  rValues = pnls.map(r => r.pnl / absAvgLoss).sort((a, b) => a - b);
900
826
  }
901
- const medianR = rValues.length > 0 ? percentile(rValues, 50) : 0;
827
+ const medianR = rValues.length > 0 ? percentile(rValues, 50, { sorted: true, empty: 0 }) : 0;
902
828
  const pctRGreater1 = rValues.length > 0 ? rValues.filter(r => r > 1).length / rValues.length : 0;
903
829
  const pctRGreater2 = rValues.length > 0 ? rValues.filter(r => r > 2).length / rValues.length : 0;
904
830
  const pctRLessNeg1 = rValues.length > 0 ? rValues.filter(r => r < -1).length / rValues.length : 0;
@@ -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,
@@ -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