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
package/CHANGELOG.md CHANGED
@@ -2,6 +2,22 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
+ ## [1.6.11] - 2026-10-02 - AMA-Slope Persistence Gate, PnL HTML Report & Fill Cache, Five Order-Engine Safety Fixes
6
+
7
+ ### 2026-10-02
8
+
9
+ - **Feat(market-adapter)**: gate AMA-slope resets behind a 3-bar persistence window and shorten the Huber lookback to 16 — two coupled default changes measured on a live 1h market-pair pool (`docs/AMA_SLOPE_WINDOW.md`). `DYNAMIC_WEIGHT_AMA_LOOKBACK_BARS` 20 → 16 buys a faster slope signal (AMA group delay 10 → 8 bars, reversal lag 22 → 20, range-tilt wrong-way 13.7% → 12.6%) at ~8% noisier bar-to-bar slope; a new persistence gate on the slope-delta reset trigger (trigger B) — `AMA_SLOPE_PERSIST_ENABLED` (true) + `AMA_SLOPE_PERSIST_BARS` (3) — requires 3 consecutive confirming bars in the same direction before the reset fires, absorbing the shorter window's extra noise. Net vs the old 20h/ungated default: resets −35% (1.45 → 0.95/day), whipsaw ~52% → ~17%, lag and tilt unchanged; the independent price/Drift trigger is unaffected. Per bot/market override via `amaSlope.persistBars` / `amaSlope.persistEnabled`; `1` = legacy fire-on-first-crossing (values `< 1` fall through to the global default). The gate is mirrored everywhere the decision path is replayed so all consumers stay on one logic path: `resolveAmaSlopePersistBars` / `advanceAmaSlopePersistence` in the service (counters persist across restarts via `normalizePersistedAmaSlopeDiagnostics` and clear on every successful reset, bootstrap included), `grid_reset_config` resolves and exports `slopePersistBars` (same override chain), `grid_reset_sim` implements the gate and its panel prints `persist K`, and the backtests follow the production default unless pinned (the fitting harness gains an optional `bandTilt` hook). New tool `analysis/trend_detection/backtest_ama_slope_huber.ts` sweeps the lookback and reports lag, reset churn and noise metrics (`--data` required). Tests: new `test_grid_reset_sim_gate.ts` (K-bar latency, 1-bar blip filtered, direction flip restarts the counter) and `testAmaSlopePersistenceGate`; existing slope-trigger tests pinned to legacy (`persistBars` 1). Verified: `tsc` clean, 305/305 tests, browser bundle 40/40 (`market_adapter/core/market_adapter_service.ts`, `analysis/tradingview/grid_reset_sim.ts`, `modules/constants.ts`, `docs/AMA_SLOPE_WINDOW.md`, `tests/test_grid_reset_sim_gate.ts`).
10
+
11
+ - **Refactor(ama)**: remove the two EMA filters and align research charts with the live engine — the AMA had accreted two independent EMA filters that both add lag to address noise the estimator now handles lag-free (the slope is a Huber-robust regression and small residuals are dead-banded): the live `ama.erSmoothPeriod` (an EMA over the raw Efficiency Ratio inside the AMA) and the research `amaEmaSpan` (`--ema`, a post-AMA input EMA in the dynamic-weight chart). Both are removed so the live engine and both research charts run one canonical 3-parameter Kaufman AMA. Live: `ama.ts` gains the 3-param constructor and drops `erSmoothAlpha`/`effectiveER` and the ER-smoothing convergence term from `getAmaWarmupBars`; `AMA_ER_SMOOTH_FAST_PERIOD` is dropped from `constants.ts`; `market_adapter.ts` / `market_adapter_service.ts` / `fetch_cex_synthetic_data.ts` / `log_format.ts` drop the ER-smoothing resolution, warmup, payload fields and the `/esN` log suffix. Research: `analyze_dynamic_weight.ts` + `dynamic_weight_chart_generator.ts` drop the `ema` knob, its Raw reference line and `--ema`; `bot_key_utils` / `resolve_source` / `grid_reset_config` stop threading `erSmoothPeriod`. Two chart divergences from the live regime gate surfaced while verifying parity are also fixed: `peNodes` (the canonical key, not `pNodes`) is passed to `bilinearInterpolate`, and the per-bar multiplier is clamped to 1.0 with 3-decimal rounding before the absolute-threshold dead-band; the AMA clip threshold always comes from `computeAmaSlopeClipThreshold`, so changing the lookback with `clip% = 0` no longer keeps a stale percentile cap. Configs still carrying `ama.erSmoothPeriod` are ignored (the default was 0, so no live behavior change in practice). Docs and tests updated. Verified: `tsc` clean, full suite 303 pass / 0 fail, browser bundle 40/40 (`market_adapter/core/strategies/ama.ts`, `modules/constants.ts`, `analysis/analyze_dynamic_weight.ts`, `analysis/dynamic_weight_chart_generator.ts`).
12
+
13
+ - **Feat(analysis)**: research tooling and findings for the AMA-slope scale bias and lookback drawdown — two research surfaces, both default-preserving and production-untouched. Scale bias: the canonical estimator's robust scale is a plug-in 1.4826·MAD of the fit's own residuals, so with two fitted parameters it reads low by ~1/(bars−1) (~13% at 8 bars, ~7% at 16) and the effective Huber constant is ~1.25 at the 16-bar window, not the nominal 1.345; `analysis/trend_detection/huber_scale_variants.ts` adds three scale modes (`none` reproduces production byte-for-byte, `df` applies the sqrt(n/(n−2)) correction, `mscale` a Huber proposal-2 M-scale) plus an outlier-fraction diagnostic, exposed via `--scale-mode` on `backtest_ama_slope_huber.ts`. Measured: compensating is decision-neutral (wobble ≤0.2%, AMA lag unchanged, resets within ≤5%, wrong-way within noise) and trades away more robustness (~0.8% RMS under 10% contamination) than it recovers in efficiency (~0.4% clean), so production keeps the uncompensated plug-in scale; the `DYNAMIC_WEIGHT_AMA_HUBER` comment records this so the next reader does not "correct" it, and a guard test asserts `none == computeHuberWindowSlopePct` bar-for-bar. Lookback drawdown: `simulatePersistentGrid` gains `params.lookbackBars` and a `--lookback` flag (unset falls back to the centralized constant), and `backtest_lookback_drawdown.ts` runs paired persistent-grid comparisons across a lookback set over one geometry grid. Finding: the window has a real per-pool effect on realized drawdown and net capture but no stable sign across pools (16h beat 12h on a long-lived liquid pool, reversed on a shorter one), so it strengthens keeping the shipped 16h without converting it into a general economic proof. Both are documented in `docs/AMA_SLOPE_WINDOW.md` with reproduction commands. Behaviour: research-only; no production logic changed (one code comment). Verified: `npm run build && npm run build:tests && node dist/tests/test_huber_scale_variants.js` (`analysis/trend_detection/huber_scale_variants.ts`, `analysis/trend_detection/backtest_lookback_drawdown.ts`, `docs/AMA_SLOPE_WINDOW.md`).
14
+
15
+ - **Feat(pnl)**: add a self-contained HTML PnL report and a per-account month-shard fill cache — `dexbot pnl` was terminal-only; a self-contained HTML report now renders per-pair 2×2 card blocks, a metrics grid and a collapsible realized-lot table (`analysis/pnl_report.ts`), wired through a thin `scripts/pnl.ts` entry and the `dexbot pnl` command. The analyzer gains `--month` (default 3), a `--pair BASE/QUOTE` filter and `--html`/`--report`, and its terminal output now shows `Net inventory delta`. Per-account `fill_order` fetches are cached in calendar-month shards (`analysis/fills_cache.ts`): settled months answer from disk, only the unsettled tail re-queries Kibana, and `--refresh-account` bypasses coverage while still merging. Account resolution checks local bot profiles before the chain (`analysis/account_resolver.ts`), and on-chain-resolved asset symbols are cached so pair filters work for non-static assets (`analysis/fills_source.ts`); `PATHS.ANALYSIS.CACHE_DIR` is added for the shard root. Doc updates: `README`, `analysis/README`, `scripts/README`, `docs/README`, `docs/WORKFLOW`, `docs/EVOLUTION`. Risk: the default lookback for the analyzer / `analysis:trade-pnl` changes from 7 days to 3 months. Verified: `tsc` root + tests clean; `tests/test_pnl_report.ts`, `tests/test_fills_cache.ts`, `tests/test_trade_profitability_fees.ts`, `tests/test_paths.ts` pass; `dexbot pnl --help` and bad-flag/missing-value paths exit correctly (`analysis/pnl_report.ts`, `analysis/fills_cache.ts`, `scripts/pnl.ts`, `docs/WORKFLOW.md`).
16
+
17
+ - **Refactor(format)**: share the 4-significant-figure funds formatter and the month→hours helper — `formatFundsValue` lived privately in `scripts/analyze-orders.ts` and trimmed trailing zeros, so four valid digits rendered as `"1.01K"` instead of `"1.010K"`, and the PnL report needed the same rule. It moves into `modules/order/format.ts` (keeps significant trailing zeros, still caps large magnitudes with K/M), and the fixed 730h/month lookback conversion shared by the chart and PnL commands is extracted into `modules/utils/time_range.ts` so they cannot disagree on `--month N`. Behavioral impact: `dexbot order` now keeps significant trailing zeros (e.g. `10000` → `"10.00K"`, `1500000` → `"1.500M"`). No caller changes beyond the import; the `format.ts` TOC is realigned to its 15 exports. Verified: `tsc` root clean; `tests/test_analyze_orders_dynamic_weight.ts` and `tests/test_dw_cli.ts` pass (`modules/order/format.ts`, `modules/utils/time_range.ts`, `scripts/analyze-orders.ts`).
18
+
19
+ - **Fix(order-engine)**: five order-engine and lifecycle safety fixes — (1) **Stop could leave a live worker behind over the running orders** (`unlock.ts`): `stop` returned as soon as the monolithic wrapper exited, but `start`'s already-running guard only checks the supervisor pid file, which is wiped on wrapper exit, so a fast stop→start could spawn a SECOND worker trading the same live orders (duplicate trades); after the wrapper exits, stop now polls `/proc` up to 15s until no dexbot worker remains (best-effort/no-op off Linux). (2) **Live `bots.json` changes did not take effect until the next fill or the 240-min fetch** (`modules/dexbot_maintenance_runtime.ts`): config pickup logged "targeted maintenance will place missing / cancel excess orders" but never ran it; it now fires the targeted drift reconciliation immediately, fire-and-forget (same callee and cooldown as the poll tick, serialized via the fill-processing lock). (3) **Live-but-slotless chain orders were invisible to fund accounting** (`modules/order/accounting.ts`): an order resting on chain without a grid slot (out-of-grid/boundary-unknown deferral) locks real funds, but `recalculateFunds` counted only grid orders, so every orphan produced a fund-invariant violation equal to its size (triggering futile recovery resyncs) and sizing could double-spend the orphan-locked funds; unmatched chain orders are now added to the on-chain committed totals (`chainBuy`/`chainSell`) with type/size validation, grid totals unchanged. (4) **An adopted orphan was then double-counted** (`modules/order/sync_engine.ts`): with (3) counting orphans as committed, adopting one into a slot left the stale record in `_lastUnmatchedChainOrders`, so it was counted twice (slot + list); both success exits of `adoptChainOrderIntoSlot` and the `createOrder` materialize branch now prune the deferred-orphan record on adoption. (5) **Freshly placed orders could be cancelled seconds after placement** (`modules/constants.ts`, `modules/order/utils/order.ts`, `modules/order/manager.ts`): spread correction and surplus sweeps run on different count snapshots within one cycle, so a fill between them made the second controller cancel what the first had just placed (fee bleed, empty levels, no net change); a new 15-minute `SURPLUS_CANCEL_GRACE_MS` window timestamps placements (`_placedAt`) when a slot gains a new `orderId`, and surplus/cancel-only cancellations skip orders inside the window and leave them queued to re-drain after it expires, covering both the batched and serial cancel paths (the batch path bypasses the per-entry check, so the drain filters fresh placements before batching); load contexts (`'grid-load'`/`'grid-init'`) are excluded so restarts do not suppress sweeps. Verified by `tests/test_accounting_logic.ts` and `tests/test_surplus_cancel_grace.ts`; `npm test` 309 files, 0 failures; `tsc --noEmit` clean (`unlock.ts`, `modules/dexbot_maintenance_runtime.ts`, `modules/order/accounting.ts`, `modules/order/sync_engine.ts`, `modules/order/manager.ts`, `tests/test_surplus_cancel_grace.ts`).
20
+
5
21
  ## [1.6.10] - 2026-10-01 - Huber-Robust AMA Slope, Asset-Pair Canonicalization, TradingView Indicator Ownership & View Preservation
