Backtest
The backtest subpackage groups components relating to backtesting.
This module provides a data client for backtesting.
class BacktestDataClient
Bases: DataClient
BacktestDataClient(ClientId client_id, MessageBus msgbus, Cache cache, Clock clock, config: NautilusConfig | None = None) -> None
Provides an implementation of DataClient for backtesting.
- Parameters:
- client_id (ClientId) – The data client ID.
 - msgbus (MessageBus) – The message bus for the client.
 - cache (Cache) – The cache for the client.
 - clock (Clock) – The clock for the client.
 - config (NautilusConfig , optional) – The configuration for the instance.
 
 
degrade(self) → void
Degrade the component.
While executing on_degrade() any exception will be logged and reraised, then the component
will remain in a DEGRADING state.
WARNING
Do not override.
If the component is not in a valid state from which to execute this method, then the component state will not change, and an error will be logged.
dispose(self) → void
Dispose of the component.
While executing on_dispose() any exception will be logged and reraised, then the component
will remain in a DISPOSING state.
WARNING
Do not override.
If the component is not in a valid state from which to execute this method, then the component state will not change, and an error will be logged.
fault(self) → void
Fault the component.
Calling this method multiple times has the same effect as calling it once (it is idempotent). Once called, it cannot be reversed, and no other methods should be called on this instance.
While executing on_fault() any exception will be logged and reraised, then the component
will remain in a FAULTING state.
WARNING
Do not override.
If the component is not in a valid state from which to execute this method, then the component state will not change, and an error will be logged.
classmethod fully_qualified_name(cls) → str
Return the fully qualified name for the components class.
- Return type: str
 
id
The components ID.
- Returns: ComponentId
 
is_connected
If the client is connected.
- Returns: bool
 
is_degraded
bool
Return whether the current component state is DEGRADED.
- Return type: bool
 - Type: Component.is_degraded
 
is_disposed
bool
Return whether the current component state is DISPOSED.
- Return type: bool
 - Type: Component.is_disposed
 
is_faulted
bool
Return whether the current component state is FAULTED.
- Return type: bool
 - Type: Component.is_faulted
 
is_initialized
bool
Return whether the component has been initialized (component.state >= INITIALIZED).
- Return type: bool
 - Type: Component.is_initialized
 
is_running
bool
Return whether the current component state is RUNNING.
- Return type: bool
 - Type: Component.is_running
 
is_stopped
bool
Return whether the current component state is STOPPED.
- Return type: bool
 - Type: Component.is_stopped
 
request(self, RequestData request) → void
reset(self) → void
Reset the component.
All stateful fields are reset to their initial value.
While executing on_reset() any exception will be logged and reraised, then the component
will remain in a RESETTING state.
WARNING
Do not override.
If the component is not in a valid state from which to execute this method, then the component state will not change, and an error will be logged.
resume(self) → void
Resume the component.
While executing on_resume() any exception will be logged and reraised, then the component
will remain in a RESUMING state.
WARNING
Do not override.
If the component is not in a valid state from which to execute this method, then the component state will not change, and an error will be logged.
shutdown_system(self, str reason=None) → void
Initiate a system-wide shutdown by generating and publishing a ShutdownSystem command.
The command is handled by the system’s NautilusKernel, which will invoke either stop (synchronously) or stop_async (asynchronously) depending on the execution context and the presence of an active event loop.
- Parameters: reason (str , optional) – The reason for issuing the shutdown command.
 
start(self) → void
Start the component.
While executing on_start() any exception will be logged and reraised, then the component
will remain in a STARTING state.
WARNING
Do not override.
If the component is not in a valid state from which to execute this method, then the component state will not change, and an error will be logged.
state
ComponentState
Return the components current state.
- Return type: ComponentState
 - Type: Component.state
 
stop(self) → void
Stop the component.
While executing on_stop() any exception will be logged and reraised, then the component
will remain in a STOPPING state.
WARNING
Do not override.
If the component is not in a valid state from which to execute this method, then the component state will not change, and an error will be logged.
subscribe(self, SubscribeData command) → void
subscribed_custom_data(self) → list
Return the custom data types subscribed to.
- Return type: list[DataType]
 
trader_id
The trader ID associated with the component.
- Returns: TraderId
 
type
The components type.
- Returns: type
 
unsubscribe(self, UnsubscribeData command) → void
venue
The clients venue ID (if applicable).
- Returns:
Venue or 
None 
class BacktestMarketDataClient
Bases: MarketDataClient
BacktestMarketDataClient(ClientId client_id, MessageBus msgbus, Cache cache, Clock clock)
Provides an implementation of MarketDataClient for backtesting.
- Parameters:
- client_id (ClientId) – The data client ID.
 - msgbus (MessageBus) – The message bus for the client.
 - cache (Cache) – The cache for the client.
 - clock (Clock) – The clock for the client.
 
 
degrade(self) → void
Degrade the component.
While executing on_degrade() any exception will be logged and reraised, then the component
will remain in a DEGRADING state.
WARNING
Do not override.
If the component is not in a valid state from which to execute this method, then the component state will not change, and an error will be logged.
dispose(self) → void
Dispose of the component.
While executing on_dispose() any exception will be logged and reraised, then the component
will remain in a DISPOSING state.
WARNING
Do not override.
If the component is not in a valid state from which to execute this method, then the component state will not change, and an error will be logged.
fault(self) → void
Fault the component.
Calling this method multiple times has the same effect as calling it once (it is idempotent). Once called, it cannot be reversed, and no other methods should be called on this instance.
While executing on_fault() any exception will be logged and reraised, then the component
will remain in a FAULTING state.
WARNING
Do not override.
If the component is not in a valid state from which to execute this method, then the component state will not change, and an error will be logged.
classmethod fully_qualified_name(cls) → str
Return the fully qualified name for the components class.
- Return type: str
 
