Skip to main content

OrderBook

Struct OrderBook 

Source
pub struct OrderBook {
    pub bids: OrderBookSide,
    pub asks: OrderBookSide,
}
Expand description

A level 2 order book.

Designed to be efficient for high-frequency trading scenarios.

Fields§

§bids: OrderBookSide

The bids side of the order book.

§asks: OrderBookSide

The asks side of the order book.

Implementations§

Source§

impl OrderBook

Source

pub fn new(max_depth: Option<usize>) -> Self

An empty book, each side capped at max_depth levels, or uncapped on None.

Source

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

The level cap both sides were built with.

Source

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

O(1) operation to get the mid price of the order book.

Returns None if either the bid or ask side is empty.

Source

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

Best ask less best bid, or None if either side is empty.

Negative when the book is crossed, which a feed can produce.

Source

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

Order book imbalance at the top: the pressure the best bid and the best ask exert against each other.

(bid - ask) / (bid + ask) over the two best amounts, which lands in [-1, 1]: 1 when only bids stand at the touch, -1 when only asks do, and zero when they match. Positive is buying pressure.

None when either side is empty, or when both best amounts are zero and there is no pressure to take a ratio of.

Exact rather than floating point. The amounts are exact, the sums are exact, and only the division rounds, at the scale everything else here carries.

Source

pub fn range_imbalance(&self, from: usize, to: usize) -> Option<Dec>

OrderBook::imbalance over a band of the book rather than the touch.

The range is half open, from..to, counted in levels from the best on each side, so range_imbalance(0, 5) weighs the top five levels of one side against the top five of the other. A to past the end of a side takes what that side holds, so an unevenly deep book still answers.

None when either side holds no level in the range, when the amounts on both sides of it are zero, or when the range is empty — to at or below from, which includes an inverted one, is a band with no levels rather than a band read backwards.

Reading deeper than the touch is the point: the best level alone is the easiest part of a book to move, and an imbalance measured there is the easiest to manufacture.

Source

pub fn apply_diff(&mut self, diff: &OrderBookDiff)

Apply every level in diff, each as OrderBookSide::set does.

Trait Implementations§

Source§

impl Clone for OrderBook

Source§

fn clone(&self) -> OrderBook

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 OrderBook

Source§

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

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

impl Default for OrderBook

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<OrderBookTop> for OrderBook

Source§

fn from(top: OrderBookTop) -> Self

Converts to this type from the input type.

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