Common
The common subpackage provides generic/common parts for assembling the frameworks various components.
More domain specific concepts are introduced above the core base layer. The ID cache is implemented, a base Clock with Test and Live implementations which can control many Timer instances.
Trading domain specific components for generating Order and Identifier objects, common logging components, a high performance Queue and UUID4 factory.
class Environment
Bases: Enum
Represents the environment context for a Nautilus system.
BACKTEST = 'backtest'
SANDBOX = 'sandbox'
LIVE = 'live'
The Actor class allows traders to implement their own customized components.
A user can inherit from Actor and optionally override any of the “on” named event handler methods. The class is not entirely initialized in a stand-alone way, the intended usage is to pass actors to a Trader so that they can be fully “wired” into the platform. Exceptions will be raised if an Actor attempts to operate without a managing Trader instance.
class Actor
Bases: Component
Actor(config: ActorConfig | None = None) -> None
The base class for all actor components.
- Parameters: config (ActorConfig , optional) – The actor configuration.
- Raises: TypeError – If config is not of type ActorConfig.
WARNING
- This class should not be used directly, but through a concrete subclass.
- Do not call components such as clock and logger in the __init__ prior to registration.
active_task_ids(self) → list
Return the active task identifiers.
- Return type: list[TaskId]
add_synthetic(self, SyntheticInstrument synthetic) → void
Add the created synthetic instrument to the cache.
- Parameters: synthetic (SyntheticInstrument) – The synthetic instrument to add to the cache.
- Raises: KeyError – If synthetic is already in the cache.
cache
The read-only cache for the actor.
- Returns: CacheFacade
cancel_all_tasks(self) → void
Cancel all queued and active tasks.
cancel_task(self, task_id: TaskId) → void
Cancel the task with the given task_id (if queued or active).
If the task is not found then a warning is logged.
- Parameters: task_id (TaskId) – The task identifier.
clock
The actors clock.
- Returns: Clock
config
The actors configuration.
- Returns: NautilusConfig
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.
deregister_warning_event(self, type event) → void
Deregister the given event type from warning log levels.
- Parameters: event (type) – The event class to deregister.
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
greeks
The read-only greeks calculator for the actor.
- Returns: GreeksCalculator
handle_bar(self, Bar bar) → void
Handle the given bar data.
If state is RUNNING
then passes to on_bar.
- Parameters: bar (Bar) – The bar received.
WARNING
System method (not intended to be called by user code).
handle_bars(self, list bars) → void
Handle the given historical bar data by handling each bar individually.
- Parameters: bars (list [Bar ]) – The bars to handle.
WARNING
System method (not intended to be called by user code).
- Raises: RuntimeError – If bar data has incorrectly sorted timestamps (not monotonically increasing).
handle_data(self, Data data) → void
Handle the given data.
If state is RUNNING
then passes to on_data.
- Parameters: data (Data) – The data received.
WARNING
System method (not intended to be called by user code).
handle_event(self, Event event) → void
Handle the given event.
If state is RUNNING
then passes to on_event.
- Parameters: event (Event) – The event received.
WARNING
System method (not intended to be called by user code).
handle_funding_rate(self, FundingRateUpdate funding_rate) → void
Handle the given funding rate update.
If state is RUNNING
then passes to on_funding_rate.
- Parameters: funding_rate (FundingRateUpdate) – The funding rate update received.
WARNING
System method (not intended to be called by user code).
handle_historical_data(self, data) → void
Handle the given historical data.
- Parameters: data (Data) – The historical data received.
WARNING
System method (not intended to be called by user code).
handle_index_price(self, IndexPriceUpdate index_price) → void
Handle the given index price update.
If state is RUNNING
then passes to on_index_price.
- Parameters: index_price (IndexPriceUpdate) – The index price update received.
WARNING
System method (not intended to be called by user code).
handle_instrument(self, Instrument instrument) → void
Handle the given instrument.
Passes to on_instrument if state is RUNNING
.
- Parameters: instrument (Instrument) – The instrument received.
WARNING
System method (not intended to be called by user code).
handle_instrument_close(self, InstrumentClose update) → void
Handle the given instrument close update.
If state is RUNNING
then passes to on_instrument_close.
- Parameters: update (InstrumentClose) – The update received.
WARNING
System method (not intended to be called by user code).
handle_instrument_status(self, InstrumentStatus data) → void
Handle the given instrument status update.
If state is RUNNING
then passes to on_instrument_status.
- Parameters: data (InstrumentStatus) – The status update received.
WARNING
System method (not intended to be called by user code).
handle_instruments(self, list instruments) → void
Handle the given instruments data by handling each instrument individually.
- Parameters: instruments (list [Instrument ]) – The instruments received.
WARNING
System method (not intended to be called by user code).
handle_mark_price(self, MarkPriceUpdate mark_price) → void
Handle the given mark price update.
If state is RUNNING
then passes to on_mark_price.
- Parameters: mark_price (MarkPriceUpdate) – The mark price update received.
WARNING
System method (not intended to be called by user code).
handle_order_book(self, OrderBook order_book) → void
Handle the given order book.
Passes to on_order_book if state is RUNNING
.
- Parameters: order_book (OrderBook) – The order book received.
WARNING
System method (not intended to be called by user code).
handle_order_book_deltas(self, deltas) → void
Handle the given order book deltas.
Passes to on_order_book_deltas if state is RUNNING
.
The deltas will be nautilus_pyo3.OrderBookDeltas if the
pyo3_conversion flag was set for the subscription.
- Parameters: deltas (OrderBookDeltas or nautilus_pyo3.OrderBookDeltas) – The order book deltas received.
WARNING
System method (not intended to be called by user code).
handle_order_book_depth(self, OrderBookDepth10 depth) → void
Handle the given order book depth
Passes to on_order_book_depth if state is RUNNING
.
- Parameters: depth (OrderBookDepth10) – The order book depth received.
WARNING
System method (not intended to be called by user code).
handle_quote_tick(self, QuoteTick tick) → void
Handle the given quote tick.
If state is RUNNING
then passes to on_quote_tick.
- Parameters: tick (QuoteTick) – The tick received.
WARNING
System method (not intended to be called by user code).
handle_quote_ticks(self, list ticks) → void
Handle the given historical quote tick data by handling each tick individually.
- Parameters: ticks (list [QuoteTick ]) – The ticks received.
WARNING
System method (not intended to be called by user code).
handle_signal(self, Data signal) → void
Handle the given signal.
If state is RUNNING
then passes to on_signal.
- Parameters: signal (Data) – The signal received.
WARNING
System method (not intended to be called by user code).
handle_trade_tick(self, TradeTick tick) → void
Handle the given trade tick.
If state is RUNNING
then passes to on_trade_tick.
- Parameters: tick (TradeTick) – The tick received.
WARNING
System method (not intended to be called by user code).
handle_trade_ticks(self, list ticks) → void
Handle the given historical trade tick data by handling each tick individually.
- Parameters: ticks (list [TradeTick ]) – The ticks received.
WARNING
System method (not intended to be called by user code).
has_active_tasks(self) → bool
Return a value indicating whether there are any active tasks.
- Return type: bool
has_any_tasks(self) → bool
Return a value indicating whether there are any queued OR active tasks.
- Return type: bool
has_pending_requests(self) → bool
Return whether the actor is pending processing for any requests.
- Returns: True if any requests are pending, else False.
- Return type: bool
has_queued_tasks(self) → bool
Return a value indicating whether there are any queued tasks.
- Return type: bool
id
The components ID.
- Returns: ComponentId
indicators_initialized(self) → bool
Return a value indicating whether all indicators are initialized.
- Returns: True if all initialized, else False
- Return type: 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_pending_request(self, UUID4 request_id) → bool
Return whether the request for the given identifier is pending processing.
- Parameters: request_id (UUID4) – The request ID to check.
- Returns: True if request is pending, else False.
- Return type: bool
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
load(self, dict state) → void
Load the actor/strategy state from the give state dictionary.
Calls on_load and passes the state.
- Parameters: state (dict *[*str , bytes ]) – The strategy state to load.
WARNING
Exceptions raised will be caught, logged, and reraised.
log
The actors logger.
- Returns: Logger
msgbus
The message bus for the actor (if registered).
- Returns:
MessageBus or
None
on_bar(self, Bar bar) → void
Actions to be performed when running and receives a bar.
- Parameters: bar (Bar) – The bar received.
WARNING
System method (not intended to be called by user code).
on_data(self, data) → void
Actions to be performed when running and receives data.
- Parameters: data (Data) – The data received.
WARNING
System method (not intended to be called by user code).
on_degrade(self) → void
Actions to be performed on degrade.
WARNING
System method (not intended to be called by user code).
Should be overridden in the actor implementation.
on_dispose(self) → void
Actions to be performed on dispose.
Cleanup/release any resources used here.
WARNING
System method (not intended to be called by user code).
on_event(self, Event event) → void
Actions to be performed running and receives an event.
- Parameters: event (Event) – The event received.
WARNING
System method (not intended to be called by user code).
on_fault(self) → void
Actions to be performed on fault.
Cleanup any resources used by the actor here.
WARNING
System method (not intended to be called by user code).
Should be overridden in the actor implementation.
on_funding_rate(self, FundingRateUpdate funding_rate) → void
Actions to be performed when running and receives a funding rate update.
- Parameters: funding_rate (FundingRateUpdate) – The funding rate update received.
WARNING
System method (not intended to be called by user code).
on_historical_data(self, data) → void
Actions to be performed when running and receives historical data.
- Parameters: data (Data) – The historical data received.
WARNING
System method (not intended to be called by user code).
on_index_price(self, IndexPriceUpdate index_price) → void
Actions to be performed when running and receives an index price update.
- Parameters: index_price (IndexPriceUpdate) – The index price update received.
WARNING
System method (not intended to be called by user code).
on_instrument(self, Instrument instrument) → void
Actions to be performed when running and receives an instrument.
- Parameters: instrument (Instrument) – The instrument received.
WARNING
System method (not intended to be called by user code).
on_instrument_close(self, InstrumentClose update) → void
Actions to be performed when running and receives an instrument close update.
- Parameters: update (InstrumentClose) – The instrument close received.
WARNING
System method (not intended to be called by user code).
on_instrument_status(self, InstrumentStatus data) → void
Actions to be performed when running and receives an instrument status update.
- Parameters: data (InstrumentStatus) – The instrument status update received.
WARNING
System method (not intended to be called by user code).
on_load(self, dict state) → void
Actions to be performed when the actor state is loaded.
Saved state values will be contained in the give state dictionary.
- Parameters: state (dict *[*str , bytes ]) – The strategy state to load.
WARNING
System method (not intended to be called by user code).
on_mark_price(self, MarkPriceUpdate mark_price) → void
Actions to be performed when running and receives a mark price update.
- Parameters: mark_price (MarkPriceUpdate) – The mark price update received.
WARNING
System method (not intended to be called by user code).
on_order_book(self, OrderBook order_book) → void
Actions to be performed when running and receives an order book.
- Parameters: order_book (OrderBook) – The order book received.
WARNING
System method (not intended to be called by user code).
on_order_book_deltas(self, deltas) → void
Actions to be performed when running and receives order book deltas.
- Parameters: deltas (OrderBookDeltas or nautilus_pyo3.OrderBookDeltas) – The order book deltas received.
WARNING
System method (not intended to be called by user code).
on_order_book_depth(self, depth) → void
Actions to be performed when running and receives an order book depth.
- Parameters: depth (OrderBookDepth10) – The order book depth received.
WARNING
System method (not intended to be called by user code).
on_order_filled(self, OrderFilled event) → void
Actions to be performed when running and receives an order filled event.
- Parameters: event (OrderFilled) – The event received.
WARNING
System method (not intended to be called by user code).
on_quote_tick(self, QuoteTick tick) → void
Actions to be performed when running and receives a quote tick.
- Parameters: tick (QuoteTick) – The tick received.
WARNING
System method (not intended to be called by user code).
on_reset(self) → void
Actions to be performed on reset.
WARNING
System method (not intended to be called by user code).
Should be overridden in a user implementation.
on_resume(self) → void
Actions to be performed on resume.
WARNING
System method (not intended to be called by user code).
on_save(self) → dict
Actions to be performed when the actor state is saved.
Create and return a state dictionary of values to be saved.
- Returns: The strategy state to save.
- Return type: dict[str, bytes]
WARNING
System method (not intended to be called by user code).
on_signal(self, signal) → void
Actions to be performed when running and receives signal data.
- Parameters: signal (Data) – The signal received.
WARNING
System method (not intended to be called by user code).
on_start(self) → void
Actions to be performed on start.
The intent is that this method is called once per trading ‘run’, when initially starting.
It is recommended to subscribe/request for data here.
WARNING
System method (not intended to be called by user code).
Should be overridden in a user implementation.
on_stop(self) → void
Actions to be performed on stop.
The intent is that this method is called to pause, or when done for day.
WARNING
System method (not intended to be called by user code).
Should be overridden in a user implementation.
on_trade_tick(self, TradeTick tick) → void
Actions to be performed when running and receives a trade tick.
- Parameters: tick (TradeTick) – The tick received.
WARNING
System method (not intended to be called by user code).
pending_requests(self) → set
Return the request IDs which are currently pending processing.
- Return type: set[UUID4]
portfolio
The read-only portfolio for the actor.
- Returns: PortfolioFacade
publish_data(self, DataType data_type, Data data) → void
Publish the given data to the message bus.
- Parameters:
publish_signal(self, str name, value, uint64_t ts_event=0) → void
Publish the given value as a signal to the message bus.
- Parameters:
- name (str) – The name of the signal being published. The signal name will be converted to title case, with each word capitalized (e.g., ‘example’ becomes ‘SignalExample’).
- value (object) – The signal data to publish.
- ts_event (uint64_t , optional) – UNIX timestamp (nanoseconds) when the signal event occurred.
If
None
then will timestamp current time.
queue_for_executor(self, func: Callable[..., Any], tuple args=None, dict kwargs=None)
Queues the callable func to be executed as fn(*args, **kwargs) sequentially.
- Parameters:
- func (Callable) – The function to be executed.
- args (positional arguments) – The positional arguments for the call to func.
- kwargs (arbitrary keyword arguments) – The keyword arguments for the call to func.
- Raises: TypeError – If func is not of type Callable.
queued_task_ids(self) → list
Return the queued task identifiers.
- Return type: list[TaskId]
register_base(self, PortfolioFacade portfolio, MessageBus msgbus, CacheFacade cache, Clock clock) → void
Register with a trader.
- Parameters:
- portfolio (PortfolioFacade) – The read-only portfolio for the actor.
- msgbus (MessageBus) – The message bus for the actor.
- cache (CacheFacade) – The read-only cache for the actor.
- clock (Clock) – The clock for the actor.
WARNING
System method (not intended to be called by user code).
register_executor(self, loop: asyncio.AbstractEventLoop, executor: Executor) → void
Register the given Executor for the actor.
- Parameters:
- loop (asyncio.AbstractEventLoop) – The event loop of the application.
- executor (concurrent.futures.Executor) – The executor to register.
- Raises: TypeError – If executor is not of type concurrent.futures.Executor
register_indicator_for_bars(self, BarType bar_type, Indicator indicator) → void
Register the given indicator with the actor/strategy to receive bar data for the given bar type.
- Parameters:
register_indicator_for_quote_ticks(self, InstrumentId instrument_id, Indicator indicator) → void
Register the given indicator with the actor/strategy to receive quote tick data for the given instrument ID.
- Parameters:
- instrument_id (InstrumentId) – The instrument ID for tick updates.
- indicator (Indicator) – The indicator to register.
register_indicator_for_trade_ticks(self, InstrumentId instrument_id, Indicator indicator) → void
Register the given indicator with the actor/strategy to receive trade tick data for the given instrument ID.
- Parameters:
- instrument_id (InstrumentId) – The instrument ID for tick updates.
- indicator (indicator) – The indicator to register.
register_warning_event(self, type event) → void
Register the given event type for warning log levels.
- Parameters: event (type) – The event class to register.
registered_indicators
Return the registered indicators for the strategy.
- Return type: list[Indicator]
request_aggregated_bars(self, list bar_types, datetime start, datetime end=None, int limit=0, ClientId client_id=None, callback: Callable[[UUID4], None] | None = None, bool include_external_data=False, bool update_subscriptions=False, update_catalog: bool = False, dict params=None) → UUID4
Request historical aggregated Bar data for multiple bar types. The first bar is used to determine which market data type will be queried. This can either be quotes, trades or bars. If bars are queried, the first bar type needs to have a composite bar that is external (i.e. not internal/aggregated). This external bar type will be queried.
If end is None
then will request up to the most recent data.
Once the response is received, the bar data is forwarded from the message bus to the on_historical_data handler. Any tick data used for aggregation is also forwarded to the on_historical_data handler.
If the request fails, then an error is logged.
- Parameters:
- bar_types (list [BarType ]) – The list of bar types for the request. Composite bars can also be used and need to figure in the list after a BarType on which it depends.
- start (datetime) – The start datetime (UTC) of request time range. Should be left-inclusive (start <= value), but inclusiveness is not currently guaranteed.
- end (datetime , optional) – The end datetime (UTC) of request time range. If None then will be replaced with the current UTC time. Should be right-inclusive (value <= end), but inclusiveness is not currently guaranteed.
- limit (int , optional) – The limit on the amount of data received (quote ticks, trade ticks or bars).
- client_id (ClientId , optional) – The specific client ID for the command.
If
None
then will be inferred from the venue in the instrument ID. - callback (Callable [ [UUID4 ] , None ] , optional) – The registered callback, to be called with the request ID when the response has completed processing.
- include_external_data (bool , default False) – If True, includes the queried external data in the response.
- update_subscriptions (bool , default False) – If True, updates the aggregators of any existing or future subscription with the queried external data.
- update_catalog (bool , optional) – Whether to update a catalog with the received data.
- params (dict *[*str , Any ] , optional) – Additional parameters potentially used by a specific client.
- Returns: The request_id for the request.
- Return type: UUID4
- Raises:
- TypeError – If start is None.
- ValueError – If start is > current timestamp (now).
- ValueError – If end is > current timestamp (now).
- ValueError – If start is > end.
- ValueError – If bar_types is empty.
- TypeError – If callback is not None and not of type Callable.
- TypeError – If bar_types is empty or contains elements not of type BarType.
request_bars(self, BarType bar_type, datetime start, datetime end=None, int limit=0, ClientId client_id=None, callback: Callable[[UUID4], None] | None = None, update_catalog: bool = False, dict params=None) → UUID4
Request historical Bar data.
If end is None
then will request up to the most recent data.
Once the response is received, the bar data is forwarded from the message bus to the on_historical_data handler.
If the request fails, then an error is logged.
- Parameters:
- bar_type (BarType) – The bar type for the request.
- start (datetime) – The start datetime (UTC) of request time range. Should be left-inclusive (start <= value), but inclusiveness is not currently guaranteed.
- end (datetime , optional) – The end datetime (UTC) of request time range. If None then will be replaced with the current UTC time. Should be right-inclusive (value <= end), but inclusiveness is not currently guaranteed.
- limit (int , optional) – The limit on the amount of bars received.
- client_id (ClientId , optional) – The specific client ID for the command.
If
None
then will be inferred from the venue in the instrument ID. - callback (Callable [ [UUID4 ] , None ] , optional) – The registered callback, to be called with the request ID when the response has completed processing.
- update_catalog (bool , optional) – Whether to update a catalog with the received data.
- params (dict *[*str , Any ] , optional) – Additional parameters potentially used by a specific client.
- Returns: The request_id for the request.
- Return type: UUID4
- Raises:
- TypeError – If start is None.
- ValueError – If start is > current timestamp (now).
- ValueError – If end is > current timestamp (now).
- ValueError – If start is > end.
- TypeError – If callback is not None and not of type Callable.
request_data(self, DataType data_type, ClientId client_id, InstrumentId instrument_id=None, datetime start=None, datetime end=None, int limit=0, callback: Callable[[UUID4], None] | None = None, update_catalog: bool = False, dict params=None) → UUID4
Request custom data for the given data type from the given data client.
Once the response is received, the data is forwarded from the message bus to the on_historical_data handler.
If the request fails, then an error is logged.
- Parameters:
- data_type (DataType) – The data type for the request.
- client_id (ClientId) – The data client ID.
- start (datetime) – The start datetime (UTC) of request time range. Cannot be None. Should be left-inclusive (start <= value), but inclusiveness is not currently guaranteed.
- end (datetime , optional) – The end datetime (UTC) of request time range. If None then will be replaced with the current UTC time. Should be right-inclusive (value <= end), but inclusiveness is not currently guaranteed.
- limit (int , optional) – The limit on the amount of data points received.
- callback (Callable [ [UUID4 ] , None ] , optional) – The registered callback, to be called with the request ID when the response has completed processing.
- update_catalog (bool , optional) – Whether to update a catalog with the received data.
- params (dict *[*str , Any ] , optional) – Additional parameters potentially used by a specific client.
- Returns: The request_id for the request.
- Return type: UUID4
- Raises:
- TypeError – If start is None.
- ValueError – If start is > current timestamp (now).
- ValueError – If end is > current timestamp (now).
- ValueError – If start is > end.
- TypeError – If callback is not None and not of type Callable.
request_instrument(self, InstrumentId instrument_id, datetime start=None, datetime end=None, ClientId client_id=None, callback: Callable[[UUID4], None] | None = None, update_catalog: bool = False, dict params=None) → UUID4
Request Instrument data for the given instrument ID.
If end is None
then will request up to the most recent data.
Once the response is received, the instrument data is forwarded from the message bus to the on_instrument handler.
If the request fails, then an error is logged.
- Parameters:
- instrument_id (InstrumentId) – The instrument ID for the request.
- start (datetime , optional) – The start datetime (UTC) of request time range. The inclusiveness depends on individual data client implementation.
- end (datetime , optional) – The end datetime (UTC) of request time range. The inclusiveness depends on individual data client implementation.
- client_id (ClientId , optional) – The specific client ID for the command.
If
None
then will be inferred from the venue in the instrument ID. - callback (Callable [ [UUID4 ] , None ] , optional) – The registered callback, to be called with the request ID when the response has completed processing.
- update_catalog (bool , optional) – Whether to update a catalog with the received data.
- params (dict *[*str , Any ] , optional) – Additional parameters potentially used by a specific client.
- Returns: The request_id for the request.
- Return type: UUID4
- Raises:
- ValueError – If start is not None and > current timestamp (now).
- ValueError – If end is not None and > current timestamp (now).
- ValueError – If start and end are not None and start is >= end.
- TypeError – If callback is not None and not of type Callable.
request_instruments(self, Venue venue, datetime start=None, datetime end=None, ClientId client_id=None, callback: Callable[[UUID4], None] | None = None, update_catalog: bool = False, dict params=None) → UUID4
Request all Instrument data for the given venue.
If end is None
then will request up to the most recent data.
Once the response is received, the instrument data is forwarded from the message bus to the on_instrument handler.
If the request fails, then an error is logged.
- Parameters:
- venue (Venue) – The venue for the request.
- start (datetime , optional) – The start datetime (UTC) of request time range. The inclusiveness depends on individual data client implementation.
- end (datetime , optional) – The end datetime (UTC) of request time range. The inclusiveness depends on individual data client implementation.
- client_id (ClientId , optional) – The specific client ID for the command.
If
None
then will be inferred from the venue in the instrument ID. - callback (Callable [ [UUID4 ] , None ] , optional) – The registered callback, to be called with the request ID when the response has completed processing.
- update_catalog (bool , optional) – Whether to update a catalog with the received data.
- params (dict *[*str , Any ] , optional) – Additional parameters potentially used by a specific client:
- only_last (default True) retains only the latest instrument record per instrument_id, based on the most recent ts_init.
- Returns: The request_id for the request.
- Return type: UUID4
- Raises:
- ValueError – If start is not None and > current timestamp (now).
- ValueError – If end is not None and > current timestamp (now).
- ValueError – If start and end are not None and start is >= end.
- TypeError – If callback is not None and not of type Callable.
request_order_book_depth(self, InstrumentId instrument_id, datetime start, datetime end=None, int limit=0, int depth=10, ClientId client_id=None, callback: Callable[[UUID4], None] | None = None, bool update_catalog: bool = True, dict params=None) → UUID4
Request historical OrderBookDepth10 snapshots.
Once the response is received, the order book depth data is forwarded from the message bus to the on_historical_data handler.
If the request fails, then an error is logged.
- Parameters:
- instrument_id (InstrumentId) – The instrument ID for the order book depths request.
- start (datetime) – The start datetime (UTC) of request time range (inclusive).
- end (datetime , optional) – The end datetime (UTC) of request time range. The inclusiveness depends on individual data client implementation.
- limit (int , optional) – The limit on the amount of depth snapshots received.
- depth (int , optional) – The maximum depth for the returned order book data (default is 10).
- client_id (ClientId , optional) – The specific client ID for the command. If None, it will be inferred from the venue in the instrument ID.
- callback (Callable [ [UUID4 ] , None ] , optional) – The registered callback, to be called with the request ID when the response has completed processing.
- update_catalog (bool , default True) – If the data catalog should be updated with the received data.
- params (dict *[*str , Any ] , optional) – Additional parameters potentially used by a specific client.
- Returns: The request_id for the request.
- Return type: UUID4
- Raises:
- ValueError – If the instrument_id is None.
- TypeError – If callback is not None and not of type Callable.
request_order_book_snapshot(self, InstrumentId instrument_id, int limit=0, ClientId client_id=None, callback: Callable[[UUID4], None] | None = None, dict params=None) → UUID4
Request an order book snapshot.
Once the response is received, the order book data is forwarded from the message bus to the on_historical_data handler.
If the request fails, then an error is logged.
- Parameters:
- instrument_id (InstrumentId) – The instrument ID for the order book snapshot request.
- limit (int , optional) – The limit on the depth of the order book snapshot.
- client_id (ClientId , optional) – The specific client ID for the command. If None, it will be inferred from the venue in the instrument ID.
- callback (Callable [ [UUID4 ] , None ] , optional) – The registered callback, to be called with the request ID when the response has completed processing.
- params (dict *[*str , Any ] , optional) – Additional parameters potentially used by a specific client.
- Returns: The request_id for the request.
- Return type: UUID4
- Raises:
- ValueError – If the instrument_id is None.
- TypeError – If callback is not None and not of type Callable.
request_quote_ticks(self, InstrumentId instrument_id, datetime start, datetime end=None, int limit=0, ClientId client_id=None, callback: Callable[[UUID4], None] | None = None, update_catalog: bool = False, dict params=None) → UUID4
Request historical QuoteTick data.
If end is None
then will request up to the most recent data.
Once the response is received, the quote tick data is forwarded from the message bus to the on_historical_data handler.
If the request fails, then an error is logged.
- Parameters:
- instrument_id (InstrumentId) – The tick instrument ID for the request.
- start (datetime) – The start datetime (UTC) of request time range. Should be left-inclusive (start <= value), but inclusiveness is not currently guaranteed.
- end (datetime , optional) – The end datetime (UTC) of request time range. If None then will be replaced with the current UTC time. Should be right-inclusive (value <= end), but inclusiveness is not currently guaranteed.
- limit (int , optional) – The limit on the amount of quote ticks received.
- client_id (ClientId , optional) – The specific client ID for the command.
If
None
then will be inferred from the venue in the instrument ID. - callback (Callable [ [UUID4 ] , None ] , optional) – The registered callback, to be called with the request ID when the response has completed processing.
- update_catalog (bool , optional) – Whether to update a catalog with the received data.
- params (dict *[*str , Any ] , optional) – Additional parameters potentially used by a specific client.
- Returns: The request_id for the request.
- Return type: UUID4
- Raises:
- TypeError – If start is None.
- ValueError – If start is > current timestamp (now).
- ValueError – If end is > current timestamp (now).
- ValueError – If start is > end.
- TypeError – If callback is not None and not of type Callable.
request_trade_ticks(self, InstrumentId instrument_id, datetime start, datetime end=None, int limit=0, ClientId client_id=None, callback: Callable[[UUID4], None] | None = None, update_catalog: bool = False, dict params=None) → UUID4
Request historical TradeTick data.
If end is None
then will request up to the most recent data.
Once the response is received, the trade tick data is forwarded from the message bus to the on_historical_data handler.
If the request fails, then an error is logged.
- Parameters:
- instrument_id (InstrumentId) – The tick instrument ID for the request.
- start (datetime) – The start datetime (UTC) of request time range. Should be left-inclusive (start <= value), but inclusiveness is not currently guaranteed.
- end (datetime , optional) – The end datetime (UTC) of request time range. If None then will be replaced with the current UTC time. Should be right-inclusive (value <= end), but inclusiveness is not currently guaranteed.
- limit (int , optional) – The limit on the amount of trade ticks received.
- client_id (ClientId , optional) – The specific client ID for the command.
If
None
then will be inferred from the venue in the instrument ID. - callback (Callable [ [UUID4 ] , None ] , optional) – The registered callback, to be called with the request ID when the response has completed processing.
- update_catalog (bool , optional) – Whether to update a catalog with the received data.
- params (dict *[*str , Any ] , optional) – Additional parameters potentially used by a specific client.
- Returns: The request_id for the request.
- Return type: UUID4
- Raises:
- TypeError – If start is None.
- ValueError – If start is > current timestamp (now).
- ValueError – If end is > current timestamp (now).
- ValueError – If start is > end.
- TypeError – If callback is not None and not of type Callable.
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.
run_in_executor(self, func: Callable[..., Any], tuple args=None, dict kwargs=None)
Schedules the callable func to be executed as fn(*args, **kwargs).
- Parameters:
- func (Callable) – The function to be executed.
- args (positional arguments) – The positional arguments for the call to func.
- kwargs (arbitrary keyword arguments) – The keyword arguments for the call to func.
- Returns: The unique task identifier for the execution. This also corresponds to any future objects memory address.
- Return type: TaskId
- Raises: TypeError – If func is not of type Callable.
save(self) → dict
Return the actor/strategy state dictionary to be saved.
Calls on_save.
- Returns: The strategy state to save.
- Return type: dict[str, bytes]
WARNING
Exceptions raised will be caught, logged, and reraised.
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_bars(self, BarType bar_type, ClientId client_id=None, bool update_catalog=False, dict params=None) → void
Subscribe to streaming Bar data for the given bar type.
Once subscribed, any matching bar data published on the message bus is forwarded to the on_bar handler.
- Parameters:
- bar_type (BarType) – The bar type to subscribe to.
- client_id (ClientId , optional) – The specific client ID for the command.
If
None
then will be inferred from the venue in the instrument ID. - update_catalog (bool , optional) – Whether to update a catalog with the received data. Only useful when downloading data during a backtest.
- params (dict *[*str , Any ] , optional) – Additional parameters potentially used by a specific client.
subscribe_data(self, DataType data_type, ClientId client_id=None, InstrumentId instrument_id=None, bool update_catalog=False, dict params=None) → void
Subscribe to data of the given data type.
Once subscribed, any matching data published on the message bus is forwarded to the on_data handler.
- Parameters:
- data_type (DataType) – The data type to subscribe to.
- client_id (ClientId , optional) – The data client ID. If supplied then a Subscribe command will be sent to the corresponding data client.
- update_catalog (bool , optional) – Whether to update a catalog with the received data. Only useful when downloading data during a backtest.
- params (dict *[*str , Any ] , optional) – Additional parameters potentially used by a specific client.
subscribe_funding_rates(self, InstrumentId instrument_id, ClientId client_id=None, dict params=None) → void
Subscribe to streaming FundingRateUpdate data for the given instrument ID.
Once subscribed, any matching funding rate updates published on the message bus are forwarded to the on_funding_rate handler.
- Parameters:
- instrument_id (InstrumentId) – The instrument to subscribe to.
- client_id (ClientId , optional) – The specific client ID for the command.
If
None
then will be inferred from the venue in the instrument ID. - params (dict *[*str , Any ] , optional) – Additional parameters potentially used by a specific client.
subscribe_index_prices(self, InstrumentId instrument_id, ClientId client_id=None, dict params=None) → void
Subscribe to streaming IndexPriceUpdate data for the given instrument ID.
Once subscribed, any matching index price updates published on the message bus are forwarded to the on_index_price handler.
- Parameters:
- instrument_id (InstrumentId) – The instrument to subscribe to.
- client_id (ClientId , optional) – The specific client ID for the command.
If
None
then will be inferred from the venue in the instrument ID. - params (dict *[*str , Any ] , optional) – Additional parameters potentially used by a specific client.
subscribe_instrument(self, InstrumentId instrument_id, ClientId client_id=None, bool update_catalog=False, dict params=None) → void
Subscribe to update Instrument data for the given instrument ID.
Once subscribed, any matching instrument data published on the message bus is forwarded to the on_instrument handler.
- Parameters:
- instrument_id (InstrumentId) – The instrument ID for the subscription.
- client_id (ClientId , optional) – The specific client ID for the command.
If
None
then will be inferred from the venue in the instrument ID. - update_catalog (bool , optional) – Whether to update a catalog with the received data. Only useful when downloading data during a backtest.
- params (dict *[*str , Any ] , optional) – Additional parameters potentially used by a specific client.
subscribe_instrument_close(self, InstrumentId instrument_id, ClientId client_id=None, dict params=None) → void
Subscribe to close updates for the given instrument ID.
Once subscribed, any matching instrument close data published on the message bus is forwarded to the on_instrument_close handler.
- Parameters:
- instrument_id (InstrumentId) – The instrument to subscribe to status updates for.
- client_id (ClientId , optional) – The specific client ID for the command.
If
None
then will be inferred from the venue in the instrument ID. - params (dict *[*str , Any ] , optional) – Additional parameters potentially used by a specific client.
subscribe_instrument_status(self, InstrumentId instrument_id, ClientId client_id=None, dict params=None) → void
Subscribe to status updates for the given instrument ID.
Once subscribed, any matching instrument status data published on the message bus is forwarded to the on_instrument_status handler.
- Parameters:
- instrument_id (InstrumentId) – The instrument to subscribe to status updates for.
- client_id (ClientId , optional) – The specific client ID for the command.
If
None
then will be inferred from the venue in the instrument ID. - params (dict *[*str , Any ] , optional) – Additional parameters potentially used by a specific client.
subscribe_instruments(self, Venue venue, ClientId client_id=None, bool update_catalog=False, dict params=None) → void
Subscribe to update Instrument data for the given venue.
Once subscribed, any matching instrument data published on the message bus is forwarded the on_instrument handler.
- Parameters:
- venue (Venue) – The venue for the subscription.
- client_id (ClientId , optional) – The specific client ID for the command.
If
None
then will be inferred from the venue. - update_catalog (bool , optional) – Whether to update a catalog with the received data. Only useful when downloading data during a backtest.
- params (dict *[*str , Any ] , optional) – Additional parameters potentially used by a specific client.
subscribe_mark_prices(self, InstrumentId instrument_id, ClientId client_id=None, dict params=None) → void
Subscribe to streaming MarkPriceUpdate data for the given instrument ID.
Once subscribed, any matching mark price updates published on the message bus are forwarded to the on_mark_price handler.
- Parameters:
- instrument_id (InstrumentId) – The instrument to subscribe to.
- client_id (ClientId , optional) – The specific client ID for the command.
If
None
then will be inferred from the venue in the instrument ID. - params (dict *[*str , Any ] , optional) – Additional parameters potentially used by a specific client.
subscribe_order_book_at_interval(self, InstrumentId instrument_id, BookType book_type=BookType.L2_MBP, int depth=0, int interval_ms=1000, ClientId client_id=None, dict params=None) → void
Subscribe to an OrderBook at a specified interval for the given instrument ID.
Once subscribed, any matching order book updates published on the message bus are forwarded to the on_order_book handler.
The DataEngine will only maintain one order book for each instrument. Because of this - the level, depth and params for the stream will be set as per the last subscription request (this will also affect all subscribers).
- Parameters:
- instrument_id (InstrumentId) – The order book instrument ID to subscribe to.
- book_type (BookType {
L1_MBP
,L2_MBP
,L3_MBO
}) – The order book type. - depth (int , optional) – The maximum depth for the order book. A depth of 0 is maximum depth.
- interval_ms (int , default 1000) – The order book snapshot interval (milliseconds).
- client_id (ClientId , optional) – The specific client ID for the command.
If
None
then will be inferred from the venue in the instrument ID. - params (dict *[*str , Any ] , optional) – Additional parameters potentially used by a specific client.
- Raises:
- ValueError – If depth is negative (< 0).
- ValueError – If interval_ms is not positive (> 0).
WARNING
Consider subscribing to order book deltas if you need intervals less than 100 milliseconds.