id
The components ID.
- Returns: ComponentId
 
is_connected
If the client is connected.
- Returns: bool
 
is_degraded
bool
Return whether the current component state is DEGRADED.
- Return type: bool
 - Type: Component.is_degraded
 
is_disposed
bool
Return whether the current component state is DISPOSED.
- Return type: bool
 - Type: Component.is_disposed
 
is_faulted
bool
Return whether the current component state is FAULTED.
- Return type: bool
 - Type: Component.is_faulted
 
is_initialized
bool
Return whether the component has been initialized (component.state >= INITIALIZED).
- Return type: bool
 - Type: Component.is_initialized
 
is_running
bool
Return whether the current component state is RUNNING.
- Return type: bool
 - Type: Component.is_running
 
is_stopped
bool
Return whether the current component state is STOPPED.
- Return type: bool
 - Type: Component.is_stopped
 
request(self, RequestData request) → void
Request data for the given data type.
- Parameters: request (RequestData) – The message for the data request.
 
request_bars(self, RequestBars request) → void
request_instrument(self, RequestInstrument request) → void
request_instruments(self, RequestInstruments request) → void
request_order_book_snapshot(self, RequestOrderBookSnapshot request) → void
request_quote_ticks(self, RequestQuoteTicks request) → void
request_trade_ticks(self, RequestTradeTicks request) → void
reset(self) → void
Reset the component.
All stateful fields are reset to their initial value.
While executing on_reset() any exception will be logged and reraised, then the component
will remain in a RESETTING state.
WARNING
Do not override.
If the component is not in a valid state from which to execute this method, then the component state will not change, and an error will be logged.
resume(self) → void
Resume the component.
While executing on_resume() any exception will be logged and reraised, then the component
will remain in a RESUMING state.
WARNING
Do not override.
If the component is not in a valid state from which to execute this method, then the component state will not change, and an error will be logged.
shutdown_system(self, str reason=None) → void
Initiate a system-wide shutdown by generating and publishing a ShutdownSystem command.
The command is handled by the system’s NautilusKernel, which will invoke either stop (synchronously) or stop_async (asynchronously) depending on the execution context and the presence of an active event loop.
- Parameters: reason (str , optional) – The reason for issuing the shutdown command.
 
start(self) → void
Start the component.
While executing on_start() any exception will be logged and reraised, then the component
will remain in a STARTING state.
WARNING
Do not override.
If the component is not in a valid state from which to execute this method, then the component state will not change, and an error will be logged.
state
ComponentState
Return the components current state.
- Return type: ComponentState
 - Type: Component.state
 
stop(self) → void
Stop the component.
While executing on_stop() any exception will be logged and reraised, then the component
will remain in a STOPPING state.
WARNING
Do not override.
If the component is not in a valid state from which to execute this method, then the component state will not change, and an error will be logged.
subscribe(self, SubscribeData command) → void
subscribe_bars(self, SubscribeBars command) → void
subscribe_funding_rates(self, SubscribeFundingRates command) → void
subscribe_index_prices(self, SubscribeIndexPrices command) → void
subscribe_instrument(self, SubscribeInstrument command) → void
subscribe_instrument_close(self, SubscribeInstrumentClose command) → void
subscribe_instrument_status(self, SubscribeInstrumentStatus command) → void
subscribe_instruments(self, SubscribeInstruments command) → void
subscribe_mark_prices(self, SubscribeMarkPrices command) → void
subscribe_order_book_deltas(self, SubscribeOrderBook command) → void
subscribe_order_book_depth(self, SubscribeOrderBook command) → void
subscribe_order_book_snapshots(self, SubscribeOrderBook command) → void
subscribe_quote_ticks(self, SubscribeQuoteTicks command) → void
subscribe_trade_ticks(self, SubscribeTradeTicks command) → void
subscribed_bars(self) → list
Return the bar types subscribed to.
- Return type: list[BarType]
 
subscribed_custom_data(self) → list
Return the custom data types subscribed to.
- Return type: list[DataType]
 
subscribed_funding_rates(self) → list
Return the funding rate update instruments subscribed to.
- Return type: list[InstrumentId]
 
subscribed_index_prices(self) → list
Return the index price update instruments subscribed to.
- Return type: list[InstrumentId]
 
subscribed_instrument_close(self) → list
Return the instrument closes subscribed to.
- Return type: list[InstrumentId]
 
subscribed_instrument_status(self) → list
Return the status update instruments subscribed to.
- Return type: list[InstrumentId]
 
subscribed_instruments(self) → list
Return the instruments subscribed to.
- Return type: list[InstrumentId]
 
subscribed_mark_prices(self) → list
Return the mark price update instruments subscribed to.
- Return type: list[InstrumentId]
 
subscribed_order_book_deltas(self) → list
Return the order book delta instruments subscribed to.
- Return type: list[InstrumentId]
 
subscribed_order_book_snapshots(self) → list
Return the order book snapshot instruments subscribed to.
- Return type: list[InstrumentId]
 
subscribed_quote_ticks(self) → list
Return the quote tick instruments subscribed to.
- Return type: list[InstrumentId]
 
subscribed_trade_ticks(self) → list
Return the trade tick instruments subscribed to.
- Return type: list[InstrumentId]
 
trader_id
The trader ID associated with the component.
- Returns: TraderId
 
type
The components type.
- Returns: type