Skip to main content

TradeEngine

Struct TradeEngine 

Source
pub struct TradeEngine {
    pub manager: PositionManager,
    pub fill_model: FillModel,
    /* private fields */
}
Expand description

The core trade engine.

Pure logic — no async, no IO. Takes inputs, returns effects.

Fields§

§manager: PositionManager§fill_model: FillModel

How fill conditions and rule triggers interpret price quotes.

Defaults to FillModel::BidAsk (the most realistic model).

Implementations§

Source§

impl TradeEngine

Source

pub fn new() -> Self

Standard engine (tick-by-tick, no alert register). Best for backtesting.

Source

pub fn with_fill_model(fill_model: FillModel) -> Self

Create a new engine with a specific fill model.

Source

pub fn with_fill_model_and_deterministic_ids(fill_model: FillModel) -> Self

Create a backtest engine whose position IDs are stable across runs.

Source

pub fn with_alert_register() -> Self

Engine with alert register for indexed evaluation. Best for real-time with many positions.

Source

pub fn with_alert_register_and_fill_model(fill_model: FillModel) -> Self

Engine with both alert register and custom fill model.

Source

pub fn last_quote(&self, symbol: &str) -> Option<&PriceQuote>

Last known quote for a symbol.

Source

pub fn get_position(&self, id: &str) -> Option<&Position>

Convenience: get a position by id.

Source

pub fn open_positions(&self) -> Vec<&Position>

All currently open positions.

Source

pub fn pending_positions(&self) -> Vec<&Position>

All currently pending positions.

Source

pub fn closed_positions(&self) -> Vec<&Position>

All closed positions still tracked by the manager.

Source

pub fn on_price(&mut self, quote: &PriceQuote) -> Vec<Effect>

Feed a new price quote into the engine.

  1. Stores the quote as the last known price for the symbol.
  2. Checks all pending positions on that symbol for fill conditions.
  3. Evaluates management rules for all open positions on that symbol.
  4. Applies resulting effects to internal state.
  5. Returns all effects for the caller to process externally.
Source

pub fn on_price_future_quote(&mut self, quote: &PriceQuote) -> Vec<Effect>

Future-quote tick processing with no retroactive rule evaluation.

This compatibility adapter uses FutureQuoteV1 without slippage. The configurable backtest path uses Self::on_price_future_effects_priced.

Source

pub fn on_price_future_effects( &mut self, quote: &PriceQuote, ) -> Vec<FutureEffect>

Compatibility FutureQuote adapter using FutureQuoteV1 without slippage.

Source

pub fn begin_on_price_future_effects_priced( &mut self, quote: &PriceQuote, prepared_pending: &[PreparedPendingFill], pricer: &ExecutionPricer, pip_size: f64, ) -> FutureApplyResult<FutureEngineTransaction>

Apply one quote in place and retain a rollback token for external processing.

Source

pub fn stage_on_price_future_effects_priced( &self, quote: &PriceQuote, prepared_pending: &[PreparedPendingFill], pricer: &ExecutionPricer, pip_size: f64, ) -> FutureApplyResult<(Self, Vec<FutureEffect>)>

Build a disposable next-engine state for one FutureQuote settlement. The caller can run accounting against it and commit only after success.

Source

pub fn on_price_future_effects_priced( &mut self, quote: &PriceQuote, prepared_pending: &[PreparedPendingFill], pricer: &ExecutionPricer, pip_size: f64, ) -> FutureApplyResult<Vec<FutureEffect>>

Atomically settle one quote with already-priced pending entries and engine-priced rule exits.

Source

pub fn set_pending_size(&mut self, position_id: &str, size: f64) -> Result<()>

Update the intended size of a still-pending order before its fill check.

Source

pub fn synchronize_latest_fill( &mut self, position_id: &str, fill: Fill, ) -> Result<()>

Synchronize a core entry fill with an externally calculated execution.

Source

pub fn close_position_with_reason( &mut self, position_id: &str, reason: CloseReason, ts: NaiveDateTime, ) -> Result<Vec<Effect>>

Close one open position with an explicit authoritative reason. This is used by deterministic end-of-data liquidation; normal callers should continue using Action::ClosePosition.

Source

pub fn apply_action( &mut self, action: Action, ts: NaiveDateTime, ) -> Result<Vec<Effect>>

Process a trading action and return effects.

This is the primary way to interact with the engine from strategies, signal providers, or manual input.

Source

pub fn begin_future_action( &mut self, action: Action, effective_ts: NaiveDateTime, ) -> FutureApplyResult<FutureEngineTransaction>

Apply one non-fill FutureQuote action in place with rollback support.

Source

pub fn apply_future_action( &mut self, action: Action, effective_ts: NaiveDateTime, ) -> FutureApplyResult<Vec<FutureEffect>>

Apply a FutureQuote action that does not create a fill. Market opens, scale-ins, and closes must use Self::apply_priced_future_action.

Source

pub fn begin_priced_future_action( &mut self, action: Action, quote: &PriceQuote, execution: ExecutionFill, ) -> FutureApplyResult<FutureEngineTransaction>

Apply one priced FutureQuote action in place with rollback support.

Source

pub fn apply_priced_future_action( &mut self, action: Action, quote: &PriceQuote, execution: ExecutionFill, ) -> FutureApplyResult<Vec<FutureEffect>>

Apply one already-priced FutureQuote market action. The supplied execution is used directly for both core state and the returned effect.

Source

pub fn close_position_with_reason_future( &mut self, position_id: &str, reason: CloseReason, quote: &PriceQuote, execution: ExecutionFill, ) -> FutureApplyResult<Vec<FutureEffect>>

Close with an explicit reason using one previously priced market exit.

Source

pub fn begin_close_position_with_reason_future_at( &mut self, position_id: &str, reason: CloseReason, quote: &PriceQuote, execution: ExecutionFill, execution_ts: NaiveDateTime, ) -> FutureApplyResult<FutureEngineTransaction>

Close in place with separate timestamps and retain rollback support.

Source

pub fn close_position_with_reason_future_at( &mut self, position_id: &str, reason: CloseReason, quote: &PriceQuote, execution: ExecutionFill, execution_ts: NaiveDateTime, ) -> FutureApplyResult<Vec<FutureEffect>>

Close with separate execution and source-quote timestamps.

Trait Implementations§

Source§

impl Clone for TradeEngine

Source§

fn clone(&self) -> TradeEngine

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TradeEngine

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TradeEngine

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PositionResolver for TradeEngine

Source§

fn resolve(&self, position: &PositionRef) -> Vec<PositionId> ⓘ

Resolve a position reference to zero or more concrete position IDs.
Source§

fn position_entry_info(&self, id: &PositionId) -> Option<(f64, Side)>

Get entry info (average_entry, side) for a position.

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V