backtest: implement event-driven ReplayEngine#11
Merged
marwinsteiner merged 3 commits intomainfrom Feb 22, 2026
Merged
Conversation
Create src/sysls/backtest/ and tests/backtest/ packages with __init__.py files for the Phase 4 backtesting engine.
Implements the event-driven historical replay engine that creates a sandboxed environment (SimulatedClock, EventBus, OMS, PaperVenue) and feeds historical data through the full live stack: - ReplayEngine class: constructor with initial_capital and commission_rate - _ns_to_datetime helper: nanosecond epoch to UTC datetime conversion - _build_event_stream: converts instrument DataFrames to sorted events using bars_to_events/trades_to_events from data.normalize - _compute_equity: mark-to-market portfolio valuation with realized and unrealized PnL tracking - async run(): full lifecycle orchestration - Wires strategy to bus (MarketDataEvent, FillEvent, PositionEvent) - Routes strategy OrderSubmitted events through OMS for execution - Advances SimulatedClock with each data point - Drains event queue between bars for deterministic simulation - Collects equity curve, trade log, final positions - Returns dict with numpy arrays compatible with metrics.py
37 tests covering: - Constructor: default and custom initial_capital, commission_rate - Validation: empty data, invalid data_type - Do-nothing strategy: result dict keys, equity curve shape, unchanged equity, no trades/positions, numpy array types - Buy-once strategy: single trade, trade details, position state, unrealized PnL in equity - Buy-and-sell strategy: round-trip trades, positive/negative realized PnL, flat position after close - Strategy lifecycle: on_start/on_stop, market data count - Strategy params: forwarded correctly, default empty dict - Multi-instrument: interleaved data, multiple positions - Commission: zero and nonzero rates - Equity curve: starts at initial capital, tracks unrealized PnL - Helpers: _ns_to_datetime, _build_event_stream sorting Test strategies: DoNothing, BuyOnce, BuyAndSell, LifecycleTracking, ParamStrategy, MultiInstrumentStrategy.
d9a5fa6 to
86979e3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements the event-driven historical replay engine (
src/sysls/backtest/replay.py) for Phase 4 backtesting:on_market_datalifecycle, identical to live executionmetrics.py(Junior-1's module)Key integrations
sysls.core.bus.EventBus— event dispatchsysls.core.clock.SimulatedClock— time advancementsysls.core.events.BarEvent/TradeEvent— data eventssysls.execution.oms.OrderManagementSystem— order processingsysls.execution.paper.PaperVenue— simulated fillssysls.strategy.base.Strategy,StrategyContext— strategy lifecycleFiles
src/sysls/backtest/__init__.py— package initsrc/sysls/backtest/replay.py— ReplayEngine (340 lines)tests/backtest/__init__.py— test package inittests/backtest/test_replay.py— 37 tests (733 lines)Test coverage
37 tests covering: validation, do-nothing strategy, buy-once, buy-and-sell round-trips, strategy lifecycle, params, multi-instrument, commissions, equity curve accuracy, helper functions. 6 test strategy implementations.
Full suite: 551 passed, 10 skipped (existing Windows arcticdb skips). Ruff clean.
Test plan