Skip to main content

FillSimulator

Struct FillSimulator 

Source
pub struct FillSimulator { /* private fields */ }
Expand description

Simulates order fills based on quote price crossing.

Shared logic used by both MockExchange and PaperExchange.

§Optimization

Orders are stored in sorted VecDeques by price (per instrument+side).

  • BUY orders: sorted descending by price (highest first)
  • SELL orders: sorted ascending by price (lowest first)

Using VecDeque allows O(1) pop_front for filling orders, avoiding O(n) shifts.

Implementations§

Source§

impl FillSimulator

Source

pub fn new(initial_balances: HashMap<AssetId, Decimal>) -> Self

Create a simulator with initial balances and no fees.

Source

pub fn new_with_fee( initial_balances: HashMap<AssetId, Decimal>, fee_rate: Decimal, ) -> Self

Create a new fill simulator with a specific fee rate

Source

pub fn set_fee_rate(&mut self, fee_rate: Decimal)

Set the fee rate (0.0004 = 0.04%)

Source

pub fn register_instrument_meta(&mut self, meta: &InstrumentMeta)

Register market metadata so paper/backtest accounting can use the right base/quote assets. Outcome markets quote in USDH, not USDC.

Source

pub fn instrument_is_perp(&self, instrument: &InstrumentId) -> bool

Return true when the instrument is configured or inferred as a perp.

Source

pub fn next_exchange_order_id(&mut self, prefix: &str) -> ExchangeOrderId

Generate next exchange order ID

Source

pub fn add_pending_order(&mut self, order: PendingOrder)

Add a pending order (maintains sorted order for O(log n) lookups)

Source

pub fn remove_order( &mut self, client_id: &ClientOrderId, ) -> Option<PendingOrder>

Remove pending order by client ID

Source

pub fn remove_orders_for_instrument( &mut self, instrument: &InstrumentId, ) -> Vec<PendingOrder>

Remove all pending orders for an instrument

Source

pub fn balance(&self, asset: &AssetId) -> Decimal

Get current balance

Source

pub fn set_balance(&mut self, asset: AssetId, amount: Decimal)

Set balance directly

Source

pub fn check_fills( &mut self, quotes: &HashMap<InstrumentId, Quote>, time_ms: i64, ) -> Vec<SimulatedFill>

Check pending orders against quotes and generate fills.

§Optimization

Orders are grouped by (instrument, side) and sorted by price. For each quote, we use binary search to find orders that could fill:

  • BUY orders fill when ask <= order.price (scan from highest price down)
  • SELL orders fill when bid >= order.price (scan from lowest price up)

This reduces complexity from O(orders) to O(fillable_orders).

Source

pub fn check_balance( &self, instrument: &InstrumentId, side: OrderSide, price: Decimal, qty: Decimal, ) -> Result<(), String>

Check if balance is sufficient for an order

Source

pub fn apply_fill(&mut self, fill: &Fill)

Apply a known fill to simulated spot-like balances.

IOC fills are constructed by PaperExchange directly, while resting fills come through check_fills.

Source

pub fn pending_orders_count(&self) -> usize

Get pending orders count (sum across all groups)

Source

pub fn pending_orders(&self) -> Vec<&PendingOrder>

Get all pending orders (for inspection) - flattened from all groups

Trait Implementations§

Source§

impl Default for FillSimulator

Source§

fn default() -> Self

Returns the “default value” for a type. 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.

Source§

impl<T> Instrument for T

Source§

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

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

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> PolicyExt for T
where T: ?Sized,

Source§

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

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

Source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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