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: OrderBookSideThe bids side of the order book.
asks: OrderBookSideThe asks side of the order book.
Implementations§
Source§impl OrderBook
impl OrderBook
Sourcepub fn new(max_depth: Option<usize>) -> Self
pub fn new(max_depth: Option<usize>) -> Self
An empty book, each side capped at max_depth levels, or uncapped on
None.
Sourcepub fn mid_price(&self) -> Option<Dec>
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.
Sourcepub fn spread(&self) -> Option<Dec>
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.
Sourcepub fn imbalance(&self) -> Option<Dec>
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.
Sourcepub fn range_imbalance(&self, from: usize, to: usize) -> Option<Dec>
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.
Sourcepub fn apply_diff(&mut self, diff: &OrderBookDiff)
pub fn apply_diff(&mut self, diff: &OrderBookDiff)
Apply every level in diff, each as OrderBookSide::set does.