Order Book

class BookLevel

Bases: object

Represents an order book price level.

A price level on one side of the order book with one or more individual orders.

This class is read-only and cannot be initialized from Python.

Parameters:
  • price (Price) – The price for the level.

  • orders (list[BookOrder]) – The orders for the level.

Raises:

ValueError – If orders is empty.

exposure(self) double

Return the exposure at this level (price * volume).

Return type:

double

orders(self) list

Return the orders for the level.

Return type:

list[BookOrder]

price

Price

Return the price for the level.

Return type:

Price

Type:

BookLevel.price

side

OrderSide

Return the side for the level.

Return type:

OrderSide

Type:

BookLevel.side

size(self) double

Return the size at this level.

Return type:

double

class OrderBook

Bases: Data

OrderBook(InstrumentId instrument_id, BookType book_type) -> None

Provides an order book which can handle L1/L2/L3 granularity data.

Parameters:
  • instrument_id (InstrumentId) – The instrument ID for the order book.

  • book_type (BookType {L1_MBP, L2_MBP, L3_MBO}) – The order book type.

add(self, BookOrder order, uint64_t ts_event, uint8_t flags=0, uint64_t sequence=0) void

Add the given order to the book.

Parameters:
  • order (BookOrder) – The order to add.

  • ts_event (uint64_t) – UNIX timestamp (nanoseconds) when the book event occurred.

  • flags (uint8_t, default 0) – The record flags bit field, indicating event end and data information.

  • sequence (uint64_t, default 0) – The unique sequence number for the update. If default 0 then will increment the sequence.

Raises:

RuntimeError – If the book type is L1_MBP.

apply(self, Data data) void

Apply the given data to the order book.

Parameters:

delta (OrderBookDelta, OrderBookDeltas) – The data to apply.

apply_delta(self, OrderBookDelta delta) void

Apply the order book delta.

Parameters:

delta (OrderBookDelta) – The delta to apply.

Raises:

ValueError – If delta.book_type is not equal to self.type.

apply_deltas(self, OrderBookDeltas deltas) void

Apply the bulk deltas to the order book.

Parameters:

deltas (OrderBookDeltas) – The deltas to apply.

apply_depth(self, OrderBookDepth10 depth) void

Apply the depth update to the order book.

Parameters:

depth (OrderBookDepth10) – The depth update to apply.

asks(self) list

Return the bid levels for the order book.

Returns:

Sorted in ascending order of price.

Return type:

list[BookLevel]

best_ask_price(self)

Return the best ask price in the book (if no asks then returns None).

Return type:

double

best_ask_size(self)

Return the best ask size in the book (if no asks then returns None).

Return type:

double or None

best_bid_price(self)

Return the best bid price in the book (if no bids then returns None).

Return type:

double

best_bid_size(self)

Return the best bid size in the book (if no bids then returns None).

Return type:

double

bids(self) list

Return the bid levels for the order book.

Returns:

Sorted in descending order of price.

Return type:

list[BookLevel]

book_type

BookType

Return the order book type.

Return type:

BookType

Type:

OrderBook.book_type

check_integrity(self) void

Check book integrity.

For all order books: - The bid side price should not be greater than the ask side price.

Raises:

RuntimeError – If book integrity check fails.

clear(self, uint64_t ts_event, uint64_t sequence=0) void

Clear the entire order book.

clear_asks(self, uint64_t ts_event, uint64_t sequence=0) void

Clear the asks from the order book.

clear_bids(self, uint64_t ts_event, uint64_t sequence=0) void

Clear the bids from the order book.

delete(self, BookOrder order, uint64_t ts_event, uint8_t flags=0, uint64_t sequence=0) void

Cancel the given order in the book.

Parameters:
  • order (Order) – The order to delete.

  • ts_event (uint64_t) – UNIX timestamp (nanoseconds) when the book event occurred.

  • flags (uint8_t, default 0) – The record flags bit field, indicating event end and data information.

  • sequence (uint64_t, default 0) – The unique sequence number for the update. If default 0 then will increment the sequence.

classmethod fully_qualified_name(cls) str

Return the fully qualified name for the Data class.

Return type:

str

get_all_crossed_levels(self, OrderSide order_side, Price price, uint8_t size_prec) list

Return all price levels that would be crossed by an order at the given price.

Unlike simulate_fills, this returns ALL crossed levels regardless of order quantity. Used when liquidity consumption tracking needs visibility into all available levels.

Parameters:
  • order_side (OrderSide) – The order side (BUY or SELL).

  • price (Price) – The limit price to check against.

  • size_prec (uint8_t) – The size precision for the quantities.

Return type:

list[(Price, Quantity)]

get_avg_px_for_quantity(self, Quantity quantity, OrderSide order_side) double

Return the average price expected for the given quantity based on the current state of the order book.

Parameters:
  • quantity (Quantity) – The quantity for the calculation.

  • order_side (OrderSide) – The order side for the calculation.

Return type:

double

Raises:

ValueError – If order_side is equal to NO_ORDER_SIDE

Warning

If no average price can be calculated then will return 0.0 (zero).

get_quantity_at_level(self, Price price, OrderSide order_side, uint8_t size_precision) Quantity

Return the quantity at a specific price level only.

Unlike get_quantity_for_price which returns cumulative quantity across multiple levels, this returns only the quantity at the exact price level.

Parameters:
  • price (Price) – The price level to query.

  • order_side (OrderSide) – The order side for the calculation.

  • size_precision (uint8_t) – The precision for the returned quantity.

Return type:

Quantity

Raises:

ValueError – If order_side is equal to NO_ORDER_SIDE

get_quantity_for_price(self, Price price, OrderSide order_side) double

Return the cumulative quantity at or better than the given price.

For a BUY order, sums ask levels at or below the price. For a SELL order, sums bid levels at or above the price.

Parameters:
  • price (Price) – The price for the calculation.

  • order_side (OrderSide) – The order side for the calculation.

Return type:

double

Raises:

ValueError – If order_side is equal to NO_ORDER_SIDE

get_worst_px_for_quantity(self, Quantity quantity, OrderSide order_side)

Return the worst (last-touched) price required to fill the given quantity based on the current state of the order book.

Parameters:
  • quantity (Quantity) – The quantity for the calculation.

  • order_side (OrderSide) – The order side for the calculation.

Return type:

Price or None

Raises:

ValueError – If order_side is equal to NO_ORDER_SIDE

Warning

If no worst price can be calculated then returns None.

instrument_id

InstrumentId

Return the books instrument ID.

Return type:

InstrumentId

Type:

OrderBook.instrument_id

classmethod is_signal(cls, str name='') bool

Determine if the current class is a signal type, optionally checking for a specific signal name.

Parameters:

name (str, optional) – The specific signal name to check. If name not provided or if an empty string is passed, the method checks whether the class name indicates a general signal type. If name is provided, the method checks if the class name corresponds to that specific signal.

Returns:

True if the class name matches the signal type or the specific signal name, otherwise False.

Return type:

bool

midpoint(self)

Return the mid point (if no market exists then returns None).

Return type:

double or None

pprint(self, int num_levels=3) str

Return a string representation of the order book in a human-readable table format.

Parameters:

num_levels (int) – The number of levels to include.

Return type:

str

reset(self) void

Reset the order book (clear all stateful values).

sequence

int

Return the last sequence number for the book.

Return type:

int

Type:

OrderBook.sequence

simulate_fills(self, Order order, uint8_t price_prec, uint8_t size_prec, bool is_aggressive) list

Simulate filling the book with the given order.

Parameters:
  • order (Order) – The order to simulate fills for.

  • price_prec (uint8_t) – The price precision for the fills.

  • size_prec (uint8_t) – The size precision for the fills (based on the instrument definition).

  • is_aggressive (bool) – If the order is an aggressive liquidity taking order.

spread(self)

Return the top-of-book spread (if no bids or asks then returns None).

Return type:

double or None

to_deltas_c(self, uint64_t ts_event, uint64_t ts_init) OrderBookDeltas
to_quote_tick(self) QuoteTick

Return a QuoteTick created from the top of book levels.

Returns None when the top-of-book bid or ask is missing or invalid (zero size).

Return type:

QuoteTick or None

ts_event

int

UNIX timestamp (nanoseconds) when the data event occurred.

Return type:

int

Type:

OrderBook.ts_event

ts_init

int

UNIX timestamp (nanoseconds) when the object was initialized.

Return type:

int

Type:

OrderBook.ts_init

ts_last

int

Return the UNIX timestamp (nanoseconds) when the order book was last updated.

Return type:

int

Type:

OrderBook.ts_last

update(self, BookOrder order, uint64_t ts_event, uint8_t flags=0, uint64_t sequence=0) void

Update the given order in the book.

Parameters:
  • order (Order) – The order to update.

  • ts_event (uint64_t) – UNIX timestamp (nanoseconds) when the book event occurred.

  • flags (uint8_t, default 0) – The record flags bit field, indicating event end and data information.

  • sequence (uint64_t, default 0) – The unique sequence number for the update. If default 0 then will increment the sequence.

update_count

int

Return the books update count.

Return type:

int

Type:

OrderBook.update_count

update_quote_tick(self, QuoteTick tick) void

Update the order book with the given quote tick.

This operation is only valid for L1_MBP books maintaining a top level.

Parameters:

tick (QuoteTick) – The quote tick to update with.

Raises:

RuntimeError – If book_type is not L1_MBP.

update_trade_tick(self, TradeTick tick) void

Update the order book with the given trade tick.

Parameters:

tick (TradeTick) – The trade tick to update with.

Raises:

RuntimeError – If book_type is not L1_MBP.

py_should_handle_own_book_order(Order order) bool