Execution
The execution subpackage groups components relating to the execution stack for the platform.
The layered architecture of the execution stack somewhat mirrors the data stack with a central engine, cache layer beneath, database layer beneath, with alternative implementations able to be written on top.
Due to the high-performance, the core components are reusable between both backtest and live implementations - helping to ensure consistent logic for trading operations.
Components
class ExecAlgorithm
Bases: Actor
ExecAlgorithm(config: ExecAlgorithmConfig | None = None)
The base class for all execution algorithms.
This class allows traders to implement their own customized execution algorithms.
- Parameters: config (ExecAlgorithmConfig , optional) – The execution algorithm configuration.
- Raises: TypeError – If config is not of type ExecAlgorithmConfig.
WARNING
This class should not be used directly, but through a concrete subclass.
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_order(self, Order order, ClientId client_id=None) → void
Cancel the given order with optional routing instructions.
A CancelOrder command will be created and then sent to either the OrderEmulator or the ExecutionEngine (depending on whether the order is emulated).
Logs an error if no VenueOrderId has been assigned to the order.
- Parameters:
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.
execute(self, TradingCommand command) → void
Handle the given trading command by processing it with the execution algorithm.
- Parameters: command (SubmitOrder) – The command to handle.
- Raises: ValueError – If command.exec_algorithm_id is not equal to self.id.
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
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).
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_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_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_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_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_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 , object ]) – The state dictionary.
- Raises: RuntimeError – If actor/strategy is not registered with a trader.
WARNING
Exceptions raised will be caught, logged, and reraised.
log
The actors logger.
- Returns: Logger
modify_order(self, Order order, Quantity quantity=None, Price price=None, Price trigger_price=None, ClientId client_id=None) → void
Modify the given order with optional parameters and routing instructions.
An ModifyOrder command will be created and then sent to the RiskEngine.
At least one value must differ from the original order for the command to be valid.
Will use an Order Cancel/Replace Request (a.k.a Order Modification) for FIX protocols, otherwise if order update is not available for the API, then will cancel and replace with a new order using the original ClientOrderId.
- Parameters:
- order (Order) – The order to update.
- quantity (Quantity , optional) – The updated quantity for the given order.
- price (Price , optional) – The updated price for the given order (if applicable).
- trigger_price (Price , optional) – The updated trigger price for the given order (if applicable).
- client_id (ClientId , optional) – The specific client ID for the command.
If
None
then will be inferred from the venue in the instrument ID.
- Raises:
- ValueError – If price is not
None
and order does not have a price. - ValueError – If trigger is not
None
and order does not have a trigger_price.
- ValueError – If price is not
WARNING
If the order is already closed or at PENDING_CANCEL status then the command will not be generated, and a warning will be logged.
modify_order_in_place(self, Order order, Quantity quantity=None, Price price=None, Price trigger_price=None) → void
Modify the given INITIALIZED
order in place (immediately) with optional parameters.
At least one value must differ from the original order for the command to be valid.
- Parameters:
- Raises:
- ValueError – If order.status is not
INITIALIZED
orRELEASED
. - ValueError – If price is not
None
and order does not have a price. - ValueError – If trigger is not
None
and order does not have a trigger_price.
- ValueError – If order.status is not
WARNING
If the order is already closed or at PENDING_CANCEL status then the command will not be generated, and a warning will be logged.
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_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_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.
WARNING
System method (not intended to be called by user code).
on_order(self, Order order) → void
Actions to be performed when running and receives an order.
- Parameters: order (Order) – The order to be handled.
WARNING
System method (not intended to be called by user code).
on_order_accepted(self, OrderAccepted event) → void
Actions to be performed when running and receives an order accepted event.
- Parameters: event (OrderAccepted) – The event 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_cancel_rejected(self, OrderCancelRejected event) → void
Actions to be performed when running and receives an order cancel rejected event.
- Parameters: event (OrderCancelRejected) – The event received.
WARNING
System method (not intended to be called by user code).
on_order_canceled(self, OrderCanceled event) → void
Actions to be performed when running and receives an order canceled event.
- Parameters: event (OrderCanceled) – The event received.
WARNING
System method (not intended to be called by user code).