Principle:Nautechsystems Nautilus trader Strategy Lifecycle
| Field | Value |
|---|---|
| sources | https://github.com/nautechsystems/nautilus_trader , https://nautilustrader.io/docs/ |
| domains | algorithmic trading, strategy lifecycle, event-driven architecture, state management |
| last_updated | 2026-02-10 12:00 GMT |
Overview
Strategy lifecycle management defines the ordered sequence of states a trading strategy transitions through from initialization to disposal, ensuring that resources are acquired, used, and released in a controlled, predictable manner.
Description
A trading strategy is not a simple function that runs once; it is a long-lived component that must be initialized, started, potentially stopped and resumed multiple times, and eventually disposed. Each transition represents a contract between the strategy and its hosting environment:
- Initialization (
__init__): The strategy object is constructed with its configuration. At this stage, no external resources (clock, cache, message bus) are available. Only internal state derived from the configuration should be set up. - Registration: The hosting system (Trader) wires the strategy into the platform by providing references to the clock, cache, portfolio, message bus, and order factory. This is a system-managed step, not a user-facing one.
- Start (
on_start): The strategy is now fully wired and begins active participation. This is where data subscriptions are established, indicators are initialized with historical data, and any initial orders are placed. - Stop (
on_stop): The strategy pauses its active participation. Subscriptions may be removed, timers cancelled, and the strategy enters a quiescent state. Importantly, the strategy is not destroyed -- it can be resumed. - Resume (
on_resume): The strategy restarts from a stopped state. This is distinct from start because the strategy may already have accumulated state (open positions, indicator values) from its previous active period. - Reset (
on_reset): The strategy clears all accumulated state and returns to a post-initialization condition, as if freshly constructed. Indicators are reset, internal counters are zeroed. - Dispose (
on_dispose): The strategy permanently releases all resources. After disposal, the strategy cannot be started again.
This lifecycle model provides several critical guarantees:
- Resource safety: Resources are acquired only when the strategy is in the correct state to use them.
- Graceful degradation: A stop/resume cycle allows strategies to be paused during market closures or system maintenance without losing state.
- Clean teardown: Disposal ensures no dangling subscriptions, timers, or resource leaks.
Usage
Apply lifecycle management when:
- Building a strategy that must subscribe to market data feeds and manage those subscriptions.
- Operating strategies that run across trading sessions (stop at close, resume at open).
- Running strategies in both backtest and live environments where lifecycle transitions are triggered by the hosting system.
- Implementing strategies that hold resources (file handles, network connections, GPU memory) that must be explicitly released.
Theoretical Basis
The strategy lifecycle is modeled as a finite state machine (FSM):
States: {INITIALIZED, STARTING, RUNNING, STOPPING, STOPPED, RESUMING, RESETTING, DISPOSING, DISPOSED, DEGRADED, FAULTED}
Transitions:
INITIALIZED --[register]--> INITIALIZED (wired)
INITIALIZED --[start]----> STARTING ----> RUNNING
RUNNING --[stop]-----> STOPPING ----> STOPPED
STOPPED --[resume]---> RESUMING ----> RUNNING
STOPPED --[reset]----> RESETTING ---> INITIALIZED
RUNNING --[reset]----> RESETTING ---> INITIALIZED
* --[dispose]--> DISPOSING ----> DISPOSED
* --[fault]----> FAULTED
Key invariants:
- No backward transitions except through explicit reset. A running strategy cannot silently revert to an uninitialized state.
- Idempotent disposal: Calling dispose on an already-disposed strategy is a no-op.
- Hook ordering: The system guarantees that
on_startis called before any data handler (on_bar,on_trade_tick) and thaton_stopis called beforeon_dispose.
Pseudocode for lifecycle management:
CLASS Strategy:
state = INITIALIZED
FUNCTION __init__(config):
STORE config as frozen reference
INITIALIZE internal state from config
FUNCTION on_start():
SUBSCRIBE to data feeds
REQUEST historical data for indicator warm-up
SET timers for periodic operations
FUNCTION on_stop():
UNSUBSCRIBE from data feeds
CANCEL active timers
OPTIONALLY cancel open orders
FUNCTION on_resume():
RE-SUBSCRIBE to data feeds
VERIFY indicator state is still valid
RESTART timers
FUNCTION on_dispose():
RELEASE all held resources
CLEAR all references