Skip to main content
Version: latest

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.

price

Price

Return the price for the level.

  • Return type: Price
  • Type: BookLevel.price

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.

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.

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.

apply_depth(self, OrderBookDepth10 depth) → void

Apply the depth update to the order book.

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.

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

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.

classmethod is_signal(cls, unicode name=u'') → 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) → unicode

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

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) → 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_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.

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.