6
22
 
7
23
  ### 2026-10-01
package/README.md CHANGED
@@ -338,6 +338,7 @@ dexbot enable {all|<bot>} # Enable bot in config
338
338
  dexbot stat # Runtime status (unlock or PM2)
339
339
  dexbot order [<bot>] # Analyze order grids (--export → HTML to analysis/charts/)
340
340
  dexbot tv <bot|pool|A/B> # TradingView 1h chart with AMA overlay (default: 3 months)
341
+ dexbot pnl <bot|account> # HTML PnL report (--month N, optional --pair A/B)
341
342
  dexbot credit [<bot>] # Live summed MPA + borrowed-credit positions
342
343
  dexbot export <bot> # Export trades + settings (CSV/JSON) for analysis/
343
344
 
@@ -21,6 +21,7 @@ Tools that inspect DEXBot trading behavior and the market data it operates on. O
21
21
  | Tool | Ask this when… | One-line command |
22
22
  |------|----------------|------------------|
23
23
  | [`trade_profitability.ts`](#trade-profitability-analyzer-trade_profitabilityts) | "Is my bot making money?" — PnL, R-multiples, drawdown | `npm run analysis:trade-pnl -- <account-id>` |
24
+ | [`dexbot pnl`](#pnl-html-report-dexbot-pnl) | "Give me a readable PnL report" — self-contained HTML with a lot table | `dexbot pnl <bot\|account> --month 3` |
24
25
  | [`grid_correction_check.ts`](#last-fill-guard-check-grid_correction_checkts) | "Are fills respecting the pivot ± half-increment guard?" | `npm run analysis:grid-check -- --bot-key <bot-key>` |
25
26
  | [`analyze_risk_profile.ts`](#risk-profile-analyzer-analyze_risk_profilets) | "How wide should my Safe Range clamps be?" | `node dist/analysis/analyze_risk_profile.js --bot-key <bot-key>` |
26
27
  | [`analyze_trade_heatmap.ts`](#trade-heatmap-analyze_trade_heatmapts) | "Where did trade volume cluster vs the AMA?" | `node dist/analysis/analyze_trade_heatmap.js --bot-key <bot-key>` |
@@ -130,20 +131,28 @@ Metrics include:
130
131
 
131
132
  Fetches `fill_order` operations for a BitShares account from Kibana within a specified time range, then computes realized PnL via sequential (LIFO) or FIFO inventory tracking per asset pair.
132
133
 
133
- **Pipeline:** Kibana fill query → on-chain asset precision resolution → buy/sell classification → chronological matching (sequential LIFO by default) → per-pair summary + optional per-match detail.
134
+ **Pipeline:** Kibana fill query → on-chain asset precision resolution → buy/sell classification → chronological matching (sequential LIFO by default) → per-pair summary + optional per-match detail, or a self-contained HTML report (`--html`).
134
135
 
135
- **Account resolution:** an account name (or a bare `1.2.x` ID) is resolved through the shared `account_resolver.ts` helper. When the name matches a bot in `profiles/bots.json`, the resolved ID is stamped onto that entry as `accountId`, so later runs resolve offline; `--refresh-account` forces a fresh lookup.
136
+ **Account resolution:** a bot profile name, an account name, or a bare `1.2.x` ID is resolved through the shared `account_resolver.ts` helper. A local bot profile is checked **first** (no chain call), then a stored `accountId`, then the chain; the resolved ID is stamped onto the matching `profiles/bots.json` entry, so later runs resolve offline. `--refresh-account` forces a fresh lookup.
137
+
138
+ `dexbot pnl` is the HTML front door (see [`scripts/README.md`](../scripts/README.md)); the raw analyzer below defaults to terminal tables.
136
139
 
137
140
  ```bash
138
- # Account by ID, last 7 days (default)
141
+ # HTML report for a local bot, last 3 months (dexbot pnl wraps this)
142
+ dexbot pnl my-bot --month 3
143
+
144
+ # HTML report for a raw account, filtered to one pair
145
+ dexbot pnl 1.2.123456 --month 6 --pair TOKENA/BTS
146
+
147
+ # Terminal output: account by ID, last 3 months (default)
139
148
  node dist/analysis/trade_profitability.js 1.2.123456
140
149
 
141
- # Account by name (auto-resolved in the background)
142
- node dist/analysis/trade_profitability.js "my-account-name" --hours 720
150
+ # Account by profile/account name (local profiles first)
151
+ node dist/analysis/trade_profitability.js "my-account-name" --month 1
143
152
 
144
- # Absolute window with asset filter
153
+ # Absolute window with pair filter
145
154
  node dist/analysis/trade_profitability.js 1.2.123456 \
146
- --start 2026-07-01 --end 2026-07-07 --asset 1.3.3291
155
+ --start 2026-07-01 --end 2026-07-07 --pair TOKENA/BTS
147
156
 
148
157
  # Export trade log and full analysis
149
158
  node dist/analysis/trade_profitability.js 1.2.123456 \
@@ -158,25 +167,31 @@ node dist/analysis/trade_profitability.js 1.2.123456 \
158
167
 
159
168
  | Flag | Default | Description |
160
169
  |------|---------|-------------|
170
+ | `--month <n>` | `3` | Lookback months (730 h each; alias `--months`) |
161
171
  | `--start <iso>` | — | Start time (ISO 8601) |
162
172
  | `--end <iso>` | — | End time |
163
- | `--hours <n>` | `168` (7d) | Lookback hours (alternative to start/end) |
173
+ | `--hours <n>` | — | Lookback hours (alternative to month/start/end) |
174
+ | `--pair <BASE/QUOTE>` | all | Filter to one pair by symbol or `1.3.x` id |
164
175
  | `--asset <id>` | all | Filter to one base asset ID |
165
- | `--refresh-account` | off | Force re-resolution and update the stored `accountId` |
176
+ | `--refresh-account` | off | Force re-resolution and bypass the fills cache (re-query the range) |
177
+ | `--html` | off | Write a self-contained HTML report instead of terminal tables |
178
+ | `--report <file>` | auto | Override the report path (implies `--html`) |
166
179
  | `--csv <file>` | — | Export chronologically sorted trade list |
167
180
  | `--json <file>` | — | Export full analysis with per-pair PnL data |
168
181
  | `--match-mode <mode>` | `sequential` | Matching mode: `sequential` (LIFO, default) or `fifo` |
169
- | `--trades` | off | Show per-order PnL detail (hidden by default) |
182
+ | `--trades` | off | Show per-order PnL detail (terminal mode only) |
170
183
  | `--fee-per-order <bts>` | `0.09652` | Blockchain fee per limit_order_create op (BTS); approximate |
171
184
  | `--verbose` | off | Print per-pair trade counts during processing |
172
185
 
173
186
  </details>
174
187
 
188
+ **Fill cache:** per-account `fill_order` fetches are cached in calendar-month shards under `<analysis>/cache/fills/<accountId>.shard_YYYY-MM.json` (see `fills_cache.ts`). A settled month is answered from disk with zero queries; only the still-unsettled tail (the last ~6 h before the previous query, where Kibana may still index late fills) is re-queried. `--refresh-account` ignores coverage and re-queries the requested range (merging, so it never drops fills outside that span). Delete the shard files to reset the cache.
189
+
175
190
  **Asset precision handling:**
176
191
 
177
192
  1. Assets listed in the static `ASSETS` table (BTS, TWENTIX, XBTSX.*, HONEST.*, IOB.*, etc.) resolve instantly.
178
193
  2. Unknown assets are resolved on-chain via `get_assets` against the built-in node pool, with results cached at runtime.
179
- 3. If resolution fails or an asset is not found on chain, the affected fills are **skipped** with a warning (no abort).
194
+ 3. If resolution fails or an asset is not found on chain, the affected fills are **skipped** with a warning (no abort). Symbols learned on-chain are cached too, so `--pair SYMBOL/QUOTE` works even for assets absent from the static table.
180
195
 
181
196
  **PnL methodology:**
182
197
 
@@ -220,6 +235,27 @@ node dist/analysis/trade_profitability.js 1.2.123456 \
220
235
 
221
236
  </details>
222
237
 
238
+ ### PnL HTML Report (`dexbot pnl`)
239
+
240
+ `dexbot pnl` is the HTML front door: it resolves the account, fetches fills (through the fill cache), computes the same realized-lot PnL as the terminal analyzer, and renders a single self-contained HTML file via `analysis/pnl_report.ts`. No network, CDN, or sibling assets — it opens from a `file://` link anywhere. The file lands under the analysis `charts/` directory (or the path given to `--report`).
241
+
242
+ ```bash
243
+ # Local bot profile, last 3 months
244
+ dexbot pnl my-bot
245
+
246
+ # Raw account, one pair, generated path printed on exit
247
+ dexbot pnl 1.2.123456 --month 6 --pair TOKENA/BTS
248
+ ```
249
+
250
+ Per pair the report shows:
251
+
252
+ - **Hero** — account (bot name + `1.2.x` id), period, pair/lot counts, match mode, active filters.
253
+ - **Card blocks (2x2 left + 2x2 right)** — left = money (Gross PnL, Net PnL, Volume, Fees); right = position/activity (Realized lots, Net inventory Δ, Avg buy/sell, Unmatched sold). When both market and blockchain fees exist they collapse into one `Fees` card so the block stays 2x2.
254
+ - **Metrics grid** — Edge, Risk, Distribution, Behavior & activity: the same metric set as the terminal output (see the metrics glossary above).
255
+ - **Realized lots table** — collapsible per-match detail (entry/exit time, prices, gross/net PnL, fees, maker/taker legs), capped at 2,000 rows — use `--csv` for the full list.
256
+
257
+ All amounts use the shared 4-significant-figure formatter (compact `K`/`M`, significant trailing zeros kept) — the same one `dexbot order` uses. `--refresh-account` re-resolves the account and bypasses the fill cache (see [Fill cache](#trade-profitability-analyzer-trade_profitabilityts)).
258
+
223
259
  ### LAST-FILL-GUARD Check (`grid_correction_check.ts`)
224
260
 
225
261
  Validates LAST-FILL-GUARD discipline from the same Kibana fill pipeline as `trade_profitability.ts`. For a previous fill at price `x` and grid increment `i`, the next order must satisfy `BUY < x × (1 − i/2/100)` and `SELL > x × (1 + i/2/100)`, regardless of the previous fill's side. A violation is a buy or sell fill inside the prohibited half-increment band around its pivot.
@@ -492,6 +528,8 @@ Details: [bot_fitting/README.md](bot_fitting/README.md)
492
528
  | `account_resolver.ts` | Account resolution for all tools: `preferredAccount` / `--account` → `1.2.x`, stamping the result into `profiles/bots.json` |
493
529
  | `chain_pool.ts` | Ephemeral read-only chain client over the built-in node pool (account + asset lookups) |
494
530
  | `fills_source.ts` | Shared `fill_order` Kibana fetch/query and the static asset-precision table + on-chain cache |
531
+ | `fills_cache.ts` | Per-account month-shard cache for `fill_order` fetches (settled months reused, unsettled tail refreshed) |
532
+ | `pnl_report.ts` | Self-contained HTML PnL renderer: hero, card blocks, metrics grid, realized-lots table |
495
533
 
496
534
  On-chain account and asset lookups in the fill-based tools go through `account_resolver.ts` / `fills_source.ts` (both built on `chain_pool.ts`): tool scripts must not open their own read-only clients or carry their own node list. The batch backfill `resolve_bot_accounts.ts` is the exception — it reuses the production chain client over one connection.
497
535
 
@@ -14,6 +14,9 @@ For each of the 4 AMA strategies, it searches for best:
14
14
 
15
15
  - `backtest_bot_fitting.ts` — lightweight sweep across spread / increment / ratio with unit-size percentage-point accounting and risk scoring
16
16
  - `backtest_ama_sweep.ts` — persistent grid simulation with capital-weighted sizing, weight profiles, and worker-thread parallelization
17
+ - `backtest_lookback_drawdown.ts` — paired Huber-lookback comparison of
18
+ drawdown / net capture / reset churn (geometry fixed); see
19
+ `docs/AMA_SLOPE_WINDOW.md`
17
20
  - `shared_utils.ts` — shared helpers (argument parsing, data loading, formatting)
18
21
 
19
22
  Both simulators share the same production grid model (ported once in
@@ -84,8 +87,17 @@ node dist/analysis/bot_fitting/backtest_bot_fitting.js \
84
87
  node dist/analysis/bot_fitting/backtest_ama_sweep.js \
85
88
  --data <path-to-lp-candles.json> \
86
89
  --results <path-to-optimization-results.json>
90
+
91
+ # Paired lookback drawdown comparison (no --results needed)
92
+ node dist/analysis/bot_fitting/backtest_lookback_drawdown.js \
93
+ --data market_adapter/data/lp/<market-pair> \
94
+ --lookbacks 12,14,16,20
87
95
  ```
88
96
 
97
+ > `backtest_ama_sweep.ts` accepts `--lookback <bars>` to override the slope
98
+ > window (`DYNAMIC_WEIGHT_AMA_LOOKBACK_BARS`); `backtest_lookback_drawdown.ts`
99
+ > builds on it and reports the paired per-lookback deltas.
100
+
89
101
  > `backtest_bot_fitting.ts` auto-derives `--results` from the `--data` filename
90
102
  > (`analysis/ama_fitting/optimization_results_<base>_w<λ1>_<λ2>_<λ3>_<λ4>.json`,
91
103
  > newest `_w*` file matching the base name; falls back to the legacy
@@ -271,7 +271,7 @@ so the chart cannot drift from the runtime. The panel labels the winning layer;
271
271
 
272
272
  The replay runs in the page, so the AMA series (and therefore the trigger
273
273
  points) follows the AMA inputs and preset buttons; the gating parameters that
274
- would come from config (`erPeriod`, lookback, warmup, clip percentile) are fixed
274
+ would come from config (`erPeriod`, lookback, warmup, clip percentile, persistence gate) are fixed
275
275
  at generation time. One slope window is shared by every consumer: the resolved
276
276
  `lookbackBars` drives both the replayed `Δs` trigger and the `Scale` band tilt,
277
277
  so a bot-configured lookback can no longer leave the plotted band measuring a
@@ -284,7 +284,7 @@ canonical `simulateGridResetSeries()` from
284
284
 
285
285
  ## Notes
286
286
 
287
- - **Slope averaging.** The AMA slope is a **Huber-robust linear regression of `ln(AMA)` over the lookback window** — `computeHuberWindowSlopePct` in `market_adapter/core/strategies/dynamic_weight_series.ts`, which is the live adapter's own definition and is embedded here verbatim, so the chart and the bot run one logic path. Its tuning (`C`, `ITERATIONS`, `SCALE_FLOOR`, `ZERO_EPSILON`) is centralized in `MARKET_ADAPTER.DYNAMIC_WEIGHT_AMA_HUBER` and injected into the page as `const AMA_SLOPE_HUBER`, so a constant change flows to both. The output is in %/bar over the same window, so the tilt scale (`clamp(slope/maxSlopePct) * maxSlopeOffset`) and the `Δs` gate (`|Δslope| >= 8% of maxSlopePct`) keep their existing units. Measured on the repo's 1h pools with the shipped AMA preset at a 20-bar window: a single-bar AMA impulse is **strongly bounded** (a 1%/3%/5%/10% one-bar spike shifts the endpoint reading by 0.050/0.150/0.250/0.499 %/bar — 7x to 70x the reset gate — while the robust fit barely moves), and the fit is the smoothest robust option measured (second-difference energy ~25x lower than the old median's). Set `CHART_SLOPE_ESTIMATOR = 'endpoint'` in the page to render the reference two-point definition instead (kept in the core for comparison); the live adapter does not use it.
287
+ - **Slope averaging.** The AMA slope is a **Huber-robust linear regression of `ln(AMA)` over the lookback window** — `computeHuberWindowSlopePct` in `market_adapter/core/strategies/dynamic_weight_series.ts`, which is the live adapter's own definition and is embedded here verbatim, so the chart and the bot run one logic path. Its tuning (`C`, `ITERATIONS`, `SCALE_FLOOR`, `ZERO_EPSILON`) is centralized in `MARKET_ADAPTER.DYNAMIC_WEIGHT_AMA_HUBER` and injected into the page as `const AMA_SLOPE_HUBER`, so a constant change flows to both. The output is in %/bar over the same window, so the tilt scale (`clamp(slope/maxSlopePct) * maxSlopeOffset`) and the `Δs` gate (`|Δslope| >= 8% of maxSlopePct`) keep their existing units. Measured on the repo's 1h pools with the shipped AMA preset at the shipped 16-bar window: a single-bar AMA impulse is **strongly bounded** (a 1%/3%/5%/10% one-bar spike shifts the two-point endpoint reading by ≈ `spike/lookbackBars` %/bar ≈ 0.06/0.19/0.31/0.63 — 8x to 80x the reset gate — while the robust fit barely moves), and the fit is the smoothest robust option measured (second-difference energy ~25x lower than the old median's). Set `CHART_SLOPE_ESTIMATOR = 'endpoint'` in the page to render the reference two-point definition instead (kept in the core for comparison); the live adapter does not use it.
288
288
  - The chart embeds the vendored `uPlot` runtime inline (no CDN, no sibling `uplot/` dir, no DEXBot2 install needed). Each export is a single self-contained HTML file that renders anywhere, even after being copied or mailed to a machine without DEXBot2.
289
289
  - The displayed indicators are computed from the 1h base candles and then sampled onto the selected timeframe.
290
290
  - The current volume-weighted overlay is a rolling `VWMA`, not a session-reset VWAP.
@@ -96,8 +96,7 @@ Output: `analysis/charts/dynamic_weight_chart.html` (open in browser)
96
96
  | `--chart` | `analysis/charts/dynamic_weight_chart.html` | Output HTML path |
97
97
  | `--alpha` | `0.5` | Initial α blend (0 = pure Kalman, 1 = pure AMA) |
98
98
  | `--dw` | `0.50` | Initial displacement weight (0 = pure velocity, 1 = full displacement) |
99
- | `--lb` | `9` | Initial lookback bars (1-32) for AMA slope calculation |
100
- | `--ema` | `0` | Initial AMA input EMA span in bars (0 = off, 0-32) |
99
+ | `--lb` | `16` | Initial lookback bars (1-32) for AMA slope calculation |
101
100
  | `--gain` | `1.0` | Initial gain multiplier |
102
101
  | `--clip` | `10` | Initial clip percentile |
103
102
  | `--quiet` | `false` | Suppress console output |
@@ -115,7 +114,6 @@ All panels share aligned vertical time grid lines, and the bottom output panel s
115
114
 
116
115
  ### Panel 2 — AMA Slope Input (14%)
117
116
  - **Orange line**: AMA3 slope percentage
118
- - **Gray dashed line**: raw (unfiltered) slope — the reference when the `ema` knob > 0
119
117
  - Shows the directional strength of the slow KAMA's trend movement
120
118
  - Values outside the clip threshold are flattened before entering the offset formula
121
119
  - Background shading mirrors the output signal direction
@@ -155,7 +153,7 @@ All panels share aligned vertical time grid lines, and the bottom output panel s
155
153
  - `kf = 100` → current adaptive EMA result
156
154
  - `kf > 100` → pushes further toward the adaptive signal (up to 200)
157
155
 
158
- **dsp (displacement scale)**: Controls how fast displacement confidence saturates. Smaller values make it saturate faster (less displacement needed for full confidence); larger values require more displacement. The live adapter clamps `dsp` ≥ 1.0 for production stability.
156
+ **dsp (displacement scale)**: Controls how fast displacement confidence saturates. Smaller values make it saturate faster (less displacement needed for full confidence); larger values require more displacement. The live default is `1.0` (`DYNAMIC_WEIGHT_DISP_SCALE_MIN_PCT`); neither the live adapter nor the chart clamps it above or below that, so the slider's `0.25` floor is usable in both.
159
157
 
160
158
  **Latched signal (cf)**: Prevents the output from flip-flopping on every bar. It holds the last direction for `cf` bars before allowing a reversal:
161
159
  - `cf = 0` → raw signal (instant flips)
@@ -167,11 +165,10 @@ All panels share aligned vertical time grid lines, and the bottom output panel s
167
165
  | Knob | Range | Default | Purpose |
168
166
  |------|-------|---------|---------|
169
167
  | **nz%** | 0–1 | 0.00 | Neutral zone: dead-band below which offset is forced to 0 |
170
- | **lb** | 1–32 | 9 | Logarithmic. Lookback bars for AMA slope calculation |
171
- | **ema** | 0–32 | 0 | AMA input EMA span in bars (0 = off). Low-pass filters the AMA before the slope is taken; the gray dashed Raw‰ line shows the unfiltered slope for comparison |
172
- | **amaS%** | 0.06–0.12 | 0.09 | Logarithmic. Gear ratio for average per-bar AMA slope saturation |
168
+ | **lb** | 1–32 | 16 | Logarithmic. Lookback bars for AMA slope calculation |
169
+ | **amaS%** | 0.06–0.12 | 0.09 | Logarithmic. Gear ratio for per-bar AMA slope saturation |
173
170
  | **kalS%** | 0.5–1.5 | 1.11 | Logarithmic. Gear ratio for Kalman composite saturation |
174
- | **clip%** | 0–55 | 10 | Percentile clip: filters extreme inputs (0 = off) |
171
+ | **clip%** | 0–20 | 10 | Percentile clip: filters extreme inputs (0 = off) |
175
172
 
176
173
  ### Output Controls
177
174
  | Knob | Range | Default | Purpose |
@@ -182,7 +179,7 @@ All panels share aligned vertical time grid lines, and the bottom output panel s
182
179
 
183
180
  ## Copy / Paste Parameters
184
181
 
185
- The **copy** button serializes all knob values (α, dw, kf, kfd, dsp, kdt, kfs, cf, lb, ema, amaS%, kalS%, th%, gain, clip%, nz%, regi) to JSON and writes them to both the clipboard and `localStorage`.
182
+ The **copy** button serializes all knob values (α, dw, kf, kfd, dsp, kdt, kfs, cf, lb, amaS%, kalS%, th%, gain, clip%, nz%, regi) to JSON and writes them to both the clipboard and `localStorage`.
186
183
 
187
184
  The **paste** button first checks `localStorage` for parameters from a previous copy in the same browser session. If none found, it prompts for Ctrl+V input. A confirmation popup shows the parsed values before applying them. Click **Apply** to set the knobs, **Cancel** or press **Escape** to dismiss.
188
185
 
@@ -192,7 +189,7 @@ The **paste** button first checks `localStorage` for parameters from a previous
192
189
 
193
190
  ### AMA slope
194
191
 
195
- Taking the slope of the AMA produces a signal that is already noise-filtered at the source — a near-zero slope genuinely means sideways, not oscillation that happened to average out. The neutral zone (`nz%` knob) dead-bands any remaining micro-slope residuals. When that is still too jumpy, the optional `ema` knob low-pass filters the AMA input itself before the slope is taken (0 = off keeps the raw input).
192
+ Taking the slope of the AMA produces a signal that is already noise-filtered at the source — a near-zero slope genuinely means sideways, not oscillation that happened to average out. The canonical slope is a Huber-robust linear regression of `ln(AMA)` over the lookback window (per-bar log-return ×100, `MARKET_ADAPTER.DYNAMIC_WEIGHT_AMA_HUBER`), so a single outlier bar is discounted instead of getting full endpoint weight; the same estimator runs in the live adapter, both research charts, and the reset replay. The neutral zone (`nz%` knob) dead-bands any remaining micro-slope residuals.
196
193
 
197
194
  ### Kalman filter
198
195
 
@@ -287,7 +284,6 @@ The lookup table can be customized per-market or per-bot (see [Custom Configurat
287
284
  ## Formulas
288
285
 
289
286
  ### AMA Offset
290
- With `ema` > 0, `AMA_now` / `AMA_lb_bars_ago` below are read from the EMA-filtered AMA input instead of the raw AMA series (everything else identical):
291
287
  ```
292
288
  amaSlope% = ((AMA_now − AMA_lb_bars_ago) / AMA_lb_bars_ago × 100) / lb
293
289
  amaClip = clamp(amaSlope%, ±clipThreshold) // percentile-based clip
@@ -360,31 +356,10 @@ Formula: `kalComp = clippedV × (1 − dw + dw × dispConf × momAlign)`
360
356
  ### lb (lookback bars)
361
357
  Number of bars to look back when computing AMA slope:
362
358
  - `lb = 4`: Very short-term, highly responsive to recent price action
363
- - `lb = 9` (default): ~9 hours of hourly candles, balanced responsiveness and smoothing
359
+ - `lb = 16` (default): ~16 hours of hourly candles, balanced responsiveness and smoothing
364
360
  - `lb = 32`: ~1.3 days of hourly candles, stable but slower to react
365
361
 
366
- AMA slope is normalized to an average percent per bar, not the cumulative move across the full lookback. Lower values = more noise, faster reaction. Higher values = smoother signals, more lag without adding gain just because the measurement window is longer.
367
-
368
- ### ema (AMA input EMA span)
369
- Optional low-pass EMA over the AMA values before the slope is taken (span in bars, α = 2/(span+1), `0` = off). Because differencing and an EMA are both linear filters, they commute — smoothing the AMA input produces exactly the smoothed slope series, modulo the seeding transient.
370
-
371
- | `ema` | noise reduction* | added lag | verdict |
372
- |---|---|---|---|
373
- | 0 | 1× (off) | 0 bars | default, current behavior |
374
- | 4 | ~2× | ~+1.5 bars | light |
375
- | 9 (= lb) | ~3× | ~+4 bars | sweet spot |
376
- | 16 | ~4× | ~+7.5 bars | heavy but usable |
377
- | 32 | ~5.7× | ~+15.5 bars | upper-bound demo, too laggy to trade |
378
-
379
- \* standard-deviation reduction for white-ish noise ≈ `sqrt(span)`; lag ≈ `(span-1)/2` bars (EMA group delay) on top of the slope window's `(lb-1)/2`.
380
-
381
- Measured on ~8.7k hourly bars with the default AMA profile (`lb = 9`): the slow AMA's slope is dominated by low-frequency moves rather than white noise, so the `sqrt(span)` rule overstates the standard-deviation gain — `ema = 9` cut slope std only ~3% but trimmed the largest peaks ~22% and zero-crossings 86 → 74; `ema = 32` cut std ~11%, peaks ~48%, crossings 86 → 44, with a measured lag of ~3 bars at span 9 (theory 4). On this input the knob acts mainly as a peak-trimmer and sign-flip reducer, not as a general noise killer.
382
-
383
- Effects while tuning:
384
- - Zero-crossings get rarer, so the `cf` latch matters less and the `nz%` dead-band sits on a smooth curve instead of on noise.
385
- - |slope| peaks shrink, so the same `amaS%` reaches full offset strength less often — expect to lower `amaS%` (or raise `gain`) to restore output amplitude. The `clip%` percentile pool is rebuilt from the filtered input, which partially self-compensates.
386
- - Keep total lag in mind: ~`lb/2 + ema/2` bars before the slope reflects a move; at defaults that is ~8 bars (~8 h on the 1 h chart).
387
- - Panel 2 plots the smoothed slope (orange) over the raw slope (gray dashed) so the noise/lag trade-off is directly visible.
362
+ AMA slope is a per-bar log-return (Huber regression of `ln(AMA)`), not the cumulative move across the full lookback. Lower values = more noise, faster reaction. Higher values = smoother signals, more lag without adding gain just because the measurement window is longer.
388
363
 
389
364
  ### regi (regime sensitivity)
390
365
  - 0 = regime multiplier is always 1.0 (Hurst+PE ignored)
@@ -415,8 +390,7 @@ The 3×3 regime multiplier table can be customized per-market or per-bot (defaul
415
390
  Candle Data
416
391
  ├── AMA3 (slow KAMA, erPeriod=781)
417
392
  │ ├── calculateAMA() → AMA3 values per bar [price panel overlay]
418
- │ ├── `ema` knob (0 = off) → EMA-filtered AMA input [research chart only]
419
- │ ├── computeAmaSlopeWeights() → slope%
393
+ │ ├── computeAmaSlopeWeights() → Huber slope% (ln-return per bar)
420
394
  │ └── Percentile clip → amaClip → amaOff
421
395
  │
422
396
  ├── Kalman Filter (tactical + modal)
@@ -17,6 +17,37 @@ This folder contains the chart generators and re-export shims used by the analys
17
17
  - `regime_chart_generator.ts`
18
18
  - `volatility_chart_generator.ts`
19
19
 
20
+ ## Backtests
21
+
22
+ - `huber_scale_variants.ts` — research-only Huber scale variants (`none`/`df`/
23
+ `mscale`) plus an outlier-fraction diagnostic; the canonical estimator lives in
24
+ `market_adapter/core/strategies/dynamic_weight_series.ts` and is untouched.
25
+ - `backtest_ama_slope_huber.ts` — sweeps the Huber slope lookback window
26
+ (`--lookback`, default 8..28 bars; the live default is
27
+ `DYNAMIC_WEIGHT_AMA_LOOKBACK_BARS` = 16) over an LP candle
28
+ shard directory and reports **lag** (cross-correlation group delay vs centred
29
+ price/AMA slope, plus reversal-confirmation delay), **resets** (the canonical
30
+ `simulateGridResetSeries` drift + slope-delta decision path: counts, reason
31
+ split, rate, gap distribution, whipsaws) and **noise** (slope std, bar-to-bar
32
+ wobble, second-difference energy, zero-crossing rate, saturation, range tilt).
33
+
34
+ ```bash
35
+ npm run build
36
+ node dist/analysis/trend_detection/backtest_ama_slope_huber.js \
37
+ --data market_adapter/data/lp/<market-pair> --lookback 8:28:2
38
+ ```
39
+
40
+ Run `--help` for the full option list (AMA preset/overrides, reset thresholds,
41
+ confirmation gate, whipsaw definition, `--scale-mode`, JSON output path).
42
+
43
+ `--scale-mode none|df|mscale` swaps the Huber robust-scale estimate (production
44
+ is `none`); see `huber_scale_variants.ts` and `docs/AMA_SLOPE_WINDOW.md` for
45
+ why the plug-in scale is kept.
46
+
47
+ `revLag` is measured against a centred reference whose half-window is
48
+ `--truth-window` (default = max lookback), so compare it across runs only at a
49
+ fixed truth window; `amaLag` (cross-correlation group delay) is reference-robust.
50
+
20
51
  The Kalman/Hurst/PE analyzers below are re-export shims; the implementations live in `market_adapter/core/signals/` and are shared with the live market adapter:
21
52
 
22
53
  - `hurst_analyzer.ts` → `market_adapter/core/signals/hurst_analyzer.ts`
package/claw/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dexbot2-claw",
3
- "version": "1.6.10",
3
+ "version": "1.6.11",
4
4
  "private": true,
5
5
  "description": "Claw bridge and runtime integration layer for BitShares and DEXBot2.",
6
6
  "type": "module",
@@ -2,7 +2,7 @@
2
2
  "id": "bitshares-claw",
3
3
  "name": "BitShares Claw",
4
4
  "description": "Native BitShares and DEXBot2 tools from the DEXBot2 claw integration",
5
- "version": "1.6.10",
5
+ "version": "1.6.11",
6
6
  "skills": [
7
7
  "skills"
8
8
  ],
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bitshares-claw-openclaw-plugin",
3
3
  "private": true,
4
- "version": "1.6.10",
4
+ "version": "1.6.11",
5
5
  "type": "module",
6
6
  "main": "index.js"
7
7
  }
@@ -10,7 +10,7 @@
10
10
  * private copy of this decision tree, of the chain lookup and of the node
11
11
  * handling — all of it lives here now.
12
12
  */
13
- import { findBotKeyByAccountRef, loadBotMeta, persistBotAccountId } from './bot_key_utils.js';
13
+ import { findBotKeyByAccountRef, loadBotMeta, persistBotAccountId, resolveBotKey } from './bot_key_utils.js';
14
14
  import { withReadOnlyClient } from './chain_pool.js';
15
15
  const ACCOUNT_ID_RE = /^1\.2\.\d+$/;
16
16
  /** Default lookup: ephemeral read-only client over the built-in node pool. */
@@ -112,6 +112,18 @@ async function resolveAccountRef(accountRef, options = {}) {
112
112
  if (ACCOUNT_ID_RE.test(ref)) {
113
113
  return { accountId: ref, botKey: null, botMeta: null, source: 'typed-id', reason: null };
114
114
  }
115
+ // Local profile first: a bot whose profile name/key matches the reference
116
+ // resolves through its stored accountId/preferredAccount without any chain
117
+ // lookup. This is what lets `dexbot pnl <bot>` work offline and avoids
118
+ // treating a bot name as a chain account name.
119
+ try {
120
+ const botKey = resolveBotKey(ref, options.botsFile);
121
+ if (botKey)
122
+ return resolveBotAccount(botKey, options);
123
+ }
124
+ catch (_) {
125
+ // bots.json issues must never break resolution; fall through to chain.
126
+ }
115
127
  // A bot claiming this name is the persist target; its preferredAccount is
116
128
  // the same name, so resolveBotAccount owns the whole decision tree.
117
129
  let match = null;
@@ -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,
@@ -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 = computeHuberWindowSlopePct(amaValues, j, 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) ──────