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

56 lines
3.8 KiB
Markdown

# C01 — Correct native terminal valuation
Parent epic: E1. Priority/severity: P0 / Critical. Relative complexity: S. Recommended specialization: Rust financial correctness.
Scheduling source of truth: [manifest entry C01](../manifest.json). 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
- **FIN-02 — Verified:** Native forced liquidation occurs after the last equity mark; reported final value reads that earlier mark, omitting terminal costs. [apps/stock/engine/src/backtest/engine.rs:135](../../../../apps/stock/engine/src/backtest/engine.rs#L135); [apps/stock/engine/src/backtest/simple_results.rs:308](../../../../apps/stock/engine/src/backtest/simple_results.rs#L308).
## Intended behavior and implementation scope
Correct the current native forced-liquidation path only: mark the final account after all closing fills and update the existing terminal-time equity sample instead of appending a duplicate timestamp. Preserve current matching, sizing, liquidation and fee policies. Do not implement the new engine here.
## Exclusions
No unrelated refactor, new service, broker enablement, or silent reinterpretation of historical results.
## Likely files and ownership
- apps/stock/engine/src/backtest/engine.rs
- apps/stock/engine/src/backtest/simple_results.rs
- test/integration/native-terminal-equity.test.ts (new, only if public API can exercise the same fixture)
Write locks: legacy-native-finance. Review concrete file overlap; first-wave writable paths are further restricted by FIRST-WAVE.md.
## Dependencies and blockers
No code prerequisite. Global execution approval and write-lock acquisition still required.
No additional economic-policy approval is needed for this narrowly scoped task.
Parallel eligibility: only with disjoint actual files and reserved write locks; read FIRST-WAVE.md for the initial exact ownership boundaries.
## Acceptance criteria
- A deterministic test strategy/direct internal fixture buys exactly1 share at4 from cash10, with1% entry/exit fees and zero slippage: final cash/equity/finalValue=9.92 and total fees=0.08.
- Positions eligible for liquidation in these fixtures are flat; every closing fill is included once, including multiple symbols. Preserve the existing micro-position threshold; any retained residual still contributes its marked value to final equity.
- No-position/zero-fee behavior remains correct; there is exactly one final-time equity sample.
## Mandatory tests
- Inline Rust fixtures: terminal_fee_equity, terminal_no_positions, terminal_zero_fee, terminal_multiple_symbols, terminal_timestamp_unique.
- cargo test -p engine --lib --offline; build current addon with bun run --cwd apps/stock/engine build:debug; then bun test test/integration/backtest-result-mapping.test.ts test/integration/backtest-ledger.test.ts.
- If a new native-terminal-equity.test.ts is added, run it too. Do not derive expected quantity from production position sizing.
Apply relevant [quality gates](../../QUALITY-GATES.md). Report exact commands/results and unavailable checks; named new cases are requirements, not claims of existing passing tests.
## 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.