Order Book

Defines real-time and simulated order book components.

Book

class L1OrderBook ( InstrumentId instrument_id , uint8_t price_precision , uint8_t size_precision )

Bases: OrderBook

Provides a L1 TBBO (top of book best bid/offer) order book.

A level 1 order book has a single (top) Level .

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

  • price_precision ( uint8 ) – The price precision of the books orders.

  • size_precision ( uint8 ) – The size precision of the books orders.

Raises :
  • OverflowError – If price_precision is negative (< 0).

  • OverflowError – If size_precision is negative (< 0).

add ( self , BookOrder order , uint64_t update_id=0 ) void

NotImplemented (Use update(order) for L1OrderBook).

apply ( self , OrderBookData data ) void

Apply the data to the order book.

Parameters :

data ( OrderBookData ) – The data to apply.

Raises :

ValueError – If data.level is not equal to self.type .

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.

Raises :

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

apply_snapshot ( self , OrderBookSnapshot snapshot ) void

Apply the bulk snapshot to the order book.

Parameters :

snapshot ( OrderBookSnapshot ) – The snapshot to apply.

Raises :

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

asks

The order books asks.

Returns :

Ladder

best_ask_level ( self ) Level

Return the best ask level.

Returns :

Level

best_ask_price ( self )

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

Returns :

double

best_ask_qty ( self )

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

Returns :

double or None

best_bid_level ( self ) Level

Return the best bid level.

Returns :

Level

best_bid_price ( self )

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

Returns :

double

best_bid_qty ( self )

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

Returns :

double

bids

The order books bids.

Returns :

Ladder

check_integrity ( self ) void

Check order book integrity.

For a L1 TBBO order book: - There should be at most one level per side. - The bid side price should not be greater than or equal to the ask side price.

Raises :

BookIntegrityError – If any check fails.

clear ( self ) void

Clear the entire order book.

clear_asks ( self ) void

Clear the asks from the order book.

clear_bids ( self ) void

Clear the bids from the order book.

count

The update count for the book.

Returns :

int

static create ( Instrument instrument , BookType book_type , bool simulated=False )

Create a new order book with the given parameters.

Parameters :
  • instrument ( Instrument ) – The instrument for the book.

  • book_type (BookType { L1_TBBO , L2_MBP , L3_MBO }) – The order book level.

  • simulated ( bool ) – If the order book should be simulated (for backtesting only).

Returns :

OrderBook

delete ( self , BookOrder order , uint64_t update_id=0 ) void

Delete the given order in the book.

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

  • update_id ( uint64 , default 0 ) – The unique ID for the update. If default 0 then will increment the last_update_id .

get_price_for_quote_volume ( self , bool is_buy , double quote_volume ) double
get_price_for_volume ( self , bool is_buy , double volume ) double
get_quote_volume_for_price ( self , bool is_buy , double price ) double
get_volume_for_price ( self , bool is_buy , double price ) double
get_vwap_for_volume ( self , bool is_buy , double volume ) double
instrument_id

The order book instrument ID.

Returns :

InstrumentId

last_update_id

The last update ID.

Returns :

int

midpoint ( self )

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

Returns :

double or None

pprint ( self , int num_levels=3 , show=u'size' ) unicode

Print the order book in a clear format.

Parameters :
  • num_levels ( int ) – The number of levels to print.

  • show ( str ) – The data to show.

Returns :

str

price_precision

The order book price precision.

Returns :

uint8

size_precision

The order book size precision.

Returns :

uint8

spread ( self )

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

Returns :

double or None

trade_side ( self , TradeTick trade ) int

Return which side of the book a trade occurred given a trade tick.

Parameters :

trade ( TradeTick ) – The trade tick.

Returns :

OrderSide

ts_last

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

Returns :

uint64_t

type

The order book type { L1_TBBO , L2_MBP , L3_MBO }.

Returns :

BookType

update ( self , BookOrder order , uint64_t update_id=0 ) void

Update the given order in the book.

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

  • update_id ( uint64 , default 0 ) – The unique ID for the update. If default 0 then will increment the last_update_id .

class L2OrderBook ( InstrumentId instrument_id , uint8_t price_precision , uint8_t size_precision )

Bases: OrderBook

Provides a L2 MBP (market by price) order book.

A level 2 order books Levels are only made up of a single order.

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

  • price_precision ( uint8 ) – The price precision of the books orders.

  • size_precision ( uint8 ) – The size precision of the books orders.

Raises :
  • OverflowError – If price_precision is negative (< 0).

  • OverflowError – If size_precision is negative (< 0).

add ( self , BookOrder order , uint64_t update_id=0 ) void

Add the given order to the book.

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

  • update_id ( uint64 , default 0 ) – The unique ID for the update. If default 0 then will increment the last_update_id .

