dexbot 1.6.9 → 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 (96) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/README.md +1 -0
  3. package/analysis/README.md +50 -12
  4. package/analysis/bot_fitting/README.md +12 -0
  5. package/analysis/tradingview/README.md +15 -10
  6. package/analysis/trend_detection/DYNAMIC_WEIGHT_RESEARCH.md +10 -36
  7. package/analysis/trend_detection/README.md +31 -0
  8. package/claw/modules/chain_queries.ts +9 -4
  9. package/claw/modules/claw_bridge.ts +9 -9
  10. package/claw/package.json +1 -1
  11. package/claw/runtimes/openclaw-plugin/openclaw.plugin.json +1 -1
  12. package/claw/runtimes/openclaw-plugin/package.json +1 -1
  13. package/dist/analysis/account_resolver.js +13 -1
  14. package/dist/analysis/ama_fitting/optimizer_high_resolution.js +2 -1
  15. package/dist/analysis/analyze_dynamic_weight.js +0 -3
  16. package/dist/analysis/bot_fitting/backtest_ama_sweep.js +52 -8
  17. package/dist/analysis/bot_fitting/backtest_bot_fitting.js +68 -10
  18. package/dist/analysis/bot_fitting/backtest_lookback_drawdown.d.ts +21 -0
  19. package/dist/analysis/bot_fitting/backtest_lookback_drawdown.js +315 -0
  20. package/dist/analysis/bot_key_utils.js +17 -17
  21. package/dist/analysis/fills_cache.d.ts +64 -0
  22. package/dist/analysis/fills_cache.js +254 -0
  23. package/dist/analysis/fills_source.js +5 -2
  24. package/dist/analysis/pnl_report.d.ts +39 -0
  25. package/dist/analysis/pnl_report.js +325 -0
  26. package/dist/analysis/resolve_source.d.ts +0 -1
  27. package/dist/analysis/trade_profitability.d.ts +11 -2
  28. package/dist/analysis/trade_profitability.js +187 -61
  29. package/dist/analysis/tradingview/analyze_tradingview.d.ts +0 -3
  30. package/dist/analysis/tradingview/analyze_tradingview.js +6 -19
  31. package/dist/analysis/tradingview/grid_reset_config.d.ts +2 -0
  32. package/dist/analysis/tradingview/grid_reset_config.js +18 -1
  33. package/dist/analysis/tradingview/grid_reset_sim.d.ts +12 -1
  34. package/dist/analysis/tradingview/grid_reset_sim.js +60 -11
  35. package/dist/analysis/tradingview/tradingview_uplot_chart_generator.js +180 -33
  36. package/dist/analysis/trend_detection/backtest_ama_slope_huber.d.ts +102 -0
  37. package/dist/analysis/trend_detection/backtest_ama_slope_huber.js +725 -0
  38. package/dist/analysis/trend_detection/dynamic_weight_chart_generator.js +29 -110
  39. package/dist/analysis/trend_detection/huber_scale_variants.d.ts +34 -0
  40. package/dist/analysis/trend_detection/huber_scale_variants.js +176 -0
  41. package/dist/dexbot.js +19 -11
  42. package/dist/market_adapter/core/market_adapter_service.d.ts +15 -0
  43. package/dist/market_adapter/core/market_adapter_service.js +64 -5
  44. package/dist/market_adapter/core/strategies/ama.d.ts +1 -1
  45. package/dist/market_adapter/core/strategies/ama.js +5 -28
  46. package/dist/market_adapter/core/strategies/ama_slope_model.d.ts +2 -2
  47. package/dist/market_adapter/core/strategies/ama_slope_model.js +12 -10
  48. package/dist/market_adapter/core/strategies/dynamic_weight_series.d.ts +41 -14
  49. package/dist/market_adapter/core/strategies/dynamic_weight_series.js +138 -30
  50. package/dist/market_adapter/inputs/fetch_cex_synthetic_data.js +1 -2
  51. package/dist/market_adapter/log_format.js +1 -2
  52. package/dist/market_adapter/lp_chart_strategy_loader.js +2 -1
  53. package/dist/market_adapter/market_adapter.d.ts +0 -1
  54. package/dist/market_adapter/market_adapter.js +2 -27
  55. package/dist/market_adapter/utils/chain.d.ts +1 -1
  56. package/dist/market_adapter/utils/chain.js +8 -6
  57. package/dist/modules/account_bots.js +5 -4
  58. package/dist/modules/bitshares-native/resolvers.js +5 -0
  59. package/dist/modules/bot_settings.js +13 -1
  60. package/dist/modules/chain_orders.js +2 -1
  61. package/dist/modules/constants.d.ts +9 -1
  62. package/dist/modules/constants.js +55 -9
  63. package/dist/modules/credential_policy.js +4 -1
  64. package/dist/modules/credit_runtime.js +5 -1
  65. package/dist/modules/dexbot_maintenance_runtime.js +12 -0
  66. package/dist/modules/order/accounting.js +25 -0
  67. package/dist/modules/order/format.d.ts +35 -15
  68. package/dist/modules/order/format.js +62 -16
  69. package/dist/modules/order/manager.d.ts +1 -0
  70. package/dist/modules/order/manager.js +22 -1
  71. package/dist/modules/order/sync_engine.js +15 -1
  72. package/dist/modules/order/utils/order.d.ts +21 -1
  73. package/dist/modules/order/utils/order.js +80 -2
  74. package/dist/modules/order/utils/system.js +11 -1
  75. package/dist/modules/paths.d.ts +2 -0
  76. package/dist/modules/paths.js +2 -0
  77. package/dist/modules/utils/asset_symbols.d.ts +59 -0
  78. package/dist/modules/utils/asset_symbols.js +128 -0
  79. package/dist/modules/utils/time_range.d.ts +15 -0
  80. package/dist/modules/utils/time_range.js +18 -0
  81. package/dist/scripts/analyze-credit.js +10 -7
  82. package/dist/scripts/analyze-orders.js +1 -44
  83. package/dist/scripts/chart_command.js +15 -8
  84. package/dist/scripts/pnl.d.ts +3 -0
  85. package/dist/scripts/pnl.js +40 -0
  86. package/dist/scripts/verify-browser-bundle.js +8 -0
  87. package/dist/unlock.js +34 -0
  88. package/docs/DEXBOT_COMPARISON.md +3 -3
  89. package/docs/EVOLUTION.md +12 -10
  90. package/docs/FUND_MOVEMENT_AND_ACCOUNTING.md +1 -1
  91. package/docs/GRID_RECALCULATION.md +33 -17
  92. package/docs/README.md +2 -2
  93. package/docs/WORKFLOW.md +1 -0
  94. package/market_adapter/README.md +6 -28
  95. package/package.json +1 -1
  96. package/scripts/README.md +14 -0
