Skip to content

Native Order Book

quantpylib.hft.orderbook exposes the native order-book implementation. It applies quantpylib.standards.models.BookUpdate events and keeps live bid/ask state in fast, native cpp implementation and exposed via nanobind to Python.

Prices and sizes are fixed-point int64 values. PRICE_SCALE and SIZE_SCALE are both 1_000_000_000, so decoded values are stored_int / 1_000_000_000. Level arrays are returned as NumPy int64 arrays with shape (n, 2), where each row is [price, size]. Bid levels are returned best bid first; ask levels are returned best ask first.

Implementations

OrderBook is the default public alias for OrderBookMap. OrderBookMap and OrderBookVec expose the same Python API:

Class Storage model Typical use
OrderBook Alias for OrderBookMap Default materialized order-book state.
OrderBookMap Native ordered-map using std::map General-purpose order-book materialization.
OrderBookVec Native vector using std:vector Reverse-vector-backed materialization.

API Reference

OrderBook

Default native order book.

OrderBook is the default runtime alias for quantpylib.hft.orderbook.OrderBookMap. It is the order-book type created by quantpylib.hft.materializers.OrderbookMaterializerSink when no existing book is supplied.

Side

Order-book side enum used by native order-book methods.

Attributes:

Name Type Description
Bid Side

Bid side.

Ask Side

Ask side.

OrderBookMap

Map-backed native order book.

OrderBookMap stores aggregate levels in native ordered maps and exposes the same Python API as quantpylib.hft.orderbook.OrderBookVec. OrderBook is the default runtime alias for this implementation.

Parameters:

Name Type Description Default
reserve_orders int

Initial native capacity for order-level storage.

1024

add_order(id, side, price, size)

Add one order-level entry and update aggregate levels.

apply(update)

Apply one native BookUpdate snapshot or delta.

apply_mbp_level(side, price, size)

Set or delete one aggregate market-by-price level.

best_ask()

Return (price, size) for the best ask, or None.

best_bid()

Return (price, size) for the best bid, or None.

cancel_order(id)

Cancel one order-level entry and update aggregate levels.

clear()

Clear all book state.

get_asks()

Return all ask levels as a best-to-worst int64 array.

get_bids()

Return all bid levels as a best-to-worst int64 array.

get_top_asks(depth)

Return up to depth best ask levels.

get_top_bids(depth)

Return up to depth best bid levels.

has_order(id)

Return whether an order id is present.

mid()

Return the midpoint price, or None when a side is missing.

modify_order(id, new_size)

Modify one order-level entry and update aggregate levels.

order(id)

Return one order-level entry as a dictionary.

OrderBookVec

Vector-backed native order book.

OrderBookVec maintains aggregate L2 levels and optional order-level state in contiguous native storage. It accepts quantpylib.standards.models.BookUpdate through apply(), supports direct MBP level mutation through apply_mbp_level(), and exposes best-to-worst bid/ask levels as NumPy int64 arrays with shape (n, 2).

Parameters:

Name Type Description Default
reserve_levels int

Initial native capacity for bid and ask price levels.

1024
reserve_orders int

Initial native capacity for order-level storage.

1024
mbp_point_threshold int

Delta size threshold below which MBP deltas use point updates instead of batch merge.

8

add_order(id, side, price, size)

Add one order-level entry and update aggregate levels.

apply(update)

Apply one native BookUpdate snapshot or delta.

apply_mbp_level(side, price, size)

Set or delete one aggregate market-by-price level.

best_ask()

Return (price, size) for the best ask, or None.

best_bid()

Return (price, size) for the best bid, or None.

cancel_order(id)

Cancel one order-level entry and update aggregate levels.

clear()

Clear all book state.

get_asks()

Return all ask levels as a best-to-worst int64 array.

get_bids()

Return all bid levels as a best-to-worst int64 array.

get_top_asks(depth)

Return up to depth best ask levels.

get_top_bids(depth)

Return up to depth best bid levels.

has_order(id)

Return whether an order id is present.

mid()

Return the midpoint price, or None when a side is missing.

modify_order(id, new_size)

Modify one order-level entry and update aggregate levels.

order(id)

Return one order-level entry as a dictionary.