Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Nautechsystems Nautilus trader BacktestEngine Init

From Leeroopedia


Field Value
sources https://github.com/nautechsystems/nautilus_trader , https://nautilustrader.io/docs/
domains backtesting, configuration, engine initialization
last_updated 2026-02-10 12:00 GMT

Overview

Concrete tool for initializing and configuring an event-driven backtest engine provided by NautilusTrader.

Description

The BacktestEngine class is a Cython extension type (cdef class) that constructs the full NautilusTrader system kernel in backtest mode. On instantiation it accepts an optional BacktestEngineConfig (defaulting to a fresh instance if None), validates the type, and then builds the NautilusKernel which owns the data engine, execution engine, risk engine, portfolio, cache, message bus, and a TestClock. It also initializes internal bookkeeping structures: a venue registry, a data list, iteration counters, timing fields, a data iterator, and a time-event accumulator backed by a Rust FFI allocation.

Usage

Import and instantiate BacktestEngine whenever you need to run a historical simulation. It is the entry point for the entire backtest workflow -- venues, instruments, data, strategies, and the run loop are all accessed through this object.

Code Reference

  • Source location: nautilus_trader/backtest/engine.pyx, lines 205--267
  • Signature:
class BacktestEngine:
    def __init__(self, config: BacktestEngineConfig | None = None) -> None
  • Import:
from nautilus_trader.backtest.engine import BacktestEngine
from nautilus_trader.config import BacktestEngineConfig

I/O Contract

Inputs:

Parameter Type Required Description
config None No Configuration object controlling trader ID, logging, risk engine, execution engine, and kernel settings. Defaults to BacktestEngineConfig() if None.

Outputs / Side Effects:

Output Type Description
Engine instance BacktestEngine Fully initialized engine with an empty venue map, empty data list, iteration counter at 0, and a wired NautilusKernel.
Kernel NautilusKernel Internal kernel owning DataEngine, ExecEngine, RiskEngine, Portfolio, Cache, MessageBus, and TestClock.
Message handler side effect Registers BacktestEngine.execute endpoint on the kernel message bus for data command routing.

Usage Examples

Minimal initialization with defaults:

from nautilus_trader.backtest.engine import BacktestEngine

engine = BacktestEngine()
print(engine.trader_id)

Initialization with explicit configuration:

from nautilus_trader.backtest.engine import BacktestEngine
from nautilus_trader.config import BacktestEngineConfig

config = BacktestEngineConfig(
    trader_id="TESTER-001",
    logging=LoggingConfig(log_level="INFO"),
)

engine = BacktestEngine(config=config)
print(engine.instance_id)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment