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
@@ -76,6 +76,74 @@ function toReal(amount, assetId) {
76
76
  return NaN;
77
77
  return amount / Math.pow(10, p);
78
78
  }
79
+ /**
80
+ * A fill needs strictly positive, finite amounts on both legs. A zero base
81
+ * amount would produce an infinite price (quoteAmount / 0) that poisons
82
+ * downstream inventory lots and PnL aggregates.
83
+ */
84
+ function isValidFillAmounts(baseAmount, quoteAmount) {
85
+ return Number.isFinite(baseAmount) && Number.isFinite(quoteAmount)
86
+ && baseAmount > 0 && quoteAmount > 0;
87
+ }
88
+ /** Match a user-supplied pair leg against a chain asset id or its symbol. */
89
+ function matchesAssetRef(ref, assetId) {
90
+ const want = String(ref).trim().toUpperCase();
91
+ if (!want)
92
+ return false;
93
+ return assetId.toUpperCase() === want || assetSymbol(assetId).toUpperCase() === want;
94
+ }
95
+ /**
96
+ * Resolve a raw fill's buy/sell direction, base/quote assets and real amounts.
97
+ *
98
+ * Direction/base/quote rules (single home for trade_profitability and
99
+ * grid_correction_check, which previously carried verbatim copies):
100
+ * - BTS on the pays side → buy the received asset, quote = BTS.
101
+ * - BTS on the receives side → sell the paid asset, quote = BTS.
102
+ * - any other cross-pair → lower asset id is the base; sell when pays < receives.
103
+ *
104
+ * Returns `null` when an asset precision is unknown or either real amount is
105
+ * not strictly positive/finite. Callers own the skip counting and any extra
106
+ * validation (e.g. the market-fee asset check in trade_profitability).
107
+ */
108
+ function classifyFill(f) {
109
+ const pAsset = f.pays.asset_id;
110
+ const rAsset = f.receives.asset_id;
111
+ let direction;
112
+ let baseAsset;
113
+ let quoteAsset;
114
+ let baseAmount;
115
+ let quoteAmount;
116
+ if (pAsset === BTS_ID && rAsset !== BTS_ID) {
117
+ direction = 'buy';
118
+ baseAsset = rAsset;
119
+ quoteAsset = BTS_ID;
120
+ baseAmount = toReal(f.receives.amount, rAsset);
121
+ quoteAmount = toReal(f.pays.amount, BTS_ID);
122
+ }
123
+ else if (rAsset === BTS_ID && pAsset !== BTS_ID) {
124
+ direction = 'sell';
125
+ baseAsset = pAsset;
126
+ quoteAsset = BTS_ID;
127
+ baseAmount = toReal(f.pays.amount, pAsset);
128
+ quoteAmount = toReal(f.receives.amount, BTS_ID);
129
+ }
130
+ else {
131
+ // Non-BTS cross-pair: consistent ordering (lower asset id = base).
132
+ const baseForCheck = pAsset < rAsset ? pAsset : rAsset;
133
+ const quoteForCheck = pAsset < rAsset ? rAsset : pAsset;
134
+ if (assetPrec(baseForCheck) === undefined || assetPrec(quoteForCheck) === undefined)
135
+ return null;
136
+ const isSell = pAsset < rAsset;
137
+ direction = isSell ? 'sell' : 'buy';
138
+ baseAsset = isSell ? pAsset : rAsset;
139
+ quoteAsset = isSell ? rAsset : pAsset;
140
+ baseAmount = toReal(isSell ? f.pays.amount : f.receives.amount, baseAsset);
141
+ quoteAmount = toReal(isSell ? f.receives.amount : f.pays.amount, quoteAsset);
142
+ }
143
+ if (!isValidFillAmounts(baseAmount, quoteAmount))
144
+ return null;
145
+ return { direction, baseAsset, quoteAsset, baseAmount, quoteAmount, price: quoteAmount / baseAmount };
146
+ }
79
147
  /**
80
148
  * Collect all unique non-BTS asset IDs from fills, resolve unknown precisions
81
149
  * from the blockchain, and populate the runtime cache.
@@ -187,5 +255,5 @@ async function fetchAllFills(config, accountId, gte, lte) {
187
255
  }
188
256
  return fills;
189
257
  }
190
- export { BTS_ID, assetSymbol, assetPrec, toReal, resolveAssetPrecisions, fetchAllFills, };
258
+ export { BTS_ID, assetSymbol, assetPrec, toReal, isValidFillAmounts, matchesAssetRef, classifyFill, resolveAssetPrecisions, fetchAllFills, };
191
259
  //# sourceMappingURL=fills_source.js.map
@@ -1,9 +1,5 @@
1
1
  #!/usr/bin/env node
2
2
  import { FillRecord } from './fills_source.js';
3
- interface OrderUpdate {
4
- orderId: string;
5
- sequence: number;
6
- }
7
3
  interface TradeFill {
8
4
  time: string;
9
5
  orderId: string;
@@ -29,19 +25,6 @@ interface Violation {
29
25
  halfInc: number;
30
26
  threshold: number;
31
27
  }
32
- interface AggregatedOrder {
33
- orderId: string;
34
- direction: 'buy' | 'sell';
35
- baseAsset: string;
36
- quoteAsset: string;
37
- baseAmount: number;
38
- quoteAmount: number;
39
- price: number;
40
- time: string;
41
- sequence: number;
42
- fillCount: number;
43
- isMaker: boolean;
44
- }
45
28
  /**
46
29
  * LAST-FILL-GUARD helper — pivot ± halfIncrement (replaces price-tolerance).
47
30
  * last fill @x with increment i: BUY < x*(1 - i/2/100), SELL > x*(1 + i/2/100)
@@ -64,10 +47,5 @@ declare function classifyFills(fills: FillRecord[]): {
64
47
  trades: TradeFill[];
65
48
  skipped: number;
66
49
  };
67
- declare function aggregateByOrder(trades: TradeFill[], updates?: OrderUpdate[]): AggregatedOrder[];
68
- declare function detectViolations(items: (TradeFill | AggregatedOrder)[], includeCrossPair: boolean, incrementPercent: number): {
69
- violations: Violation[];
70
- checkedTransitions: number;
71
- };
72
- export { isLastFillGuardBlocked, classifyFills, aggregateByOrder, detectViolations, TradeFill, Violation, AggregatedOrder };
50
+ export { isLastFillGuardBlocked, classifyFills, TradeFill, Violation };
73
51
  //# sourceMappingURL=grid_correction_check.d.ts.map
@@ -38,7 +38,7 @@ import * as KC from '../market_adapter/core/kibana_client.js';
38
38
  import * as C from '../modules/constants.js';
39
39
  import { loadBotSettings, computeBotKey } from './bot_key_utils.js';
40
40
  import { resolveBotAccount } from './account_resolver.js';
41
- import { BTS_ID, assetPrec, assetSymbol, fetchAllFills, resolveAssetPrecisions, toReal, } from './fills_source.js';
41
+ import { classifyFill, assetSymbol, fetchAllFills, resolveAssetPrecisions, } from './fills_source.js';
42
42
  const { kibanaSearch, DEFAULT_CONFIG: BASE_CONFIG } = KC;
43
43
  const OP_LIMIT_ORDER_UPDATE = 77;
44
44
  // ─── LAST-FILL-GUARD helper (1:1 with dexbot_cow_runtime.ts) ─────────────────
@@ -324,70 +324,27 @@ async function fetchAllOrderUpdates(config, accountId, gte, lte) {
324
324
  return updates;
325
325
  }
326
326
  // ─── Fill classification ─────────────────────────────────────────────────────
327
+ // Direction/base/quote resolution is shared with trade_profitability via
328
+ // fills_source.classifyFill; this wrapper only owns the skip count and the
329
+ // TradeFill shape (no market-fee fields — grid checks prices, not PnL).
327
330
  function classifyFills(fills) {
328
331
  const trades = [];
329
332
  let skipped = 0;
330
333
  for (const f of fills) {
331
- const pAsset = f.pays.asset_id;
332
- const rAsset = f.receives.asset_id;
333
- let direction;
334
- let baseAsset;
335
- let quoteAsset;
336
- let baseAmount;
337
- let quoteAmount;
338
- let price;
339
- if (pAsset === BTS_ID && rAsset !== BTS_ID) {
340
- direction = 'buy';
341
- baseAsset = rAsset;
342
- quoteAsset = BTS_ID;
343
- baseAmount = toReal(f.receives.amount, rAsset);
344
- quoteAmount = toReal(f.pays.amount, BTS_ID);
345
- if (!Number.isFinite(baseAmount) || !Number.isFinite(quoteAmount) || baseAmount <= 0 || quoteAmount <= 0) {
346
- skipped++;
347
- continue;
348
- }
349
- price = quoteAmount / baseAmount;
350
- }
351
- else if (rAsset === BTS_ID && pAsset !== BTS_ID) {
352
- direction = 'sell';
353
- baseAsset = pAsset;
354
- quoteAsset = BTS_ID;
355
- baseAmount = toReal(f.pays.amount, pAsset);
356
- quoteAmount = toReal(f.receives.amount, BTS_ID);
357
- if (!Number.isFinite(baseAmount) || !Number.isFinite(quoteAmount) || baseAmount <= 0 || quoteAmount <= 0) {
358
- skipped++;
359
- continue;
360
- }
361
- price = quoteAmount / baseAmount;
362
- }
363
- else {
364
- const baseForCheck = pAsset < rAsset ? pAsset : rAsset;
365
- const quoteForCheck = pAsset < rAsset ? rAsset : pAsset;
366
- if (assetPrec(baseForCheck) === undefined || assetPrec(quoteForCheck) === undefined) {
367
- skipped++;
368
- continue;
369
- }
370
- const isSell = pAsset < rAsset;
371
- direction = isSell ? 'sell' : 'buy';
372
- baseAsset = isSell ? pAsset : rAsset;
373
- quoteAsset = isSell ? rAsset : pAsset;
374
- baseAmount = toReal(isSell ? f.pays.amount : f.receives.amount, baseAsset);
375
- quoteAmount = toReal(isSell ? f.receives.amount : f.pays.amount, quoteAsset);
376
- if (!Number.isFinite(baseAmount) || !Number.isFinite(quoteAmount) || baseAmount <= 0 || quoteAmount <= 0) {
377
- skipped++;
378
- continue;
379
- }
380
- price = quoteAmount / baseAmount;
334
+ const c = classifyFill(f);
335
+ if (!c) {
336
+ skipped++;
337
+ continue;
381
338
  }
382
339
  trades.push({
383
340
  time: f.time,
384
341
  orderId: f.orderId,
385
- direction,
386
- baseAsset,
387
- quoteAsset,
388
- baseAmount,
389
- quoteAmount,
390
- price,
342
+ direction: c.direction,
343
+ baseAsset: c.baseAsset,
344
+ quoteAsset: c.quoteAsset,
345
+ baseAmount: c.baseAmount,
346
+ quoteAmount: c.quoteAmount,
347
+ price: c.price,
391
348
  isMaker: f.isMaker,
392
349
  sequence: f.blockNum * 1e6 + f.opNum,
393
350
  });
@@ -729,7 +686,7 @@ async function main() {
729
686
  exportCsv(opts.csv, violations);
730
687
  process.exit(violations.length > 0 ? 2 : 0);
731
688
  }
732
- export { isLastFillGuardBlocked, classifyFills, aggregateByOrder, detectViolations };
689
+ export { isLastFillGuardBlocked, classifyFills };
733
690
  if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
734
691
  main().catch(e => {
735
692
  console.error('\n[fatal]', getErrorMessage(e));
@@ -8,6 +8,66 @@ import { getCandleClose, getCandleTimestamp, normalizeCandle } from '../market_a
8
8
  */
9
9
  declare function range(min: number, max: number, step: number, decimals?: number): number[];
10
10
  declare function calcStdDev(arr: number[]): number;
11
+ /**
12
+ * Median of a numeric array (average of the two middle values for even
13
+ * lengths). Returns `null` for an empty array. Shared by the analysis
14
+ * backtests instead of each keeping a local copy.
15
+ */
16
+ declare function median(values: number[]): number | null;
17
+ interface PercentileOptions {
18
+ /** 'linear' interpolates between the two bracketing ranks (R-7, default);
19
+ * 'nearest' picks a single rank. */
20
+ interpolation?: 'linear' | 'nearest';
21
+ /** For 'nearest': round the rank (default) or floor it. */
22
+ rounding?: 'round' | 'floor';
23
+ /** Skip the internal sort when the input is already ascending. */
24
+ sorted?: boolean;
25
+ /** Value returned for an empty input (default null). */
26
+ empty?: number | null;
27
+ }
28
+ /**
29
+ * Percentile of a numeric array.
30
+ *
31
+ * Single home for the four copies previously kept by optimizer_high_resolution
32
+ * (fractional q, linear), backtest_ama_slope_huber (nearest/round),
33
+ * trade_profitability (pre-sorted, linear) and backtest_lookback_drawdown
34
+ * (nearest/floor). The options preserve each caller's exact ranking rule so
35
+ * centralizing does not silently change any reported number.
36
+ *
37
+ * @param values numeric array (copied + sorted unless `sorted` is true)
38
+ * @param p percentile position in [0, 100]
39
+ */
40
+ declare function percentile(values: number[], p: number, opts: PercentileOptions & {
41
+ empty: number;
42
+ }): number;
43
+ declare function percentile(values: number[], p: number, opts?: PercentileOptions): number | null;
44
+ interface FmtNumOptions {
45
+ /** Text returned for a value that is not finite. */
46
+ fallback?: string;
47
+ /** When true, only a real finite `number` counts (null/undefined fall back);
48
+ * when false (default), values are coerced with `Number()` first, so
49
+ * `null` formats as `0`. */
50
+ strict?: boolean;
51
+ }
52
+ /**
53
+ * Fixed-decimal number formatting with a non-finite fallback.
54
+ *
55
+ * Single home for the identical `fmt(x, d)` copies in backtest_ama_slope_huber
56
+ * and backtest_lookback_drawdown, plus the strict/padded variant in
57
+ * bot_fitting/shared_utils (`fmt(x, d, { fallback: ' n/a', strict: true })`).
58
+ */
59
+ declare function fmtNum(x: number | null | undefined, d?: number, { fallback, strict }?: FmtNumOptions): string;
60
+ /**
61
+ * Evenly spaced geometric progression from `min` to `max` (endpoints forced).
62
+ * Values are optionally snapped to `quantum` and clamped back into the range,
63
+ * rounded to `decimals`, deduped and sorted ascending. Single home for the
64
+ * helper previously copied into analyze_lambda_vs_slow and
65
+ * optimizer_high_resolution (which used different rounding precision).
66
+ */
67
+ declare function geometricRange(min: number, max: number, count: number, { decimals, quantum }?: {
68
+ decimals?: number | undefined;
69
+ quantum?: number | null | undefined;
70
+ }): number[];
11
71
  /**
12
72
  * Parse a candle JSON file with format detection:
13
73
  * flat array → {candles: [...]} → {data: [...]}
@@ -17,5 +77,29 @@ export interface CandleFile {
17
77
  meta: Record<string, unknown> | null;
18
78
  }
19
79
  declare function loadCandleFile(filePath: string): CandleFile;
20
- export { range, calcStdDev, getCandleClose, getCandleTimestamp, normalizeCandle, loadCandleFile, };
80
+ /** Normalized OHLCV candle (millisecond-free epoch seconds, like the adapter). */
81
+ type Candle = {
82
+ time: number;
83
+ open: number;
84
+ high: number;
85
+ low: number;
86
+ close: number;
87
+ volume: number;
88
+ };
89
+ /**
90
+ * Load candle records from a single JSON file or a directory of monthly JSON
91
+ * shards.
92
+ *
93
+ * Directory entries are filtered to `*.json` (manifests skipped) and sorted;
94
+ * shards are merged and deduped by candle time (a later file overwrites an
95
+ * earlier duplicate). Returns the merged candles, the first non-null `meta` and
96
+ * the file count. Single home for the loader previously copied into
97
+ * backtest_ama_slope_huber.
98
+ */
99
+ declare function loadCandleSeries(input: string): {
100
+ candles: Candle[];
101
+ meta: Record<string, unknown> | null;
102
+ files: number;
103
+ };
104
+ export { range, calcStdDev, geometricRange, median, percentile, fmtNum, getCandleClose, getCandleTimestamp, normalizeCandle, loadCandleFile, loadCandleSeries, };
21
105
  //# sourceMappingURL=math_utils.d.ts.map
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
  import fs from 'node:fs';
