dexbot 1.6.10 → 1.6.11

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 (76) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +1 -0
  3. package/analysis/README.md +49 -11
  4. package/analysis/bot_fitting/README.md +12 -0
  5. package/analysis/tradingview/README.md +2 -2
  6. package/analysis/trend_detection/DYNAMIC_WEIGHT_RESEARCH.md +10 -36
  7. package/analysis/trend_detection/README.md +31 -0
  8. package/claw/package.json +1 -1
  9. package/claw/runtimes/openclaw-plugin/openclaw.plugin.json +1 -1
  10. package/claw/runtimes/openclaw-plugin/package.json +1 -1
  11. package/dist/analysis/account_resolver.js +13 -1
  12. package/dist/analysis/analyze_dynamic_weight.js +0 -3
  13. package/dist/analysis/bot_fitting/backtest_ama_sweep.js +51 -7
  14. package/dist/analysis/bot_fitting/backtest_bot_fitting.js +65 -7
  15. package/dist/analysis/bot_fitting/backtest_lookback_drawdown.d.ts +21 -0
  16. package/dist/analysis/bot_fitting/backtest_lookback_drawdown.js +315 -0
  17. package/dist/analysis/bot_key_utils.js +3 -13
  18. package/dist/analysis/fills_cache.d.ts +64 -0
  19. package/dist/analysis/fills_cache.js +254 -0
  20. package/dist/analysis/fills_source.js +5 -2
  21. package/dist/analysis/pnl_report.d.ts +39 -0
  22. package/dist/analysis/pnl_report.js +325 -0
  23. package/dist/analysis/resolve_source.d.ts +0 -1
  24. package/dist/analysis/trade_profitability.d.ts +11 -2
  25. package/dist/analysis/trade_profitability.js +187 -61
  26. package/dist/analysis/tradingview/analyze_tradingview.js +3 -5
  27. package/dist/analysis/tradingview/grid_reset_config.d.ts +2 -0
  28. package/dist/analysis/tradingview/grid_reset_config.js +18 -1
  29. package/dist/analysis/tradingview/grid_reset_sim.d.ts +1 -0
  30. package/dist/analysis/tradingview/grid_reset_sim.js +38 -2
  31. package/dist/analysis/tradingview/tradingview_uplot_chart_generator.js +2 -1
  32. package/dist/analysis/trend_detection/backtest_ama_slope_huber.d.ts +102 -0
  33. package/dist/analysis/trend_detection/backtest_ama_slope_huber.js +725 -0
  34. package/dist/analysis/trend_detection/dynamic_weight_chart_generator.js +22 -108
  35. package/dist/analysis/trend_detection/huber_scale_variants.d.ts +34 -0
  36. package/dist/analysis/trend_detection/huber_scale_variants.js +176 -0
  37. package/dist/dexbot.js +6 -3
  38. package/dist/market_adapter/core/market_adapter_service.d.ts +15 -0
  39. package/dist/market_adapter/core/market_adapter_service.js +64 -5
  40. package/dist/market_adapter/core/strategies/ama.d.ts +1 -1
  41. package/dist/market_adapter/core/strategies/ama.js +5 -28
  42. package/dist/market_adapter/core/strategies/dynamic_weight_series.d.ts +2 -2
  43. package/dist/market_adapter/core/strategies/dynamic_weight_series.js +2 -2
  44. package/dist/market_adapter/inputs/fetch_cex_synthetic_data.js +1 -2
  45. package/dist/market_adapter/log_format.js +1 -2
  46. package/dist/market_adapter/market_adapter.d.ts +0 -1
  47. package/dist/market_adapter/market_adapter.js +2 -27
  48. package/dist/modules/constants.d.ts +3 -1
  49. package/dist/modules/constants.js +39 -15
  50. package/dist/modules/dexbot_maintenance_runtime.js +12 -0
  51. package/dist/modules/order/accounting.js +25 -0
  52. package/dist/modules/order/format.d.ts +35 -15
  53. package/dist/modules/order/format.js +62 -16
  54. package/dist/modules/order/manager.d.ts +1 -0
  55. package/dist/modules/order/manager.js +22 -1
  56. package/dist/modules/order/sync_engine.js +15 -1
  57. package/dist/modules/order/utils/order.d.ts +21 -1
  58. package/dist/modules/order/utils/order.js +80 -2
  59. package/dist/modules/paths.d.ts +2 -0
  60. package/dist/modules/paths.js +2 -0
  61. package/dist/modules/utils/time_range.d.ts +15 -0
  62. package/dist/modules/utils/time_range.js +18 -0
  63. package/dist/scripts/analyze-orders.js +1 -44
  64. package/dist/scripts/chart_command.js +3 -4
  65. package/dist/scripts/pnl.d.ts +3 -0
  66. package/dist/scripts/pnl.js +40 -0
  67. package/dist/unlock.js +34 -0
  68. package/docs/DEXBOT_COMPARISON.md +3 -3
  69. package/docs/EVOLUTION.md +10 -9
  70. package/docs/FUND_MOVEMENT_AND_ACCOUNTING.md +1 -1
  71. package/docs/GRID_RECALCULATION.md +12 -15
  72. package/docs/README.md +2 -2
  73. package/docs/WORKFLOW.md +1 -0
  74. package/market_adapter/README.md +4 -26
  75. package/package.json +1 -1
  76. package/scripts/README.md +14 -0
