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 ) double

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

Returns :

double

orders ( self ) list

Return the orders for the level.

Returns :

list[BookOrder]

price

Price

Return the price for the level.

Returns :

Price

Type :

Level.price

size ( self ) double

Return the size at this level.

Returns :

double

class OrderBook ( InstrumentId instrument_id , BookType book_type )

Bases: Data

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

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

Add the given order to the book.

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

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

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 :

list[Level] – Sorted in ascending order of price.

best_ask_price ( self )

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

Returns :

double

best_ask_size ( self )

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

Returns :

double or None

best_bid_price ( self )

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

Returns :

double

best_bid_size ( self )

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

Returns :

double

bids ( self ) list

Return the bid levels for the order book.

Returns :

list[Level] – Sorted in descending order of price.

book_type

BookType

Return the order book type.

Returns :

BookType

Type :

OrderBook.book_type

check_integrity ( self ) void

Check book integrity.

For now will panic from Rust and print the error message to stdout.

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

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.

Returns :

int

Type :

OrderBook.count

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

Cancel the given order in the book.

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

  • sequence ( uint64 , 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.

Returns :

str

References

https://www.python.org/dev/peps/pep-3155/

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.

Returns :

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.

Returns :

double

Raises :

ValueError – If order_side is equal to NO_ORDER_SIDE

instrument_id

InstrumentId

Return the books instrument ID.

Returns :

InstrumentId

Type :

OrderBook.instrument_id

midpoint ( self )

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

Returns :

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.

Returns :

str

reset ( self ) void

Reset the order book (clear all stateful values).

sequence

int

Return the last sequence number for the book.

Returns :

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

Returns :

double or None

ts_event

int

The UNIX timestamp (nanoseconds) when the data event occurred.

Returns :

int

Type :

OrderBook.ts_event

ts_init

int

The UNIX timestamp (nanoseconds) when the object was initialized.

Returns :

int

Type :

OrderBook.ts_init

ts_last

int

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

Returns :

int

Type :

OrderBook.ts_last

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

Update the given order in the book.

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

  • sequence ( uint64 , 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.

Parameters :

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

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.