@@ -11,6 +11,7 @@ import { PATHS } from '../../modules/paths.js';
11
11
  import { ensureDir } from '../../modules/order/utils/system.js';
12
12
  import { range } from '../math_utils.js';
13
13
  import { getStorage } from '../../modules/storage/index.js';
14
+ import { normalizeAssetSymbol } from '../../modules/utils/asset_symbols.js';
14
15
  const { readJSON, writeJSON } = getStorage();
15
16
  import { loadLpDataFile, } from '../../market_adapter/lp_chart_runner.js';
16
17
  const AMA_PROFILES_FILE = PATHS.PROFILES.MARKET_PROFILES_JSON;
@@ -301,7 +302,7 @@ function boundaryFlags(winner, erValues, fastValues, slowValues, fixedEr = null,
301
302
  }
302
303
  // ── Data loaders ──────────────────────────────────────────────────────────────
303
304
  function normalizeSymbol(value) {
304
- return String(value || '').trim().toUpperCase();
305
+ return normalizeAssetSymbol(value);
305
306
  }
306
307
  function inferIntervalLabel(dataFile, meta) {
307
308
  const fromMeta = Number(meta?.intervalSeconds);
@@ -78,8 +78,6 @@ function parseArgs() {
78
78
  config.dispWeight = parseFloat(args[++i]);
79
79
  else if (arg === '--lb')
80
80
  config.lookbackBars = parseInt(args[++i], 10);
81
- else if (arg === '--ema')
82
- config.amaEmaSpan = parseInt(args[++i], 10);
83
81
  else if (arg === '--clip')
84
82
  config.clipPct = parseFloat(args[++i]);
85
83
  else if (arg === '--list-bots')
@@ -182,7 +180,6 @@ async function main() {
182
180
  ...AMA_WEIGHT_CONFIG,
183
181
  lookbackBars: config.lookbackBars ?? AMA_WEIGHT_CONFIG.lookbackBars,
184
182
  },
185
- amaEmaSpan: config.amaEmaSpan,
186
183
  alpha: config.alpha,
187
184
  gain: config.gain,
188
185
  dispWeight: config.dispWeight,
@@ -4,7 +4,7 @@ import os from 'node:os';
4
4
  import { pathToFileURL, fileURLToPath } from 'node:url';
5
5
  import { Worker, isMainThread, parentPort, workerData } from 'node:worker_threads';
6
6
  import { calculateAMA, getAmaWarmupBars } from '../../market_adapter/core/strategies/ama.js';
7
- import { computeAverageAmaSlopePct } from '../../market_adapter/core/strategies/ama_slope_model.js';
7
+ import { computeHuberWindowSlopePct } from '../../market_adapter/core/strategies/ama_slope_model.js';
8
8
  import { range } from '../math_utils.js';
9
9
  import { parseListOrRange, loadLpData, fmt } from './shared_utils.js';
10
10
  import { getStorage } from '../../modules/storage/index.js';
@@ -84,6 +84,7 @@ function parseArgs() {
84
84
  makerCreateFactor: DEFAULT_BTS_MAKER_CREATE_FACTOR,
85
85
  txFeePrice: DEFAULT_TX_FEE_PRICE,
86
86
  topN: 15,
87
+ lookbackBars: null,
87
88
  };
88
89
  for (let i = 0; i < args.length; i++) {
89
90
  const arg = args[i];
@@ -159,6 +160,10 @@ function parseArgs() {
159
160
  out.topN = Number(val);
160
161
  i++;
161
162
  break;
163
+ case '--lookback':
164
+ out.lookbackBars = Math.max(1, Math.round(Number(val)));
165
+ i++;
166
+ break;
162
167
  }
163
168
  }
164
169
  if (!out.dataPath) {
@@ -192,6 +197,7 @@ function printHelp() {
192
197
  console.log(' --maker-create-factor Maker share of create fee (default: 0.10)');
193
198
  console.log(' --tx-fee-price <n> Convert BTS fees into backtest units (default: 1.0)');
194
199
  console.log(' --top <n> Show top N results (default: 15)');
200
+ console.log(` --lookback <bars> Huber slope lookback override (default: ${SLOPE_LOOKBACK_BARS})`);
195
201
  }
196
202
  function loadAmaStrategies(resultsPath) {
197
203
  const json = readJSON(resultsPath);
@@ -292,6 +298,14 @@ function markInventoryAtPrice(inventory, exitPrice, feeRoundtripPct) {
292
298
  }
293
299
  function simulatePersistentGrid(candles, amaValues, params, weightName, weightFactor) {
294
300
  const { spreadPct, incrementPct, maxMinRatio, feeRoundtripPct, capital, repositionThreshold, asymmetricBounds, btsCreateFee, btsCancelFee, makerCreateFactor, txFeePrice } = params;
301
+ // Slope-delta persistence gate: follow the production default (constants)
302
+ // unless the caller pins a value, so the sweep's reset path matches live
303
+ // whenever asymmetricBounds enables trigger B.
304
+ const slopePersistBars = Number.isFinite(params.slopePersistBars) && params.slopePersistBars >= 1
305
+ ? Math.round(params.slopePersistBars)
306
+ : (MARKET_ADAPTER.AMA_SLOPE_PERSIST_ENABLED === true && Number(MARKET_ADAPTER.AMA_SLOPE_PERSIST_BARS) >= 1
307
+ ? Math.round(Number(MARKET_ADAPTER.AMA_SLOPE_PERSIST_BARS))
308
+ : 1);
295
309
  // Warmup follows production AMA seeding/convergence (getAmaWarmupBars,
296
310
  // passed via params.warmupBars from sweepOneAma) instead of an arbitrary
297
311
  // fraction of the dataset. Direct callers that omit warmupBars fall back
@@ -316,9 +330,14 @@ function simulatePersistentGrid(candles, amaValues, params, weightName, weightFa
316
330
  }
317
331
  // Production AMA slope series (%/bar averaged over the lookback window) —
318
332
  // feeds trigger B and the grid price offset when asymmetricBounds is on.
333
+ // Lookback is overridable for research (params.lookbackBars); production
334
+ // callers leave it unset and get the centralized constant.
335
+ const slopeLookbackBars = Number.isFinite(params.lookbackBars) && params.lookbackBars >= 1
336
+ ? Math.round(params.lookbackBars)
337
+ : SLOPE_LOOKBACK_BARS;
319
338
  const slopeAt = new Array(candles.length).fill(null);
320
- for (let j = SLOPE_LOOKBACK_BARS; j < candles.length; j++) {
321
- const s = computeAverageAmaSlopePct(amaValues[j], amaValues[j - SLOPE_LOOKBACK_BARS], SLOPE_LOOKBACK_BARS);
339
+ for (let j = slopeLookbackBars; j < candles.length; j++) {
340
+ const s = computeHuberWindowSlopePct(amaValues, j, slopeLookbackBars);
322
341
  if (s != null && Number.isFinite(s))
323
342
  slopeAt[j] = s;
324
343
  }
@@ -392,6 +411,9 @@ function simulatePersistentGrid(candles, amaValues, params, weightName, weightFa
392
411
  // Slope-delta baseline: mirrors botState.gridRangeScalingAmaSlope —
393
412
  // seeded at bootstrap and re-seeded on every reset.
394
413
  let slopeBaseline = null;
414
+ // Persistence-gate state for trigger B.
415
+ let slopePersistCount = 0;
416
+ let slopePersistDir = 0;
395
417
  for (let j = startIdx + 1; j < candles.length; j++) {
396
418
  if (slopeAt[j] != null) {
397
419
  slopeBaseline = slopeAt[j];
@@ -421,13 +443,34 @@ function simulatePersistentGrid(candles, amaValues, params, weightName, weightFa
421
443
  driftTriggerCount++;
422
444
  }
423
445
  if (!shouldReposition && asymmetricBounds && slopeBaseline != null && slopeAt[i] != null) {
424
- const slopeDeltaPct = Math.abs(slopeAt[i] - slopeBaseline);
425
- if (slopeDeltaPct >= slopeDeltaThresholdPct) {
426
- shouldReposition = true;
427
- slopeTriggerCount++;
446
+ const crossed = Math.abs(slopeAt[i] - slopeBaseline) >= slopeDeltaThresholdPct;
447
+ if (slopePersistBars <= 1) {
448
+ if (crossed) {
449
+ shouldReposition = true;
450
+ slopeTriggerCount++;
451
+ }
452
+ }
453
+ else if (!crossed) {
454
+ slopePersistCount = 0;
455
+ slopePersistDir = 0;
456
+ }
457
+ else {
458
+ const dir = Math.sign(slopeAt[i] - slopeBaseline);
459
+ if (dir !== 0 && dir === slopePersistDir)
460
+ slopePersistCount++;
461
+ else {
462
+ slopePersistDir = dir;
463
+ slopePersistCount = 1;
464
+ }
465
+ if (slopePersistCount >= slopePersistBars) {
466
+ shouldReposition = true;
467
+ slopeTriggerCount++;
468
+ }
428
469
  }
429
470
  }
430
471
  if (shouldReposition && Number.isFinite(ama) && ama > 0) {
472
+ slopePersistCount = 0;
473
+ slopePersistDir = 0;
431
474
  canceledOnReposition += orders.size;
432
475
  btsFeesBts += orders.size * btsCancelFee;
433
476
  orders.clear(); // inventory survives — resync never market-sells
@@ -674,6 +717,7 @@ function sweepOneAma(strategy, candles, closes, weightEntries, cfg) {
674
717
  makerCreateFactor: cfg.makerCreateFactor,
675
718
  txFeePrice: cfg.txFeePrice,
676
719
  warmupBars,
720
+ lookbackBars: cfg.lookbackBars,
677
721
  }, weightName, weightFactor);
678
722
  if (!best || sim.score > best.score)
679
723
  best = sim;
@@ -746,7 +790,7 @@ async function run() {
746
790
  console.log(` Spread floor: > fee (${cfg.feeRoundtripPct}%)`);
747
791
  console.log(` Reset (A): ${cfg.repositionPct}% AMA drift from grid center (ratchet)`);
748
792
  console.log(` Asym. bounds: ${cfg.asymmetricBounds ? 'ON — slope reset (B) + grid price offset enabled (whitelist semantics)' : 'OFF — typical non-whitelisted bot (production default)'}`);
749
- console.log(` Reset (B): |slope - slope@lastReset| >= ${fmt((SLOPE_TRIGGER_FACTOR / 100) * SLOPE_MAX_PCT, 4)}% (lookback ${SLOPE_LOOKBACK_BARS})${cfg.asymmetricBounds ? '' : ' [gated off]'}`);
793
+ console.log(` Reset (B): |slope - slope@lastReset| >= ${fmt((SLOPE_TRIGGER_FACTOR / 100) * SLOPE_MAX_PCT, 4)}% (lookback ${cfg.lookbackBars ?? SLOPE_LOOKBACK_BARS})${cfg.asymmetricBounds ? '' : ' [gated off]'}`);
750
794
  console.log(` Tx model: create=${fmt(cfg.btsCreateFee, 5)} BTS, cancel=${fmt(cfg.btsCancelFee, 5)} BTS, maker=${fmt(cfg.makerCreateFactor * 100, 1)}%, 1 BTS=${fmt(cfg.txFeePrice, 2)} units`);
751
795
  console.log(` Combos/AMA: ${totalCombos} | Total: ${totalCombos * strategies.length}\n`);
752
796
  // ── Run AMA sweeps in parallel (one worker per AMA strategy) ──────
@@ -3,7 +3,8 @@ import fs from 'node:fs';
3
3
  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
- import { computeAverageAmaSlopePct } from '../../market_adapter/core/strategies/ama_slope_model.js';
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';
@@ -43,8 +44,8 @@ const DEFAULT_REPOSITION_PCT = MARKET_ADAPTER.AMA_DELTA_THRESHOLD_PERCENT;
43
44
  // bot): --asymmetric-bounds enables slope-delta resets AND slope-ratio offset.
44
45
  // Trigger B fires when |slopePct_now − baseline| reaches
45
46
  // (AMA_SLOPE_DELTA_THRESHOLD_PERCENT / 100) × DYNAMIC_WEIGHT_AMA_MAX_SLOPE_PCT,
46
- // where slopePct is the average per-bar AMA change over
47
- // DYNAMIC_WEIGHT_AMA_LOOKBACK_BARS (computeAverageAmaSlopePct) and the
47
+ // where slopePct is the Huber-robust per-bar AMA slope over
48
+ // DYNAMIC_WEIGHT_AMA_LOOKBACK_BARS (computeHuberWindowSlopePct) and the
48
49
  // baseline mirrors botState.gridRangeScalingAmaSlope (re-seeded every reset).
49
50
  const SLOPE_TRIGGER_FACTOR = MARKET_ADAPTER.AMA_SLOPE_DELTA_THRESHOLD_PERCENT;
50
51
  const SLOPE_MAX_PCT = MARKET_ADAPTER.DYNAMIC_WEIGHT_AMA_MAX_SLOPE_PCT;
@@ -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 = computeAverageAmaSlopePct(amaValues[j], amaValues[j - SLOPE_LOOKBACK_BARS], 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