Skip to main content

OrderBookSide

Struct OrderBookSide 

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

One side of a level 2 book.

Levels are kept best first — descending for bids, ascending for asks — so the best level, the n-th level and the depth cut are all index arithmetic.

Implementations§

Source§

impl OrderBookSide

Source

pub fn len(&self) -> usize

The number of levels held.

Source

pub fn is_empty(&self) -> bool

Whether the side holds no levels.

Source

pub fn max_depth(&self) -> Option<usize>

The level cap this side was built with, or None when uncapped.

Source

pub fn iter(&self) -> Iter<'_, PriceAmount> ⓘ

Iterate every level, best first.

Source

pub fn find(&self, price: Dec) -> Option<&PriceAmount>

The level at exactly price, if the side holds one.

Keyed by price, where OrderBookSide::at is keyed by depth.

Source

pub fn best(&self) -> Option<&PriceAmount>

The best level: highest bid, lowest ask.

Source

pub fn best_price(&self) -> Option<Dec>

The price of the best level.

Source

pub fn worst(&self) -> Option<&PriceAmount>

The worst level held, which is the deepest one, not the worst possible.

Source

pub fn worst_price(&self) -> Option<Dec>

The price of the worst level held.

Source

pub fn at(&self, n: usize) -> Option<&PriceAmount>

The n-th level from the best, counting from zero.

Source

pub fn price_at(&self, n: usize) -> Option<Dec>

The price of the n-th level from the best.

Source

pub fn set(&mut self, entry: PriceAmount) -> Option<Dec>

Set a level, returning the amount it replaced. A zero amount removes it.

A level failing PriceAmount::is_valid is rejected and the book left untouched. None therefore covers four cases: the level was inserted, it was rejected as invalid, it was refused for sitting past max_depth, or a zero amount asked to remove a price that was absent.

Source

pub fn set_price_amount(&mut self, price: Dec, amount: Dec) -> Option<Dec>

OrderBookSide::set taking the price and amount separately.

Source

pub fn trim(&mut self, depth: usize)

Retain at most depth levels, dropping the worst ones beyond that.

Source

pub fn range(&self, low: Dec, high: Dec) -> Iter<'_, PriceAmount> ⓘ

Iterate levels within [low, high] in best-first order.

An empty iterator when no level falls inside, low above high included.

Source

pub fn volume_at(&self, level: usize) -> Option<Dec>

Get the volume up to a given level in the orderbook side

Source

pub fn price_for_quantity(&self, quantity: f64) -> Option<f64>

Volume weighted price paid to fill quantity, or None if the side is too thin.

Source

pub fn amount_mean(&self) -> Option<f64>

Mean level amount, or None when the side is empty.

Summed on demand rather than kept as a running total. A running one would have to be corrected on every set, which is the hot path, to save an addition per level here, which is not; and one non-finite amount would poison it for good, since the subtraction that removes the level cannot take a NaN back out again.

Source

pub fn amount_std_dev(&self) -> Option<f64>

Population standard deviation of the level amounts, or None when the side is empty.

Computed from the levels rather than from a running sum of squares. A running one drifts: adding and removing a level far larger than the rest cancels badly enough to report no dispersion at all on a book that has some, and the clamp that hid it turned corruption into a plausible number. The mean comes from the exact Dec total, so only the deviations are floating point, and their squares cannot go negative.

Two passes, then: the mean has to be known before a deviation can be measured against it, which is the same reason the one-pass form is the one that drifts.

Trait Implementations§

Source§

impl Clone for OrderBookSide

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for OrderBookSide

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for OrderBookSide

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for OrderBookSide

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.