stock-bot/docs/development/backlog/tasks/F08.md

5.2 KiB

F08 — Publish canonical metrics and versioned result projections

Parent epic: E2. Priority/severity: P1 / High. Relative complexity: M. Recommended specialization: Quant analytics/Rust integration.

Scheduling source of truth: manifest entry F08. No task is authorized until the plan is approved. Dependencies and decision references below describe the design; the manifest controls current readiness.

Evidence and root cause

Intended behavior and implementation scope

Use canonical Rust ledger postings, owned fills, occupied intervals and session-ending marks as the sole financial inputs. Reuse reviewed TypeScript statistical functions in libs/calculations/src/analytics/ for return ratios and reports; do not port them to Rust merely for language uniformity. Add a small adapter for the engine-declared session calendar rather than blindly regrouping every venue by UTC date.

Version economics and metrics independently. Define sampling, annualization, benchmark and risk-free conventions in the result policy. Missing benchmark/risk-free inputs must not silently become invented measurements. Undefined or unsupported metrics are null with reasons. Adapt existing result DTOs without rewriting old rows.

Measurable benefit: comparable, independently checked rankings without a second ledger or a needless analytics rewrite.

Exclusions

No accounting/fill recomputation in TypeScript, statistical library rewrite, forced native port, new service, or silent reinterpretation of historical results. Native analytics remain an option only if measured as a bottleneck.

Likely files and ownership

  • apps/stock/engine/src/backtest/simple_results.rs (authoritative projections, not new statistical formulas)
  • apps/stock/orchestrator/src/backtest/native-result.ts
  • apps/stock/orchestrator/src/backtest/BacktestReport.ts
  • libs/calculations/src/analytics/return-statistics.ts
  • libs/calculations/src/analytics/equity.ts (review existing UTC grouping; use explicit session marks)
  • libs/core/types/src/backtest-run.ts

Write locks: result-contract. One agent owns this change. Lock names do not override actual overlapping files.

Dependencies and blockers

Implementation requires F06 integrated.

Architecture decisions: ADR-002, ADR-003. Agents may not independently amend their policies.

Parallel eligibility: only with disjoint actual files and reserved write locks; read FIRST-WAVE.md for the initial exact ownership boundaries.

Acceptance criteria

  • Fees/P&L/equity/trades reconcile to Rust outputs; session returns include the final session and use the declared risk-session/calendar policy.
  • Sharpe uses periodic excess-return mean/std; Sortino uses downside deviation relative to the declared target; zero-variance/undefined cases are null with reasons.
  • Exposure uses the union of occupied intervals; open positions count; attribution distinguishes fills and round trips.
  • Reviewed calculation functions are reused where their conventions match. Any deliberate difference has a versioned policy and independent arithmetic fixture, not an unexplained second formula.

Mandatory tests

  • two_session_marks, constant_negative_returns, zero_variance, overlapping_positions, open_position_exposure.
  • Existing result mapping and frontend metric unit fixtures; documented independent arithmetic cross-check.

Apply the relevant quality gates; submit exact commands, outcomes and skipped checks. Named cases above are required regression scenarios, not claims that those tests already exist.

Regression risks and rollback

Financial or compatibility regression; preserve existing fixtures and version changed semantics. Keep migrations additive and previous results readable. Do not add a switch that restores a correctness defect. Versioned cutover/migration is required only when persisted contracts or active engines change; never silently fall back to old economics.

Handoff

Report scope changes (if any), fixture expectations, API/schema changes, measurements, remaining limitations and commit/PR references once authorized. Request independent review; only the integration/planning owner may mark the manifest done.