pub struct SimulatedExchange {
pub id: Venue,
pub oms_type: OmsType,
pub account_type: AccountType,
pub base_currency: Option<Currency>,
/* private fields */
}Expand description
Simulated exchange venue for realistic trading execution during backtesting.
The SimulatedExchange provides a simulation of a trading venue,
including order matching engines, account management, and realistic execution
models. It maintains order books, processes market data, and executes trades
with configurable latency and fill models to accurately simulate real market
conditions during backtesting.
Key features:
- Multi-instrument order matching with realistic execution
- Configurable fee, fill, and latency models
- Support for various order types and execution options
- Account balance and position management
- Market data processing and order book maintenance
- Simulation modules for custom venue behaviors
Fields§
§id: VenueThe venue identifier.
oms_type: OmsTypeThe order management system type.
account_type: AccountTypeThe account type for the venue.
base_currency: Option<Currency>The optional base currency for single-currency accounts.
Implementations§
Source§impl SimulatedExchange
impl SimulatedExchange
Sourcepub fn new(
config: SimulatedVenueConfig,
cache: Rc<RefCell<Cache>>,
clock: Rc<RefCell<dyn Clock>>,
) -> Result<Self>
pub fn new( config: SimulatedVenueConfig, cache: Rc<RefCell<Cache>>, clock: Rc<RefCell<dyn Clock>>, ) -> Result<Self>
Creates a new SimulatedExchange instance from a venue configuration.
§Errors
Returns an error if:
starting_balancesis empty.base_currencyisSomebutstarting_balancescontains multiple currencies.
Sourcepub fn register_client(&mut self, client: Rc<dyn ExecutionClient>)
pub fn register_client(&mut self, client: Rc<dyn ExecutionClient>)
Registers the execution client for the exchange.
Sourcepub fn set_fill_model(&mut self, fill_model: FillModelAny)
pub fn set_fill_model(&mut self, fill_model: FillModelAny)
Sets the fill model for the exchange.
Sourcepub fn set_latency_model(&mut self, latency_model: Box<dyn LatencyModel>)
pub fn set_latency_model(&mut self, latency_model: Box<dyn LatencyModel>)
Sets the latency model for the exchange.
Sourcepub fn set_settlement_price(
&mut self,
instrument_id: InstrumentId,
price: Price,
)
pub fn set_settlement_price( &mut self, instrument_id: InstrumentId, price: Price, )
Sets the settlement price for the given instrument.
Sourcepub fn instrument_ids(&self) -> impl Iterator<Item = &InstrumentId>
pub fn instrument_ids(&self) -> impl Iterator<Item = &InstrumentId>
Returns an iterator over the instrument IDs registered with this exchange.
pub fn initialize_account(&mut self)
Sourcepub fn load_open_orders(&mut self)
pub fn load_open_orders(&mut self)
Loads non-emulated open orders from the cache into matching engines.
Sourcepub fn add_instrument(&mut self, instrument: InstrumentAny) -> Result<()>
pub fn add_instrument(&mut self, instrument: InstrumentAny) -> Result<()>
Sourcepub fn best_bid_price(&self, instrument_id: InstrumentId) -> Option<Price>
pub fn best_bid_price(&self, instrument_id: InstrumentId) -> Option<Price>
Returns the best bid price for the given instrument, if available.
Sourcepub fn best_ask_price(&self, instrument_id: InstrumentId) -> Option<Price>
pub fn best_ask_price(&self, instrument_id: InstrumentId) -> Option<Price>
Returns the best ask price for the given instrument, if available.
Sourcepub fn get_book(&self, instrument_id: InstrumentId) -> Option<&OrderBook>
pub fn get_book(&self, instrument_id: InstrumentId) -> Option<&OrderBook>
Returns a reference to the order book for the given instrument, if available.
Sourcepub fn get_matching_engine(
&self,
instrument_id: &InstrumentId,
) -> Option<&OrderMatchingEngine>
pub fn get_matching_engine( &self, instrument_id: &InstrumentId, ) -> Option<&OrderMatchingEngine>
Returns a reference to the matching engine for the given instrument, if available.
Sourcepub const fn get_matching_engines(
&self,
) -> &AHashMap<InstrumentId, OrderMatchingEngine>
pub const fn get_matching_engines( &self, ) -> &AHashMap<InstrumentId, OrderMatchingEngine>
Returns a reference to all matching engines keyed by instrument ID.
Sourcepub fn get_books(&self) -> AHashMap<InstrumentId, OrderBook>
pub fn get_books(&self) -> AHashMap<InstrumentId, OrderBook>
Returns all order books keyed by instrument ID.
Sourcepub fn get_open_orders(
&self,
instrument_id: Option<InstrumentId>,
) -> Vec<OrderMatchInfo>
pub fn get_open_orders( &self, instrument_id: Option<InstrumentId>, ) -> Vec<OrderMatchInfo>
Returns all open orders, optionally filtered by instrument ID.
Sourcepub fn get_open_bid_orders(
&self,
instrument_id: Option<InstrumentId>,
) -> Vec<OrderMatchInfo>
pub fn get_open_bid_orders( &self, instrument_id: Option<InstrumentId>, ) -> Vec<OrderMatchInfo>
Returns all open bid orders, optionally filtered by instrument ID.
Sourcepub fn get_open_ask_orders(
&self,
instrument_id: Option<InstrumentId>,
) -> Vec<OrderMatchInfo>
pub fn get_open_ask_orders( &self, instrument_id: Option<InstrumentId>, ) -> Vec<OrderMatchInfo>
Returns all open ask orders, optionally filtered by instrument ID.
Sourcepub fn get_account(&self) -> Option<AccountAny>
pub fn get_account(&self) -> Option<AccountAny>
Returns the account for this exchange, if an execution client is registered.
Sourcepub fn adjust_account(&mut self, adjustment: Money)
pub fn adjust_account(&mut self, adjustment: Money)
Adjusts the account balance by the given amount.
§Panics
Panics if generating account state fails during adjustment.
Sourcepub fn has_pending_commands(&self, ts_now: UnixNanos) -> bool
pub fn has_pending_commands(&self, ts_now: UnixNanos) -> bool
Returns whether there are pending commands at or before ts_now.
Sourcepub fn iterate_matching_engines(&mut self, ts_now: UnixNanos)
pub fn iterate_matching_engines(&mut self, ts_now: UnixNanos)
Iterates all matching engines so newly submitted orders can match against the current market state.
Sourcepub fn set_clock_time(&self, ts_now: UnixNanos)
pub fn set_clock_time(&self, ts_now: UnixNanos)
Advances the exchange clock to the given timestamp so that any event generators (modules, account state) see the correct time even when no commands are pending.
§Panics
Panics if the clock is not a [TestClock].
Sourcepub fn send(&mut self, command: TradingCommand)
pub fn send(&mut self, command: TradingCommand)
Sends a trading command to the exchange for processing.
Sourcepub fn process_order_book_delta(&mut self, delta: OrderBookDelta)
pub fn process_order_book_delta(&mut self, delta: OrderBookDelta)
Processes a single order book delta.
§Panics
Panics if adding a missing instrument during delta processing fails.
Sourcepub fn process_order_book_deltas(&mut self, deltas: &OrderBookDeltas)
pub fn process_order_book_deltas(&mut self, deltas: &OrderBookDeltas)
Processes a batch of order book deltas.
§Panics
Panics if adding a missing instrument during deltas processing fails.
Sourcepub fn process_order_book_depth10(&mut self, depth: &OrderBookDepth10)
pub fn process_order_book_depth10(&mut self, depth: &OrderBookDepth10)
Processes an L2 order book depth snapshot.
§Panics
Panics if adding a missing instrument during depth10 processing fails.
Sourcepub fn process_quote_tick(&mut self, quote: &QuoteTick)
pub fn process_quote_tick(&mut self, quote: &QuoteTick)
Processes a quote tick and updates the matching engine.
§Panics
Panics if adding a missing instrument during quote tick processing fails.
Sourcepub fn process_trade_tick(&mut self, trade: &TradeTick)
pub fn process_trade_tick(&mut self, trade: &TradeTick)
Processes a trade tick and updates the matching engine.
§Panics
Panics if adding a missing instrument during trade tick processing fails.
Sourcepub fn process_bar(&mut self, bar: Bar)
pub fn process_bar(&mut self, bar: Bar)
Processes a bar and updates the matching engine.
§Panics
Panics if adding a missing instrument during bar processing fails.
Sourcepub fn process_instrument_status(&mut self, status: InstrumentStatus)
pub fn process_instrument_status(&mut self, status: InstrumentStatus)
Processes an instrument status update.
§Panics
Panics if adding a missing instrument during instrument status processing fails.
Sourcepub fn process_instrument_close(&mut self, close: InstrumentClose)
pub fn process_instrument_close(&mut self, close: InstrumentClose)
Processes an instrument close event.
§Panics
Panics if adding a missing instrument during instrument close processing fails.
Sourcepub fn process(&mut self, ts_now: UnixNanos)
pub fn process(&mut self, ts_now: UnixNanos)
Processes all pending inflight and queued trading commands up to ts_now.
§Panics
Panics if popping an inflight command fails during processing.
Sourcepub fn process_modules(&mut self, ts_now: UnixNanos)
pub fn process_modules(&mut self, ts_now: UnixNanos)
Runs all simulation modules for the given timestamp.
Must be called once per time step after all command queues have fully settled, not inside the settle loop.
Sourcepub fn log_diagnostics(&self)
pub fn log_diagnostics(&self)
Logs diagnostic information from all simulation modules.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for SimulatedExchange
impl !RefUnwindSafe for SimulatedExchange
impl !Send for SimulatedExchange
impl !Sync for SimulatedExchange
impl Unpin for SimulatedExchange
impl UnsafeUnpin for SimulatedExchange
impl !UnwindSafe for SimulatedExchange
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more