apply ( self , OrderBookData data ) void

Apply the data to the order book.

Parameters :

data ( OrderBookData ) – The data to apply.

Raises :

ValueError – If data.level is not equal to self.type .

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.

Raises :

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

apply_snapshot ( self , OrderBookSnapshot snapshot ) void

Apply the bulk snapshot to the order book.

Parameters :

snapshot ( OrderBookSnapshot ) – The snapshot to apply.

Raises :

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

asks

The order books asks.

Returns :

Ladder

best_ask_level ( self ) Level

Return the best ask level.

Returns :

Level

best_ask_price ( self )

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

Returns :

double

best_ask_qty ( self )

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

Returns :

double or None

best_bid_level ( self ) Level

Return the best bid level.

Returns :

Level

best_bid_price ( self )

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

Returns :

double

best_bid_qty ( self )

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

Returns :

double

bids

The order books bids.

Returns :

Ladder

check_integrity ( self ) void

Check order book integrity.

For a L2_MBP order book: - There should be at most one order per level. - The bid side price should not be greater than or equal to the ask side price.

Raises :

BookIntegrityError – If any check fails.

clear ( self ) void

Clear the entire order book.

clear_asks ( self ) void

Clear the asks from the order book.

clear_bids ( self ) void

Clear the bids from the order book.

count

The update count for the book.

Returns :

int

static create ( Instrument instrument , BookType book_type , bool simulated=False )

Create a new order book with the given parameters.

Parameters :
  • instrument ( Instrument ) – The instrument for the book.

  • book_type (BookType { L1_TBBO , L2_MBP , L3_MBO }) – The order book level.

  • simulated ( bool ) – If the order book should be simulated (for backtesting only).

Returns :

OrderBook

delete ( self , BookOrder order , uint64_t update_id=0 ) void

Delete the given order in the book.

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

  • update_id ( uint64 , default 0 ) – The unique ID for the update. If default 0 then will increment the last_update_id .

get_price_for_quote_volume ( self , bool is_buy , double quote_volume ) double
get_price_for_volume ( self , bool is_buy , double volume ) double
get_quote_volume_for_price ( self , bool is_buy , double price ) double
get_volume_for_price ( self , bool is_buy , double price ) double
get_vwap_for_volume ( self , bool is_buy , double volume ) double
instrument_id

The order book instrument ID.

Returns :

InstrumentId

last_update_id

The last update ID.

Returns :

int

midpoint ( self )

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

Returns :

double or None

pprint ( self , int num_levels=3 , show=u'size' ) unicode

Print the order book in a clear format.

Parameters :
  • num_levels ( int ) – The number of levels to print.

  • show ( str ) – The data to show.

Returns :

str

price_precision

The order book price precision.

Returns :

uint8

size_precision

The order book size precision.

Returns :

uint8

spread ( self )

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

Returns :

double or None

trade_side ( self , TradeTick trade ) int

Return which side of the book a trade occurred given a trade tick.

Parameters :

trade ( TradeTick ) – The trade tick.

Returns :

OrderSide

ts_last

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

Returns :

uint64_t

type

The order book type { L1_TBBO , L2_MBP , L3_MBO }.

Returns :

BookType

update ( self , BookOrder order , uint64_t update_id=0 ) void

Update the given order in the book.

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

  • update_id ( uint64 , default 0 ) – The unique ID for the update. If default 0 then will increment the last_update_id .

class L3OrderBook ( InstrumentId instrument_id , uint8_t price_precision , uint8_t size_precision )

Bases: OrderBook

Provides an L3 MBO (market by order) order book.

A level 3 order books Levels can be made up of multiple orders. This class maps directly to the functionality of the base class.

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

  • price_precision ( uint8 ) – The price precision of the books orders.

  • size_precision ( uint8 ) – The size precision of the books orders.

Raises :
  • OverflowError – If price_precision is negative (< 0).

  • OverflowError – If size_precision is negative (< 0).

add ( self , BookOrder order , uint64_t update_id=0 ) void

Add the given order to the book.

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

  • update_id ( uint64 , default 0 ) – The unique ID for the update. If default 0 then will increment the last_update_id .

apply ( self , OrderBookData data ) void

Apply the data to the order book.

Parameters :

data ( OrderBookData ) – The data to apply.

Raises :

ValueError – If data.level is not equal to self.type .

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.

Raises :

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

apply_snapshot ( self , OrderBookSnapshot snapshot ) void

Apply the bulk snapshot to the order book.

Parameters :

snapshot ( OrderBookSnapshot ) – The snapshot to apply.

Raises :

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

asks

The order books asks.

Returns :

Ladder

