Skip to main content

SimulatedExchange

Struct SimulatedExchange 

Source
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: Venue

The venue identifier.

§oms_type: OmsType

The order management system type.

§account_type: AccountType

The account type for the venue.

§base_currency: Option<Currency>

The optional base currency for single-currency accounts.

Implementations§

Source§

impl SimulatedExchange

Source

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_balances is empty.
  • base_currency is Some but starting_balances contains multiple currencies.
Source

pub fn register_client(&mut self, client: Rc<dyn ExecutionClient>)

Registers the execution client for the exchange.

Source

pub fn set_fill_model(&mut self, fill_model: FillModelAny)

Sets the fill model for the exchange.

Source

pub fn set_latency_model(&mut self, latency_model: Box<dyn LatencyModel>)

Sets the latency model for the exchange.

Source

pub fn set_settlement_price( &mut self, instrument_id: InstrumentId, price: Price, )

Sets the settlement price for the given instrument.

Source

pub const fn book_type(&self) -> BookType

Returns the configured book type for this venue.

Source

pub fn instrument_ids(&self) -> impl Iterator<Item = &InstrumentId>

Returns an iterator over the instrument IDs registered with this exchange.

Source

pub fn initialize_account(&mut self)

Source

pub fn load_open_orders(&mut self)

Loads non-emulated open orders from the cache into matching engines.

Source

pub fn add_instrument(&mut self, instrument: InstrumentAny) -> Result<()>

Adds an instrument to the simulated exchange and initializes its matching engine.

§Errors

Returns an error if the exchange account type is Cash and the instrument is a CryptoPerpetual or CryptoFuture.

§Panics

Panics if the instrument cannot be added to the exchange.

Source

pub fn best_bid_price(&self, instrument_id: InstrumentId) -> Option<Price>

Returns the best bid price for the given instrument, if available.

Source

pub fn best_ask_price(&self, instrument_id: InstrumentId) -> Option<Price>

Returns the best ask price for the given instrument, if available.

Source

pub fn get_book(&self, instrument_id: InstrumentId) -> Option<&OrderBook>

Returns a reference to the order book for the given instrument, if available.

Source

pub fn get_matching_engine( &self, instrument_id: &InstrumentId, ) -> Option<&OrderMatchingEngine>

Returns a reference to the matching engine for the given instrument, if available.

Source

pub const fn get_matching_engines( &self, ) -> &AHashMap<InstrumentId, OrderMatchingEngine>

Returns a reference to all matching engines keyed by instrument ID.

Source

pub fn get_books(&self) -> AHashMap<InstrumentId, OrderBook>

Returns all order books keyed by instrument ID.

Source

pub fn get_open_orders( &self, instrument_id: Option<InstrumentId>, ) -> Vec<OrderMatchInfo>

Returns all open orders, optionally filtered by instrument ID.

Source

pub fn get_open_bid_orders( &self, instrument_id: Option<InstrumentId>, ) -> Vec<OrderMatchInfo>

Returns all open bid orders, optionally filtered by instrument ID.

Source

pub fn get_open_ask_orders( &self, instrument_id: Option<InstrumentId>, ) -> Vec<OrderMatchInfo>

Returns all open ask orders, optionally filtered by instrument ID.

Source

pub fn get_account(&self) -> Option<AccountAny>

Returns the account for this exchange, if an execution client is registered.

Source

pub fn cache(&self) -> &Rc<RefCell<Cache>>

Returns a reference to the cache.

Source

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.

Source

pub fn has_pending_commands(&self, ts_now: UnixNanos) -> bool

Returns whether there are pending commands at or before ts_now.

Source

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.

Source

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

Source

pub fn send(&mut self, command: TradingCommand)

Sends a trading command to the exchange for processing.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn reset(&mut self)

Resets the exchange to its initial state.

Source

pub fn log_diagnostics(&self)

Logs diagnostic information from all simulation modules.

Trait Implementations§

Source§

impl Debug for SimulatedExchange

Source§

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

Formats the value using the given formatter. Read more

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: 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
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: 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> Same for T

Source§

type Output = T

Should always be Self
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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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