BacktestEngine.reset() then run() logs 'Invalid state trigger READY -> INITIALIZE' and skips ts_started
Bug Report
Confirmation
- I've re-read the relevant sections of the documentation.
- I've searched existing issues and discussions to avoid duplicates.
- I've reviewed or skimmed the source code (or examples) to confirm the behavior is not by design.
- I've tested this issue using a recent pre-release or development wheel and can still reproduce it.
Reproduced on
2.0.0rc5(latest pre-release). Not run on a development wheel; by source reading, the code path below is unchanged ondevelopatf73a6acb2d.
Expected behavior
BacktestEngine.reset() followed by BacktestEngine.run() starts the kernel cleanly, as the first run does.
Actual behavior
The second run() logs:
[ERROR] TESTER-001.nautilus_system::kernel: Error initializing trader: Invalid state trigger READY -> INITIALIZEThe replay itself still happens (strategies receive the bars and fills are identical to the first run),
but NautilusKernel::start() returns early after the error, so ts_started is not set and "Started" is
not logged for the second run.
Steps to reproduce the problem
from nautilus_trader.backtest import BacktestEngine
from nautilus_trader.common import LogLevel
from nautilus_trader.config import BacktestEngineConfig, LoggerConfig
from nautilus_trader.model import (
AccountType, Bar, BarType, Currency, Equity, InstrumentId, Money, OmsType, Price, Quantity,
Symbol, TraderId, Venue,
)
from nautilus_trader.trading import Strategy
HOUR = 3_600_000_000_000
USD = Currency.from_str("USD")
class BarCounter(Strategy):
def __new__(cls, *args, **kwargs):
return super().__new__(cls)
def __init__(self, bar_type: BarType) -> None:
super().__init__()
self.bar_type = bar_type
self.count = 0
def on_start(self) -> None:
self.subscribe_bars(self.bar_type)
def on_bar(self, bar: Bar) -> None:
self.count += 1
inst = Equity(
instrument_id=InstrumentId(Symbol("TEST"), Venue("SIM")),
raw_symbol=Symbol("TEST"),
currency=USD,
price_precision=2,
price_increment=Price.from_str("0.01"),
ts_event=0,
ts_init=0,
lot_size=Quantity.from_int(1),
)
bar_type = BarType.from_str(f"{inst.id}-1-HOUR-LAST-EXTERNAL")
bars = [
Bar(bar_type, Price.from_str("100.00"), Price.from_str("101.00"), Price.from_str("99.00"),
Price.from_str("100.50"), Quantity.from_str("1000"), (i + 1) * HOUR, (i + 1) * HOUR)
for i in range(5)
]
engine = BacktestEngine(config=BacktestEngineConfig(
trader_id=TraderId.from_str("TESTER-001"),
logging=LoggerConfig(stdout_level=LogLevel.WARNING),
))
engine.add_venue(venue=Venue("SIM"), oms_type=OmsType.NETTING, account_type=AccountType.CASH,
starting_balances=[Money.from_str("100000 USD")], base_currency=USD)
engine.add_instrument(inst)
engine.add_data(bars)
strategy = BarCounter(bar_type)
engine.add_strategy(strategy)
engine.run()
print("first run bars:", strategy.count) # 5, no error
engine.reset()
engine.run() # ERROR ... Invalid state trigger READY -> INITIALIZE
print("second run bars:", strategy.count) # 10: the replay still ran
engine.dispose()Cause (from source at the v2.0.0rc5 tag)
NautilusKernel::reset() resets the trader, which leaves it READY. NautilusKernel::start()
(crates/system/src/kernel.rs, around line 731 at the tag and 781 on develop) then calls
self.trader.borrow_mut().initialize() unconditionally. The READY -> INITIALIZE transition is invalid,
so start() logs the error and returns before setting ts_started.
A possible fix is to initialise the trader only when it is PRE_INITIALIZED, as NautilusKernel::dispose()
already does on develop (if trader.state() == ComponentState::PreInitialized && ...initialize()).
Specifications
- OS platform: macOS 27 (arm64)
- Python version: 3.13
nautilus_traderversion: 2.0.0rc5 (tag commit1b0a49d2792a9432a3aca3fcb617ce7a630d905e)
Source: nautechsystems/nautilus_trader