best_ask_level ( self ) Level

Return the best ask level.

Returns :

Level

best_ask_price ( self )

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

Returns :

double

best_ask_qty ( self )

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

Returns :

double or None

best_bid_level ( self ) Level

Return the best bid level.

Returns :

Level

best_bid_price ( self )

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

Returns :

double

best_bid_qty ( self )

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

Returns :

double

bids

The order books bids.

Returns :

Ladder

check_integrity ( self ) void

Check order book integrity.

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

Raises :

BookIntegrityError – If any check fails.

clear ( self ) void

Clear the entire order book.

clear_asks ( self ) void

Clear the asks from the order book.

clear_bids ( self ) void

Clear the bids from the order book.

count

The update count for the book.

Returns :

int

static create ( Instrument instrument , BookType book_type , bool simulated=False )

Create a new order book with the given parameters.

Parameters :
  • instrument ( Instrument ) – The instrument for the book.

  • book_type (BookType { L1_TBBO , L2_MBP , L3_MBO }) – The order book level.

  • simulated ( bool ) – If the order book should be simulated (for backtesting only).

Returns :

OrderBook

delete ( self , BookOrder order , uint64_t update_id=0 ) void

Delete the given order in the book.

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

  • update_id ( uint64 , default 0 ) – The unique ID for the update. If default 0 then will increment the last_update_id .

get_price_for_quote_volume ( self , bool is_buy , double quote_volume ) double
get_price_for_volume ( self , bool is_buy , double volume ) double
get_quote_volume_for_price ( self , bool is_buy , double price ) double
get_volume_for_price ( self , bool is_buy , double price ) double
get_vwap_for_volume ( self , bool is_buy , double volume ) double
instrument_id

The order book instrument ID.

Returns :

InstrumentId

last_update_id

The last update ID.

Returns :

int

midpoint ( self )

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

Returns :

double or None

pprint ( self , int num_levels=3 , show=u'size' ) unicode

Print the order book in a clear format.

Parameters :
  • num_levels ( int ) – The number of levels to print.

  • show ( str ) – The data to show.

Returns :

str

price_precision

The order book price precision.

Returns :

uint8

size_precision

The order book size precision.

Returns :

uint8

spread ( self )

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

Returns :

double or None

trade_side ( self , TradeTick trade ) int

Return which side of the book a trade occurred given a trade tick.

Parameters :

trade ( TradeTick ) – The trade tick.

Returns :

OrderSide

ts_last

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

Returns :

uint64_t

type

The order book type { L1_TBBO , L2_MBP , L3_MBO }.

Returns :

BookType

update ( self , BookOrder order , uint64_t update_id=0 ) void

Update the given order in the book.

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

  • update_id ( uint64 , default 0 ) – The unique ID for the update. If default 0 then will increment the last_update_id .

class OrderBook ( InstrumentId instrument_id , BookType book_type , uint8_t price_precision , uint8_t size_precision )

Bases: object

The base class for all order books.

Provides a L1/L2/L3 order book as an L3OrderBook which can be proxied to L2OrderBook or L1OrderBook classes.

add ( self , BookOrder order , uint64_t update_id=0 ) void

Add the given order to the book.

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

  • update_id ( uint64 , default 0 ) – The unique ID for the update. If default 0 then will increment the last_update_id .

apply ( self , OrderBookData data ) void

Apply the data to the order book.

Parameters :

data ( OrderBookData ) – The data to apply.

Raises :

ValueError – If data.level is not equal to self.type .

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.

Raises :

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

apply_snapshot ( self , OrderBookSnapshot snapshot ) void

Apply the bulk snapshot to the order book.

Parameters :

snapshot ( OrderBookSnapshot ) – The snapshot to apply.

Raises :

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

asks

The order books asks.

Returns :

Ladder

best_ask_level ( self ) Level

Return the best ask level.

Returns :

Level

best_ask_price ( self )

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

Returns :

double

best_ask_qty ( self )

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

Returns :

double or None

best_bid_level ( self ) Level

Return the best bid level.

Returns :

Level

best_bid_price ( self )

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

Returns :

double

best_bid_qty ( self )

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

Returns :

double

bids

The order books bids.

Returns :

Ladder

check_integrity ( self ) void

Check order book integrity.

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

Raises :

BookIntegrityError – If any check fails.

clear ( self ) void

Clear the entire order book.

clear_asks ( self ) void

Clear the asks from the order book.

clear_bids ( self ) void

Clear the bids from the order book.

count

The update count for the book.

Returns :

int

static create ( Instrument instrument , BookType book_type , bool simulated=False )

Create a new order book with the given parameters.