3
+ import path from 'node:path';
3
4
  import { getStorage } from '../modules/storage/index.js';
4
5
  const { readJSON } = getStorage();
5
6
  import { getCandleClose, getCandleTimestamp, normalizeCandle, } from '../market_adapter/candle_utils.js';
@@ -21,6 +22,73 @@ function calcStdDev(arr) {
21
22
  const sqDiffs = arr.reduce((sum, v) => sum + (v - mean) ** 2, 0);
22
23
  return Math.sqrt(sqDiffs / arr.length);
23
24
  }
25
+ /**
26
+ * Median of a numeric array (average of the two middle values for even
27
+ * lengths). Returns `null` for an empty array. Shared by the analysis
28
+ * backtests instead of each keeping a local copy.
29
+ */
30
+ function median(values) {
31
+ if (values.length === 0)
32
+ return null;
33
+ const s = values.slice().sort((a, b) => a - b);
34
+ const mid = s.length >> 1;
35
+ return s.length % 2 ? s[mid] : (s[mid - 1] + s[mid]) / 2;
36
+ }
37
+ function percentile(values, p, { interpolation = 'linear', rounding = 'round', sorted = false, empty = null } = {}) {
38
+ if (values.length === 0)
39
+ return empty;
40
+ const s = sorted ? values : values.slice().sort((a, b) => a - b);
41
+ const pct = Math.max(0, Math.min(100, p));
42
+ const idx = (pct / 100) * (s.length - 1);
43
+ if (interpolation === 'linear') {
44
+ const lo = Math.floor(idx);
45
+ const hi = Math.ceil(idx);
46
+ if (lo === hi)
47
+ return s[lo];
48
+ const t = idx - lo;
49
+ return s[lo] * (1 - t) + s[hi] * t;
50
+ }
51
+ const i = rounding === 'floor' ? Math.floor(idx) : Math.round(idx);
52
+ return s[Math.min(s.length - 1, Math.max(0, i))];
53
+ }
54
+ /**
55
+ * Fixed-decimal number formatting with a non-finite fallback.
56
+ *
57
+ * Single home for the identical `fmt(x, d)` copies in backtest_ama_slope_huber
58
+ * and backtest_lookback_drawdown, plus the strict/padded variant in
59
+ * bot_fitting/shared_utils (`fmt(x, d, { fallback: ' n/a', strict: true })`).
60
+ */
61
+ function fmtNum(x, d = 2, { fallback = 'n/a', strict = false } = {}) {
62
+ const finite = strict ? Number.isFinite(x) : Number.isFinite(Number(x));
63
+ return finite ? Number(x).toFixed(d) : fallback;
64
+ }
65
+ function quantize(value, quantum) {
66
+ if (quantum == null || !Number.isFinite(quantum) || quantum <= 0)
67
+ return value;
68
+ return Math.round(value / quantum) * quantum;
69
+ }
70
+ /**
71
+ * Evenly spaced geometric progression from `min` to `max` (endpoints forced).
72
+ * Values are optionally snapped to `quantum` and clamped back into the range,
73
+ * rounded to `decimals`, deduped and sorted ascending. Single home for the
74
+ * helper previously copied into analyze_lambda_vs_slow and
75
+ * optimizer_high_resolution (which used different rounding precision).
76
+ */
77
+ function geometricRange(min, max, count, { decimals = 10, quantum = null } = {}) {
78
+ const out = [];
79
+ const ratio = Math.pow(max / min, 1 / (count - 1));
80
+ for (let i = 0; i < count; i++) {
81
+ let v = min * Math.pow(ratio, i);
82
+ if (i === 0)
83
+ v = min;
84
+ if (i === count - 1)
85
+ v = max;
86
+ v = quantize(v, quantum);
87
+ v = Math.max(min, Math.min(max, v));
88
+ out.push(parseFloat(v.toFixed(decimals)));
89
+ }
90
+ return [...new Set(out)].sort((a, b) => a - b);
91
+ }
24
92
  function loadCandleFile(filePath) {
25
93
  if (!filePath || !fs.existsSync(filePath))
26
94
  return { candles: [], meta: null };
@@ -34,5 +102,52 @@ function loadCandleFile(filePath) {
34
102
  return { candles: r.data, meta: r };
35
103
  return { candles: [], meta: null };
36
104
  }
37
- export { range, calcStdDev, getCandleClose, getCandleTimestamp, normalizeCandle, loadCandleFile, };
105
+ /**
106
+ * Load candle records from a single JSON file or a directory of monthly JSON
107
+ * shards.
108
+ *
109
+ * Directory entries are filtered to `*.json` (manifests skipped) and sorted;
110
+ * shards are merged and deduped by candle time (a later file overwrites an
111
+ * earlier duplicate). Returns the merged candles, the first non-null `meta` and
112
+ * the file count. Single home for the loader previously copied into
113
+ * backtest_ama_slope_huber.
114
+ */
115
+ function loadCandleSeries(input) {
116
+ const resolved = path.resolve(input);
117
+ if (!fs.existsSync(resolved))
118
+ throw new Error(`Data path not found: ${resolved}`);
119
+ const stat = fs.statSync(resolved);
120
+ const files = stat.isDirectory()
121
+ ? fs.readdirSync(resolved)
122
+ .filter((name) => /\.json$/i.test(name) && !/manifest/i.test(name))
123
+ .map((name) => path.join(resolved, name))
124
+ .sort()
125
+ : [resolved];
126
+ const byTime = new Map();
127
+ let meta = null;
128
+ for (const file of files) {
129
+ let raw = null;
130
+ try {
131
+ raw = readJSON(file);
132
+ }
133
+ catch {
134
+ continue;
135
+ }
136
+ const arr = Array.isArray(raw?.candles) ? raw.candles : (Array.isArray(raw) ? raw : null);
137
+ if (!arr || arr.length === 0)
138
+ continue;
139
+ if (!meta && raw?.meta)
140
+ meta = raw.meta;
141
+ for (const c of arr) {
142
+ const n = normalizeCandle(c);
143
+ if (n)
144
+ byTime.set(n.time, { time: n.time, open: n.open, high: n.high, low: n.low, close: n.close, volume: n.volume });
145
+ }
146
+ }
147
+ const candles = [...byTime.values()].sort((a, b) => a.time - b.time);
148
+ if (candles.length === 0)
149
+ throw new Error(`No candles found under ${resolved}`);
150
+ return { candles, meta, files: files.length };
151
+ }
152
+ export { range, calcStdDev, geometricRange, median, percentile, fmtNum, getCandleClose, getCandleTimestamp, normalizeCandle, loadCandleFile, loadCandleSeries, };
38
153
  //# sourceMappingURL=math_utils.js.map
@@ -20,6 +20,27 @@ declare class JsonFileSource {
20
20
  timestamp: number;
21
21
  };
22
22
  }
