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
@@ -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;
@@ -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;