Parameters :
  • instrument ( Instrument ) – The instrument for the book.

  • book_type (BookType { L1_TBBO , L2_MBP , L3_MBO }) – The order book level.

  • simulated ( bool ) – If the order book should be simulated (for backtesting only).

Returns :

OrderBook

delete ( self , BookOrder order , uint64_t update_id=0 ) void

Delete the given order in the book.

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

  • update_id ( uint64 , default 0 ) – The unique ID for the update. If default 0 then will increment the last_update_id .

get_price_for_quote_volume ( self , bool is_buy , double quote_volume ) double
get_price_for_volume ( self , bool is_buy , double volume ) double
get_quote_volume_for_price ( self , bool is_buy , double price ) double
get_volume_for_price ( self , bool is_buy , double price ) double
get_vwap_for_volume ( self , bool is_buy , double volume ) double
instrument_id

The order book instrument ID.

Returns :

InstrumentId

last_update_id

The last update ID.

Returns :

int

midpoint ( self )

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

Returns :

double or None

pprint ( self , int num_levels=3 , show=u'size' ) unicode

Print the order book in a clear format.

Parameters :
  • num_levels ( int ) – The number of levels to print.

  • show ( str ) – The data to show.

Returns :

str

price_precision

The order book price precision.

Returns :

uint8

size_precision

The order book size precision.

Returns :

uint8

spread ( self )

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

Returns :

double or None

trade_side ( self , TradeTick trade ) int

Return which side of the book a trade occurred given a trade tick.

Parameters :

trade ( TradeTick ) – The trade tick.

Returns :

OrderSide

ts_last

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

Returns :

uint64_t

type

The order book type { L1_TBBO , L2_MBP , L3_MBO }.

Returns :

BookType

update ( self , BookOrder order , uint64_t update_id=0 ) void

Update the given order in the book.

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

  • update_id ( uint64 , default 0 ) – The unique ID for the update. If default 0 then will increment the last_update_id .

Data

class BookOrder ( double price , double size , OrderSide side , unicode id=None )

Bases: object

Represents an order in a book.

Parameters :
  • price ( double ) – The order price.

  • size ( double ) – The order size.

  • side (OrderSide { BUY , SELL }) – The order side.

  • id ( str ) – The order ID.

exposure ( self ) double

Return the total exposure for this order (price * size).

Returns :

double

static from_dict ( dict values ) BookOrder

Return an order from the given dict values.

Parameters :

values ( dict [ str , object ] ) – The values for initialization.

Returns :

BookOrder

id

The orders ID.

Returns :

str

price

The orders price.

Returns :

double

side

The orders side.

Returns :

OrderSide

signed_size ( self ) double

Return the signed size of the order (negative for SELL ).

Returns :

double

size

The orders size.

Returns :

double

static to_dict ( BookOrder obj )

Return a dictionary representation of this object.

Returns :

dict[str, object]

update_id ( self , unicode value ) void

Update the orders ID.

Parameters :

value ( str ) – The updated ID.

update_price ( self , double price ) void

Update the orders price.

Parameters :

price ( double ) – The updated price.

update_size ( self , double size ) void

Update the orders size.

Parameters :

size ( double ) – The updated size.

class OrderBookData ( InstrumentId instrument_id , BookType book_type , uint64_t update_id , uint64_t ts_event , uint64_t ts_init )

Bases: Data

The abstract base class for all OrderBook data.

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

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

  • update_id ( uint64 , default 0 ) – The unique ID for the data.

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

  • ts_init ( uint64_t ) – The UNIX timestamp (nanoseconds) when the data object was initialized.

Warning

This class should not be used directly, but through a concrete subclass.

book_type

The order book type (L1_TBBO, L2_MBP, L3_MBO).

Returns :

BookType

classmethod fully_qualified_name ( type cls ) str

Return the fully qualified name for the Data class.

Returns :

str

References

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

instrument_id

The instrument ID for the order book.

Returns :

InstrumentId

ts_event

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

Returns :

uint64_t

ts_init

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

Returns :

uint64_t

update_id

The update ID.

Returns :

uint64

class OrderBookDelta ( InstrumentId instrument_id , BookType book_type , BookAction action , BookOrder order , uint64_t ts_event , uint64_t ts_init , uint64_t update_id=0 )

Bases: OrderBookData

Represents a single difference on an OrderBook .

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

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

  • action (BookAction { ADD , UPDATED , DELETE , CLEAR }) – The order book delta action.

  • order ( Order ) – The order to apply.

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

  • ts_init ( uint64_t ) – The UNIX timestamp (nanoseconds) when the data object was initialized.

  • update_id ( uint64_t , default 0 ) – The unique ID for the data. If zero then order book will maintain ID internally.

action

The order book delta action { ADD , UPDATED , DELETE , CLEAR }.

