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 comprehensive 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
§oms_type: OmsType
§account_type: AccountType
§base_currency: Option<Currency>
Implementations§
Source§impl SimulatedExchange
impl SimulatedExchange
Sourcepub fn new(
venue: Venue,
oms_type: OmsType,
account_type: AccountType,
starting_balances: Vec<Money>,
base_currency: Option<Currency>,
default_leverage: Decimal,
leverages: HashMap<InstrumentId, Decimal>,
modules: Vec<Box<dyn SimulationModule>>,
cache: Rc<RefCell<Cache>>,
clock: Rc<RefCell<dyn Clock>>,
fill_model: FillModel,
fee_model: FeeModelAny,
book_type: BookType,
latency_model: Option<LatencyModel>,
frozen_account: Option<bool>,
bar_execution: Option<bool>,
reject_stop_orders: Option<bool>,
support_gtd_orders: Option<bool>,
support_contingent_orders: Option<bool>,
use_position_ids: Option<bool>,
use_random_ids: Option<bool>,
use_reduce_only: Option<bool>,
use_message_queue: Option<bool>,
) -> Result<Self>
pub fn new( venue: Venue, oms_type: OmsType, account_type: AccountType, starting_balances: Vec<Money>, base_currency: Option<Currency>, default_leverage: Decimal, leverages: HashMap<InstrumentId, Decimal>, modules: Vec<Box<dyn SimulationModule>>, cache: Rc<RefCell<Cache>>, clock: Rc<RefCell<dyn Clock>>, fill_model: FillModel, fee_model: FeeModelAny, book_type: BookType, latency_model: Option<LatencyModel>, frozen_account: Option<bool>, bar_execution: Option<bool>, reject_stop_orders: Option<bool>, support_gtd_orders: Option<bool>, support_contingent_orders: Option<bool>, use_position_ids: Option<bool>, use_random_ids: Option<bool>, use_reduce_only: Option<bool>, use_message_queue: Option<bool>, ) -> Result<Self>
Creates a new SimulatedExchange
instance.
§Errors
Returns an error if:
starting_balances
is empty.base_currency
isSome
butstarting_balances
contains multiple currencies.
pub fn register_client(&mut self, client: Rc<dyn ExecutionClient>)
pub fn set_fill_model(&mut self, fill_model: FillModel)
pub const fn set_latency_model(&mut self, latency_model: LatencyModel)
pub fn initialize_account(&mut self)
Sourcepub fn add_instrument(&mut self, instrument: InstrumentAny) -> Result<()>
pub fn add_instrument(&mut self, instrument: InstrumentAny) -> Result<()>
pub fn best_bid_price(&self, instrument_id: InstrumentId) -> Option<Price>
pub fn best_ask_price(&self, instrument_id: InstrumentId) -> Option<Price>
pub fn get_book(&self, instrument_id: InstrumentId) -> Option<&OrderBook>
pub fn get_matching_engine( &self, instrument_id: &InstrumentId, ) -> Option<&OrderMatchingEngine>
pub const fn get_matching_engines( &self, ) -> &HashMap<InstrumentId, OrderMatchingEngine>
pub fn get_books(&self) -> HashMap<InstrumentId, OrderBook>
pub fn get_open_orders( &self, instrument_id: Option<InstrumentId>, ) -> Vec<PassiveOrderAny>
pub fn get_open_bid_orders( &self, instrument_id: Option<InstrumentId>, ) -> Vec<PassiveOrderAny>
pub fn get_open_ask_orders( &self, instrument_id: Option<InstrumentId>, ) -> Vec<PassiveOrderAny>
Sourcepub fn get_account(&self) -> Option<AccountAny>
pub fn get_account(&self) -> Option<AccountAny>
§Panics
Panics if retrieving the account from the execution client fails.
Sourcepub fn adjust_account(&mut self, adjustment: Money)
pub fn adjust_account(&mut self, adjustment: Money)
§Panics
Panics if generating account state fails during adjustment.
pub fn send(&mut self, command: TradingCommand)
Sourcepub fn generate_inflight_command(
&mut self,
command: &TradingCommand,
) -> (UnixNanos, u32)
pub fn generate_inflight_command( &mut self, command: &TradingCommand, ) -> (UnixNanos, u32)
§Panics
Panics if the command is invalid when generating inflight command.
Sourcepub fn process_order_book_delta(&mut self, delta: OrderBookDelta)
pub fn process_order_book_delta(&mut self, delta: OrderBookDelta)
§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)
§Panics
Panics if adding a missing instrument during deltas processing fails.
Sourcepub fn process_quote_tick(&mut self, quote: &QuoteTick)
pub fn process_quote_tick(&mut self, quote: &QuoteTick)
§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)
§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)
§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)
§Panics
Panics if adding a missing instrument during instrument status processing fails.
Sourcepub fn process(&mut self, ts_now: UnixNanos)
pub fn process(&mut self, ts_now: UnixNanos)
§Panics
Panics if popping an inflight command fails during processing.
pub fn reset(&mut self)
Sourcepub fn process_trading_command(&mut self, command: TradingCommand)
pub fn process_trading_command(&mut self, command: TradingCommand)
§Panics
Panics if execution client is uninitialized when processing trading command.
Sourcepub fn generate_fresh_account_state(&self)
pub fn generate_fresh_account_state(&self)
§Panics
Panics if generating fresh account state fails.
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 !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