23
+ interface InlineConfig {
24
+ candles: Record<string, unknown>[];
25
+ meta?: Record<string, unknown> | null;
26
+ name?: string;
27
+ }
28
+ /**
29
+ * In-memory candle series (no backing file). Used when discovery assembles a
30
+ * series across month shards, where there is no single whole-history file to
31
+ * hand JsonFileSource.
32
+ */
33
+ declare class InlineCandleSource {
34
+ candles: Record<string, unknown>[];
35
+ meta: Record<string, unknown> | null;
36
+ name: string;
37
+ constructor(config: InlineConfig);
38
+ fetchCandles(): Promise<Record<string, unknown>[]>;
39
+ extractMarketPrice(candle: unknown): {
40
+ marketPrice: number;
41
+ timestamp: number;
42
+ };
43
+ }
23
44
  interface MarketAdapterConfig {
24
45
  stateDir?: string;
25
46
  botKey: string;
@@ -36,6 +57,6 @@ declare class MarketAdapterSource {
36
57
  timestamp: number;
37
58
  };
38
59
  }
39
- declare function createSource(type: string, config: JsonFileConfig | MarketAdapterConfig): JsonFileSource | MarketAdapterSource;
60
+ declare function createSource(type: string, config: JsonFileConfig | InlineConfig | MarketAdapterConfig): JsonFileSource | InlineCandleSource | MarketAdapterSource;
40
61
  export { createSource };
