pub struct Engine { /* private fields */ }Expand description
The main trading engine.
Owns:
- Order state (OrderManager)
- Inventory/balance state (InventoryLedger)
- Exchange adapters
- Strategies
- Exchange health state
Implementations§
Source§impl Engine
impl Engine
Sourcepub fn new(config: EngineConfig) -> Self
pub fn new(config: EngineConfig) -> Self
Create an empty engine with the supplied polling/backoff config.
Sourcepub fn register_exchange(&mut self, exchange: Arc<dyn Exchange>)
pub fn register_exchange(&mut self, exchange: Arc<dyn Exchange>)
Register an exchange adapter
Sourcepub fn register_instrument(&mut self, meta: InstrumentMeta)
pub fn register_instrument(&mut self, meta: InstrumentMeta)
Register an instrument
Sourcepub fn register_strategy(&mut self, strategy: Box<dyn Strategy>)
pub fn register_strategy(&mut self, strategy: Box<dyn Strategy>)
Register a strategy
Sourcepub fn dispatch_event(&mut self, event: &Event) -> Vec<Command>
pub fn dispatch_event(&mut self, event: &Event) -> Vec<Command>
Dispatch an event to all strategies.
Returns all commands emitted by strategies while handling this event.
Sourcepub fn update_quote(&mut self, quote: Quote)
pub fn update_quote(&mut self, quote: Quote)
Update quote for an instrument
Sourcepub fn set_exchange_health(
&mut self,
instance: &ExchangeInstance,
health: ExchangeHealth,
)
pub fn set_exchange_health( &mut self, instance: &ExchangeInstance, health: ExchangeHealth, )
Update exchange health
Sourcepub fn start_strategies(&mut self) -> Vec<Command>
pub fn start_strategies(&mut self) -> Vec<Command>
Start all strategies
Sourcepub fn stop_strategies(&mut self) -> Vec<Command>
pub fn stop_strategies(&mut self) -> Vec<Command>
Stop all strategies
Sourcepub fn is_strategy_stopped(&self, strategy_id: &StrategyId) -> bool
pub fn is_strategy_stopped(&self, strategy_id: &StrategyId) -> bool
Check if a strategy has been stopped
Sourcepub fn stopped_strategies(&self) -> &HashSet<StrategyId>
pub fn stopped_strategies(&self) -> &HashSet<StrategyId>
Get all stopped strategies
Sourcepub fn order_manager(&self) -> &OrderManager
pub fn order_manager(&self) -> &OrderManager
Get reference to order manager
Sourcepub fn order_manager_mut(&mut self) -> &mut OrderManager
pub fn order_manager_mut(&mut self) -> &mut OrderManager
Get mutable reference to order manager
Sourcepub fn instrument_meta(
&self,
instrument: &InstrumentId,
) -> Option<&InstrumentMeta>
pub fn instrument_meta( &self, instrument: &InstrumentId, ) -> Option<&InstrumentMeta>
Get instrument metadata (needed for command execution).
Sourcepub fn inventory(&self) -> &InventoryLedger
pub fn inventory(&self) -> &InventoryLedger
Get reference to inventory ledger
Sourcepub fn inventory_mut(&mut self) -> &mut InventoryLedger
pub fn inventory_mut(&mut self) -> &mut InventoryLedger
Get mutable reference to inventory ledger
Sourcepub fn apply_position_fill(
&mut self,
instrument: &InstrumentId,
side: OrderSide,
qty: Qty,
price: Price,
)
pub fn apply_position_fill( &mut self, instrument: &InstrumentId, side: OrderSide, qty: Qty, price: Price, )
Apply a fill to position tracking (for perpetuals).
- Buy fill: increases position (adds qty)
- Sell fill: decreases position (subtracts qty)
- Tracks realized PnL when reducing/closing position
Sourcepub fn apply_fill_fee(&mut self, instrument: &InstrumentId, fee: Decimal)
pub fn apply_fill_fee(&mut self, instrument: &InstrumentId, fee: Decimal)
Apply fee from a fill to position tracking
Sourcepub fn position(&self, instrument: &InstrumentId) -> Position
pub fn position(&self, instrument: &InstrumentId) -> Position
Get current position for an instrument. Automatically computes unrealized_pnl from current quotes if available.
Sourcepub fn get_positions(&self) -> HashMap<InstrumentId, Position>
pub fn get_positions(&self) -> HashMap<InstrumentId, Position>
Get all positions (for testing/WASM/UI snapshots). Computes unrealized_pnl for each position from current quotes.
Sourcepub fn sync_mechanism(&self) -> SyncMechanism
pub fn sync_mechanism(&self) -> SyncMechanism
Get the sync mechanism from the first strategy (assumes all strategies use the same mechanism)
Sourcepub fn apply_snapshot(
&mut self,
instrument: &InstrumentId,
qty: Decimal,
avg_entry_px: Option<Price>,
unrealized_pnl: Option<Decimal>,
)
pub fn apply_snapshot( &mut self, instrument: &InstrumentId, qty: Decimal, avg_entry_px: Option<Price>, unrealized_pnl: Option<Decimal>, )
Apply a position snapshot (absolute update)
Used for snapshot-based synchronization (e.g., clearinghouseState)
Sourcepub fn record_fill(&mut self, fill: OrderFilledEvent)
pub fn record_fill(&mut self, fill: OrderFilledEvent)
Record a fill in the engine’s fill history. This is called when processing OrderFilled events (e.g., from exchange or paper_exchange).
Sourcepub fn get_fills(&self) -> &[OrderFilledEvent]
pub fn get_fills(&self) -> &[OrderFilledEvent]
Get all recorded fills (for backtesting/reporting).
Sourcepub fn clear_fills(&mut self)
pub fn clear_fills(&mut self)
Clear fill history (e.g., on reset).