Returns :

BookAction

book_type

The order book type (L1_TBBO, L2_MBP, L3_MBO).

Returns :

BookType

static from_dict ( dict values ) OrderBookDelta

Return an order book delta from the given dict values.

Parameters :

values ( dict [ str , object ] ) – The values for initialization.

Returns :

OrderBookDelta

classmethod fully_qualified_name ( type cls ) str

Return the fully qualified name for the Data class.

Returns :

str

References

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

instrument_id

The instrument ID for the order book.

Returns :

InstrumentId

order

The order to apply.

Returns :

Order

static to_dict ( OrderBookDelta obj )

Return a dictionary representation of this object.

Returns :

dict[str, object]

ts_event

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

Returns :

uint64_t

ts_init

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

Returns :

uint64_t

update_id

The update ID.

Returns :

uint64

class OrderBookDeltas ( InstrumentId instrument_id , BookType book_type , list deltas , uint64_t ts_event , uint64_t ts_init , uint64_t update_id=0 )

Bases: OrderBookData

Represents bulk changes for an OrderBook .

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

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

  • deltas ( list [ OrderBookDelta ] ) – The list of order book changes.

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

  • ts_init ( uint64_t ) – The UNIX timestamp (nanoseconds) when the data object was initialized.

  • update_id ( uint64 , default 0 ) – The unique ID for the data. If zero then order book will maintain ID internally.

book_type

The order book type (L1_TBBO, L2_MBP, L3_MBO).

Returns :

BookType

deltas

The order book deltas.

Returns :

list[OrderBookDelta]

static from_dict ( dict values ) OrderBookDeltas

Return order book deltas from the given dict values.

Parameters :

values ( dict [ str , object ] ) – The values for initialization.

Returns :

OrderBookDeltas

classmethod fully_qualified_name ( type cls ) str

Return the fully qualified name for the Data class.

Returns :

str

References

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

instrument_id

The instrument ID for the order book.

Returns :

InstrumentId

static to_dict ( OrderBookDeltas obj )

Return a dictionary representation of this object.

Returns :

dict[str, object]

ts_event

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

Returns :

uint64_t

ts_init

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

Returns :

uint64_t

update_id

The update ID.

Returns :

uint64

class OrderBookSnapshot ( InstrumentId instrument_id , BookType book_type , list bids , list asks , uint64_t ts_event , uint64_t ts_init , uint64_t update_id=0 )

Bases: OrderBookData

Represents a snapshot in time for an OrderBook .

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

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

  • bids ( list ) – The bids for the snapshot.

  • asks ( list ) – The asks for the snapshot.

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

  • ts_init ( uint64_t ) – The UNIX timestamp (nanoseconds) when the data object was initialized.

  • update_id ( uint64 , default 0 ) – The unique ID for the data. If zero then order book will maintain ID internally.

asks

The snapshot asks.

Returns :

list

bids

The snapshot bids.

Returns :

list

book_type

The order book type (L1_TBBO, L2_MBP, L3_MBO).

Returns :

BookType

static from_dict ( dict values ) OrderBookSnapshot

Return an order book snapshot from the given dict values.

Parameters :

values ( dict [ str , object ] ) – The values for initialization.

Returns :

OrderBookSnapshot

classmethod fully_qualified_name ( type cls ) str

Return the fully qualified name for the Data class.

Returns :

str

References

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

instrument_id

The instrument ID for the order book.

Returns :

InstrumentId

static to_dict ( OrderBookSnapshot obj )

Return a dictionary representation of this object.

Returns :

dict[str, object]

ts_event

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

Returns :

uint64_t

ts_init

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

Returns :

uint64_t

update_id

The update ID.

Returns :

uint64

Ladder

class Ladder ( bool reverse , uint8_t price_precision , uint8_t size_precision )

Bases: object

Represents a ladder of price levels in a book.

A ladder is on one side of the book, either bid or ask/offer.

Parameters :
  • reverse ( bool ) – If the ladder should be represented in reverse order of price (bids).

  • price_precision ( uint8 ) – The price precision of the books orders.

  • size_precision ( uint8 ) – The size precision of the books orders.

Raises :
  • OverflowError – If price_precision is negative (< 0).

  • OverflowError – If size_precision is negative (< 0).

add ( self , BookOrder order ) void

Add the given order to the ladder.

Parameters :

order ( BookOrder ) – The order to add.

delete ( self , BookOrder order ) void

Delete the given order in the ladder.

Parameters :

order ( BookOrder ) –

Raises :

KeyError – If order.id is not contained in the order ID level index.

depth ( self , int n=1 ) list

Return the levels in the ladder to the given depth.

Parameters :

n ( int ) – The maximum level to query.

Returns :

list[Level]

