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: FillModelHow fill conditions and rule triggers interpret price quotes.
Defaults to FillModel::BidAsk (the most realistic model).
Implementations§
Source§impl TradeEngine
impl TradeEngine
Sourcepub fn with_fill_model(fill_model: FillModel) -> Self
pub fn with_fill_model(fill_model: FillModel) -> Self
Create a new engine with a specific fill model.
Sourcepub fn with_fill_model_and_deterministic_ids(fill_model: FillModel) -> Self
pub fn with_fill_model_and_deterministic_ids(fill_model: FillModel) -> Self
Create a backtest engine whose position IDs are stable across runs.
Sourcepub fn with_alert_register() -> Self
pub fn with_alert_register() -> Self
Engine with alert register for indexed evaluation. Best for real-time with many positions.
Sourcepub fn with_alert_register_and_fill_model(fill_model: FillModel) -> Self
pub fn with_alert_register_and_fill_model(fill_model: FillModel) -> Self
Engine with both alert register and custom fill model.
Sourcepub fn last_quote(&self, symbol: &str) -> Option<&PriceQuote>
pub fn last_quote(&self, symbol: &str) -> Option<&PriceQuote>
Last known quote for a symbol.
Sourcepub fn get_position(&self, id: &str) -> Option<&Position>
pub fn get_position(&self, id: &str) -> Option<&Position>
Convenience: get a position by id.
Sourcepub fn open_positions(&self) -> Vec<&Position>
pub fn open_positions(&self) -> Vec<&Position>
All currently open positions.
Sourcepub fn pending_positions(&self) -> Vec<&Position>
pub fn pending_positions(&self) -> Vec<&Position>
All currently pending positions.
Sourcepub fn closed_positions(&self) -> Vec<&Position>
pub fn closed_positions(&self) -> Vec<&Position>
All closed positions still tracked by the manager.
Sourcepub fn on_price(&mut self, quote: &PriceQuote) -> Vec<Effect>
pub fn on_price(&mut self, quote: &PriceQuote) -> Vec<Effect>
Feed a new price quote into the engine.
- Stores the quote as the last known price for the symbol.
- Checks all pending positions on that symbol for fill conditions.
- Evaluates management rules for all open positions on that symbol.
- Applies resulting effects to internal state.
- Returns all effects for the caller to process externally.
Sourcepub fn on_price_future_quote(&mut self, quote: &PriceQuote) -> Vec<Effect>
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.
Sourcepub fn on_price_future_effects(
&mut self,
quote: &PriceQuote,
) -> Vec<FutureEffect>
pub fn on_price_future_effects( &mut self, quote: &PriceQuote, ) -> Vec<FutureEffect>
Compatibility FutureQuote adapter using FutureQuoteV1 without slippage.
Sourcepub fn begin_on_price_future_effects_priced(
&mut self,
quote: &PriceQuote,
prepared_pending: &[PreparedPendingFill],
pricer: &ExecutionPricer,
pip_size: f64,
) -> FutureApplyResult<FutureEngineTransaction>
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.
Sourcepub fn stage_on_price_future_effects_priced(
&self,
quote: &PriceQuote,
prepared_pending: &[PreparedPendingFill],
pricer: &ExecutionPricer,
pip_size: f64,
) -> FutureApplyResult<(Self, Vec<FutureEffect>)>
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.
Sourcepub fn on_price_future_effects_priced(
&mut self,
quote: &PriceQuote,
prepared_pending: &[PreparedPendingFill],
pricer: &ExecutionPricer,
pip_size: f64,
) -> FutureApplyResult<Vec<FutureEffect>>
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.
Sourcepub fn set_pending_size(&mut self, position_id: &str, size: f64) -> Result<()>
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.
Sourcepub fn synchronize_latest_fill(
&mut self,
position_id: &str,
fill: Fill,
) -> Result<()>
pub fn synchronize_latest_fill( &mut self, position_id: &str, fill: Fill, ) -> Result<()>
Synchronize a core entry fill with an externally calculated execution.
Sourcepub fn close_position_with_reason(
&mut self,
position_id: &str,
reason: CloseReason,
ts: NaiveDateTime,
) -> Result<Vec<Effect>>
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.
Sourcepub fn apply_action(
&mut self,
action: Action,
ts: NaiveDateTime,
) -> Result<Vec<Effect>>
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.
Sourcepub fn begin_future_action(
&mut self,
action: Action,
effective_ts: NaiveDateTime,
) -> FutureApplyResult<FutureEngineTransaction>
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.
Sourcepub fn apply_future_action(
&mut self,
action: Action,
effective_ts: NaiveDateTime,
) -> FutureApplyResult<Vec<FutureEffect>>
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.
Sourcepub fn begin_priced_future_action(
&mut self,
action: Action,
quote: &PriceQuote,
execution: ExecutionFill,
) -> FutureApplyResult<FutureEngineTransaction>
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.
Sourcepub fn apply_priced_future_action(
&mut self,
action: Action,
quote: &PriceQuote,
execution: ExecutionFill,
) -> FutureApplyResult<Vec<FutureEffect>>
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.
Sourcepub fn close_position_with_reason_future(
&mut self,
position_id: &str,
reason: CloseReason,
quote: &PriceQuote,
execution: ExecutionFill,
) -> FutureApplyResult<Vec<FutureEffect>>
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.
Sourcepub fn begin_close_position_with_reason_future_at(
&mut self,
position_id: &str,
reason: CloseReason,
quote: &PriceQuote,
execution: ExecutionFill,
execution_ts: NaiveDateTime,
) -> FutureApplyResult<FutureEngineTransaction>
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.
Sourcepub fn close_position_with_reason_future_at(
&mut self,
position_id: &str,
reason: CloseReason,
quote: &PriceQuote,
execution: ExecutionFill,
execution_ts: NaiveDateTime,
) -> FutureApplyResult<Vec<FutureEffect>>
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
impl Clone for TradeEngine
Source§fn clone(&self) -> TradeEngine
fn clone(&self) -> TradeEngine
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more