Skip to main content

Engine

Struct Engine 

Source
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

Source

pub fn new(config: EngineConfig) -> Self

Create an empty engine with the supplied polling/backoff config.

Source

pub fn register_exchange(&mut self, exchange: Arc<dyn Exchange>)

Register an exchange adapter

Source

pub fn register_instrument(&mut self, meta: InstrumentMeta)

Register an instrument

Source

pub fn register_strategy(&mut self, strategy: Box<dyn Strategy>)

Register a strategy

Source

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.

Source

pub fn update_quote(&mut self, quote: Quote)

Update quote for an instrument

Source

pub fn set_exchange_health( &mut self, instance: &ExchangeInstance, health: ExchangeHealth, )

Update exchange health

Source

pub fn start_strategies(&mut self) -> Vec<Command>

Start all strategies

Source

pub fn stop_strategies(&mut self) -> Vec<Command>

Stop all strategies

Source

pub fn is_strategy_stopped(&self, strategy_id: &StrategyId) -> bool

Check if a strategy has been stopped

Source

pub fn stopped_strategies(&self) -> &HashSet<StrategyId>

Get all stopped strategies

Source

pub fn order_manager(&self) -> &OrderManager

Get reference to order manager

Source

pub fn order_manager_mut(&mut self) -> &mut OrderManager

Get mutable reference to order manager

Source

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

Get instrument metadata (needed for command execution).

Source

pub fn inventory(&self) -> &InventoryLedger

Get reference to inventory ledger

Source

pub fn inventory_mut(&mut self) -> &mut InventoryLedger

Get mutable reference to inventory ledger

Source

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
Source

pub fn apply_fill_fee(&mut self, instrument: &InstrumentId, fee: Decimal)

Apply fee from a fill to position tracking

Source

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

Get current position for an instrument. Automatically computes unrealized_pnl from current quotes if available.

Source

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.

Source

pub fn sync_mechanism(&self) -> SyncMechanism

Get the sync mechanism from the first strategy (assumes all strategies use the same mechanism)

Source

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)

Source

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).

Source

pub fn get_fills(&self) -> &[OrderFilledEvent]

Get all recorded fills (for backtesting/reporting).

Source

pub fn clear_fills(&mut self)

Clear fill history (e.g., on reset).

Auto Trait Implementations§

§

impl !RefUnwindSafe for Engine

§

impl !Sync for Engine

§

impl !UnwindSafe for Engine

§

impl Freeze for Engine

§

impl Send for Engine

§

impl Unpin for Engine

§

impl UnsafeUnpin for Engine

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more