41
62
  //# sourceMappingURL=price_sources.d.ts.map
@@ -40,6 +40,27 @@ class JsonFileSource {
40
40
  return { marketPrice: getCandleClose(candle), timestamp: getCandleTimestamp(candle) };
41
41
  }
42
42
  }
43
+ /**
44
+ * In-memory candle series (no backing file). Used when discovery assembles a
45
+ * series across month shards, where there is no single whole-history file to
46
+ * hand JsonFileSource.
47
+ */
48
+ class InlineCandleSource {
49
+ candles;
50
+ meta;
51
+ name;
52
+ constructor(config) {
53
+ this.candles = config.candles || [];
54
+ this.meta = config.meta || null;
55
+ this.name = `inline:${config.name || 'lp-series'}`;
56
+ }
57
+ async fetchCandles() {
58
+ return this.candles;
59
+ }
60
+ extractMarketPrice(candle) {
61
+ return { marketPrice: getCandleClose(candle), timestamp: getCandleTimestamp(candle) };
62
+ }
63
+ }
43
64
  class MarketAdapterSource {
44
65
  stateDir;
45
66
  centersFile;
@@ -91,6 +112,8 @@ function createSource(type, config) {
91
112
  switch (type.toLowerCase()) {
92
113
  case 'json':
93
114
  return new JsonFileSource(config);
115
+ case 'inline':
116
+ return new InlineCandleSource(config);
94
117
  case 'market_adapter':
95
118
  return new MarketAdapterSource(config);
96
119
  default:
@@ -4,7 +4,7 @@ import { PATHS } from '../modules/paths.js';
4
4
  import { MARKET_ADAPTER } from '../modules/constants.js';
5
5
  import { createSource } from './price_sources.js';
6
6
  import { resolveCandleFile, resolveAmaConfig, resolveAmaKey, loadBotSettings, computeBotKey } from './bot_key_utils.js';
7
- import { findLatestLpData } from '../market_adapter/utils/data_discovery.js';
7
+ import { loadLatestLpSeries, loadLpSeriesFromPath } from '../market_adapter/utils/data_discovery.js';
8
8
  import { loadCandleFile } from './math_utils.js';
9
9
  const INTERVAL_LABEL = MARKET_ADAPTER.RUNTIME_DEFAULTS.intervalLabel;
10
10
  function listAvailableBots() {
@@ -51,19 +51,40 @@ function resolveSource(config, options = {}) {
51
51
  };
52
52
  }
53
53
  if (config.type === 'json') {
54
- let filePath = config.filePath;
54
+ const filePath = config.filePath;
55
55
  if (!filePath) {
56
- const autoFile = findLatestLpData();
57
- if (autoFile) {
58
- filePath = autoFile;
59
- if (!quiet)
60
- console.log(`[Source] Auto-discovered LP data: ${autoFile}`);
61
- }
62
- else {
56
+ const series = loadLatestLpSeries();
57
+ if (!series) {
63
58
  throw new Error('No --file provided and no LP data auto-discovered in market_adapter/data/lp');
64
59
  }
60
+ if (!quiet) {
61
+ console.log(`[Source] Auto-discovered LP data: ${series.path}${series.assembled ? ' (assembled from month shards)' : ''}`);
62
+ }
63
+ const source = createSource('inline', {
64
+ candles: series.candles,
65
+ meta: series.meta,
66
+ name: series.path,
67
+ });
68
+ if (config.botKey) {
69
+ return { source, botKey: config.botKey, amaConfig: resolveAmaConfig(config.botKey), amaKey: resolveAmaKey(config.botKey), meta: series.meta };
70
+ }
71
+ return { source, amaConfig: resolveAmaConfig(''), amaKey: 'AMA3', meta: series.meta };
72
+ }
73
+ const series = loadLpSeriesFromPath(path.resolve(filePath));
74
+ if (series && series.assembled) {
75
+ if (!quiet)
76
+ console.log(`[Source] Assembled ${path.basename(filePath)} across month shards`);
77
+ const source = createSource('inline', {
78
+ candles: series.candles,
79
+ meta: series.meta,
80
+ name: series.path,
81
+ });
82
+ if (config.botKey) {
83
+ return { source, botKey: config.botKey, amaConfig: resolveAmaConfig(config.botKey), amaKey: resolveAmaKey(config.botKey), meta: series.meta };
84
+ }
85
+ return { source, amaConfig: resolveAmaConfig(''), amaKey: 'AMA3', meta: series.meta };
65
86
  }
66
- const source = createSource('json', { filePath: filePath });
87
+ const source = createSource('json', { filePath });
67
88
  const meta = loadCandleFile(filePath).meta;
68
89
  if (config.botKey) {
69
90
  return { source, botKey: config.botKey, amaConfig: resolveAmaConfig(config.botKey), amaKey: resolveAmaKey(config.botKey), meta };
@@ -72,6 +72,12 @@ interface TradeProfitabilityOptions {
72
72
  report: string | null;
73
73
  }
74
74
  declare function parseArgs(argv?: string[]): TradeProfitabilityOptions;
75
+ /**
76
+ * Classify fills into trade records. Direction/base/quote resolution and the
77
+ * amount sanity check live in fills_source.classifyFill (single home, also used
78
+ * by grid_correction_check); this wrapper owns the PnL-specific market-fee
79
+ * handling, the asset/pair filters and the unknown-precision skip count.
80
+ */
75
81
  declare function classifyFills(fills: FillRecord[], filterAsset: string | null, pairFilter?: {
76
82
  base: string;
77
83
  quote: string;