Strategy

Trait Strategy 

Source
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 DataActor capabilities (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§

Source

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§

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

fn handle_order_event(&mut self, event: OrderEventAny)

Handles an order event, dispatching to the appropriate handler and routing to the order manager.

Source

fn handle_position_event(&mut self, event: PositionEvent)

Handles a position event, dispatching to the appropriate handler.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

fn on_order_expired(&mut self, event: OrderExpired)

Called when an order expires.

Override this method to implement custom logic when an order expires.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

fn cancel_gtd_expiry(&mut self, client_order_id: &ClientOrderId)

Cancels a GTD expiry timer for an order.

Source

fn has_gtd_expiry_timer(&mut self, client_order_id: &ClientOrderId) -> bool

Checks if a GTD expiry timer exists for an order.

Source

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.

Source

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.

Implementors§