exposures ( self ) list

The exposures in the ladder.

Returns :

list[double]

is_reversed

If the ladder is in reverse order.

Returns :

bool

levels

The ladders levels.

Returns :

list[Level]

price_precision

The ladders price precision.

Returns :

uint8

prices ( self ) list

The prices in the ladder.

Returns :

list[double]

simulate_order_fills ( self , BookOrder order , DepthType depth_type=DepthType.VOLUME ) list

Return a simulation of where this order would be filled in the ladder.

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

  • depth_type ( DepthType ) – The depth type to simulate.

Returns :

list[(Price, Quantity)]

size_precision

The ladders size precision.

Returns :

uint8

top ( self ) Level

The top Level in the ladder.

Returns :

Level or None

update ( self , BookOrder order ) void

Update the given order in the ladder.

Parameters :

order ( BookOrder ) – The order to add.

volumes ( self ) list

The volumes in the ladder.

Returns :

list[double]

Level

class Level ( double price )

Bases: object

Represents an OrderBook level.

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

Parameters :

price ( double ) – The price for the level.

add ( self , BookOrder order ) void

Add the given order to this level.

Parameters :

order ( BookOrder ) – The order to add.

Raises :

ValueError – If order.price is not equal to the levels price.

bulk_add ( self , list orders ) void

Add the list of bulk orders to this level.

Parameters :

orders ( list [ BookOrder ] ) – The orders to add.

delete ( self , BookOrder order ) void

Delete the given order from this level.

Parameters :

order ( BookOrder ) – The order to delete.

exposure ( self ) double

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

Returns :

double

orders

The orders at the level.

Returns :

list[Order]

price

The levels price.

Returns :

double

update ( self , BookOrder order ) void

Update the given order on this level.

Parameters :

order ( BookOrder ) – The order to update.

Raises :

KeyError – If order is not found at this level.

volume ( self ) double

Return the volume at this level.

Returns :

double

Simulated

class SimulatedL1OrderBook ( InstrumentId instrument_id , uint8_t price_precision , uint8_t size_precision )

Bases: L1OrderBook

Provides a simulated level 1 order book for backtesting.

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

  • price_precision ( uint8 ) – The price precision of the books orders.

  • size_precision ( uint8 ) – The size precision of the books orders.

Raises :
  • OverflowError – If price_precision is negative (< 0).

  • OverflowError – If size_precision is negative (< 0).

add ( self , BookOrder order , uint64_t update_id=0 ) void

NotImplemented (Use update(order) for SimulatedOrderBook).

apply ( self , OrderBookData data ) void

Apply the data to the order book.

Parameters :

data ( OrderBookData ) – The data to apply.

Raises :

ValueError – If data.level is not equal to self.type .

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.

Raises :

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

apply_snapshot ( self , OrderBookSnapshot snapshot ) void

Apply the bulk snapshot to the order book.

Parameters :

snapshot ( OrderBookSnapshot ) – The snapshot to apply.

Raises :

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

asks

The order books asks.

Returns :

Ladder

best_ask_level ( self ) Level

Return the best ask level.

Returns :

Level

best_ask_price ( self )

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

Returns :

double

best_ask_qty ( self )

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

Returns :

double or None

best_bid_level ( self ) Level

Return the best bid level.

Returns :

Level

best_bid_price ( self )

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

Returns :

double

best_bid_qty ( self )

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

Returns :

double

bids

The order books bids.

Returns :

Ladder

check_integrity ( self ) void

Check order book integrity.

For a L1 TBBO order book: - There should be at most one level per side. - The bid side price should not be greater than or equal to the ask side price.

Raises :

BookIntegrityError – If any check fails.

clear ( self ) void

Clear the entire order book.

clear_asks ( self ) void

Clear the asks from the order book.

clear_bids ( self ) void

Clear the bids from the order book.

count

The update count for the book.

Returns :

int

static create ( Instrument instrument , BookType book_type , bool simulated=False )

Create a new order book with the given parameters.

Parameters :
  • instrument ( Instrument ) – The instrument for the book.

  • book_type (BookType { L1_TBBO , L2_MBP , L3_MBO }) – The order book level.

  • simulated ( bool ) – If the order book should be simulated (for backtesting only).

Returns :

OrderBook

delete ( self , BookOrder order , uint64_t update_id=0 ) void

Delete the given order in the book.

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

  • update_id ( uint64 , default 0 ) – The unique ID for the update. If default 0 then will increment the last_update_id .

get_price_for_quote_volume ( self , bool is_buy , double quote_volume ) double
get_price_for_volume ( self , bool is_buy , double volume ) double
get_quote_volume_for_price ( self , bool is_buy , double price ) double
get_volume_for_price ( self , bool is_buy , double price ) double
get_vwap_for_volume ( self , bool is_buy , double volume ) double
instrument_id