@@ -4,6 +4,7 @@ import path from 'node:path';
4
4
  import { pathToFileURL } from 'node:url';
5
5
  import { calculateAMA, getAmaWarmupBars } from '../../market_adapter/core/strategies/ama.js';
6
6
  import { computeHuberWindowSlopePct } from '../../market_adapter/core/strategies/ama_slope_model.js';
7
+ import { applyAsymmetricBounds } from '../../market_adapter/core/asymmetric_bounds.js';
7
8
  import { range } from '../math_utils.js';
8
9
  import { parseListOrRange, loadLpData, fmt } from './shared_utils.js';
9
10
  import { getStorage } from '../../modules/storage/index.js';
@@ -399,6 +400,20 @@ function simulateForParams(candles, amaValues, params) {
399
400
  const slopeDeltaThresholdPct = (SLOPE_TRIGGER_FACTOR / 100) * SLOPE_MAX_PCT;
400
401
  const makerCreateFeeBts = btsCreateFee * makerCreateFactor;
401
402
  const stepUpFrac = 1 + incrementPct; // one-rail-step rotation distance
403
+ // Optional gate/band overrides. `slopePersistBars` follows the production
404
+ // default (constants) unless the caller pins a value, so the fill model's
405
+ // reset path matches live whenever asymmetricBounds enables trigger B.
406
+ // slopePersistBars persistence gate on trigger B
407
+ // bandTilt apply applyAsymmetricBounds to the built band
408
+ const slopeLookbackBars = Number.isFinite(params.slopeLookbackBars) && params.slopeLookbackBars >= 2
409
+ ? Math.round(params.slopeLookbackBars) : SLOPE_LOOKBACK_BARS;
410
+ const slopePersistBars = Number.isFinite(params.slopePersistBars) && params.slopePersistBars >= 1
411
+ ? Math.round(params.slopePersistBars)
412
+ : (MARKET_ADAPTER.AMA_SLOPE_PERSIST_ENABLED === true && Number(MARKET_ADAPTER.AMA_SLOPE_PERSIST_BARS) >= 1
413
+ ? Math.round(Number(MARKET_ADAPTER.AMA_SLOPE_PERSIST_BARS)) : 1);
414
+ const bandTilt = params.bandTilt === true;
415
+ const maxAsymmetryFactor = Number.isFinite(params.maxAsymmetryFactor)
416
+ ? Number(params.maxAsymmetryFactor) : MARKET_ADAPTER.ASYMMETRIC_BOUNDS_MAX_ASYMMETRY_FACTOR;
402
417
  // First tradable bar: need a finite positive AMA to anchor the chain.
403
418
  let startIdx = Math.min(skip, candles.length - 1);
404
419
  let gridCenter = Number.NaN;
@@ -413,8 +428,8 @@ function simulateForParams(candles, amaValues, params) {
413
428
  // Production AMA slope series (%/bar averaged over the lookback window).
414
429
  // Evaluated over full history like the live adapter (only bar-index guards).
415
430
  const slopeAt = new Array(candles.length).fill(null);
416
- for (let j = SLOPE_LOOKBACK_BARS; j < candles.length; j++) {
417
- const s = computeHuberWindowSlopePct(amaValues, j, SLOPE_LOOKBACK_BARS);
431
+ for (let j = slopeLookbackBars; j < candles.length; j++) {
432
+ const s = computeHuberWindowSlopePct(amaValues, j, slopeLookbackBars);
418
433
  if (s != null && Number.isFinite(s))
419
434
  slopeAt[j] = s;
420
435
  }
@@ -436,7 +451,26 @@ function simulateForParams(candles, amaValues, params) {
436
451
  : 0;
437
452
  if (offsetPct !== 0)
438
453
  offsetAppliedCount++;
439
- const effCenter = center * (1 + offsetPct / 100);
454
+ // applyAsymmetricBounds scales BOTH base bounds by one factor, so the
455
+ // tilted band is exactly a same-ratio band centred at center*scale
456
+ // (when the narrowing-side guard does not bind).
457
+ let scale = 1;
458
+ if (bandTilt && slopePct != null && Math.abs(slopePct) > SLOPE_NEUTRAL_ZONE_PCT) {
459
+ const trend = slopePct > 0 ? 'UP' : 'DOWN';
460
+ const tilt = applyAsymmetricBounds({
461
+ centerPrice: center,
462
+ minPrice: center / maxMinRatio,
463
+ maxPrice: center * maxMinRatio,
464
+ trend,
465
+ slopeOffset: Math.abs(slopePct),
466
+ maxSlopeOffset: SLOPE_MAX_PCT,
467
+ maxAsymmetryFactor,
468
+ });
469
+ const baseMax = center * maxMinRatio;
470
+ if (Number.isFinite(tilt.resolvedMaxPrice) && baseMax > 0)
471
+ scale = tilt.resolvedMaxPrice / baseMax;
472
+ }
473
+ const effCenter = center * scale * (1 + offsetPct / 100);
440
474
  const built = buildProductionGrid(effCenter, spreadPct, incrementPct, maxMinRatio, activeOrders);
441
475
  activeRail = built.rail;
442
476
  orders.clear();
@@ -481,6 +515,9 @@ function simulateForParams(candles, amaValues, params) {
481
515
  break;
482
516
  }
483
517
  }
518
+ // Persistence-gate state for trigger B.
519
+ let slopePersistCount = 0;
520
+ let slopePersistDir = 0;
484
521
  for (let i = startIdx + 1; i < candles.length; i++) {
485
522
  const ama = amaValues[i];
486
523
  const hi = candles[i].high;
@@ -496,13 +533,34 @@ function simulateForParams(candles, amaValues, params) {
496
533
  }
497
534
  }
498
535
  if (!shouldReset && asymmetricBounds && slopeBaseline != null && slopeAt[i] != null) {
499
- const slopeDeltaPct = Math.abs(slopeAt[i] - slopeBaseline);
500
- if (slopeDeltaPct >= slopeDeltaThresholdPct) {
501
- shouldReset = true;
502
- slopeTriggerCount++;
536
+ const crossed = Math.abs(slopeAt[i] - slopeBaseline) >= slopeDeltaThresholdPct;
537
+ if (slopePersistBars <= 1) {
538
+ if (crossed) {
539
+ shouldReset = true;
540
+ slopeTriggerCount++;
541
+ }
542
+ }
543
+ else if (!crossed) {
544
+ slopePersistCount = 0;
545
+ slopePersistDir = 0;
546
+ }
547
+ else {
548
+ const dir = Math.sign(slopeAt[i] - slopeBaseline);
549
+ if (dir !== 0 && dir === slopePersistDir)
550
+ slopePersistCount++;
551
+ else {
552
+ slopePersistDir = dir;
553
+ slopePersistCount = 1;
554
+ }
555
+ if (slopePersistCount >= slopePersistBars) {
556
+ shouldReset = true;
557
+ slopeTriggerCount++;
558
+ }
503
559
  }
504
560
  }
505
561
  if (shouldReset && Number.isFinite(ama) && ama > 0) {
562
+ slopePersistCount = 0;
563
+ slopePersistDir = 0;
506
564
  canceledOnReposition += orders.size;
507
565
  btsFeesBts += orders.size * btsCancelFee;
508
566
  orders.clear(); // inventory survives — resync never market-sells
@@ -0,0 +1,21 @@
1
+ declare function parseArgs(): {
2
+ dataPath: string;
3
+ amaName: string;
4
+ lookbacks: number[];
5
+ spreads: number[];
6
+ increments: number[];
7
+ ratios: number[];
8
+ maxOrders: number;
9
+ feeRoundtripPct: number;
10
+ capital: number;
11
+ repositionPct: number;
12
+ asymmetricBounds: boolean;
13
+ btsCreateFee: number;
14
+ btsCancelFee: number;
15
+ makerCreateFactor: number;
16
+ txFeePrice: number;
17
+ outPath: string | null;
18
+ };
19
+ declare function run(): void;
20
+ export { run, parseArgs };
21
+ //# sourceMappingURL=backtest_lookback_drawdown.d.ts.map
@@ -0,0 +1,315 @@
1
+ 'use strict';
2
+ /**
3
+ * LOOKBACK → FILL-MODEL DRAWDOWN BACKTEST
4
+ *
5
+ * Runs the persistent-grid simulation (`simulatePersistentGrid`, shared with
6
+ * `backtest_ama_sweep`) across a set of Huber slope lookbacks over ONE geometry
7
+ * grid, so the lookback's effect on realized drawdown / net capture / reset
8
+ * churn is compared with geometry held fixed. Every
9
+ * (spread, increment, ratio, weight) combo is run once per lookback and matched
10
+ * by key, so the comparison is PAIRED — the delta isolates the window, not the
11
+ * strategy shape.
12
+ *
13
+ * Why this exists (see docs/AMA_SLOPE_WINDOW.md): the signal-side window metrics
14
+ * (wobble / lag / wrong-way) are second-order, so the open question is whether
15
+ * the window moves the fill-model economics. It does, but the SIGN IS
16
+ * POOL-DEPENDENT — on a long-lived liquid pair a 16h window beat 12h on both
17
+ * drawdown and net capture across 100% of geometries, while on a shorter pair
18
+ * the ordering reversed. Treat the paired delta, never the absolute level (the
19
+ * realized-equity drawdown here is model-shaped and can exceed capital), and
20
+ * re-run per pool before drawing a conclusion.
21
+ *
22
+ * Trigger B (slope-delta reset) and the slope-ratio grid offset only act under
23
+ * the production asymmetricBounds whitelist, so the lookback has no effect
24
+ * unless `--asymmetric-bounds` is on — it defaults ON here, since a lookback
25
+ * comparison is the whole point. Pass `--no-asymmetric-bounds` for the
26
+ * non-whitelisted path (where the result collapses to lookback-independent).
27
+ *
28
+ * Usage:
29
+ * node dist/analysis/bot_fitting/backtest_lookback_drawdown.js \
30
+ * --data market_adapter/data/lp/<market-pair> --lookbacks 12,14,16,20
31
+ */
32
+ import fs from 'node:fs';
33
+ import path from 'node:path';
34
+ import { pathToFileURL } from 'node:url';
35
+ import { calculateAMA, getAmaWarmupBars } from '../../market_adapter/core/strategies/ama.js';
36
+ import { loadCandles } from '../trend_detection/backtest_ama_slope_huber.js';
37
+ import { parseListOrRange } from './shared_utils.js';
38
+ import { simulatePersistentGrid, WEIGHT_PROFILES } from './backtest_ama_sweep.js';
39
+ import { getStorage } from '../../modules/storage/index.js';
40
+ import { PATHS } from '../../modules/paths.js';
41
+ import { MARKET_ADAPTER } from '../../modules/constants.js';
42
+ const { writeJSON } = getStorage();
43
+ const MA = MARKET_ADAPTER;
44
+ // A modest geometry grid — enough spread/increment/ratio variety to keep the
45
+ // paired comparison honest without the full sweep's cost. Override per run.
46
+ const DEFAULT_SPREADS = [1, 2, 3, 4];
47
+ const DEFAULT_INCREMENTS = [0.5, 1, 2];
48
+ const DEFAULT_RATIOS = [1.2, 2, 5];
49
+ const DEFAULT_LOOKBACKS = [12, 14, 16, 20];
50
+ // The shipped default window — the reference for the paired delta table.
51
+ const REFERENCE_LOOKBACK = MA.DYNAMIC_WEIGHT_AMA_LOOKBACK_BARS;
52
+ function parseArgs() {
53
+ const args = process.argv.slice(2);
54
+ const out = {
55
+ dataPath: '',
56
+ amaName: MA.DEFAULT_AMA_KEY,
57
+ lookbacks: [...DEFAULT_LOOKBACKS],
58
+ spreads: [...DEFAULT_SPREADS],
59
+ increments: [...DEFAULT_INCREMENTS],
60
+ ratios: [...DEFAULT_RATIOS],
61
+ maxOrders: 20,
62
+ feeRoundtripPct: 0.20,
63
+ capital: 10000,
64
+ repositionPct: MA.AMA_DELTA_THRESHOLD_PERCENT,
65
+ asymmetricBounds: true,
66
+ btsCreateFee: 0.4826,
67
+ btsCancelFee: 0.00482,
68
+ makerCreateFactor: 0.10,
69
+ txFeePrice: 1.0,
70
+ outPath: null,
71
+ };
72
+ for (let i = 0; i < args.length; i++) {
73
+ const arg = args[i];
74
+ if (arg === '--help' || arg === '-h') {
75
+ printHelp();
76
+ process.exit(0);
77
+ }
78
+ if (arg === '--asymmetric-bounds') {
79
+ out.asymmetricBounds = true;
80
+ continue;
81
+ }
82
+ if (arg === '--no-asymmetric-bounds') {
83
+ out.asymmetricBounds = false;
84
+ continue;
85
+ }
86
+ const val = args[i + 1];
87
+ if (!val)
88
+ continue;
89
+ switch (arg) {
90
+ case '--data':
91
+ out.dataPath = val;
92
+ i++;
93
+ break;
94
+ case '--ama':
95
+ out.amaName = val;
96
+ i++;
97
+ break;
98
+ case '--lookbacks':
99
+ out.lookbacks = parseListOrRange(val, out.lookbacks).map((v) => Math.round(v));
100
+ i++;
101
+ break;
102
+ case '--spread':
103
+ out.spreads = parseListOrRange(val, out.spreads);
104
+ i++;
105
+ break;
106
+ case '--increment':
107
+ out.increments = parseListOrRange(val, out.increments);
108
+ i++;
109
+ break;
110
+ case '--ratio':
111
+ out.ratios = parseListOrRange(val, out.ratios);
112
+ i++;
113
+ break;
114
+ case '--max-orders':
115
+ out.maxOrders = Number(val);
116
+ i++;
117
+ break;
118
+ case '--fee':
119
+ out.feeRoundtripPct = Number(val);
120
+ i++;
121
+ break;
122
+ case '--capital':
123
+ out.capital = Number(val);
124
+ i++;
125
+ break;
126
+ case '--reposition':
127
+ out.repositionPct = Number(val);
128
+ i++;
129
+ break;
130
+ case '--bts-create-fee':
131
+ out.btsCreateFee = Number(val);
132
+ i++;
133
+ break;
134
+ case '--bts-cancel-fee':
135
+ out.btsCancelFee = Number(val);
136
+ i++;
137
+ break;
138
+ case '--maker-create-factor':
139
+ out.makerCreateFactor = Number(val);
140
+ i++;
141
+ break;
142
+ case '--tx-fee-price':
143
+ out.txFeePrice = Number(val);
144
+ i++;
145
+ break;
146
+ case '--out':
147
+ out.outPath = path.resolve(val);
148
+ i++;
149
+ break;
150
+ }
151
+ }
152
+ if (!out.dataPath)
153
+ throw new Error('--data <path-to-lp-candles.json|dir> is required');
154
+ out.lookbacks = [...new Set(out.lookbacks)].filter((v) => Number.isFinite(v) && v >= 2).sort((a, b) => a - b);
155
+ if (out.lookbacks.length === 0)
156
+ throw new Error('--lookbacks must resolve to a non-empty list of bars >= 2');
157
+ return out;
158
+ }
159
+ function printHelp() {
160
+ console.log('Lookback -> fill-model drawdown backtest (paired persistent-grid simulation)');
161
+ console.log('');
162
+ console.log('Usage: node dist/analysis/bot_fitting/backtest_lookback_drawdown.js [options]');
163
+ console.log('');
164
+ console.log('Options:');
165
+ console.log(' --data <path> LP candle file or shard directory (required)');
166
+ console.log(` --ama <AMA1..AMA4> Built-in AMA preset (default: ${MA.DEFAULT_AMA_KEY})`);
167
+ console.log(` --lookbacks <spec> Lookbacks to compare: 12,14,16,20 (default: ${DEFAULT_LOOKBACKS.join(',')})`);
168
+ console.log(` --spread <spec> Spread grid (default: ${DEFAULT_SPREADS.join(',')})`);
169
+ console.log(` --increment <spec> Increment grid (default: ${DEFAULT_INCREMENTS.join(',')})`);
170
+ console.log(` --ratio <spec> Max/min ratio grid (default: ${DEFAULT_RATIOS.join(',')})`);
171
+ console.log(' --max-orders <n> Size cap per side (default: 20)');
172
+ console.log(' --fee <pct> Round-trip fee % (default: 0.20)');
173
+ console.log(' --capital <n> Notional capital per side (default: 10000)');
174
+ console.log(` --reposition <pct> AMA drift % to re-center (default: ${MA.AMA_DELTA_THRESHOLD_PERCENT})`);
175
+ console.log(' --no-asymmetric-bounds Disable trigger B + slope offset (default: enabled)');
176
+ console.log(' --out <path> JSON output path');
177
+ }
178
+ function resolveAma(name) {
179
+ const presets = MA.AMAS;
180
+ const preset = presets[name];
181
+ if (!preset)
182
+ throw new Error(`Unknown AMA preset '${name}' (expected ${Object.keys(presets).join(', ')})`);
183
+ return { name, er: preset.erPeriod, fast: preset.fastPeriod, slow: preset.slowPeriod };
184
+ }
185
+ function median(values) {
186
+ const s = values.slice().sort((a, b) => a - b);
187
+ const mid = s.length >> 1;
188
+ return s.length % 2 ? s[mid] : (s[mid - 1] + s[mid]) / 2;
189
+ }
190
+ function percentile(values, pct) {
191
+ const s = values.slice().sort((a, b) => a - b);
192
+ return s[Math.min(s.length - 1, Math.max(0, Math.floor((pct / 100) * (s.length - 1))))];
193
+ }
194
+ function fmt(x, d = 2) {
195
+ return Number.isFinite(Number(x)) ? Number(x).toFixed(d) : 'n/a';
196
+ }
197
+ function run() {
198
+ const cfg = parseArgs();
199
+ const amaDef = resolveAma(cfg.amaName);
200
+ const { candles, files } = loadCandles(cfg.dataPath);
201
+ const closes = candles.map((c) => c.close);
202
+ const amaValues = calculateAMA(closes, { erPeriod: amaDef.er, fastPeriod: amaDef.fast, slowPeriod: amaDef.slow });
203
+ const warmupBars = getAmaWarmupBars(amaDef.er, amaDef.slow, 0, amaDef.fast);
204
+ const weightEntries = Object.entries(WEIGHT_PROFILES);
205
+ const combos = [];
206
+ for (const spreadPct of cfg.spreads)
207
+ for (const incrementPct of cfg.increments)
208
+ for (const maxMinRatio of cfg.ratios)
209
+ for (const [weightName, weightFactor] of weightEntries)
210
+ combos.push({ spreadPct, incrementPct, maxMinRatio, weightName, weightFactor });
211
+ console.log('================================================================================');
212
+ console.log(' LOOKBACK -> FILL-MODEL DRAWDOWN (paired persistent-grid simulation)');
213
+ console.log('================================================================================');
214
+ console.log(` Data: ${path.resolve(cfg.dataPath)} (${files} file(s), ${candles.length} candles)`);
215
+ console.log(` AMA: ${amaDef.name} (er=${amaDef.er}, fast=${amaDef.fast}, slow=${amaDef.slow}), warmup ${warmupBars} bars`);
216
+ console.log(` Lookbacks: ${cfg.lookbacks.join(', ')} bars | geometries: ${combos.length}`);
217
+ console.log(` Bounds: asymmetricBounds ${cfg.asymmetricBounds ? 'ON (trigger B + slope offset active)' : 'OFF (lookback-independent)'}`);
218
+ console.log(` Reference: ${REFERENCE_LOOKBACK}h (shipped default) for the paired delta\n`);
219
+ const byLookback = {};
220
+ for (const lb of cfg.lookbacks) {
221
+ const perGeom = {};
222
+ for (const c of combos) {
223
+ const sim = simulatePersistentGrid(candles, amaValues, {
224
+ spreadPct: c.spreadPct,
225
+ incrementPct: c.incrementPct / 100,
226
+ maxMinRatio: c.maxMinRatio,
227
+ maxOrders: cfg.maxOrders,
228
+ feeRoundtripPct: cfg.feeRoundtripPct,
229
+ capital: cfg.capital,
230
+ repositionThreshold: cfg.repositionPct / 100,
231
+ asymmetricBounds: cfg.asymmetricBounds,
232
+ btsCreateFee: cfg.btsCreateFee,
233
+ btsCancelFee: cfg.btsCancelFee,
234
+ makerCreateFactor: cfg.makerCreateFactor,
235
+ txFeePrice: cfg.txFeePrice,
236
+ warmupBars,
237
+ lookbackBars: lb,
238
+ }, c.weightName, c.weightFactor);
239
+ perGeom[`${c.spreadPct}|${c.incrementPct}|${c.maxMinRatio}|${c.weightName}`] = sim;
240
+ }
241
+ byLookback[lb] = perGeom;
242
+ process.stdout.write(` lb=${String(lb).padStart(2)} done (${combos.length} geometries)\n`);
243
+ }
244
+ // Paired keys: geometries with fills under EVERY lookback.
245
+ const ref = byLookback[REFERENCE_LOOKBACK] ?? byLookback[cfg.lookbacks[0]];
246
+ const keys = Object.keys(ref).filter((k) => cfg.lookbacks.every((lb) => byLookback[lb][k]?.matchedPairs > 0));
247
+ console.log('\n================================================================================');
248
+ console.log(' PER-LOOKBACK MEDIANS (over paired geometries)');
249
+ console.log('================================================================================');
250
+ const fields = [
251
+ ['maxDrawdown %', 'maxDrawdownPct', 1, 2],
252
+ ['netProfit / capital %', 'netProfitPerCapital', 100, 2],
253
+ ['pairs / day', 'pairsPerDay', 1, 2],
254
+ ['repositions', 'repositionCount', 1, 0],
255
+ ['slope-trigger resets', 'slopeTriggerCount', 1, 0],
256
+ ['maxInventoryExposure', 'maxInventoryExposure', 1, 0],
257
+ ];
258
+ console.log(' lb |' + cfg.lookbacks.map((lb) => String(lb).padStart(12)).join(''));
259
+ console.log(' ---+' + cfg.lookbacks.map(() => '------------').join(''));
260
+ for (const [label, field, mul, d] of fields) {
261
+ let row = ` ${label.padEnd(20).slice(0, 20)} |`;
262
+ for (const lb of cfg.lookbacks) {
263
+ row += fmt(median(keys.map((k) => byLookback[lb][k][field] * mul)), d).padStart(12);
264
+ }
265
+ console.log(row);
266
+ }
267
+ console.log('\n================================================================================');
268
+ console.log(` PAIRED DELTA vs ${REFERENCE_LOOKBACK}h (max drawdown) — negative = lower DD than reference`);
269
+ console.log('================================================================================');
270
+ console.log(' lb | median delta | p25 .. p75 | lower-DD geometries');
271
+ for (const lb of cfg.lookbacks) {
272
+ const deltas = keys.map((k) => byLookback[lb][k].maxDrawdownPct - ref[k].maxDrawdownPct);
273
+ const lower = deltas.filter((d) => d < 0).length;
274
+ console.log(` ${String(lb).padStart(2)} | ` +
275
+ `${fmt(median(deltas)).padStart(12)} | ` +
276
+ `${fmt(percentile(deltas, 25))} .. ${fmt(percentile(deltas, 75))} | ` +
277
+ `${lower}/${deltas.length} (${fmt((100 * lower) / deltas.length, 0)}%)`);
278
+ }
279
+ console.log('\n Reminder: the sign is pool-dependent and the absolute level is model-shaped');
280
+ console.log(' (realized-equity DD, bags excluded) — compare within a pool, not across.');
281
+ const base = path.basename(path.resolve(cfg.dataPath)).replace(/\.json$/i, '');
282
+ const outPath = cfg.outPath ?? path.join(PATHS.ANALYSIS.RESULTS_DIR, `lookback_drawdown_${base}.json`);
283
+ fs.mkdirSync(path.dirname(outPath), { recursive: true });
284
+ writeJSON(outPath, {
285
+ meta: {
286
+ generatedAt: new Date().toISOString(),
287
+ dataPath: path.relative(process.cwd(), path.resolve(cfg.dataPath)),
288
+ files,
289
+ candles: candles.length,
290
+ ama: amaDef,
291
+ warmupBars,
292
+ lookbacks: cfg.lookbacks,
293
+ referenceLookback: REFERENCE_LOOKBACK,
294
+ geometries: combos.length,
295
+ asymmetricBounds: cfg.asymmetricBounds,
296
+ feeRoundtripPct: cfg.feeRoundtripPct,
297
+ capital: cfg.capital,
298
+ repositionPct: cfg.repositionPct,
299
+ note: 'Paired persistent-grid comparison: each geometry is simulated once per lookback so the delta isolates the window. Drawdown is realized-equity peak-to-trough (carried bags excluded); the sign of the lookback delta is pool-dependent.',
300
+ },
301
+ results: byLookback,
302
+ });
303
+ console.log(`\nSaved: ${path.relative(process.cwd(), outPath)}`);
304
+ }
305
+ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
306
+ try {
307
+ run();
308
+ }
309
+ catch (err) {
310
+ console.error(err);
311
+ process.exit(1);
312
+ }
313
+ }
314
+ export { run, parseArgs };
315
+ //# sourceMappingURL=backtest_lookback_drawdown.js.map
@@ -459,20 +459,11 @@ function findBotKeyByAccountRef(ref, filePath = PATHS.PROFILES.BOTS_JSON) {
459
459
  }
460
460
  function resolveAmaConfig(botKey) {
461
461
  const botMeta = loadBotMeta(botKey);
462
- // Unknown bot key: fall back to the global erSmoothPeriod default (same
463
- // resolution path production uses) instead of forcing 0 — a typo'd
464
- // --bot-key must not silently research with different ER smoothing.
465
462
  if (!botMeta)
466
- return { ...MARKET_ADAPTER.AMAS.AMA3, erSmoothPeriod: Number(MARKET_ADAPTER.AMA_ER_SMOOTH_FAST_PERIOD) };
463
+ return { ...MARKET_ADAPTER.AMAS.AMA3 };
467
464
  const rawGridPrice = String(botMeta?.gridPrice || '').trim().toLowerCase();
468
465
  const isAmaKeyword = AMA_KEYWORDS.has(rawGridPrice);
469
- // erSmoothPeriod: bot.ama inline > global default (matches production resolveErSmoothPeriodForBot)
470
466
  const botAmaInline = (botMeta?.ama && typeof botMeta.ama === 'object') ? botMeta.ama : null;
471
- const rawErSmooth = botAmaInline && Object.prototype.hasOwnProperty.call(botAmaInline, 'erSmoothPeriod')
472
- ? Number(botAmaInline.erSmoothPeriod)
473
- : Number(MARKET_ADAPTER.AMA_ER_SMOOTH_FAST_PERIOD);
474
- const erSmoothPeriod = rawErSmooth === 0 ? 0
475
- : (Number.isFinite(rawErSmooth) && rawErSmooth >= 1 ? rawErSmooth : 0);
476
467
  // Priority: inline bot.ama overrides market profile for analysis visibility.
477
468
  // Production inverts this (profiles first, inline as fallback — market_adapter.ts:694-730).
478
469
  if (botAmaInline) {
@@ -480,7 +471,6 @@ function resolveAmaConfig(botKey) {
480
471
  erPeriod: Number(botAmaInline.erPeriod),
481
472
  fastPeriod: Number(botAmaInline.fastPeriod),
482
473
  slowPeriod: Number(botAmaInline.slowPeriod),
483
- erSmoothPeriod,
484
474
  };
485
475
  if (isAmaKeyword) {
486
476
  const key = rawGridPrice === 'ama' ? DEFAULT_AMA_KEY : rawGridPrice.toUpperCase();
@@ -525,14 +515,14 @@ function resolveAmaConfig(botKey) {
525
515
  const fromBuiltin = BUILTIN_AMAS[requestedKey] || BUILTIN_AMAS[fallbackKey];
526
516
  const base = fromProfile || fromBuiltin;
527
517
  if (base) {
528
- return { erPeriod: base.erPeriod, fastPeriod: base.fastPeriod, slowPeriod: base.slowPeriod, erSmoothPeriod };
518
+ return { erPeriod: base.erPeriod, fastPeriod: base.fastPeriod, slowPeriod: base.slowPeriod };
529
519
  }
530
520
  }
531
521
  const key = isAmaKeyword
532
522
  ? (rawGridPrice === 'ama' ? DEFAULT_AMA_KEY : rawGridPrice.toUpperCase())
533
523
  : DEFAULT_AMA_KEY;
534
524
  const builtin = BUILTIN_AMAS[key] || MARKET_ADAPTER.AMAS.AMA3;
535
- return { erPeriod: builtin.erPeriod, fastPeriod: builtin.fastPeriod, slowPeriod: builtin.slowPeriod, erSmoothPeriod };
525
+ return { erPeriod: builtin.erPeriod, fastPeriod: builtin.fastPeriod, slowPeriod: builtin.slowPeriod };
536
526
  }
537
527
  function resolveAmaKey(botKey) {
538
528
  const botMeta = loadBotMeta(botKey);
@@ -0,0 +1,64 @@
1
+ import { FillRecord } from './fills_source.js';
2
+ /** How long after a query its trailing fills may still be late-indexed. */
3
+ declare const TAIL_SETTLE_LAG_MS: number;
4
+ /** Upper bound on spans persisted per shard (keeps coverage bookkeeping small). */
5
+ declare const MAX_COVERAGE_SPANS = 64;
6
+ interface QueriedRange {
7
+ gte: number;
8
+ lte: number;
9
+ at: number | null;
10
+ }
11
+ interface Span {
12
+ gte: number;
13
+ lte: number;
14
+ }
15
+ interface FillsShard {
16
+ meta: {
17
+ accountId: string;
18
+ fetchedAt: string;
19
+ queriedRanges: QueriedRange[];
20
+ };
21
+ fills: FillRecord[];
22
+ }
23
+ interface FetchFillsOptions {
24
+ /** Override the cache root (tests). */
25
+ cacheDir?: string;
26
+ /** Ignore coverage and re-query the requested range (still merges results). */
27
+ refresh?: boolean;
28
+ /** Fixed "now" for deterministic settle math (tests). */
29
+ nowMs?: number;
30
+ /** Injected range fetcher (tests); defaults to the real Kibana query. */
31
+ fetchRange?: (gteIso: string, lteIso: string) => Promise<FillRecord[]>;
32
+ quiet?: boolean;
33
+ }
34
+ declare function shardKeyForTimestamp(tsMs: number): string;
35
+ declare function shardBoundsForKey(key: string): {
36
+ start: number;
37
+ end: number;
38
+ };
39
+ declare function shardKeysForRange(gteMs: number, lteMs: number): string[];
40
+ declare function basePathFor(cacheDir: string, accountId: string): string;
41
+ declare function shardPathFor(basePath: string, shardKey: string): string;
42
+ /** Sort, merge overlapping/adjacent same-`at` spans, cap the list length. */
43
+ declare function normalizeQueried(ranges: QueriedRange[]): QueriedRange[];
44
+ /** Record a freshly queried span, dropping older spans it fully supersedes. */
45
+ declare function addCoverage(queried: QueriedRange[], span: Span, atMs: number): QueriedRange[];
46
+ /**
47
+ * Portions of queried spans that are safe to reuse: the part up to
48
+ * `at - TAIL_SETTLE_LAG_MS`. A span with an unknown `at` certifies nothing.
49
+ */
50
+ declare function trustedSegments(queried: QueriedRange[], nowMs: number): Span[];
51
+ /** `span` minus the union of `covered` (both as inclusive ms ranges). */
52
+ declare function subtractRanges(span: Span, covered: Span[]): Span[];
53
+ declare function fillKey(f: FillRecord): string;
54
+ /** Union two fill lists, deduped by the unique operation identity. */
55
+ declare function mergeFills(existing: FillRecord[], incoming: FillRecord[]): FillRecord[];
56
+ /**
57
+ * Fetch every fill_order for an account over [gteIso, lteIso], reusing cached
58
+ * month shards and querying only their missing spans. Falls back to a direct
59
+ * fetch when the range is malformed.
60
+ */
61
+ declare function fetchFillsCached(config: any, accountId: string, gteIso: string, lteIso: string, options?: FetchFillsOptions): Promise<FillRecord[]>;
62
+ export { fetchFillsCached, shardKeyForTimestamp, shardBoundsForKey, shardKeysForRange, basePathFor, shardPathFor, normalizeQueried, addCoverage, trustedSegments, subtractRanges, mergeFills, fillKey, TAIL_SETTLE_LAG_MS, MAX_COVERAGE_SPANS, };
63
+ export type { QueriedRange, FillsShard, FetchFillsOptions };
64
+ //# sourceMappingURL=fills_cache.d.ts.map