Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Nautechsystems Nautilus trader BacktestEngine Add Strategy

From Leeroopedia
Revision as of 16:00, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Nautechsystems_Nautilus_trader_BacktestEngine_Add_Strategy.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Field Value
sources https://github.com/nautechsystems/nautilus_trader , https://nautilustrader.io/docs/
domains backtesting, strategy management, trading system architecture
last_updated 2026-02-10 12:00 GMT

Overview

Concrete tool for attaching a trading strategy to the backtest engine provided by NautilusTrader.

Description

The add_strategy method on BacktestEngine delegates to the kernel's Trader.add_strategy() method, which wires the strategy into the message bus, cache, portfolio, clock, and execution engine. The strategy will receive lifecycle events (on_start, on_stop) and data/execution callbacks during the backtest run. Type and identity validation is handled internally by the Trader.

Usage

Call engine.add_strategy(strategy) after configuring venues, instruments, and data, but before calling engine.run(). Multiple strategies can be added for portfolio-level backtesting.

Code Reference

  • Source location: nautilus_trader/backtest/engine.pyx, lines 1119--1130
  • Signature:
def add_strategy(self, strategy: Strategy) -> None
  • Import:
from nautilus_trader.backtest.engine import BacktestEngine
from nautilus_trader.trading.strategy import Strategy

I/O Contract

Inputs:

Parameter Type Required Description
strategy Strategy Yes The strategy instance to register. Must have a unique StrategyId not already registered with the trader.

Outputs / Side Effects:

Output Type Description
None (return) None Method mutates the internal trader state.
Trader registration side effect The strategy is added to the kernel's Trader component, which wires it to the message bus, cache, portfolio, clock, and execution engine.
Strategy ID tagging side effect All orders and positions generated by this strategy are tagged with its StrategyId.

Usage Examples

Adding a single strategy:

from nautilus_trader.backtest.engine import BacktestEngine
from nautilus_trader.examples.strategies.ema_cross import EMACross, EMACrossConfig

config = EMACrossConfig(
    instrument_id="AAPL.XNAS",
    bar_type="AAPL.XNAS-1-MINUTE-LAST-EXTERNAL",
    fast_ema_period=10,
    slow_ema_period=20,
    trade_size="100",
)

strategy = EMACross(config=config)
engine.add_strategy(strategy)

Adding multiple strategies for portfolio-level backtesting:

from nautilus_trader.backtest.engine import BacktestEngine
from nautilus_trader.trading.strategy import Strategy

strategy_a = MyMomentumStrategy(config_a)
strategy_b = MyMeanReversionStrategy(config_b)

engine.add_strategy(strategy_a)
engine.add_strategy(strategy_b)

# Or equivalently:
engine.add_strategies([strategy_a, strategy_b])

Related Pages

Page Connections

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