Skip to main content
Version: latest

Order Book

class Level

Bases: object

Represents a read-only order book Level.

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

  • Parameters:
    • price (Price) – The price for the level.
    • orders (list [BookOrder ]) – The orders for the level.
  • Raises: ValueError – If orders is empty.

exposure(self)

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

  • Return type: double

orders(self)

Return the orders for the level.

price

Price

Return the price for the level.

  • Return type: Price
  • Type: Level.price

size(self)

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.

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

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)

Apply the given data to the order book.

apply_delta(self, OrderBookDelta delta)

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)

Apply the bulk deltas to the order book.

apply_depth(self, OrderBookDepth10 depth)

Apply the depth update to the order book.

asks(self)

Return the bid levels for the order book.

  • Returns: Sorted in ascending order of price.
  • Return type: list[Level]

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)

Return the bid levels for the order book.

  • Returns: Sorted in descending order of price.
  • Return type: list[Level]

book_type

BookType

Return the order book type.

  • Return type: BookType
  • Type: OrderBook.book_type

check_integrity(self)

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)

Clear the entire order book.

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

Clear the asks from the order book.

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

Clear the bids from the order book.

count

int

Return the books update count.

  • Return type: int
  • Type: OrderBook.count

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

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)

Return the fully qualified name for the Data class.

  • Return type: str

get_avg_px_for_quantity(self, Quantity quantity, OrderSide order_side)

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_for_price(self, Price price, OrderSide order_side)

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

  • Parameters:
    • price (Price) – 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

instrument_id

InstrumentId

Return the books instrument ID.

midpoint(self)

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

  • Return type: double or None

pprint(self, int num_levels=3)

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)

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, bool is_aggressive)

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.

spread(self)

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

  • Return type: double 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)

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_quote_tick(self, QuoteTick tick)

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)

Update the order book with the given trade tick.

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

  • Parameters: tick (TradeTick) – The trade tick to update with.
  • Raises: RuntimeError – If book_type is not L1_MBP.