pub trait Strategy: DataActor {
Show 41 methods
// Required method
fn core_mut(&mut self) -> &mut StrategyCore;
// Provided methods
fn submit_order(
&mut self,
order: OrderAny,
position_id: Option<PositionId>,
client_id: Option<ClientId>,
) -> Result<()> { ... }
fn submit_order_with_params(
&mut self,
order: OrderAny,
position_id: Option<PositionId>,
client_id: Option<ClientId>,
params: IndexMap<String, String>,
) -> Result<()> { ... }
fn submit_order_list(
&mut self,
order_list: OrderList,
position_id: Option<PositionId>,
client_id: Option<ClientId>,
) -> Result<()> { ... }
fn modify_order(
&mut self,
order: OrderAny,
quantity: Option<Quantity>,
price: Option<Price>,
trigger_price: Option<Price>,
client_id: Option<ClientId>,
) -> Result<()> { ... }
fn modify_order_with_params(
&mut self,
order: OrderAny,
quantity: Option<Quantity>,
price: Option<Price>,
trigger_price: Option<Price>,
client_id: Option<ClientId>,
params: IndexMap<String, String>,
) -> Result<()> { ... }
fn cancel_order(
&mut self,
order: OrderAny,
client_id: Option<ClientId>,
) -> Result<()> { ... }
fn cancel_order_with_params(
&mut self,
order: OrderAny,
client_id: Option<ClientId>,
params: IndexMap<String, String>,
) -> Result<()> { ... }
fn cancel_orders(
&mut self,
orders: Vec<OrderAny>,
client_id: Option<ClientId>,
params: Option<IndexMap<String, String>>,
) -> Result<()> { ... }
fn cancel_all_orders(
&mut self,
instrument_id: InstrumentId,
order_side: Option<OrderSide>,
client_id: Option<ClientId>,
) -> Result<()> { ... }
fn cancel_all_orders_with_params(
&mut self,
instrument_id: InstrumentId,
order_side: Option<OrderSide>,
client_id: Option<ClientId>,
params: IndexMap<String, String>,
) -> Result<()> { ... }
fn close_position(
&mut self,
position: &Position,
client_id: Option<ClientId>,
tags: Option<Vec<Ustr>>,
time_in_force: Option<TimeInForce>,
reduce_only: Option<bool>,
quote_quantity: Option<bool>,
) -> Result<()> { ... }
fn close_all_positions(
&mut self,
instrument_id: InstrumentId,
position_side: Option<PositionSide>,
client_id: Option<ClientId>,
tags: Option<Vec<Ustr>>,
time_in_force: Option<TimeInForce>,
reduce_only: Option<bool>,
quote_quantity: Option<bool>,
) -> Result<()> { ... }
fn query_account(
&mut self,
account_id: AccountId,
client_id: Option<ClientId>,
) -> Result<()> { ... }
fn query_order(
&mut self,
order: &OrderAny,
client_id: Option<ClientId>,
) -> Result<()> { ... }
fn handle_order_event(&mut self, event: OrderEventAny) { ... }
fn handle_position_event(&mut self, event: PositionEvent) { ... }
fn on_start(&mut self) -> Result<()> { ... }
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_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_position_opened(&mut self, event: PositionOpened) { ... }
fn on_position_changed(&mut self, event: PositionChanged) { ... }
fn on_position_closed(&mut self, event: PositionClosed) { ... }
fn set_gtd_expiry(&mut self, order: &OrderAny) -> Result<()> { ... }
fn cancel_gtd_expiry(&mut self, client_order_id: &ClientOrderId) { ... }
fn has_gtd_expiry_timer(&mut self, client_order_id: &ClientOrderId) -> bool { ... }
fn expire_gtd_order(&mut self, event: TimeEvent) { ... }
fn reactivate_gtd_timers(&mut self) { ... }
}Expand description
Core trait for implementing trading strategies in NautilusTrader.
Strategies are specialized DataActors that combine data ingestion capabilities with
comprehensive order and position management functionality. By implementing this trait,
custom strategies gain access to the full trading execution stack including order
submission, modification, cancellation, and position management.
§Key Capabilities
- All
DataActorcapabilities (data subscriptions, event handling, timers). - Order lifecycle management (submit, modify, cancel).
- Position management (open, close, monitor).
- Access to the trading cache and portfolio.
- Event routing to order manager and emulator.
§Implementation
User strategies should implement the Strategy::core_mut method to provide
access to their internal StrategyCore, which handles the integration with
the trading engine. All order and position management methods are provided
as default implementations.
Required Methods§
Sourcefn core_mut(&mut self) -> &mut StrategyCore
fn core_mut(&mut self) -> &mut StrategyCore
Provides mutable access to the internal StrategyCore.
This method must be implemented by the user’s strategy struct, typically
by returning a mutable reference to its StrategyCore member.
Provided Methods§
Sourcefn submit_order(
&mut self,
order: OrderAny,
position_id: Option<PositionId>,
client_id: Option<ClientId>,
) -> Result<()>
fn submit_order( &mut self, order: OrderAny, position_id: Option<PositionId>, client_id: Option<ClientId>, ) -> Result<()>
Submits an order.
§Errors
Returns an error if the strategy is not registered or order submission fails.
Sourcefn submit_order_with_params(
&mut self,
order: OrderAny,
position_id: Option<PositionId>,
client_id: Option<ClientId>,
params: IndexMap<String, String>,
) -> Result<()>
fn submit_order_with_params( &mut self, order: OrderAny, position_id: Option<PositionId>, client_id: Option<ClientId>, params: IndexMap<String, String>, ) -> Result<()>
Submits an order with adapter-specific parameters.
§Errors
Returns an error if the strategy is not registered or order submission fails.
Sourcefn submit_order_list(
&mut self,
order_list: OrderList,
position_id: Option<PositionId>,
client_id: Option<ClientId>,
) -> Result<()>
fn submit_order_list( &mut self, order_list: OrderList, position_id: Option<PositionId>, client_id: Option<ClientId>, ) -> Result<()>
Submits an order list.
§Errors
Returns an error if the strategy is not registered, the order list is invalid, or order list submission fails.
Sourcefn modify_order(
&mut self,
order: OrderAny,
quantity: Option<Quantity>,
price: Option<Price>,
trigger_price: Option<Price>,
client_id: Option<ClientId>,
) -> Result<()>
fn modify_order( &mut self, order: OrderAny, quantity: Option<Quantity>, price: Option<Price>, trigger_price: Option<Price>, client_id: Option<ClientId>, ) -> Result<()>
Modifies an order.
§Errors
Returns an error if the strategy is not registered or order modification fails.
Sourcefn modify_order_with_params(
&mut self,
order: OrderAny,
quantity: Option<Quantity>,
price: Option<Price>,
trigger_price: Option<Price>,
client_id: Option<ClientId>,
params: IndexMap<String, String>,
) -> Result<()>
fn modify_order_with_params( &mut self, order: OrderAny, quantity: Option<Quantity>, price: Option<Price>, trigger_price: Option<Price>, client_id: Option<ClientId>, params: IndexMap<String, String>, ) -> Result<()>
Modifies an order with adapter-specific parameters.
§Errors
Returns an error if the strategy is not registered or order modification fails.
Sourcefn cancel_order(
&mut self,
order: OrderAny,
client_id: Option<ClientId>,
) -> Result<()>
fn cancel_order( &mut self, order: OrderAny, client_id: Option<ClientId>, ) -> Result<()>
Cancels an order.
§Errors
Returns an error if the strategy is not registered or order cancellation fails.
Sourcefn cancel_order_with_params(
&mut self,
order: OrderAny,
client_id: Option<ClientId>,
params: IndexMap<String, String>,
) -> Result<()>
fn cancel_order_with_params( &mut self, order: OrderAny, client_id: Option<ClientId>, params: IndexMap<String, String>, ) -> Result<()>
Cancels an order with adapter-specific parameters.
§Errors
Returns an error if the strategy is not registered or order cancellation fails.
Sourcefn cancel_orders(
&mut self,
orders: Vec<OrderAny>,
client_id: Option<ClientId>,
params: Option<IndexMap<String, String>>,
) -> Result<()>
fn cancel_orders( &mut self, orders: Vec<OrderAny>, client_id: Option<ClientId>, params: Option<IndexMap<String, String>>, ) -> Result<()>
Batch cancels multiple orders for the same instrument.
§Errors
Returns an error if the strategy is not registered, the orders span multiple instruments, or contain emulated/local orders.
Sourcefn cancel_all_orders(
&mut self,
instrument_id: InstrumentId,
order_side: Option<OrderSide>,
client_id: Option<ClientId>,
) -> Result<()>
fn cancel_all_orders( &mut self, instrument_id: InstrumentId, order_side: Option<OrderSide>, client_id: Option<ClientId>, ) -> Result<()>
Cancels all open orders for the given instrument.
§Errors
Returns an error if the strategy is not registered or order cancellation fails.
Sourcefn cancel_all_orders_with_params(
&mut self,
instrument_id: InstrumentId,
order_side: Option<OrderSide>,
client_id: Option<ClientId>,
params: IndexMap<String, String>,
) -> Result<()>
fn cancel_all_orders_with_params( &mut self, instrument_id: InstrumentId, order_side: Option<OrderSide>, client_id: Option<ClientId>, params: IndexMap<String, String>, ) -> Result<()>
Cancels all open orders for the given instrument with adapter-specific parameters.
§Errors
Returns an error if the strategy is not registered or order cancellation fails.
Sourcefn close_position(
&mut self,
position: &Position,
client_id: Option<ClientId>,
tags: Option<Vec<Ustr>>,
time_in_force: Option<TimeInForce>,
reduce_only: Option<bool>,
quote_quantity: Option<bool>,
) -> Result<()>
fn close_position( &mut self, position: &Position, client_id: Option<ClientId>, tags: Option<Vec<Ustr>>, time_in_force: Option<TimeInForce>, reduce_only: Option<bool>, quote_quantity: Option<bool>, ) -> Result<()>
Closes a position by submitting a market order for the opposite side.
§Errors
Returns an error if the strategy is not registered or position closing fails.
Sourcefn close_all_positions(
&mut self,
instrument_id: InstrumentId,
position_side: Option<PositionSide>,
client_id: Option<ClientId>,
tags: Option<Vec<Ustr>>,
time_in_force: Option<TimeInForce>,
reduce_only: Option<bool>,
quote_quantity: Option<bool>,
) -> Result<()>
fn close_all_positions( &mut self, instrument_id: InstrumentId, position_side: Option<PositionSide>, client_id: Option<ClientId>, tags: Option<Vec<Ustr>>, time_in_force: Option<TimeInForce>, reduce_only: Option<bool>, quote_quantity: Option<bool>, ) -> Result<()>
Closes all open positions for the given instrument.
§Errors
Returns an error if the strategy is not registered or position closing fails.
Sourcefn query_account(
&mut self,
account_id: AccountId,
client_id: Option<ClientId>,
) -> Result<()>
fn query_account( &mut self, account_id: AccountId, client_id: Option<ClientId>, ) -> Result<()>
Queries account state from the execution client.
Creates a QueryAccount command and sends it to the execution engine,
which will request the current account state from the execution client.
§Errors
Returns an error if the strategy is not registered.
Sourcefn query_order(
&mut self,
order: &OrderAny,
client_id: Option<ClientId>,
) -> Result<()>
fn query_order( &mut self, order: &OrderAny, client_id: Option<ClientId>, ) -> Result<()>
Queries order state from the execution client.
Creates a QueryOrder command and sends it to the execution engine,
which will request the current order state from the execution client.
§Errors
Returns an error if the strategy is not registered.
Sourcefn handle_order_event(&mut self, event: OrderEventAny)
fn handle_order_event(&mut self, event: OrderEventAny)
Handles an order event, dispatching to the appropriate handler and routing to the order manager.
Sourcefn handle_position_event(&mut self, event: PositionEvent)
fn handle_position_event(&mut self, event: PositionEvent)
Handles a position event, dispatching to the appropriate handler.
Sourcefn on_start(&mut self) -> Result<()>
fn on_start(&mut self) -> Result<()>
Called when the strategy is started.
Override this method to implement custom initialization logic.
The default implementation reactivates GTD timers if manage_gtd_expiry is enabled.
§Errors
Returns an error if strategy initialization fails.
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.
Routes GTD expiry timer events to the expiry handler.
§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.
Override this method to implement custom logic when an order is first created.
Sourcefn on_order_denied(&mut self, event: OrderDenied)
fn on_order_denied(&mut self, event: OrderDenied)
Called when an order is denied by the system.
Override this method to implement custom logic when an order is denied before submission.
Sourcefn on_order_emulated(&mut self, event: OrderEmulated)
fn on_order_emulated(&mut self, event: OrderEmulated)
Called when an order is emulated.
Override this method to implement custom logic when an order is taken over by the emulator.
Sourcefn on_order_released(&mut self, event: OrderReleased)
fn on_order_released(&mut self, event: OrderReleased)
Called when an order is released from emulation.
Override this method to implement custom logic when an emulated order is released.
Sourcefn on_order_submitted(&mut self, event: OrderSubmitted)
fn on_order_submitted(&mut self, event: OrderSubmitted)
Called when an order is submitted to the venue.
Override this method to implement custom logic 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 by the venue.
Override this method to implement custom logic 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 by the venue.
Override this method to implement custom logic when an order is accepted.
Sourcefn on_order_expired(&mut self, event: OrderExpired)
fn on_order_expired(&mut self, event: OrderExpired)
Called when an order expires.
Override this method to implement custom logic 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.
Override this method to implement custom logic when a stop or conditional 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.
Override this method to implement custom logic when an order is pending modification.
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.
Override this method to implement custom logic when an order is pending cancellation.
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.
Override this method to implement custom logic 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.
Override this method to implement custom logic 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.
Override this method to implement custom logic when an order is modified.
Sourcefn on_position_opened(&mut self, event: PositionOpened)
fn on_position_opened(&mut self, event: PositionOpened)
Called when a position is opened.
Override this method to implement custom logic 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 (quantity or price updated).
Override this method to implement custom logic when a position changes.
Sourcefn on_position_closed(&mut self, event: PositionClosed)
fn on_position_closed(&mut self, event: PositionClosed)
Called when a position is closed.
Override this method to implement custom logic when a position is closed.
Sourcefn set_gtd_expiry(&mut self, order: &OrderAny) -> Result<()>
fn set_gtd_expiry(&mut self, order: &OrderAny) -> Result<()>
Sets a GTD expiry timer for an order.
Creates a timer that will automatically cancel the order when it expires.
§Errors
Returns an error if timer creation fails.
Sourcefn cancel_gtd_expiry(&mut self, client_order_id: &ClientOrderId)
fn cancel_gtd_expiry(&mut self, client_order_id: &ClientOrderId)
Cancels a GTD expiry timer for an order.
Sourcefn has_gtd_expiry_timer(&mut self, client_order_id: &ClientOrderId) -> bool
fn has_gtd_expiry_timer(&mut self, client_order_id: &ClientOrderId) -> bool
Checks if a GTD expiry timer exists for an order.
Sourcefn expire_gtd_order(&mut self, event: TimeEvent)
fn expire_gtd_order(&mut self, event: TimeEvent)
Handles GTD order expiry by canceling the order.
This method is called when a GTD expiry timer fires.
Sourcefn reactivate_gtd_timers(&mut self)
fn reactivate_gtd_timers(&mut self)
Reactivates GTD timers for open orders on strategy start.
Queries the cache for all open GTD orders and creates timers for those that haven’t expired yet. Orders that have already expired are canceled immediately.