Centralize strategy contracts and share indicator calculations

This commit is contained in:
Boki 2026-10-21 15:50:39 -04:00
parent 366d39001b
commit 4207bf2c1c
25 changed files with 1323 additions and 477 deletions

View file

@ -97,6 +97,33 @@ nothing. History is still retained in memory for the run; this is not a streamin
solution. Run `bun run scripts/benchmark-execution.ts` for a local journal-read microbenchmark,
not an end-to-end backtest performance estimate.
### Strategy parameters and indicator math
`contracts/strategy-names.ts` owns built-in aliases; spaces, underscores and hyphens normalize
to the same identity. `contracts/strategies.ts` owns backend-specific parameter schemas, and
`backtest/strategy-plan.ts` applies shared defaults followed by per-strategy overrides for
both backends. Disabled entries do not run or consume TypeScript allocation. Invalid built-in
parameters fail before historical input loading; unknown/unsupported keys, numeric strings,
fractional periods and nonfinite values are rejected rather than ignored or truncated.
Custom TypeScript factories retain their own parameter contracts and extension fields.
Periods are integer bars bounded by 10000, with `fastPeriod < slowPeriod` for SMA. Both SMA
backends default to 5/15 and accept an explicit `quantity` in shares (1e-8 through 1e9).
Without `quantity`, TypeScript SMA uses `positionFraction` (default 0.1 of account equity),
while native SMA uses 100 shares. Native mean reversion, momentum and pairs retain their
`positionSize` parameter in **shares**, not an equity fraction. TypeScript mean reversion
accepts configurable lookback/trend/RSI windows, entry/exit thresholds and notional-volume
filter; its optional `quantity` is also shares. Do not transfer backend-only parameters blindly.
`indicators/window-statistics.ts` shares slice-free trailing mean, population deviation
and crossover calculations across TypeScript strategies. A crossover requires `slowPeriod + 1`
bars, including a complete previous window; missing warm-up is not a synthetic zero average.
Tests compare the scalar math with real Rust batch/incremental indicators and exercise
configured SMA fills in both backends. This is not full algorithm/result parity: entry/exit
and RSI rules differ, and native backtests force liquidation at the end while TypeScript
backtests retain open positions. Corrected warm-up and previously ignored settings can change
historical results.
### TypeScript backtest composition
`BacktestEngine` coordinates fresh components for each run:
@ -227,6 +254,14 @@ ML paper strategies fail **before** model startup: asynchronous model training a
do not yet have a replay-safe artifact contract. Run-local backtests can still use explicitly
unversioned experimental strategies, without claiming restart recovery for their custom state.
The configurable SMA and mean-reversion implementations now use checkpoint version **v2**.
They do not resume v1 registrations. Before deploying against an existing paper account,
explicitly pause the affected v1 registrations using the compatible runtime, review pending
intents/open orders, and register new strategy IDs with validated v2 parameters and fresh
indicator warm-up. Enabled v1 registrations block recovery even if a new ID is also supplied.
Pausing does not cancel committed work. No upgrade step rewrites checkpoints, resets account
balances or deletes historical registrations; tests do not modify running development accounts.
Strategy evaluation must be bounded and deterministic: no fire-and-forget state mutation,
wall-clock/random decisions, broker calls, model training or external I/O in callbacks.
Repository I/O/direct submissions from a callback fail closed; use the as-of account view