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 Strategy Init

From Leeroopedia


Field Value
sources https://github.com/nautechsystems/nautilus_trader , https://nautilustrader.io/docs/
domains algorithmic trading, strategy lifecycle, NautilusTrader
last_updated 2026-02-10 12:00 GMT

Overview

Concrete tool for initializing a trading strategy with its configuration and lifecycle hooks provided by NautilusTrader.

Description

The Strategy class is the base class for all trading strategies in NautilusTrader. It inherits from Actor and provides the full lifecycle management for a trading component. The __init__ method accepts an optional StrategyConfig (defaulting to a bare StrategyConfig() if None) and initializes the strategy's identity, order management settings, logging preferences, and internal state.

During initialization, the strategy:

  • Derives its StrategyId from the class name and the order_id_tag configuration field.
  • Configures the order management system type (HEDGING, NETTING, or UNSPECIFIED).
  • Sets up flags for contingent order management, GTD expiry management, and automatic market exit on stop.
  • Registers warning events for order denials, rejections, and cancel rejections.
  • Prepares placeholder references for cache, portfolio, and order_factory that are populated later during registration.

Users implement custom strategies by subclassing Strategy and overriding lifecycle hooks: on_start, on_stop, on_resume, on_reset, and on_dispose.

Usage

Import and subclass Strategy when:

  • Building any custom trading strategy for NautilusTrader.
  • You need access to lifecycle hooks, order submission, position management, and data event handling.

Code Reference

Source Location

nautilus_trader/trading/strategy.pyx, lines 109-189.

Signature

cdef class Strategy(Actor):
    def __init__(self, config: StrategyConfig | None = None):
        ...

Key lifecycle hooks (overridable):

cpdef void on_start(self)       # Called when strategy transitions to RUNNING
cpdef void on_stop(self)        # Called when strategy transitions to STOPPED
cpdef void on_resume(self)      # Called when strategy resumes from STOPPED to RUNNING
cpdef void on_reset(self)       # Called when strategy resets to INITIALIZED
cpdef void on_dispose(self)     # Called when strategy is permanently disposed

Import

from nautilus_trader.trading.strategy import Strategy
from nautilus_trader.trading.config import StrategyConfig

I/O Contract

Inputs

Parameter Type Required Description
config StrategyConfig or None No The trading strategy configuration. If None, a default StrategyConfig() is used.

Outputs

Output Type Description
Strategy instance Strategy A fully constructed (but not yet registered) strategy object with identity, config, and lifecycle hooks.

Attributes set during __init__:

Attribute Type Description
id StrategyId Composite ID from class name and order_id_tag, e.g., "MyStrategy-001".
config StrategyConfig Frozen configuration reference.
oms_type OmsType Order management system type (HEDGING, NETTING, UNSPECIFIED).
order_id_tag str The string tag for order ID generation.
manage_contingent_orders bool Whether to auto-manage contingent orders.
manage_gtd_expiry bool Whether to auto-manage GTD expirations.
clock Clock Clock reference (populated during registration).
cache Cache Cache reference (populated during registration).
portfolio PortfolioFacade Portfolio reference (populated during registration).
order_factory OrderFactory Order factory reference (populated during registration).

Usage Examples

Minimal strategy subclass:

from nautilus_trader.trading.strategy import Strategy
from nautilus_trader.trading.config import StrategyConfig


class MyStrategy(Strategy):
    def __init__(self, config: StrategyConfig | None = None):
        super().__init__(config)
        # Custom initialization here (no clock/cache access yet)
        self._indicator_ready = False

    def on_start(self):
        # Called when fully wired into the platform
        self.subscribe_bars(self._bar_type)
        self.log.info("Strategy started")

    def on_stop(self):
        self.unsubscribe_bars(self._bar_type)
        self.log.info("Strategy stopped")

    def on_resume(self):
        self.subscribe_bars(self._bar_type)
        self.log.info("Strategy resumed")

    def on_reset(self):
        self._indicator_ready = False
        self.log.info("Strategy reset")

    def on_dispose(self):
        self.log.info("Strategy disposed")

Strategy with custom config:

from nautilus_trader.trading.strategy import Strategy
from nautilus_trader.trading.config import StrategyConfig


class EMACrossConfig(StrategyConfig, frozen=True):
    instrument_id: str = "AAPL.XNAS"
    fast_ema_period: int = 10
    slow_ema_period: int = 20


class EMACrossStrategy(Strategy):
    def __init__(self, config: EMACrossConfig):
        super().__init__(config)
        self.instrument_id = InstrumentId.from_str(config.instrument_id)
        self.fast_period = config.fast_ema_period
        self.slow_period = config.slow_ema_period

    def on_start(self):
        bar_type = BarType.from_str(f"{self.instrument_id}-1-MINUTE-LAST-EXTERNAL")
        self.subscribe_bars(bar_type)

Related Pages

Page Connections

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