pub trait ExecutionAlgorithm: DataActor {
Show 46 methods
// Required method
fn on_order(&mut self, order: OrderAny) -> Result<()>;
// Provided methods
fn id(&self) -> ExecAlgorithmId
where Self: ExecutionAlgorithmNative { ... }
fn execute(&mut self, command: TradingCommand) -> Result<()>
where Self: ExecutionAlgorithmNative + 'static + Debug + Sized { ... }
fn on_order_list(
&mut self,
_order_list: OrderList,
orders: Vec<OrderAny>,
) -> Result<()> { ... }
fn handle_cancel_order(&mut self, command: CancelOrder) -> Result<()>
where Self: ExecutionAlgorithmNative { ... }
fn generate_order_canceled(&mut self, order: &OrderAny) -> OrderCanceled
where Self: ExecutionAlgorithmNative { ... }
fn generate_order_pending_update(
&mut self,
order: &OrderAny,
) -> OrderPendingUpdate
where Self: ExecutionAlgorithmNative { ... }
fn generate_order_pending_cancel(
&mut self,
order: &OrderAny,
) -> OrderPendingCancel
where Self: ExecutionAlgorithmNative { ... }
fn spawn_market(
&mut self,
primary: &mut OrderAny,
quantity: Quantity,
time_in_force: TimeInForce,
reduce_only: bool,
tags: Option<Vec<Ustr>>,
reduce_primary: bool,
) -> MarketOrder
where Self: ExecutionAlgorithmNative { ... }
fn spawn_limit(
&mut self,
primary: &mut OrderAny,
quantity: Quantity,
price: Price,
time_in_force: TimeInForce,
expire_time: Option<UnixNanos>,
post_only: bool,
reduce_only: bool,
display_qty: Option<Quantity>,
emulation_trigger: Option<TriggerType>,
tags: Option<Vec<Ustr>>,
reduce_primary: bool,
) -> LimitOrder
where Self: ExecutionAlgorithmNative { ... }
fn spawn_market_to_limit(
&mut self,
primary: &mut OrderAny,
quantity: Quantity,
time_in_force: TimeInForce,
expire_time: Option<UnixNanos>,
reduce_only: bool,
display_qty: Option<Quantity>,
emulation_trigger: Option<TriggerType>,
tags: Option<Vec<Ustr>>,
reduce_primary: bool,
) -> MarketToLimitOrder
where Self: ExecutionAlgorithmNative { ... }
fn reduce_primary_order(
&mut self,
primary: &mut OrderAny,
spawn_qty: Quantity,
)
where Self: ExecutionAlgorithmNative { ... }
fn restore_primary_order_quantity(&mut self, order: &OrderAny)
where Self: ExecutionAlgorithmNative { ... }
fn submit_order(
&mut self,
order: OrderAny,
position_id: Option<PositionId>,
client_id: Option<ClientId>,
) -> Result<()>
where Self: ExecutionAlgorithmNative { ... }
fn modify_order(
&mut self,
order: &mut OrderAny,
quantity: Option<Quantity>,
price: Option<Price>,
trigger_price: Option<Price>,
client_id: Option<ClientId>,
) -> Result<()>
where Self: ExecutionAlgorithmNative { ... }
fn modify_order_in_place(
&mut self,
order: &mut OrderAny,
quantity: Option<Quantity>,
price: Option<Price>,
trigger_price: Option<Price>,
) -> Result<()>
where Self: ExecutionAlgorithmNative { ... }
fn cancel_order(
&mut self,
order: &mut OrderAny,
client_id: Option<ClientId>,
) -> Result<()>
where Self: ExecutionAlgorithmNative { ... }
fn subscribe_to_strategy_events(&mut self, strategy_id: StrategyId)
where Self: ExecutionAlgorithmNative + 'static + Debug + Sized { ... }
fn unsubscribe_all_strategy_events(&mut self)
where Self: ExecutionAlgorithmNative { ... }
fn handle_order_event(&mut self, event: OrderEventAny)
where Self: ExecutionAlgorithmNative { ... }
fn handle_position_event(&mut self, event: PositionEvent)
where Self: ExecutionAlgorithmNative { ... }
fn on_start(&mut self) -> Result<()>
where Self: ExecutionAlgorithmNative { ... }
fn on_stop(&mut self) -> Result<()> { ... }
fn on_reset(&mut self) -> Result<()>
where Self: ExecutionAlgorithmNative { ... }
fn on_time_event(&mut self, _event: &TimeEvent) -> Result<()> { ... }
fn on_order_initialized(&mut self, event: OrderInitialized) { ... }
fn on_order_denied(&mut self, event: OrderDenied) { ... }
fn on_order_emulated(&mut self, event: OrderEmulated) { ... }
fn on_order_released(&mut self, event: OrderReleased) { ... }
fn on_order_submitted(&mut self, event: OrderSubmitted) { ... }
fn on_order_rejected(&mut self, event: OrderRejected) { ... }
fn on_order_accepted(&mut self, event: OrderAccepted) { ... }
fn on_algo_order_canceled(&mut self, event: OrderCanceled) { ... }
fn on_order_expired(&mut self, event: OrderExpired) { ... }
fn on_order_triggered(&mut self, event: OrderTriggered) { ... }
fn on_order_pending_update(&mut self, event: OrderPendingUpdate) { ... }
fn on_order_pending_cancel(&mut self, event: OrderPendingCancel) { ... }
fn on_order_modify_rejected(&mut self, event: OrderModifyRejected) { ... }
fn on_order_cancel_rejected(&mut self, event: OrderCancelRejected) { ... }
fn on_order_updated(&mut self, event: OrderUpdated) { ... }
fn on_algo_order_filled(&mut self, event: OrderFilled) { ... }
fn on_order_event(&mut self, event: OrderEventAny) { ... }
fn on_position_opened(&mut self, event: PositionOpened) { ... }
fn on_position_changed(&mut self, event: PositionChanged) { ... }
fn on_position_closed(&mut self, event: PositionClosed) { ... }
fn on_position_event(&mut self, event: PositionEvent) { ... }
}Expand description
Core trait for implementing execution algorithms in NautilusTrader.
Execution algorithms are specialized DataActors that receive orders from strategies
and execute them by spawning child orders. They are used for order slicing algorithms
like TWAP and VWAP.
§Key Capabilities
- All
DataActorcapabilities (data subscriptions, event handling, timers) - Order spawning (market, limit, market-to-limit)
- Order lifecycle management (submit, modify, cancel)
- Event filtering for algorithm-owned orders
§Implementation
Use the nautilus_execution_algorithm! macro to generate the native runtime
wiring and ExecutionAlgorithm implementation, including the required
on_order() method. Normal execution algorithm logic should call facade
methods such as submit_order(), spawn_market(), and
unsubscribe_all_strategy_events(). Native runtime code that needs the
internal core should use ExecutionAlgorithmNative.
Required Methods§
Provided Methods§
Sourcefn id(&self) -> ExecAlgorithmIdwhere
Self: ExecutionAlgorithmNative,
fn id(&self) -> ExecAlgorithmIdwhere
Self: ExecutionAlgorithmNative,
Returns the execution algorithm ID.
Sourcefn execute(&mut self, command: TradingCommand) -> Result<()>
fn execute(&mut self, command: TradingCommand) -> Result<()>
Executes a trading command.
This is the main entry point for commands routed to the algorithm. Dispatches to the appropriate handler based on command type.
Commands are only processed when the algorithm is in Running state.
§Errors
Returns an error if command handling fails.
Sourcefn on_order_list(
&mut self,
_order_list: OrderList,
orders: Vec<OrderAny>,
) -> Result<()>
fn on_order_list( &mut self, _order_list: OrderList, orders: Vec<OrderAny>, ) -> Result<()>
Called when an order list is received for execution.
Override this method to handle order lists. The default implementation processes each order individually.
§Errors
Returns an error if order list handling fails.
Sourcefn handle_cancel_order(&mut self, command: CancelOrder) -> Result<()>where
Self: ExecutionAlgorithmNative,
fn handle_cancel_order(&mut self, command: CancelOrder) -> Result<()>where
Self: ExecutionAlgorithmNative,
Handles a cancel order command for algorithm-managed orders.
This generates an internal cancel event and publishes it. The order is canceled locally without sending a command to the execution engine.
§Errors
Returns an error if cancellation fails.
Sourcefn generate_order_canceled(&mut self, order: &OrderAny) -> OrderCanceledwhere
Self: ExecutionAlgorithmNative,
fn generate_order_canceled(&mut self, order: &OrderAny) -> OrderCanceledwhere
Self: ExecutionAlgorithmNative,
Generates an OrderCanceled event for an order.
Sourcefn generate_order_pending_update(
&mut self,
order: &OrderAny,
) -> OrderPendingUpdatewhere
Self: ExecutionAlgorithmNative,
fn generate_order_pending_update(
&mut self,
order: &OrderAny,
) -> OrderPendingUpdatewhere
Self: ExecutionAlgorithmNative,
Generates an OrderPendingUpdate event for an order.
Sourcefn generate_order_pending_cancel(
&mut self,
order: &OrderAny,
) -> OrderPendingCancelwhere
Self: ExecutionAlgorithmNative,
fn generate_order_pending_cancel(
&mut self,
order: &OrderAny,
) -> OrderPendingCancelwhere
Self: ExecutionAlgorithmNative,
Generates an OrderPendingCancel event for an order.
Sourcefn spawn_market(
&mut self,
primary: &mut OrderAny,
quantity: Quantity,
time_in_force: TimeInForce,
reduce_only: bool,
tags: Option<Vec<Ustr>>,
reduce_primary: bool,
) -> MarketOrderwhere
Self: ExecutionAlgorithmNative,
fn spawn_market(
&mut self,
primary: &mut OrderAny,
quantity: Quantity,
time_in_force: TimeInForce,
reduce_only: bool,
tags: Option<Vec<Ustr>>,
reduce_primary: bool,
) -> MarketOrderwhere
Self: ExecutionAlgorithmNative,
Spawns a market order from a primary order.
Creates a new market order with:
- A unique client order ID:
{primary_id}-E{sequence}. - The primary order’s trader ID, strategy ID, and instrument ID.
- The algorithm’s
exec_algorithm_id. exec_spawn_idset to the primary order’s client order ID.
If reduce_primary is true, the primary order’s quantity will be reduced
by the spawned quantity. If the spawned order is subsequently denied or
rejected (before acceptance), the deducted quantity is automatically
restored to the primary order.
Sourcefn spawn_limit(
&mut self,
primary: &mut OrderAny,
quantity: Quantity,
price: Price,
time_in_force: TimeInForce,
expire_time: Option<UnixNanos>,
post_only: bool,
reduce_only: bool,
display_qty: Option<Quantity>,
emulation_trigger: Option<TriggerType>,
tags: Option<Vec<Ustr>>,
reduce_primary: bool,
) -> LimitOrderwhere
Self: ExecutionAlgorithmNative,
fn spawn_limit(
&mut self,
primary: &mut OrderAny,
quantity: Quantity,
price: Price,
time_in_force: TimeInForce,
expire_time: Option<UnixNanos>,
post_only: bool,
reduce_only: bool,
display_qty: Option<Quantity>,
emulation_trigger: Option<TriggerType>,
tags: Option<Vec<Ustr>>,
reduce_primary: bool,
) -> LimitOrderwhere
Self: ExecutionAlgorithmNative,
Spawns a limit order from a primary order.
Creates a new limit order with:
- A unique client order ID:
{primary_id}-E{sequence} - The primary order’s trader ID, strategy ID, and instrument ID
- The algorithm’s
exec_algorithm_id exec_spawn_idset to the primary order’s client order ID
If reduce_primary is true, the primary order’s quantity will be reduced
by the spawned quantity. If the spawned order is subsequently denied or
rejected (before acceptance), the deducted quantity is automatically
restored to the primary order.
Sourcefn spawn_market_to_limit(
&mut self,
primary: &mut OrderAny,
quantity: Quantity,
time_in_force: TimeInForce,
expire_time: Option<UnixNanos>,
reduce_only: bool,
display_qty: Option<Quantity>,
emulation_trigger: Option<TriggerType>,
tags: Option<Vec<Ustr>>,
reduce_primary: bool,
) -> MarketToLimitOrderwhere
Self: ExecutionAlgorithmNative,
fn spawn_market_to_limit(
&mut self,
primary: &mut OrderAny,
quantity: Quantity,
time_in_force: TimeInForce,
expire_time: Option<UnixNanos>,
reduce_only: bool,
display_qty: Option<Quantity>,
emulation_trigger: Option<TriggerType>,
tags: Option<Vec<Ustr>>,
reduce_primary: bool,
) -> MarketToLimitOrderwhere
Self: ExecutionAlgorithmNative,
Spawns a market-to-limit order from a primary order.
Creates a new market-to-limit order with:
- A unique client order ID:
{primary_id}-E{sequence} - The primary order’s trader ID, strategy ID, and instrument ID
- The algorithm’s
exec_algorithm_id exec_spawn_idset to the primary order’s client order ID
If reduce_primary is true, the primary order’s quantity will be reduced
by the spawned quantity. If the spawned order is subsequently denied or
rejected (before acceptance), the deducted quantity is automatically
restored to the primary order.
Sourcefn reduce_primary_order(&mut self, primary: &mut OrderAny, spawn_qty: Quantity)where
Self: ExecutionAlgorithmNative,
fn reduce_primary_order(&mut self, primary: &mut OrderAny, spawn_qty: Quantity)where
Self: ExecutionAlgorithmNative,
Reduces the primary order’s quantity by the spawn quantity.
Generates an OrderUpdated event and applies it to the primary order,
then updates the order in the cache.
§Panics
Panics if spawn_qty exceeds the primary order’s leaves_qty.
Sourcefn restore_primary_order_quantity(&mut self, order: &OrderAny)where
Self: ExecutionAlgorithmNative,
fn restore_primary_order_quantity(&mut self, order: &OrderAny)where
Self: ExecutionAlgorithmNative,
Restores the primary order quantity after a spawned order is denied or rejected.
This is called when a spawned order fails before acceptance. The quantity
that was deducted from the primary order is restored (up to the spawned
order’s leaves_qty to handle partial fills).
Sourcefn submit_order(
&mut self,
order: OrderAny,
position_id: Option<PositionId>,
client_id: Option<ClientId>,
) -> Result<()>where
Self: ExecutionAlgorithmNative,
fn submit_order(
&mut self,
order: OrderAny,
position_id: Option<PositionId>,
client_id: Option<ClientId>,
) -> Result<()>where
Self: ExecutionAlgorithmNative,
Submits an order to the execution engine via the risk engine.
§Errors
Returns an error if order submission fails.
Sourcefn modify_order(
&mut self,
order: &mut OrderAny,
quantity: Option<Quantity>,
price: Option<Price>,
trigger_price: Option<Price>,
client_id: Option<ClientId>,
) -> Result<()>where
Self: ExecutionAlgorithmNative,
fn modify_order(
&mut self,
order: &mut OrderAny,
quantity: Option<Quantity>,
price: Option<Price>,
trigger_price: Option<Price>,
client_id: Option<ClientId>,
) -> Result<()>where
Self: ExecutionAlgorithmNative,
Sourcefn modify_order_in_place(
&mut self,
order: &mut OrderAny,
quantity: Option<Quantity>,
price: Option<Price>,
trigger_price: Option<Price>,
) -> Result<()>where
Self: ExecutionAlgorithmNative,
fn modify_order_in_place(
&mut self,
order: &mut OrderAny,
quantity: Option<Quantity>,
price: Option<Price>,
trigger_price: Option<Price>,
) -> Result<()>where
Self: ExecutionAlgorithmNative,
Modifies an INITIALIZED or RELEASED order in place without sending a command.
This is useful for adjusting order parameters before submission. The order
is updated locally by applying an OrderUpdated event and updating the cache.
At least one parameter must differ from the current order values.
§Errors
Returns an error if the order status is not INITIALIZED or RELEASED, or if no parameters would change.
Sourcefn cancel_order(
&mut self,
order: &mut OrderAny,
client_id: Option<ClientId>,
) -> Result<()>where
Self: ExecutionAlgorithmNative,
fn cancel_order(
&mut self,
order: &mut OrderAny,
client_id: Option<ClientId>,
) -> Result<()>where
Self: ExecutionAlgorithmNative,
Sourcefn subscribe_to_strategy_events(&mut self, strategy_id: StrategyId)
fn subscribe_to_strategy_events(&mut self, strategy_id: StrategyId)
Subscribes to events from a strategy.
This is called automatically when the first order is received from a strategy.
Sourcefn unsubscribe_all_strategy_events(&mut self)where
Self: ExecutionAlgorithmNative,
fn unsubscribe_all_strategy_events(&mut self)where
Self: ExecutionAlgorithmNative,
Unsubscribes from all strategy event handlers.
This should be called before reset to properly clean up msgbus subscriptions.
Sourcefn handle_order_event(&mut self, event: OrderEventAny)where
Self: ExecutionAlgorithmNative,
fn handle_order_event(&mut self, event: OrderEventAny)where
Self: ExecutionAlgorithmNative,
Handles an order event, filtering for algorithm-owned orders.
Sourcefn handle_position_event(&mut self, event: PositionEvent)where
Self: ExecutionAlgorithmNative,
fn handle_position_event(&mut self, event: PositionEvent)where
Self: ExecutionAlgorithmNative,
Handles a position event.
Sourcefn on_start(&mut self) -> Result<()>where
Self: ExecutionAlgorithmNative,
fn on_start(&mut self) -> Result<()>where
Self: ExecutionAlgorithmNative,
Called when the algorithm is started.
Override this method to implement custom initialization logic.
§Errors
Returns an error if start fails.
Sourcefn on_reset(&mut self) -> Result<()>where
Self: ExecutionAlgorithmNative,
fn on_reset(&mut self) -> Result<()>where
Self: ExecutionAlgorithmNative,
Sourcefn on_time_event(&mut self, _event: &TimeEvent) -> Result<()>
fn on_time_event(&mut self, _event: &TimeEvent) -> Result<()>
Called when a time event is received.
Override this method for timer-based algorithms like TWAP.
§Errors
Returns an error if time event handling fails.
Sourcefn on_order_initialized(&mut self, event: OrderInitialized)
fn on_order_initialized(&mut self, event: OrderInitialized)
Called when an order is initialized.
Sourcefn on_order_denied(&mut self, event: OrderDenied)
fn on_order_denied(&mut self, event: OrderDenied)
Called when an order is denied.
Sourcefn on_order_emulated(&mut self, event: OrderEmulated)
fn on_order_emulated(&mut self, event: OrderEmulated)
Called when an order is emulated.
Sourcefn on_order_released(&mut self, event: OrderReleased)
fn on_order_released(&mut self, event: OrderReleased)
Called when an order is released from emulation.
Sourcefn on_order_submitted(&mut self, event: OrderSubmitted)
fn on_order_submitted(&mut self, event: OrderSubmitted)
Called when an order is submitted.
Sourcefn on_order_rejected(&mut self, event: OrderRejected)
fn on_order_rejected(&mut self, event: OrderRejected)
Called when an order is rejected.
Sourcefn on_order_accepted(&mut self, event: OrderAccepted)
fn on_order_accepted(&mut self, event: OrderAccepted)
Called when an order is accepted.
Sourcefn on_algo_order_canceled(&mut self, event: OrderCanceled)
fn on_algo_order_canceled(&mut self, event: OrderCanceled)
Called when an order is canceled.
Sourcefn on_order_expired(&mut self, event: OrderExpired)
fn on_order_expired(&mut self, event: OrderExpired)
Called when an order expires.
Sourcefn on_order_triggered(&mut self, event: OrderTriggered)
fn on_order_triggered(&mut self, event: OrderTriggered)
Called when an order is triggered.
Sourcefn on_order_pending_update(&mut self, event: OrderPendingUpdate)
fn on_order_pending_update(&mut self, event: OrderPendingUpdate)
Called when an order modification is pending.
Sourcefn on_order_pending_cancel(&mut self, event: OrderPendingCancel)
fn on_order_pending_cancel(&mut self, event: OrderPendingCancel)
Called when an order cancellation is pending.
Sourcefn on_order_modify_rejected(&mut self, event: OrderModifyRejected)
fn on_order_modify_rejected(&mut self, event: OrderModifyRejected)
Called when an order modification is rejected.
Sourcefn on_order_cancel_rejected(&mut self, event: OrderCancelRejected)
fn on_order_cancel_rejected(&mut self, event: OrderCancelRejected)
Called when an order cancellation is rejected.
Sourcefn on_order_updated(&mut self, event: OrderUpdated)
fn on_order_updated(&mut self, event: OrderUpdated)
Called when an order is updated.
Sourcefn on_algo_order_filled(&mut self, event: OrderFilled)
fn on_algo_order_filled(&mut self, event: OrderFilled)
Called when an order is filled.
Sourcefn on_order_event(&mut self, event: OrderEventAny)
fn on_order_event(&mut self, event: OrderEventAny)
Called for any order event (after specific handler).
Sourcefn on_position_opened(&mut self, event: PositionOpened)
fn on_position_opened(&mut self, event: PositionOpened)
Called when a position is opened.
Sourcefn on_position_changed(&mut self, event: PositionChanged)
fn on_position_changed(&mut self, event: PositionChanged)
Called when a position is changed.
Sourcefn on_position_closed(&mut self, event: PositionClosed)
fn on_position_closed(&mut self, event: PositionClosed)
Called when a position is closed.
Sourcefn on_position_event(&mut self, event: PositionEvent)
fn on_position_event(&mut self, event: PositionEvent)
Called for any position event (after specific handler).
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".