The order book instrument ID.

Returns :

InstrumentId

last_update_id

The last update ID.

Returns :

int

midpoint ( self )

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

Returns :

double or None

pprint ( self , int num_levels=3 , show=u'size' ) unicode

Print the order book in a clear format.

Parameters :
  • num_levels ( int ) – The number of levels to print.

  • show ( str ) – The data to show.

Returns :

str

price_precision

The order book price precision.

Returns :

uint8

size_precision

The order book size precision.

Returns :

uint8

spread ( self )

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

Returns :

double or None

trade_side ( self , TradeTick trade ) int

Return which side of the book a trade occurred given a trade tick.

Parameters :

trade ( TradeTick ) – The trade tick.

Returns :

OrderSide

ts_last

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

Returns :

uint64_t

type

The order book type { L1_TBBO , L2_MBP , L3_MBO }.

Returns :

BookType

update ( self , BookOrder order , uint64_t update_id=0 ) void

Update the given order in the book.

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

  • update_id ( uint64 , default 0 ) – The unique ID for the update. If default 0 then will increment the last_update_id .

class SimulatedL2OrderBook ( InstrumentId instrument_id , uint8_t price_precision , uint8_t size_precision )

Bases: L2OrderBook

Provides a simulated level 2 order book for backtesting.

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

  • price_precision ( uint8 ) – The price precision of the books orders.

  • size_precision ( uint8 ) – The size precision of the books orders.

Raises :
  • OverflowError – If price_precision is negative (< 0).

  • OverflowError – If size_precision is negative (< 0).

add ( self , BookOrder order , uint64_t update_id=0 ) void

Add the given order to the book.

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

  • update_id ( uint64 , default 0 ) – The unique ID for the update. If default 0 then will increment the last_update_id .

apply ( self , OrderBookData data ) void

Apply the data to the order book.

Parameters :

data ( OrderBookData ) – The data to apply.

Raises :

ValueError – If data.level is not equal to self.type .

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.

Raises :

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

apply_snapshot ( self , OrderBookSnapshot snapshot ) void

Apply the bulk snapshot to the order book.

Parameters :

snapshot ( OrderBookSnapshot ) – The snapshot to apply.

Raises :

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

asks

The order books asks.

Returns :

Ladder

best_ask_level ( self ) Level

Return the best ask level.

Returns :

Level

best_ask_price ( self )

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

Returns :

double

best_ask_qty ( self )

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

Returns :

double or None

best_bid_level ( self ) Level

Return the best bid level.

Returns :

Level

best_bid_price ( self )

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

Returns :

double

best_bid_qty ( self )

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

Returns :

double

bids

The order books bids.

Returns :

Ladder

check_integrity ( self ) void

Check order book integrity.

For a L2_MBP order book: - There should be at most one order per level. - The bid side price should not be greater than or equal to the ask side price.

Raises :

BookIntegrityError – If any check fails.

clear ( self ) void

Clear the entire order book.

clear_asks ( self ) void

Clear the asks from the order book.

clear_bids ( self ) void

Clear the bids from the order book.

count

The update count for the book.

Returns :

int

static create ( Instrument instrument , BookType book_type , bool simulated=False )

Create a new order book with the given parameters.

Parameters :
  • instrument ( Instrument ) – The instrument for the book.

  • book_type (BookType { L1_TBBO , L2_MBP , L3_MBO }) – The order book level.

  • simulated ( bool ) – If the order book should be simulated (for backtesting only).

Returns :

OrderBook

delete ( self , BookOrder order , uint64_t update_id=0 ) void

Delete the given order in the book.

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

  • update_id ( uint64 , default 0 ) – The unique ID for the update. If default 0 then will increment the last_update_id .

get_price_for_quote_volume ( self , bool is_buy , double quote_volume ) double
get_price_for_volume ( self , bool is_buy , double volume ) double
get_quote_volume_for_price ( self , bool is_buy , double price ) double
get_volume_for_price ( self , bool is_buy , double price ) double
get_vwap_for_volume ( self , bool is_buy , double volume ) double
instrument_id

The order book instrument ID.

Returns :

InstrumentId

last_update_id

The last update ID.

Returns :

int

midpoint ( self )

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

Returns :

double or None

pprint ( self , int num_levels=3 , show=u'size' ) unicode

Print the order book in a clear format.

Parameters :
  • num_levels ( int ) – The number of levels to print.

  • show ( str ) – The data to show.

Returns :

str

price_precision

The order book price precision.

Returns :

uint8

size_precision

The order book size precision.

Returns :

uint8

spread ( self )

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

Returns :

