Skip to main content

Strategy

Trait Strategy 

Source
pub trait Strategy: DataActor {
Show 54 methods // Required methods fn core(&self) -> &StrategyCore; fn core_mut(&mut self) -> &mut StrategyCore; // Provided methods fn external_order_claims(&self) -> Option<Vec<InstrumentId>> { ... } 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, orders: Vec<OrderAny>, position_id: Option<PositionId>, client_id: Option<ClientId>, ) -> Result<()> { ... } fn submit_order_list_with_params( &mut self, orders: Vec<OrderAny>, position_id: Option<PositionId>, client_id: Option<ClientId>, params: IndexMap<String, String>, ) -> 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 on_market_exit(&mut self) { ... } fn post_market_exit(&mut self) { ... } fn is_exiting(&self) -> bool { ... } fn market_exit(&mut self) -> Result<()> { ... } fn check_market_exit(&mut self, _event: TimeEvent) { ... } fn finalize_market_exit(&mut self) { ... } fn cancel_market_exit(&mut self) { ... } fn stop(&mut self) -> bool { ... } fn deny_order(&mut self, order: &OrderAny, reason: Ustr) { ... } fn deny_order_list(&mut self, orders: &[OrderAny], reason: Ustr) { ... } 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 [DataActor]s 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(&self) -> &StrategyCore

Provides access to the internal StrategyCore.

This method must be implemented by the user’s strategy struct, typically by returning a reference to its StrategyCore member.

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 external_order_claims(&self) -> Option<Vec<InstrumentId>>

Returns the external order claims for this strategy.

These are instrument IDs whose external orders should be claimed by this strategy during reconciliation.

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, orders: Vec<OrderAny>, 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 submit_order_list_with_params( &mut self, orders: Vec<OrderAny>, position_id: Option<PositionId>, client_id: Option<ClientId>, params: IndexMap<String, String>, ) -> Result<()>

Submits an order list with adapter-specific parameters.

§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 and market exit timer events to the market exit checker.

§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 on_market_exit(&mut self)

Called when a market exit has been initiated.

Override this method to implement custom logic when a market exit begins.

Source

fn post_market_exit(&mut self)

Called after a market exit has completed.

Override this method to implement custom logic after a market exit completes.

Source

fn is_exiting(&self) -> bool

Returns whether the strategy is currently executing a market exit.

Strategies can check this to avoid submitting new orders during exit.

Source

fn market_exit(&mut self) -> Result<()>

Initiates an iterative market exit for the strategy.

Will cancel all open orders and close all open positions, and wait for all in-flight orders to resolve and positions to close. The strategy remains running after the exit completes.

The on_market_exit hook is called when the exit process begins. The post_market_exit hook is called when the exit process completes.

Uses market_exit_time_in_force and market_exit_reduce_only from the strategy config for closing market orders.

§Errors

Returns an error if the market exit cannot be initiated.

Source

fn check_market_exit(&mut self, _event: TimeEvent)

Checks if the market exit is complete and finalizes if so.

This method is called by the market exit timer.

Source

fn finalize_market_exit(&mut self)

Finalizes the market exit process.

Cancels the market exit timer, resets state, calls the post_market_exit hook, and stops the strategy if a stop was pending.

Source

fn cancel_market_exit(&mut self)

Cancels an active market exit without calling hooks.

Used when stop() is called during an active market exit to avoid state leaks.

Source

fn stop(&mut self) -> bool

Stops the strategy with optional managed stop behavior.

If manage_stop is enabled in the config, the strategy will first complete any active market exit (or initiate one) before stopping. If manage_stop is disabled, the strategy stops immediately, cleaning up any active market exit state.

§Returns

Returns true if the strategy should proceed with stopping, false if the stop is being deferred until market exit completes.

Source

fn deny_order(&mut self, order: &OrderAny, reason: Ustr)

Denies an order by generating an OrderDenied event.

This method creates an OrderDenied event, applies it to the order, and updates the cache.

Source

fn deny_order_list(&mut self, orders: &[OrderAny], reason: Ustr)

Denies all orders in an order list.

This method denies each non-closed order in the list.

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§