dexbot 1.6.8 → 1.6.9
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.
- package/CHANGELOG.md +24 -0
- package/claw/package.json +1 -1
- package/claw/runtimes/openclaw-plugin/openclaw.plugin.json +1 -1
- package/claw/runtimes/openclaw-plugin/package.json +1 -1
- package/dist/dexbot.js +44 -21
- package/dist/market_adapter/core/market_adapter_service.d.ts +81 -1
- package/dist/market_adapter/core/market_adapter_service.js +225 -18
- package/dist/market_adapter/core/signals/hurst_analyzer.d.ts +10 -1
- package/dist/market_adapter/core/signals/hurst_analyzer.js +40 -18
- package/dist/market_adapter/core/signals/permutation_entropy_analyzer.d.ts +26 -2
- package/dist/market_adapter/core/signals/permutation_entropy_analyzer.js +132 -29
- package/dist/market_adapter/core/strategies/regime_gate.d.ts +61 -3
- package/dist/market_adapter/core/strategies/regime_gate.js +207 -7
- package/dist/market_adapter/interval_utils.d.ts +18 -1
- package/dist/market_adapter/interval_utils.js +31 -1
- package/dist/market_adapter/market_adapter.d.ts +151 -2
- package/dist/market_adapter/market_adapter.js +378 -62
- package/dist/market_adapter/test_helpers.d.ts +2 -2
- package/dist/market_adapter/test_helpers.js +2 -2
- package/dist/market_adapter/utils/file_json_cache.d.ts +29 -0
- package/dist/market_adapter/utils/file_json_cache.js +106 -0
- package/dist/modules/config.d.ts +6 -0
- package/dist/modules/config.js +1 -0
- package/dist/modules/constants.d.ts +5 -1
- package/dist/modules/constants.js +41 -14
- package/dist/modules/order/grid.js +95 -25
- package/dist/modules/version_notice.d.ts +166 -20
- package/dist/modules/version_notice.js +401 -56
- package/dist/pm2.js +9 -2
- package/dist/unlock.js +7 -7
- package/docs/DEXBOT_COMPARISON.md +3 -3
- package/docs/EVOLUTION.md +13 -12
- package/docs/FUND_MOVEMENT_AND_ACCOUNTING.md +1 -1
- package/docs/GRID_RECALCULATION.md +6 -2
- package/docs/README.md +2 -2
- package/market_adapter/README.md +28 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,8 +2,32 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
|
|
5
|
+
## [1.6.9] - 2026-09-30 - Market-Adapter Cycle CPU & Off-Hour Idle, Grid Spread-Tightening Correction
|
|
6
|
+
|
|
7
|
+
### 2026-09-29
|
|
8
|
+
|
|
9
|
+
- **Perf(market-adapter)**: cut hourly cycle CPU via regime memo, incremental analyzers and a single AMA — the hourly cycle re-created both signal analyzers and replayed the whole candle history per bot per cycle (~99.8% identical numbers), re-ran `calculateAMA` four extra times per bot for an `AMA1..AMA4` preset sweep that only fed a log line, and re-parsed ~250 KiB of candle JSON per bot per cycle that the process had just serialized itself. Measured on the production shape (capped sliding window): regime gate ~3.7 ms cold → 0.05–0.24 ms resumed (median 0.088 ms), cycle CPU ~155 ms → ~103 ms. The cross-cycle regime memo is keyed by `<botKey>:<intervalSeconds>` and resumes only when the incoming window ADVANCES the cached one (k leading bars dropped, m appended) with a leading-drop tolerance for the capped sliding window (a strict prefix check missed every steady-state cycle); exactness rests on the analyzers being rolling — resume requires the shared region to cover `bufferBars` (read from the analyzer instances, fails closed), so any verified alignment is exact by construction, and ambiguous/related-window mismatches, parameter changes and history rewrites all fall back to a cold recompute. Permutation entropy now keeps an incremental count map with factorial-number-system integer pattern keys (`update()` allocates nothing, O(1) per bar); Hurst uses preallocated rolling buffers and `computeRS` takes a `[from, to)` range instead of slicing ~30 arrays per bar. `calcAmaComparison` becomes `buildAmaRecord`, so the cycle reports the ONE AMA the bot trades on (resolved preset name + the already-computed value) instead of a 4-preset sweep; the persisted `state.bots[key].amaComparison` field keeps its key but holds a single entry (was four) — its only repo reader, the cycle log formatter, tolerates both shapes, and the log label changes `AMA compare:` → `AMA active:`. Config: `market_adapter` no longer honours `ama.enabled`; `gridPrice` is the only switch that makes a bot AMA-driven (the resolver previously disagreed with itself depending on whether a market profile matched, so a bot with `ama.enabled: false` kept trading while its published center froze) — no shipped bot sets the flag, and `docs/GRID_RECALCULATION.md` documents the single-switch rule plus the profile/keyword/defaultAmaKey/bot-block precedence. A write-through candle-JSON read cache (`market_adapter/utils/file_json_cache.ts`) is mtime/size-validated, consumed on read, and fails closed to the loader whenever the file cannot be `stat`ed. Coverage: a new entropy-equivalence suite (bar-by-bar equivalence vs verbatim pre-optimization reference algorithms, incl. sliding-window, alignment and ambiguity cases, all shown non-vacuous by defect re-injection), a file-JSON-cache suite, and AMA layering/precedence tests. Risk: the `amaComparison` shape change from four entries to one is verified to have no other repo consumer. Validation: 302/302 tests, `tsc` src+tests clean, browser bundle 39 passed (`market_adapter/core/strategies/regime_gate.ts`, `market_adapter/core/signals/permutation_entropy_analyzer.ts`, `market_adapter/core/signals/hurst_analyzer.ts`, `market_adapter/market_adapter.ts`, `market_adapter/core/market_adapter_service.ts`, `market_adapter/utils/file_json_cache.ts`, `modules/constants.ts`, `docs/GRID_RECALCULATION.md`, `tests/test_market_adapter_entropy_equivalence.ts`, `tests/test_market_adapter_file_json_cache.ts`, `tests/test_market_adapter_logic.ts`, `tests/test_market_adapter_service.ts`).
|
|
10
|
+
|
|
11
|
+
- **Perf(market-adapter)**: skip off-hour work and sleep to the candle boundary — the adapter only has work once per closed candle, but a respawn ran a full cycle immediately and an extra cycle could re-fetch, re-parse and re-serialize the same ~250 KiB candle file for zero new information. A per-bot closed-candle gate now runs before the native overlap fetch, the Kibana stale-tail check and the re-serialize: when the persisted `lastClosedCandleTs` already equals the newest closed bucket and the candle file covers that bucket (source match, no unresolved gaps, AMA warmup target met), the cycle records `lastCycleSource=off-hour-skip` and returns. One-shot entry points (`--once`, `runOnceForAma` backing `ama_signal_runner`) are exempt so they always emit a computed AMA. Daemon startup now decides before connecting whether every active AMA bot is caught up, and if so sleeps to the same poll boundary the loop uses, otherwise runs a catch-up cycle; the verdict is per bot (never max-aggregated across bots), reports every veto reason with the offending bot keys, and treats a known warmup target with an unknown candle count as outstanding. Refactor with no behavior change: the candle-grid arithmetic is centralized as `bucketStartMs`/`latestClosedBucketStartMs` in `interval_utils.ts` (shared by `sleepUntilAlignedBoundary`, `selectClosedCandles` and the startup sleep), `isCandleSourceMismatch` is shared between the off-hour gate and the full-path cache reset, and `evaluateStateRepairVeto` is shared as the state-side mirror of `candleFileCoversClosedBucket`. The socket stays open only for a working cycle; between cycles the process holds just an unref'd lock heartbeat, and the startup wait is always shorter than one poll period so no candle is ever skipped. An off-hour-skip state record is a last-known snapshot: data fields keep the last full cycle's values, only `lastCycleSource`/`lastCycleAt`/`pendingClosedCandle`/`lastTriggerSuppressedReason` are refreshed. (`market_adapter/market_adapter.ts`, `market_adapter/core/market_adapter_service.ts`, `market_adapter/interval_utils.ts`, `market_adapter/test_helpers.ts`, `market_adapter/README.md`, `tests/test_market_adapter_service.ts`).
|
|
12
|
+
|
|
13
|
+
- **Fix(grid)**: make spread correction tighten the spread instead of walking to the rail edge — spread correction chose candidates via a two-branch comparator (`windowFirst` on a live rail, `edgeFirst` otherwise) written as exact opposites, so on a SELL rail with a live window the branch sorted descending and correction picked the grid ceiling (BUY mirrored it at the rail floor). Neither placement moves bestBuy/bestSell, so the measured spread was unchanged and the next resync cancelled the orders as surplus. Both branches are replaced with one market-nearest comparator, and a spread-tightening guard anchors the placement to the live book: a SELL candidate must sit below the lowest live sell and a BUY above the highest live buy, using the same on-chain order set `calculateCurrentSpread` measures from (an empty rail has no anchor and stays open). The guard is the fix — the orphan pool is rail-wide, so market-nearest alone still resolves to the first empty slot past the live window; with the guard that pool empties, the promotion gate opens and the gap band is asked. Gap-band promotion candidates are now placed ahead of the rail pools, and a promotion that yields nothing names its binding constraint (stranding cap, spread reserve, no contiguous run) instead of skipping silently. Coverage: `tests/test_spread_correction_market_nearest.ts` (6 cases; 5 fail against pre-fix code, 1 passes both ways by design) (`modules/order/grid.ts`, `tests/test_spread_correction_market_nearest.ts`).
|
|
14
|
+
|
|
15
|
+
- **Docs(agents)**: require an explicit user request before any version bump — an agent that treats "the work is done" as "ship a release" will bump `package.json`, retag and push on its own initiative, and worse will end every task by asking whether to bump; the ask is the failure mode, turning a finished change into a decision the operator has to make twice and training a reflex that fires whether or not a release is warranted. The rule is one line: never bump, never propose, never ask; the user alone decides. The existing three-step procedure stays, scoped to a turn where the bump was explicitly requested, and that request is also the authorization for the commits/tags it needs so a real release is not gated twice. Documentation only, no runtime change (`AGENTS.md`).
|
|
16
|
+
|
|
17
|
+
### Version-notice hardening
|
|
18
|
+
|
|
19
|
+
The `fix(version-notice)` work committed after the 1.6.8 tag (real verdict from every entry point, staged `dexbot stat` status, npm-then-GitHub probe) is documented under [1.6.8]'s `2026-09-29` section above; no additional code change ships in 1.6.9.
|
|
20
|
+
|
|
5
21
|
## [1.6.8] - 2026-09-28 - Correction-Drain Bounds, PM2 Log Capture, Version Notice, Genesis Refusal & Legacy Matcher Removal, TV Grid-Reset Replay & Packaging Trim
|
|
6
22
|
|
|
23
|
+
### 2026-09-29
|
|
24
|
+
|
|
25
|
+
- **Fix(version-notice)**: one version check on every entry point, and never a silent one — the audit found the check was centralized but its *rendering* was not: `dexbot stat` fell back to a bare `DEXBot2 vX.Y.Z` header when the notice was switched off (`DEXBOT_SKIP_VERSION_NOTICE=1` / `UPDATER.NOTICE_ENABLED=false`) while `dexbot pm2` and `unlock` printed nothing at all, so on a node with the notice off the operator learned their build from one command and not the others; and `pm2.ts` exited 1 on a credential-daemon failure without awaiting the probe it had already started, discarding the answer it had paid for. The fallback is now the module's decision, not each entry point's: `printVersionStatusOrHeader` / `flushVersionStatusOrHeader` render the status line when there is one and the bare header when there is not, and `printVersionStatusWhenReady` (the non-awaited isolated-foreground path) uses the same contract, so `stat`, `pm2`, `start`, `restart`, `stop` and `reload` cannot disagree. `printVersionStatus(null)` stays a no-op for callers that only want the line. The `pm2` failure path now flushes before exiting. `maybePrintVersionStatus` (zero callers since `stat` went staged) is REMOVED rather than left as a second entry path, and the dead `intervalMs`-ignored option now actually drives the success window. Behavioral impact: with the notice disabled, `dexbot pm2`/`start`/`restart` now name the running build where they previously printed nothing; no wording, colour or network behaviour changes when the notice is on (`modules/version_notice.ts`, `dexbot.ts`, `pm2.ts`, `unlock.ts`, `tests/test_version_notice.ts`).
|
|
26
|
+
|
|
27
|
+
- **Feat(version-notice)**: stage the version verdict around the `dexbot stat` report, and say "no current version information" when the answer never arrives — a status command that shows a process table is the wrong place to block on the network, but printing the verdict inline under a short cap made a reachable registry read as "unknown", while deferring it unconditionally left a MISSING line that an operator can easily read as "you are up to date". `startStagedVersionStatus` now owns the escalation as two promises: a 1s grace (`UPDATER.NOTICE_STAGE_GRACE_MS`) at the top of the report, which a valid cache or a quick answer satisfies without the command ever noticing the wait; and a `settled` promise for the end, which gives the still-in-flight probe 1s more, then ASKS AGAIN with a forced, full-budget probe (`UPDATER.NOTICE_STATUS_TIMEOUT_MS`, 3s) — the first attempt may have failed fast or spent its share of the budget on a blocked source, and `force` matters because a cached FAILURE would otherwise be re-reported for the whole 15min backoff without a single request being made. When even that answers nothing, the verdict is the explicit `? No current version information (npm / github did not answer within 3000ms).` — a distinct `exhausted` state, not a failed check: the inline launcher path (`start`/`pm2`, which never escalates) keeps the more specific "could not check (reason)" wording, and neither path may ever render an unknown answer as green. The total wait is ~4s, all of it at the bottom of the report, and the report costs at most 1s up front. `case 'status'` funnels every branch through one `finish()` so no early `process.exit()` can truncate the line (`modules/version_notice.ts`, `dexbot.ts`, `modules/constants.ts`, `tests/test_version_notice.ts`).
|
|
28
|
+
|
|
29
|
+
- **Fix(version-notice)**: make the version probe answer instead of shrugging — a node that could not reach `registry.npmjs.org` reported `? Could not check for a newer version.` forever, and a single unreachable host, a too-tight timeout or a 5s network hiccup were indistinguishable from each other and from "you are current". Four defects, one symptom: (1) ONE source was a single point of failure, so the probe now queries npm's dist-tag document FIRST and falls back to the GitHub latest-release endpoint, derived from `UPDATER.REPOSITORY_URL` (`GITHUB_RELEASE_URL` pins it, `'off'` disables it, `GITHUB_API_BASE` retargets it at a mirror) — a host that cannot reach the registry now degrades to a slower probe instead of a permanent "unknown"; the total budget is split EVENLY across sources so a hanging first source cannot starve the fallback, and `compareVersions` strips a leading `v` so a `v1.6.8` GitHub tag does not parse as an ancient version and masquerade as an available update. The npm-first order is deliberate and was benchmarked, not assumed: latency is a wash (warm p50 npm 21ms / github 18ms, cold 92-102ms / 97-99ms), npm's document is 5x smaller (8KB vs 40KB), the GitHub API is 60 req/hour per IP unauthenticated (403 when spent) while npm has no comparable ceiling, and decisively, the release pipeline creates the GitHub release ~4-9 minutes BEFORE it publishes to npm (1.6.8 10:15:14Z vs 10:24:00Z) — GitHub-first would advertise a version `dexbot update` cannot yet install, which is a wrong hint rather than an early one. (2) EVERY failure path now records WHY (`ENOTFOUND`, `ECONNREFUSED`, `HTTP 403`, `timeout after Nms`, `no version in response`, `no fetch available`) and the gray verdict names it, persisted in the cache so even a throttled run can explain itself; an unnamed "?" is not actionable. (3) A FAILURE was throttled for the full 24h `NOTICE_INTERVAL_MS`, so one transient outage pinned the "?" for a day with no retry left to disprove it — a success is still cached (`UPDATER.NOTICE_INTERVAL_MS`, now 12h rather than 24h: the published version does not move, but `dexbot stat` is the command an operator runs to ask "am I current?" and a 24h window let that answer come from the previous morning), a failure now backs off for `UPDATER.NOTICE_RETRY_MS` (15min), and `DEXBOT_VERSION_CHECK_FORCE=1` bypasses the cache entirely as the diagnostic escape hatch. Every entry point consults the cache on every run; none of them re-probe on a timer of its own. (4) `dexbot stat` awaited the probe inline under a 750ms cap that a cold DNS + TLS handshake to the registry routinely exceeds; the probe is now STARTED at the top of the report and its verdict APPENDED at the end, so the process table is never delayed, the line can no longer be truncated by an early `process.exit()` (every branch in `case 'status'` now funnels through one `finish()` that flushes first), and the probe gets a real budget (`UPDATER.NOTICE_STATUS_TIMEOUT_MS` 6s) now that it is off the critical path. `UPDATER.NOTICE_TIMEOUT_MS` 2s → 4s for the same reason on the launcher path. `dexbot stat` then wraps the probe in the staged wait above. Behavioral impact: strictly more information and no new failure mode — the notice still never changes code, still never blocks a start beyond its bounded budget, and an unknown verdict is still never dressed up as "up to date". Risk: two requests instead of one when the first source fails (bounded by the same total budget), and a genuinely offline node now retries every 15min instead of once a day (`modules/version_notice.ts`, `dexbot.ts`, `modules/config.ts`, `modules/constants.ts`, `docs/README.md`, `tests/test_version_notice.ts`).
|
|
30
|
+
|
|
7
31
|
### 2026-09-26
|
|
8
32
|
|
|
9
33
|
- **Fix(concurrency)**: bound the price-correction drain and keep timer lock-waiters out of long broadcast regions — `correctAllPriceMismatches` held `_gridLock` (no acquisition timeout) across every queued correction, running price updates sequentially with `SYNC_DELAY_MS` between each, so a large `ordersNeedingPriceCorrection` backlog could hold the lock for minutes while every concurrent actor died at the 20s fill-lock timeout. Cancel-class entries (duplicate orphans / surplus / type mismatch) are still fully drained — batched, zero-delay, fund-safety-critical — but the sequential update loop is now capped at `FILL_PROCESSING.CORRECTION_MAX_UPDATES_PER_CYCLE` (default 5); the unselected remainder stays queued durably and re-drains next cycle, keeping its queue position so newer entries cannot be starved. A rate-limited backlog alarm (`CORRECTION_QUEUE_WARN_THRESHOLD` default 10, `CORRECTION_QUEUE_WARN_RATE_LIMIT_MS` 5 min) fires even while the drain defers, which is exactly when the queue is growing. The drain also defers *before* acquiring `_gridLock` when `isBroadcastingActive()`, and the maintenance timer loops (open-orders sync, periodic blockchain fetch) get the same pre-acquire deferral via `shouldDeferMaintenanceForBroadcast`; that helper is age-bounded by `BROADCAST_STALE_CLEAR_MS` so a leaked broadcast flag still lets the periodic tick through to run `_clearStaleBroadcastFlag` (deferring on a stale flag would otherwise remove the only watchdog that clears it). `TIMING.FILL_BROADCAST_DEFER_MAX_MS` is now derived as `max(configured, BROADCAST_STALE_CLEAR_MS + 30s)` so a legitimately long startup reconcile Phase 2 cannot outlast the fill-consumer deferral and drop it into the in-lock wait, and the deferral bound re-arms whenever `manager._broadcastingStartedAt` advances (a live region) while a frozen flag still trips the fallback. Startup create-group execution yields to the event loop between groups (`setTimeout(0)`, browser-safe) so the region's timers/watchdogs (region-end reschedule, `_awaitBroadcastIdle`, stale-flag clear) are not starved. Behavioral impact: a correction backlog no longer monopolizes `_gridLock`; fills/corrections still drain, bounded per cycle; the broadcast-deferral bound now outlasts the stale-flag watchdog it depends on; a leaked broadcast flag still self-heals. Risk: only the sequential price-update loop is budgeted, so a very large update backlog drains over multiple cycles (`modules/order/utils/order.ts`, `modules/constants.ts`, `modules/dexbot_fill_runtime.ts`, `modules/dexbot_maintenance_runtime.ts`, `modules/order/grid_reconcile_internal.ts`, `modules/order/manager.ts`, `tests/test_correction_queue_staleness.ts`, `tests/test_fill_pipeline_robustness.ts`, `tests/test_lock_bypass_guards.ts`).
|
package/claw/package.json
CHANGED
package/dist/dexbot.js
CHANGED
|
@@ -88,7 +88,6 @@ const { initializeFeeCache, ensureProfilesDirectory, readInput } = require('./mo
|
|
|
88
88
|
const accountBots = require('./modules/account_bots');
|
|
89
89
|
const SharedDEXBot = require('./modules/dexbot_class').default;
|
|
90
90
|
const fundRegistry = require('./modules/fund_registry');
|
|
91
|
-
const { maybePrintVersionStatus } = require('./modules/version_notice');
|
|
92
91
|
/**
|
|
93
92
|
* Resolve a collateral asset reference (symbol or ID) to its canonical asset ID.
|
|
94
93
|
* Uses the already-connected BitShares DB instance.
|
|
@@ -1076,20 +1075,49 @@ async function handleCLICommands() {
|
|
|
1076
1075
|
// Installed-vs-published version status, from the single shared
|
|
1077
1076
|
// renderer in modules/version_notice.ts: green when current, orange
|
|
1078
1077
|
// when a newer release exists, gray when the probe cannot answer.
|
|
1079
|
-
//
|
|
1080
|
-
//
|
|
1081
|
-
//
|
|
1082
|
-
//
|
|
1083
|
-
//
|
|
1078
|
+
//
|
|
1079
|
+
// The probe is STAGED around the report instead of being awaited
|
|
1080
|
+
// inline (see startStagedVersionStatus):
|
|
1081
|
+
// top 1s grace. A valid cache or a quick registry answer lands
|
|
1082
|
+
// inside it and the report never notices the wait.
|
|
1083
|
+
// bottom 1s more for the probe still in flight, then a forced
|
|
1084
|
+
// re-probe with the full NOTICE_STATUS_TIMEOUT_MS budget.
|
|
1085
|
+
// If even that is silent, the verdict says so explicitly
|
|
1086
|
+
// rather than leaving a missing line to be read as
|
|
1087
|
+
// "you are up to date".
|
|
1088
|
+
// So the report costs at most 1s up front, and the verdict is
|
|
1089
|
+
// printed at exactly one terminal point, which no early
|
|
1090
|
+
// `process.exit()` in a branch below can truncate.
|
|
1084
1091
|
const { UPDATER } = require('./modules/constants');
|
|
1085
|
-
const
|
|
1092
|
+
const { startStagedVersionStatus, printVersionStatusOrHeader } = require('./modules/version_notice');
|
|
1093
|
+
const versionWait = startStagedVersionStatus({
|
|
1094
|
+
graceMs: UPDATER.NOTICE_STAGE_GRACE_MS,
|
|
1086
1095
|
timeoutMs: UPDATER.NOTICE_STATUS_TIMEOUT_MS,
|
|
1087
|
-
|
|
1088
|
-
|
|
1096
|
+
// DEXBOT_VERSION_CHECK_FORCE=1 re-probes instead of answering
|
|
1097
|
+
// from the cache: the escape hatch when a cached failure is
|
|
1098
|
+
// indistinguishable from a live one.
|
|
1099
|
+
force: Config.DEXBOT_VERSION_CHECK_FORCE,
|
|
1089
1100
|
});
|
|
1090
|
-
|
|
1091
|
-
|
|
1101
|
+
// Top-of-report grace. The VALUE is unused on purpose, only the
|
|
1102
|
+
// timing matters: `settled` is what gets displayed at the end.
|
|
1103
|
+
await versionWait.quick;
|
|
1092
1104
|
console.log();
|
|
1105
|
+
// The ONE terminal point of this command: every branch below ends
|
|
1106
|
+
// in `finish(...)` instead of `process.exit(...)`, so the deferred
|
|
1107
|
+
// version line is always printed — including on the "no processes"
|
|
1108
|
+
// and delegated-`unlock status` paths, which used to exit before
|
|
1109
|
+
// the probe had anything to say.
|
|
1110
|
+
const finish = async (code) => {
|
|
1111
|
+
// Blank line, then the verdict: it is the last thing the
|
|
1112
|
+
// operator reads, so it gets its own block at the bottom. With
|
|
1113
|
+
// the check switched off, the shared helper falls back to the
|
|
1114
|
+
// bare "DEXBot2 vX.Y.Z" header, so the running build is always
|
|
1115
|
+
// named - the inline path's long-standing behaviour.
|
|
1116
|
+
console.log();
|
|
1117
|
+
printVersionStatusOrHeader(await versionWait.settled, { indent: '', surround: false });
|
|
1118
|
+
process.exit(code);
|
|
1119
|
+
return true;
|
|
1120
|
+
};
|
|
1093
1121
|
const { spawnSync, execSync } = require('child_process');
|
|
1094
1122
|
const MONOLITHIC_PID_FILE = PATHS.PROFILES.MONOLITHIC_PID;
|
|
1095
1123
|
const MONOLITHIC_CRED_PID_FILE = PATHS.PROFILES.MONOLITHIC_CRED_PID;
|
|
@@ -1149,22 +1177,19 @@ async function handleCLICommands() {
|
|
|
1149
1177
|
cwd: PATHS.PROJECT_ROOT,
|
|
1150
1178
|
stdio: 'inherit',
|
|
1151
1179
|
});
|
|
1152
|
-
|
|
1153
|
-
return true;
|
|
1180
|
+
return finish(result.status ?? 0);
|
|
1154
1181
|
}
|
|
1155
1182
|
try {
|
|
1156
1183
|
const output = execSync('pm2 jlist 2>/dev/null', { encoding: 'utf8', timeout: 5000 }).toString().trim();
|
|
1157
1184
|
const jsonStart = output.indexOf('[');
|
|
1158
1185
|
if (jsonStart === -1) {
|
|
1159
1186
|
console.log('No DEXBot2 processes running.');
|
|
1160
|
-
|
|
1161
|
-
return true;
|
|
1187
|
+
return finish(0);
|
|
1162
1188
|
}
|
|
1163
1189
|
const allProcs = JSON.parse(output.slice(jsonStart));
|
|
1164
1190
|
if (!Array.isArray(allProcs) || allProcs.length === 0) {
|
|
1165
1191
|
console.log('No DEXBot2 processes running.');
|
|
1166
|
-
|
|
1167
|
-
return true;
|
|
1192
|
+
return finish(0);
|
|
1168
1193
|
}
|
|
1169
1194
|
const serviceNames = new Set(['dexbot-cred', 'dexbot-adapter', 'dexbot-update']);
|
|
1170
1195
|
const botNames = new Set();
|
|
@@ -1183,8 +1208,7 @@ async function handleCLICommands() {
|
|
|
1183
1208
|
});
|
|
1184
1209
|
if (dexbotProcs.length === 0) {
|
|
1185
1210
|
console.log('No DEXBot2 processes running.');
|
|
1186
|
-
|
|
1187
|
-
return true;
|
|
1211
|
+
return finish(0);
|
|
1188
1212
|
}
|
|
1189
1213
|
console.log('='.repeat(50));
|
|
1190
1214
|
console.log('DEXBot2 PM2 Processes');
|
|
@@ -1235,8 +1259,7 @@ async function handleCLICommands() {
|
|
|
1235
1259
|
catch {
|
|
1236
1260
|
console.log('No DEXBot2 processes running.');
|
|
1237
1261
|
}
|
|
1238
|
-
|
|
1239
|
-
return true;
|
|
1262
|
+
return finish(0);
|
|
1240
1263
|
}
|
|
1241
1264
|
case 'delete':
|
|
1242
1265
|
case 'stop':
|
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
declare const AMA_SLOPE_PERCENT_MODE_PER_BAR = "perBar";
|
|
2
2
|
declare function normalizeAmaSlopePercentMode(value: any): "perBar" | "window" | null;
|
|
3
3
|
declare function convertSlopePercentToPerBar(value: any, lookbackBars: any, mode: any): number | null;
|
|
4
|
+
/**
|
|
5
|
+
* Standing repair verdict for a PERSISTED STATE entry — the summary the
|
|
6
|
+
* previous cycle wrote. Returns a reason string when repair is still owed,
|
|
7
|
+
* null when the cache is clean.
|
|
8
|
+
*
|
|
9
|
+
* State-side mirror of candleFileCoversClosedBucket: that gate judges the
|
|
10
|
+
* on-disk candle file, this one judges the state summary, and the two must
|
|
11
|
+
* describe the same standing conditions so the startup sleep and the in-cycle
|
|
12
|
+
* skip cannot silently disagree. When a new standing signal is added to one,
|
|
13
|
+
* add it to the other (source/newest-candle checks are file-only and cannot
|
|
14
|
+
* appear here; unresolvedGapCount and the warmup shortfall exist in both).
|
|
15
|
+
*/
|
|
16
|
+
declare function evaluateStateRepairVeto(entry: any): "unresolved_gaps" | "cache_count_unknown" | "cache_below_warmup_target" | null;
|
|
4
17
|
declare class MarketAdapterService {
|
|
5
18
|
deps: any;
|
|
6
19
|
constructor(deps?: any);
|
|
@@ -16,6 +29,73 @@ declare class MarketAdapterService {
|
|
|
16
29
|
* deeper history than the analytical minimum.
|
|
17
30
|
*/
|
|
18
31
|
bootstrapKibanaLookbackHours(cfg: any, keepCount: any): number;
|
|
32
|
+
/**
|
|
33
|
+
* Closed-bucket arithmetic shared by the off-hour skip logic.
|
|
34
|
+
*
|
|
35
|
+
* Thin instance wrapper around the shared bucket helper so the `getNowMs`
|
|
36
|
+
* injection used by tests and embedders keeps working. `intervalSeconds`
|
|
37
|
+
* is validated by the helper: a missing/invalid interval can never
|
|
38
|
+
* masquerade as "nothing new" — it returns null and the caller must fall
|
|
39
|
+
* through to the network path instead of skipping a cycle it cannot
|
|
40
|
+
* evaluate.
|
|
41
|
+
*/
|
|
42
|
+
latestClosedBucketStartMs(intervalSeconds: any, nowMs?: any): number | null;
|
|
43
|
+
/**
|
|
44
|
+
* Off-hour skip check: is there any closed candle this bot has not
|
|
45
|
+
* consumed yet? Runs on persisted state + the wall clock only — no
|
|
46
|
+
* chain RPC, no candle parse. Returns null when the answer cannot be
|
|
47
|
+
* determined (invalid interval, unknown state shape, one-shot mode), in
|
|
48
|
+
* which case the caller must run the full cycle instead of skipping.
|
|
49
|
+
*
|
|
50
|
+
* `once` (the `--once` CLI and the embedded runOnceForAma) always runs the
|
|
51
|
+
* full cycle: those entry points exist to *produce* a result on demand —
|
|
52
|
+
* the signal runner prints the AMA, an operator asks for a fresh cycle —
|
|
53
|
+
* and returning a state-only "skipped" record with null prices would
|
|
54
|
+
* quietly degrade their output. Only the long-running daemon, which has a
|
|
55
|
+
* schedule of its own, may answer "nothing new, skipping".
|
|
56
|
+
*
|
|
57
|
+
* The verdict requires the consumed marker to equal the newest closed
|
|
58
|
+
* bucket exactly. A marker AHEAD of that bucket means the two clocks
|
|
59
|
+
* disagree (clock skew, a state file carried across hosts, a synthetic
|
|
60
|
+
* fixture); skipping there would discard a candle the adapter may not
|
|
61
|
+
* actually have processed, so the conservative answer is always "run".
|
|
62
|
+
*
|
|
63
|
+
* Skipped bots still refresh `lastCycleAt`/`lastCycleSource` in state so
|
|
64
|
+
* monitoring can tell "idle by design" apart from "dead process".
|
|
65
|
+
*/
|
|
66
|
+
shouldSkipBotForClosedCandle(_bot: any, botState: any, intervalSeconds: any, nowMs?: any, cfg?: any): {
|
|
67
|
+
latestClosedBucketStartMs: number;
|
|
68
|
+
previousClosedCandleTs: number;
|
|
69
|
+
} | null;
|
|
70
|
+
/**
|
|
71
|
+
* Does the cached candle file belong to a different market source than the
|
|
72
|
+
* bot is configured for?
|
|
73
|
+
*
|
|
74
|
+
* Single source of truth for the reset rule: the off-hour gate
|
|
75
|
+
* (candleFileCoversClosedBucket) and the full path must agree, otherwise a
|
|
76
|
+
* source switch could be skipped off-hour and the cache would keep serving
|
|
77
|
+
* candles from the old source until the next close forced a full run.
|
|
78
|
+
* The pool/book cross-checks matter for legacy caches that predate
|
|
79
|
+
* `meta.marketSource`: a stored pool id alongside a book-configured bot (or
|
|
80
|
+
* a book cache under a pool bot) is just as much a mismatch.
|
|
81
|
+
*/
|
|
82
|
+
isCandleSourceMismatch(meta: any, marketSource: any): boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Does the local candle cache already hold the data for a closed bucket?
|
|
85
|
+
*
|
|
86
|
+
* Second half of the off-hour skip verdict (see
|
|
87
|
+
* shouldSkipBotForClosedCandle): the full cycle repairs history even
|
|
88
|
+
* without a new close (Kibana backfill, gap fill), so skipping is only
|
|
89
|
+
* safe when the cache is healthy for that bucket. False whenever
|
|
90
|
+
* anything needs repair: a source switch, unresolved gaps, an empty or
|
|
91
|
+
* short file (fewer candles than the AMA warmup target — the exact case
|
|
92
|
+
* the backfill path exists for), or a newest candle that predates the
|
|
93
|
+
* bucket.
|
|
94
|
+
*
|
|
95
|
+
* The state-side mirror of the standing conditions (unresolved gaps, warmup
|
|
96
|
+
* shortfall) is evaluateStateRepairVeto above; keep the two in step.
|
|
97
|
+
*/
|
|
98
|
+
candleFileCoversClosedBucket(candles: any, meta: any, marketSource: any, closedBucketStartMs: any, requiredCount?: any): boolean;
|
|
19
99
|
selectClosedCandles(candles: any, intervalSeconds: any, nowMs?: any): {
|
|
20
100
|
closedCandles: any[];
|
|
21
101
|
currentBucketStartMs: null;
|
|
@@ -231,5 +311,5 @@ declare class MarketAdapterService {
|
|
|
231
311
|
};
|
|
232
312
|
processBot(bot: any, state: any, cfg: any, contextCache: any, hooks?: any): Promise<any>;
|
|
233
313
|
}
|
|
234
|
-
export { MarketAdapterService, AMA_SLOPE_PERCENT_MODE_PER_BAR, normalizeAmaSlopePercentMode, convertSlopePercentToPerBar };
|
|
314
|
+
export { MarketAdapterService, AMA_SLOPE_PERCENT_MODE_PER_BAR, normalizeAmaSlopePercentMode, convertSlopePercentToPerBar, evaluateStateRepairVeto };
|
|
235
315
|
//# sourceMappingURL=market_adapter_service.d.ts.map
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { calculateATR } from './strategies/atr/calculator.js';
|
|
3
3
|
import { normalizeMarketSource, hasNumericStartPrice, resolveMarketSourceForBot } from '../utils/chain.js';
|
|
4
4
|
import { computeRegimeMultiplier } from './strategies/regime_gate.js';
|
|
5
|
+
import { noteJsonWritten, readCachedJson } from '../utils/file_json_cache.js';
|
|
5
6
|
import { calculateAMA, getAmaWarmupBars } from './strategies/ama.js';
|
|
6
7
|
import { computeDynamicWeightSeries } from './strategies/dynamic_weight_series.js';
|
|
7
8
|
import { KalmanTrendAnalyzer } from './signals/kalman_trend_analyzer.js';
|
|
@@ -16,6 +17,7 @@ import { normalizeAtrPeriod, normalizeMaxVolatilityOffset, normalizeVolatilityTh
|
|
|
16
17
|
import { buildKalmanVelocitySeries, computeAbsolutePercentileThreshold, } from './signals/kalman_velocity_smoothing.js';
|
|
17
18
|
import { resolveMaxAsymmetryFactor, computeAsymmetricBoundsMetrics, } from './asymmetric_bounds.js';
|
|
18
19
|
import { getErrorMessage } from '../../modules/utils/errors.js';
|
|
20
|
+
import { bucketStartMs, latestClosedBucketStartMs as latestClosedBucket } from '../interval_utils.js';
|
|
19
21
|
const marketAdapterServiceLogger = new Logger('MarketAdapterService');
|
|
20
22
|
const AMA_SLOPE_PERCENT_MODE_PER_BAR = 'perBar';
|
|
21
23
|
const AMA_SLOPE_PERCENT_MODE_WINDOW = 'window';
|
|
@@ -94,6 +96,34 @@ function computeGridPriceOffsetPlan(bot, amaSlope) {
|
|
|
94
96
|
gridPriceOffsetPct,
|
|
95
97
|
};
|
|
96
98
|
}
|
|
99
|
+
/**
|
|
100
|
+
* Standing repair verdict for a PERSISTED STATE entry — the summary the
|
|
101
|
+
* previous cycle wrote. Returns a reason string when repair is still owed,
|
|
102
|
+
* null when the cache is clean.
|
|
103
|
+
*
|
|
104
|
+
* State-side mirror of candleFileCoversClosedBucket: that gate judges the
|
|
105
|
+
* on-disk candle file, this one judges the state summary, and the two must
|
|
106
|
+
* describe the same standing conditions so the startup sleep and the in-cycle
|
|
107
|
+
* skip cannot silently disagree. When a new standing signal is added to one,
|
|
108
|
+
* add it to the other (source/newest-candle checks are file-only and cannot
|
|
109
|
+
* appear here; unresolvedGapCount and the warmup shortfall exist in both).
|
|
110
|
+
*/
|
|
111
|
+
function evaluateStateRepairVeto(entry) {
|
|
112
|
+
if (Number(entry?.unresolvedGapCount) > 0)
|
|
113
|
+
return 'unresolved_gaps';
|
|
114
|
+
const keepTarget = Number(entry?.rawKeepCount);
|
|
115
|
+
if (!Number.isFinite(keepTarget) || keepTarget <= 0)
|
|
116
|
+
return null;
|
|
117
|
+
const candleCount = Number(entry?.candleCount);
|
|
118
|
+
// Target known but current count unknown is NOT "nothing owed": an
|
|
119
|
+
// older or half-written row would otherwise read as healthy. Veto, and
|
|
120
|
+
// let the next full cycle rewrite the row with both fields.
|
|
121
|
+
if (!Number.isFinite(candleCount))
|
|
122
|
+
return 'cache_count_unknown';
|
|
123
|
+
if (candleCount < keepTarget)
|
|
124
|
+
return 'cache_below_warmup_target';
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
97
127
|
class MarketAdapterService {
|
|
98
128
|
deps;
|
|
99
129
|
constructor(deps = {}) {
|
|
@@ -130,18 +160,123 @@ class MarketAdapterService {
|
|
|
130
160
|
: 1;
|
|
131
161
|
return Math.max(Number(cfg?.bootstrapLookbackHours) || 0, Number(keepCount) * intervalHours);
|
|
132
162
|
}
|
|
163
|
+
/**
|
|
164
|
+
* Closed-bucket arithmetic shared by the off-hour skip logic.
|
|
165
|
+
*
|
|
166
|
+
* Thin instance wrapper around the shared bucket helper so the `getNowMs`
|
|
167
|
+
* injection used by tests and embedders keeps working. `intervalSeconds`
|
|
168
|
+
* is validated by the helper: a missing/invalid interval can never
|
|
169
|
+
* masquerade as "nothing new" — it returns null and the caller must fall
|
|
170
|
+
* through to the network path instead of skipping a cycle it cannot
|
|
171
|
+
* evaluate.
|
|
172
|
+
*/
|
|
173
|
+
latestClosedBucketStartMs(intervalSeconds, nowMs = this.getNowMs()) {
|
|
174
|
+
return latestClosedBucket(nowMs, intervalSeconds);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Off-hour skip check: is there any closed candle this bot has not
|
|
178
|
+
* consumed yet? Runs on persisted state + the wall clock only — no
|
|
179
|
+
* chain RPC, no candle parse. Returns null when the answer cannot be
|
|
180
|
+
* determined (invalid interval, unknown state shape, one-shot mode), in
|
|
181
|
+
* which case the caller must run the full cycle instead of skipping.
|
|
182
|
+
*
|
|
183
|
+
* `once` (the `--once` CLI and the embedded runOnceForAma) always runs the
|
|
184
|
+
* full cycle: those entry points exist to *produce* a result on demand —
|
|
185
|
+
* the signal runner prints the AMA, an operator asks for a fresh cycle —
|
|
186
|
+
* and returning a state-only "skipped" record with null prices would
|
|
187
|
+
* quietly degrade their output. Only the long-running daemon, which has a
|
|
188
|
+
* schedule of its own, may answer "nothing new, skipping".
|
|
189
|
+
*
|
|
190
|
+
* The verdict requires the consumed marker to equal the newest closed
|
|
191
|
+
* bucket exactly. A marker AHEAD of that bucket means the two clocks
|
|
192
|
+
* disagree (clock skew, a state file carried across hosts, a synthetic
|
|
193
|
+
* fixture); skipping there would discard a candle the adapter may not
|
|
194
|
+
* actually have processed, so the conservative answer is always "run".
|
|
195
|
+
*
|
|
196
|
+
* Skipped bots still refresh `lastCycleAt`/`lastCycleSource` in state so
|
|
197
|
+
* monitoring can tell "idle by design" apart from "dead process".
|
|
198
|
+
*/
|
|
199
|
+
shouldSkipBotForClosedCandle(_bot, botState, intervalSeconds, nowMs = this.getNowMs(), cfg = null) {
|
|
200
|
+
if (cfg?.once)
|
|
201
|
+
return null;
|
|
202
|
+
if (!botState || typeof botState !== 'object' || Array.isArray(botState))
|
|
203
|
+
return null;
|
|
204
|
+
const latestClosed = this.latestClosedBucketStartMs(intervalSeconds, nowMs);
|
|
205
|
+
if (latestClosed === null)
|
|
206
|
+
return null;
|
|
207
|
+
const previousClosedCandleTs = Number(botState.lastClosedCandleTs || 0);
|
|
208
|
+
// No consumed marker yet (fresh state / cleared state): the full
|
|
209
|
+
// cycle must run — bootstrap and warmup checks live there.
|
|
210
|
+
if (!Number.isFinite(previousClosedCandleTs) || previousClosedCandleTs <= 0)
|
|
211
|
+
return null;
|
|
212
|
+
if (latestClosed === previousClosedCandleTs) {
|
|
213
|
+
return { latestClosedBucketStartMs: latestClosed, previousClosedCandleTs };
|
|
214
|
+
}
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Does the cached candle file belong to a different market source than the
|
|
219
|
+
* bot is configured for?
|
|
220
|
+
*
|
|
221
|
+
* Single source of truth for the reset rule: the off-hour gate
|
|
222
|
+
* (candleFileCoversClosedBucket) and the full path must agree, otherwise a
|
|
223
|
+
* source switch could be skipped off-hour and the cache would keep serving
|
|
224
|
+
* candles from the old source until the next close forced a full run.
|
|
225
|
+
* The pool/book cross-checks matter for legacy caches that predate
|
|
226
|
+
* `meta.marketSource`: a stored pool id alongside a book-configured bot (or
|
|
227
|
+
* a book cache under a pool bot) is just as much a mismatch.
|
|
228
|
+
*/
|
|
229
|
+
isCandleSourceMismatch(meta, marketSource) {
|
|
230
|
+
const storedSource = normalizeMarketSource(meta?.marketSource);
|
|
231
|
+
const hasStoredPoolContext = meta?.pool != null && String(meta.pool).trim() !== '';
|
|
232
|
+
return (!!storedSource && storedSource !== marketSource)
|
|
233
|
+
|| (marketSource === 'book' && hasStoredPoolContext)
|
|
234
|
+
|| (marketSource === 'pool' && storedSource === 'book');
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Does the local candle cache already hold the data for a closed bucket?
|
|
238
|
+
*
|
|
239
|
+
* Second half of the off-hour skip verdict (see
|
|
240
|
+
* shouldSkipBotForClosedCandle): the full cycle repairs history even
|
|
241
|
+
* without a new close (Kibana backfill, gap fill), so skipping is only
|
|
242
|
+
* safe when the cache is healthy for that bucket. False whenever
|
|
243
|
+
* anything needs repair: a source switch, unresolved gaps, an empty or
|
|
244
|
+
* short file (fewer candles than the AMA warmup target — the exact case
|
|
245
|
+
* the backfill path exists for), or a newest candle that predates the
|
|
246
|
+
* bucket.
|
|
247
|
+
*
|
|
248
|
+
* The state-side mirror of the standing conditions (unresolved gaps, warmup
|
|
249
|
+
* shortfall) is evaluateStateRepairVeto above; keep the two in step.
|
|
250
|
+
*/
|
|
251
|
+
candleFileCoversClosedBucket(candles, meta, marketSource, closedBucketStartMs, requiredCount = 0) {
|
|
252
|
+
if (!Number.isFinite(closedBucketStartMs) || closedBucketStartMs <= 0)
|
|
253
|
+
return false;
|
|
254
|
+
if (this.isCandleSourceMismatch(meta, marketSource))
|
|
255
|
+
return false;
|
|
256
|
+
if (Number(meta?.unresolvedGapCount) > 0)
|
|
257
|
+
return false;
|
|
258
|
+
if (!Array.isArray(candles) || candles.length === 0)
|
|
259
|
+
return false;
|
|
260
|
+
// Short history means the warmup/backfill path still has work to do.
|
|
261
|
+
const required = Number(requiredCount);
|
|
262
|
+
if (Number.isFinite(required) && required > 0 && candles.length < required)
|
|
263
|
+
return false;
|
|
264
|
+
const newestTs = Number(candles[candles.length - 1]?.[0]);
|
|
265
|
+
if (!Number.isFinite(newestTs))
|
|
266
|
+
return false;
|
|
267
|
+
return newestTs >= closedBucketStartMs;
|
|
268
|
+
}
|
|
133
269
|
selectClosedCandles(candles, intervalSeconds, nowMs = this.getNowMs()) {
|
|
134
|
-
const
|
|
135
|
-
if (
|
|
270
|
+
const currentBucketStart = bucketStartMs(nowMs, intervalSeconds);
|
|
271
|
+
if (currentBucketStart === null) {
|
|
136
272
|
return {
|
|
137
273
|
closedCandles: Array.isArray(candles) ? candles.slice() : [],
|
|
138
274
|
currentBucketStartMs: null,
|
|
139
275
|
};
|
|
140
276
|
}
|
|
141
|
-
const currentBucketStartMs = Math.floor(Number(nowMs) / bucketMs) * bucketMs;
|
|
142
277
|
const closedCandles = (Array.isArray(candles) ? candles : [])
|
|
143
|
-
.filter((c) => Array.isArray(c) && Number.isFinite(c[0]) && c[0] <
|
|
144
|
-
return { closedCandles, currentBucketStartMs };
|
|
278
|
+
.filter((c) => Array.isArray(c) && Number.isFinite(c[0]) && c[0] < currentBucketStart);
|
|
279
|
+
return { closedCandles, currentBucketStartMs: currentBucketStart };
|
|
145
280
|
}
|
|
146
281
|
buildBotContextSignature(bot) {
|
|
147
282
|
return [
|
|
@@ -627,6 +762,12 @@ class MarketAdapterService {
|
|
|
627
762
|
regimeTable: cfg.regimeTable,
|
|
628
763
|
hurstZoneBand: cfg.hurstZoneBand,
|
|
629
764
|
peNodes: cfg.peNodes,
|
|
765
|
+
// Stable per-market key: lets the gate resume from the previous
|
|
766
|
+
// cycle's analyzer state instead of replaying the whole history.
|
|
767
|
+
// The gate still recomputes from scratch whenever the cached
|
|
768
|
+
// closes are not a prefix of the incoming window (gap repair,
|
|
769
|
+
// rewritten bar, shortened window) or a parameter changed.
|
|
770
|
+
cacheKey: `${bot?.botKey ?? 'bot'}:${cfg.intervalSeconds}`,
|
|
630
771
|
});
|
|
631
772
|
regimeMultiplier = regimeResult.isReady && Math.abs(regimeResult.multiplier - 1.0) >= absoluteThreshold
|
|
632
773
|
? regimeResult.multiplier
|
|
@@ -943,17 +1084,84 @@ class MarketAdapterService {
|
|
|
943
1084
|
});
|
|
944
1085
|
}
|
|
945
1086
|
const botAma = deps.resolveAmaForBot(bot, ctx, cfg);
|
|
1087
|
+
// `botAma.enabled` is a derived invariant (always true): `gridPrice` is
|
|
1088
|
+
// the single switch that makes a bot AMA-driven, and
|
|
1089
|
+
// usesAmaGridPrice(bot) below is what actually gates AMA work for this
|
|
1090
|
+
// bot. This guard stays only so a future resolver that can fail to
|
|
1091
|
+
// resolve an AMA config cannot silently trade on a missing one.
|
|
946
1092
|
if (!botAma.enabled) {
|
|
947
1093
|
return { ok: false, reason: 'ama disabled' };
|
|
948
1094
|
}
|
|
949
1095
|
const lookbackBars = normalizeAmaSlopeLookbackBars(cfg.amaSlope?.lookbackBars);
|
|
950
1096
|
const filePath = deps.candleFileForBot(bot.botKey, cfg.intervalSeconds);
|
|
951
|
-
|
|
1097
|
+
// mtime-validated read cache: the previous cycle serialized this very
|
|
1098
|
+
// file, so re-parsing ~250 KiB of JSON per bot per hour is pure waste.
|
|
1099
|
+
// The cache decides on the file's stat alone — an unstattable file (or a
|
|
1100
|
+
// storage adapter that reports no mtime) is never served from the cache
|
|
1101
|
+
// and always goes through the loader, so it can never serve stale data.
|
|
1102
|
+
// Injecting deps.loadJson does not change that decision; it only changes
|
|
1103
|
+
// what a cache miss parses.
|
|
1104
|
+
const existing = readCachedJson(filePath, () => deps.loadJson(filePath, null));
|
|
952
1105
|
const existingMeta = existing?.meta && typeof existing.meta === 'object' ? existing.meta : {};
|
|
953
1106
|
let existingCandles = Array.isArray(existing?.candles) ? existing.candles : [];
|
|
954
|
-
const existingMarketSource = normalizeMarketSource(existingMeta.marketSource);
|
|
955
1107
|
const marketSource = resolveMarketSourceForBot(bot) || 'pool';
|
|
956
1108
|
const isBookSource = marketSource === 'book';
|
|
1109
|
+
// Retention targets, derived from the bot's own AMA parameters. Pure and
|
|
1110
|
+
// local, so they are computed before the off-hour gate below (which
|
|
1111
|
+
// needs the warmup target) and reused by the full path unchanged.
|
|
1112
|
+
const amaWarmupBars = getAmaWarmupBars(botAma.erPeriod, botAma.slowPeriod, lookbackBars, botAma.fastPeriod, botAma.erSmoothPeriod ?? 0);
|
|
1113
|
+
const analysisKeepCount = amaWarmupBars + 1;
|
|
1114
|
+
// Retain one extra raw candle so the closed-candle analysis window still keeps a
|
|
1115
|
+
// full warmup/history set when the newest bucket is the current in-progress bar.
|
|
1116
|
+
const rawKeepCount = analysisKeepCount + 1;
|
|
1117
|
+
// Off-hour skip: the newest closed 1h bucket is already consumed, so
|
|
1118
|
+
// this bot cannot produce a new AMA, trigger, or snapshot this cycle.
|
|
1119
|
+
// Skip BEFORE the stale-tail Kibana verification and the native
|
|
1120
|
+
// overlap fetch below — on a mid-hour restart those are pure waste
|
|
1121
|
+
// (a per-bot RPC fetch, a Kibana round trip, and a ~250 KiB candle
|
|
1122
|
+
// re-serialize) for zero new information. Every input is already
|
|
1123
|
+
// local: persisted state, the candle file we just read, and the wall
|
|
1124
|
+
// clock. Note the chain connection itself is NOT saved here — the
|
|
1125
|
+
// daemon already opened it before runOnce; that socket is closed right
|
|
1126
|
+
// after the cycle, and the startup sleep is what avoids opening one.
|
|
1127
|
+
//
|
|
1128
|
+
// The local candle file is part of the verdict on purpose. The full
|
|
1129
|
+
// cycle also repairs history (Kibana backfill, gap fill) even when no
|
|
1130
|
+
// new candle closed, so a bot whose cache does not yet COVER the
|
|
1131
|
+
// newest closed bucket — short history, unresolved gaps, a source
|
|
1132
|
+
// change — must still run. Only a bot that has demonstrably consumed
|
|
1133
|
+
// that bucket and holds the data for it is skipped.
|
|
1134
|
+
const persistedBotState = (state?.bots && typeof state.bots === 'object')
|
|
1135
|
+
? state.bots[bot.botKey]
|
|
1136
|
+
: null;
|
|
1137
|
+
const offHourSkip = this.shouldSkipBotForClosedCandle(bot, persistedBotState, cfg.intervalSeconds, undefined, cfg);
|
|
1138
|
+
const offHourSkipCovered = !!offHourSkip && this.candleFileCoversClosedBucket(existingCandles, existingMeta, marketSource, offHourSkip.latestClosedBucketStartMs, rawKeepCount);
|
|
1139
|
+
if (offHourSkip && offHourSkipCovered) {
|
|
1140
|
+
const skipNowIso = new Date().toISOString();
|
|
1141
|
+
const skipThreshold = typeof deps.calculateBotThreshold === 'function'
|
|
1142
|
+
? deps.calculateBotThreshold(cfg)
|
|
1143
|
+
: null;
|
|
1144
|
+
const skippedState = {
|
|
1145
|
+
...(persistedBotState && typeof persistedBotState === 'object' ? persistedBotState : {}),
|
|
1146
|
+
botName: bot.name,
|
|
1147
|
+
botKey: bot.botKey,
|
|
1148
|
+
lastCycleSource: 'off-hour-skip',
|
|
1149
|
+
lastCycleAt: skipNowIso,
|
|
1150
|
+
pendingClosedCandle: true,
|
|
1151
|
+
lastTriggerSuppressedReason: 'waiting_for_new_closed_candle',
|
|
1152
|
+
};
|
|
1153
|
+
state.bots[bot.botKey] = skippedState;
|
|
1154
|
+
return this.buildDefaultResult(bot, {
|
|
1155
|
+
dryRunMessages,
|
|
1156
|
+
source: 'off-hour-skip',
|
|
1157
|
+
thresholdPercent: skipThreshold,
|
|
1158
|
+
triggerSuppressedReason: 'waiting_for_new_closed_candle',
|
|
1159
|
+
lastCandleTs: persistedBotState?.lastCandleTs ?? null,
|
|
1160
|
+
rawLastCandleTs: persistedBotState?.rawLastCandleTs ?? null,
|
|
1161
|
+
lastClosedCandleTs: offHourSkip.previousClosedCandleTs || null,
|
|
1162
|
+
pendingClosedCandle: true,
|
|
1163
|
+
});
|
|
1164
|
+
}
|
|
957
1165
|
const kibanaRequestTimeoutMs = Number.isFinite(cfg.kibanaRequestTimeoutMs) && cfg.kibanaRequestTimeoutMs > 0
|
|
958
1166
|
? cfg.kibanaRequestTimeoutMs
|
|
959
1167
|
: MARKET_ADAPTER.KIBANA_REQUEST_TIMEOUT_MS;
|
|
@@ -1055,10 +1263,7 @@ class MarketAdapterService {
|
|
|
1055
1263
|
existingCandles = verified.candles;
|
|
1056
1264
|
applyStaleTailVerificationMeta(verified);
|
|
1057
1265
|
}
|
|
1058
|
-
const
|
|
1059
|
-
const sourceMismatch = (existingMarketSource && existingMarketSource !== marketSource)
|
|
1060
|
-
|| (marketSource === 'book' && hasStoredPoolContext)
|
|
1061
|
-
|| (marketSource === 'pool' && existingMarketSource === 'book');
|
|
1266
|
+
const sourceMismatch = this.isCandleSourceMismatch(existingMeta, marketSource);
|
|
1062
1267
|
if (sourceMismatch) {
|
|
1063
1268
|
existingCandles = [];
|
|
1064
1269
|
existingMeta.nativeRecentTradeSequences = [];
|
|
@@ -1067,11 +1272,6 @@ class MarketAdapterService {
|
|
|
1067
1272
|
existingMeta.staleTailVerifiedEndTs = null;
|
|
1068
1273
|
}
|
|
1069
1274
|
const needBootstrap = existingCandles.length === 0;
|
|
1070
|
-
const amaWarmupBars = getAmaWarmupBars(botAma.erPeriod, botAma.slowPeriod, lookbackBars, botAma.fastPeriod, botAma.erSmoothPeriod ?? 0);
|
|
1071
|
-
const analysisKeepCount = amaWarmupBars + 1;
|
|
1072
|
-
// Retain one extra raw candle so the closed-candle analysis window still keeps a
|
|
1073
|
-
// full warmup/history set when the newest bucket is the current in-progress bar.
|
|
1074
|
-
const rawKeepCount = analysisKeepCount + 1;
|
|
1075
1275
|
const nowIso = new Date().toISOString();
|
|
1076
1276
|
const botThreshold = deps.calculateBotThreshold(cfg);
|
|
1077
1277
|
if (!Number.isFinite(botThreshold) || botThreshold <= 0) {
|
|
@@ -1722,6 +1922,9 @@ class MarketAdapterService {
|
|
|
1722
1922
|
candles: nextCandles,
|
|
1723
1923
|
};
|
|
1724
1924
|
deps.saveJson(filePath, candlePayload);
|
|
1925
|
+
// The object just serialized IS the file's content now, so the next
|
|
1926
|
+
// cycle's read can skip the parse.
|
|
1927
|
+
noteJsonWritten(filePath, candlePayload);
|
|
1725
1928
|
if (!hasNewClosedCandle) {
|
|
1726
1929
|
const { staleData, staleAgeHours } = deps.computeCandleStaleness(lastClosedCandleTs, cfg.maxStaleHours);
|
|
1727
1930
|
const pendingClosedCandle = !staleData;
|
|
@@ -1868,7 +2071,11 @@ class MarketAdapterService {
|
|
|
1868
2071
|
const amaSlopeThresholdPercent = amaSlopeResetDetails.thresholdPercent;
|
|
1869
2072
|
// 4. Advisory collateral-ratio hint only; execution is owned by the debt runtime.
|
|
1870
2073
|
const collateralRecommendation = canExposeDynamicWeights ? adjustCollateralRatio(slopeResult, 1.5, 2.0) : null;
|
|
1871
|
-
|
|
2074
|
+
// Reports the single AMA this bot trades on (its own resolved
|
|
2075
|
+
// parameters, value already computed above as `amaPrice`). No extra
|
|
2076
|
+
// calculateAMA pass: the AMA1..AMA4 preset sweep this replaced fed
|
|
2077
|
+
// nothing but a log line.
|
|
2078
|
+
const amaComparison = deps.buildAmaRecord(botAma, amaPrice);
|
|
1872
2079
|
const closedCandleTs = lastCandle[0] || null;
|
|
1873
2080
|
const { staleData, staleAgeHours } = deps.computeCandleStaleness(closedCandleTs, cfg.maxStaleHours);
|
|
1874
2081
|
const referencePrice = amaPrice;
|
|
@@ -2321,5 +2528,5 @@ class MarketAdapterService {
|
|
|
2321
2528
|
};
|
|
2322
2529
|
}
|
|
2323
2530
|
}
|
|
2324
|
-
export { MarketAdapterService, AMA_SLOPE_PERCENT_MODE_PER_BAR, normalizeAmaSlopePercentMode, convertSlopePercentToPerBar };
|
|
2531
|
+
export { MarketAdapterService, AMA_SLOPE_PERCENT_MODE_PER_BAR, normalizeAmaSlopePercentMode, convertSlopePercentToPerBar, evaluateStateRepairVeto };
|
|
2325
2532
|
//# sourceMappingURL=market_adapter_service.js.map
|