double or None

trade_side ( self , TradeTick trade ) int

Return which side of the book a trade occurred given a trade tick.

Parameters :

trade ( TradeTick ) – The trade tick.

Returns :

OrderSide

ts_last

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

Returns :

uint64_t

type

The order book type { L1_TBBO , L2_MBP , L3_MBO }.

Returns :

BookType

update ( self , BookOrder order , uint64_t update_id=0 ) void

Update the given order in the book.

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

  • update_id ( uint64 , default 0 ) – The unique ID for the update. If default 0 then will increment the last_update_id .

class SimulatedL3OrderBook ( InstrumentId instrument_id , uint8_t price_precision , uint8_t size_precision )

Bases: L3OrderBook

Provides a simulated level 3 order book for backtesting.

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

  • price_precision ( uint8 ) – The price precision of the books orders.

  • size_precision ( uint8 ) – The size precision of the books orders.

Raises :
  • OverflowError – If price_precision is negative (< 0).

  • OverflowError – If size_precision is negative (< 0).

add ( self , BookOrder order , uint64_t update_id=0 ) void

Add the given order to the book.

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

  • update_id ( uint64 , default 0 ) – The unique ID for the update. If default 0 then will increment the last_update_id .

apply ( self , OrderBookData data ) void

Apply the data to the order book.

Parameters :

data ( OrderBookData ) – The data to apply.

Raises :

ValueError – If data.level is not equal to self.type .

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.

Raises :

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

apply_snapshot ( self , OrderBookSnapshot snapshot ) void

Apply the bulk snapshot to the order book.

Parameters :

snapshot ( OrderBookSnapshot ) – The snapshot to apply.

Raises :

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

asks

The order books asks.

Returns :

Ladder

best_ask_level ( self ) Level

Return the best ask level.

Returns :

Level

best_ask_price ( self )

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

Returns :

double

best_ask_qty ( self )

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

Returns :

double or None

best_bid_level ( self ) Level

Return the best bid level.

Returns :

Level

best_bid_price ( self )

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

Returns :

double

best_bid_qty ( self )

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

Returns :

double

bids

The order books bids.

Returns :

Ladder

check_integrity ( self ) void

Check order book integrity.

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

Raises :

BookIntegrityError – If any check fails.

clear ( self ) void

Clear the entire order book.

clear_asks ( self ) void

Clear the asks from the order book.

clear_bids ( self ) void

Clear the bids from the order book.

count

The update count for the book.

Returns :

int

static create ( Instrument instrument , BookType book_type , bool simulated=False )

Create a new order book with the given parameters.

Parameters :
  • instrument ( Instrument ) – The instrument for the book.

  • book_type (BookType { L1_TBBO , L2_MBP , L3_MBO }) – The order book level.

  • simulated ( bool ) – If the order book should be simulated (for backtesting only).

Returns :

OrderBook

delete ( self , BookOrder order , uint64_t update_id=0 ) void

Delete the given order in the book.

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

  • update_id ( uint64 , default 0 ) – The unique ID for the update. If default 0 then will increment the last_update_id .

get_price_for_quote_volume ( self , bool is_buy , double quote_volume ) double
get_price_for_volume ( self , bool is_buy , double volume ) double
get_quote_volume_for_price ( self , bool is_buy , double price ) double
get_volume_for_price ( self , bool is_buy , double price ) double
get_vwap_for_volume ( self , bool is_buy , double volume ) double
instrument_id

The order book instrument ID.

Returns :

InstrumentId

last_update_id

The last update ID.

Returns :

int

midpoint ( self )

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

Returns :

double or None

pprint ( self , int num_levels=3 , show=u'size' ) unicode

Print the order book in a clear format.

Parameters :
  • num_levels ( int ) – The number of levels to print.

  • show ( str ) – The data to show.

Returns :

str

price_precision

The order book price precision.

Returns :

uint8

size_precision

The order book size precision.

Returns :

uint8

spread ( self )

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

Returns :

double or None

trade_side ( self , TradeTick trade ) int

Return which side of the book a trade occurred given a trade tick.

Parameters :

trade ( TradeTick ) – The trade tick.

Returns :

OrderSide

ts_last

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

Returns :

uint64_t

type

The order book type { L1_TBBO , L2_MBP , L3_MBO }.

Returns :

BookType

update ( self , BookOrder order , uint64_t update_id=0 ) void

Update the given order in the book.

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

  • update_id ( uint64 , default 0 ) – The unique ID for the update. If default 0 then will increment the last_update_id .

Error

exception BookIntegrityError

Bases: Exception

Represents an error condition where the order books integrity has been lost.

add_note ( )

Exception.add_note(note) – add a note to the exception

with_traceback ( )

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.