Skip to main content

StrategyContext

Trait StrategyContext 

Source
pub trait StrategyContext {
Show 20 methods // Required methods fn place_order(&mut self, cmd: PlaceOrder); fn place_orders(&mut self, cmds: Vec<PlaceOrder>); fn cancel_order(&mut self, cmd: CancelOrder); fn cancel_all(&mut self, cmd: CancelAll); fn stop_strategy(&mut self, strategy_id: StrategyId, reason: &str); fn set_timer(&mut self, delay: Duration) -> TimerId; fn set_interval(&mut self, interval: Duration) -> TimerId; fn cancel_timer(&mut self, timer_id: TimerId); fn mid_price(&self, instrument: &InstrumentId) -> Option<Price>; fn quote(&self, instrument: &InstrumentId) -> Option<Quote>; fn instrument_meta( &self, instrument: &InstrumentId, ) -> Option<&InstrumentMeta>; fn balance(&self, asset: &AssetId) -> Balance; fn position(&self, instrument: &InstrumentId) -> Position; fn exchange_health(&self, exchange: &ExchangeInstance) -> ExchangeHealth; fn order(&self, client_id: &ClientOrderId) -> Option<&LiveOrder>; fn now_ms(&self) -> i64; fn log_info(&self, msg: &str); fn log_warn(&self, msg: &str); fn log_error(&self, msg: &str); fn log_debug(&self, msg: &str);
}
Expand description

Strategy context - what strategies use to interact with the engine.

This is passed to strategy methods and provides:

  • Command emission (place/cancel orders)
  • Timer management
  • Read-only access to market data and state
  • Logging

Required Methods§

Source

fn place_order(&mut self, cmd: PlaceOrder)

Place a new order. Returns immediately; acceptance/rejection comes via events.

Source

fn place_orders(&mut self, cmds: Vec<PlaceOrder>)

Place multiple orders in a single batch. Returns immediately; acceptance/rejection comes via events.

Source

fn cancel_order(&mut self, cmd: CancelOrder)

Cancel an existing order by client_id.

Source

fn cancel_all(&mut self, cmd: CancelAll)

Cancel all open orders (optionally for an instrument).

Source

fn stop_strategy(&mut self, strategy_id: StrategyId, reason: &str)

Request strategy stop. This will trigger on_stop callback after current event processing.

Source

fn set_timer(&mut self, delay: Duration) -> TimerId

Register a one-shot timer that fires after delay.

Source

fn set_interval(&mut self, interval: Duration) -> TimerId

Register a repeating timer that fires every interval.

Source

fn cancel_timer(&mut self, timer_id: TimerId)

Cancel a previously set timer.

Source

fn mid_price(&self, instrument: &InstrumentId) -> Option<Price>

Get the current mid price for an instrument (if available from quote poller)

Source

fn quote(&self, instrument: &InstrumentId) -> Option<Quote>

Get the current best bid/ask quote for an instrument

Source

fn instrument_meta(&self, instrument: &InstrumentId) -> Option<&InstrumentMeta>

Get instrument metadata

Source

fn balance(&self, asset: &AssetId) -> Balance

Get this strategy’s current balance for an asset

Source

fn position(&self, instrument: &InstrumentId) -> Position

Get the current position for an instrument

Source

fn exchange_health(&self, exchange: &ExchangeInstance) -> ExchangeHealth

Check if exchange is Active or Halted

Source

fn order(&self, client_id: &ClientOrderId) -> Option<&LiveOrder>

Get an order by client_id (if tracked)

Source

fn now_ms(&self) -> i64

Current time in milliseconds (deterministic in backtests)

Source

fn log_info(&self, msg: &str)

Log an info message

Source

fn log_warn(&self, msg: &str)

Log a warning message

Source

fn log_error(&self, msg: &str)

Log an error message

Source

fn log_debug(&self, msg: &str)

Log a debug message

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§