Skip to content

fix(trading-strategies): call strategy init in BacktestExecutor to mirror TradingSession#1189

Open
bennycode wants to merge 1 commit into
mainfrom
fix/backtest-executor-strategy-init
Open

fix(trading-strategies): call strategy init in BacktestExecutor to mirror TradingSession#1189
bennycode wants to merge 1 commit into
mainfrom
fix/backtest-executor-strategy-init

Conversation

@bennycode

Copy link
Copy Markdown
Owner

Problem

TradingSession.start() calls strategy.init?.(broker, pair) before the first candle — BacktestExecutor never did. A strategy that warms itself up in init() (seeding indicators from history, loading datasets) runs warm live but cold in backtests, so backtest results silently misrepresent live behavior. Parity between the two executors is the backtester's core promise.

Fix

  • BacktestExecutor.execute() now calls await strategy.init?.(...) before the candle loop, mirroring TradingSession.
  • Lookahead-bias guard: init's market view is fed exclusively from the new optional BacktestConfig.warmupCandles (history from before the backtest window) — never from the exchange. init structurally cannot see the candles it is about to be tested on.

Impact

No existing strategy implements init, so current backtest results are unchanged. Only strategies opting into the hook are affected.

Test plan

  • init fires before the first candle
  • init sees exactly the configured warmup candles (and never the run candles)
  • init sees empty history when no warmup is configured
  • Full suite: 262 tests green, lint clean

…rror TradingSession

TradingSession.start() calls strategy.init() before the first candle, but
BacktestExecutor never did — a strategy that warms up in init() ran warm in
live trading and cold in backtests, silently diverging.

The executor now inits the strategy before the candle loop. Its market view
is fed exclusively from the new optional BacktestConfig.warmupCandles (data
from before the backtest window), never from the exchange, so init cannot
see the candles under test (lookahead-bias guard).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant