orderbookmdp.order_book package

Submodules

orderbookmdp.order_book.constants module

All indexes for accessing information in different lists such as a trade, order etc.

BUY = 0
SELL = 1
Limit Order

[side, price, size, trader_id, order_id]

orderbookmdp.order_book.constants.Trade

(trader_id, counter_part_id, price, size, order_id, side, time)

Order in Book

(order_id, size, side, price)

orderbookmdp.order_book.constants.Quotes

(ask, ask_v, bid, bid_v)

External Trader = -1
Snapshot Order

[price, size, external_market_order_id]

orderbookmdp.order_book.market module

A limit order book market with defined order types in OrderBookRL.order_book.order_types.

The main function is to send a message to the market, receiving a possible trade or the order placed in the book. A trade and a order is simply a list contaning information about them, see OrderBookRL.order_book.constants for indexes of a trade and a order.

The abstract class Market must implement send_message. An implementation of an ExternalMarket is implemented which can be used with an external flow of messages such as level 3 orderbook data. The ExternalMarket is first filled with a snapshot of the external order book and then artificial or real messages can be sent.

orderbookmdp.order_book.market.get_ob(ob_type: str, price_levels_type: str, price_level_type: str) → orderbookmdp.order_book.order_books.OrderBook[source]

Returns an order book depending on the parameters

Parameters:
  • ob_type (str) – Type of order book.
  • price_levels_type (str) – Type or price levels for the order book.
  • price_level_type (str) – Type of order level for the price levels
Returns:

orderbook

Return type:

OrderBook

class orderbookmdp.order_book.market.Market(tick_size=0.01, ob_type='py', price_level_type='ordered_dict', price_levels_type='sorted_dict')[source]

Bases: object

An abstract market class where an implementation needs to implement send_messages. A market always has an order book which is set in __init__().

ob

OrderBook – The markets order book.

tick_size

float – The minimum multiple of price difference in a market.

tick_dec

int – Number of decimals in tick_size

multipler

int – 10**tick_dec. Used to multiply price to int

__init__(tick_size=0.01, ob_type='py', price_level_type='ordered_dict', price_levels_type='sorted_dict')[source]
Parameters:
  • tick_size (float) – The minimum multiple of price difference in a market.
  • ob_type (str) – Type of order book.
  • price_levels_type (str) – Type or price levels for the order book.
  • price_level_type (str) – Type of order level for the price levels
send_message(message: dict) -> (<class 'list'>, <class 'tuple'>)[source]

The market receives a message and returns a possible trade or an order in the book.

Parameters:message (SimpleNamespace) – Can for example be a limit order, market order, cancelation message etc. See OrderBookRL.order_book.order_types for different types
Returns:
trades: list
If trades have occurred due the the message, all the trades are returned. Otherwise empty list
order_in_book: tuple
If a limit order has been placed which size has not fully been matched, the remaining order in book is returned. Otherwise -1.
Return type:trades, order_in_book
class orderbookmdp.order_book.market.ExternalMarket(tick_size=0.01, ob_type='py', price_level_type='ordered_dict', price_levels_type='sorted_dict')[source]

Bases: orderbookmdp.order_book.market.Market

An implementation of a Market which can be used with an external flow of messages. When initiated, should be filled with a snapshot of the order book and then progressed with artificial or real messages.

external_market_order_ids

dict – Keeps track of the external order ids if for example a cancellation or update of an external order occurs.

time

str – The current time of the market

__init__(tick_size=0.01, ob_type='py', price_level_type='ordered_dict', price_levels_type='sorted_dict')[source]
Parameters:
  • tick_size (float) – The minimum multiple of price difference in a market.
  • ob_type (str) – Type of order book.
  • price_levels_type (str) – Type or price levels for the order book.
  • price_level_type (str) – Type of order level for the price levels
send_message(mess: dict, external=False) -> (<class 'list'>, <class 'tuple'>)[source]

The market receives a message and returns a possible trade or an order in the book.

Parameters:
  • mess
  • external (bool) – If the message is an external message
Returns:

trades: list

If trades have occurred due the the message, all the trades are returned. Otherwise empty list

order_in_book: tuple

If a limit order has been placed which size has not fully been matched, the remaining order in book is returned. Otherwise -1.

Return type:

trades, order_in_book

fill_snap(snap: dict)[source]

Fills the market with orders from a snapshot. The snapshot contains all limit orders in a market at a given time.

Parameters:snap (dict) – Contains all the limit orders in the market. Format: {‘asks’:[order1, order2, …], ‘bids’: [order1, order2, …]

orderbookmdp.order_book.order_books module

orderbookmdp.order_book.order_books.get_price_levels(price_levels_type: str, price_level_type: str, **kwargs) → orderbookmdp.order_book.price_levels.PriceLevels[source]

Returns a type or price levels based on the parameters

Parameters:
  • price_levels_type (str) – Type or price levels for the order book.
  • price_level_type (str) – Type of order level for the price levels
  • kwargs
Returns:

price_levels

Return type:

PriceLevels

class orderbookmdp.order_book.order_books.OrderBook(price_level_type='cydeque', price_levels_type='cylist')[source]

Bases: object

A abstract class defining a order book interface.

The main functions are the functions for the messages. Sending a limit order, market order etc. The order book handles the matching and storing of limit orders.

Important notes is the speed of adding a limit order, cancelling a limit order or updating a limit order.

orders

dict – All current orders in the order book, key is the order id

order_id

int – The internal order id set by the order book. Is incremented for each order sent to the order book.

__init__(price_level_type='cydeque', price_levels_type='cylist')[source]

Initialize self. See help(type(self)) for accurate signature.

limit(price: int, side: int, size: float, trader_id: int, time: str) -> (<class 'list'>, <class 'tuple'>)[source]

Handles a limit order sent to the order book. Matches the limit order if possible, otherwise puts it in the order book.

Parameters:
  • price (float) – Price of the order.
  • side (int) – BUY or SELL, see :py:mod:´OrderBookRL.order_book.constants´
  • size (float) – Size of the order.
  • trader_id (int) – Id of the trader sending the order
  • time (str) –
Returns:

trades: list

If trades have occurred due the the message, all the trades are returned. Otherwise empty list

order_in_book: tuple

If a limit order has been placed which size has not fully been matched, the remaining order in book is returned. Otherwise -1.

Return type:

trades, order_in_book

market_order(size: float, side: int, trader_id: int, time: str) → list[source]

Handles a market order sent to the order book. Matches the market order if possible.

Parameters:
  • side (int) – BUY or SELL, see :py:mod:´OrderBookRL.order_book.constants´
  • size (float) – Size of the order.
  • trader_id (int) – Id of the trader sending the order
  • time (str) –
Returns:

trades – If trades have occurred due the the message, all the trades are returned. Otherwise empty list

Return type:

list

market_order_funds(funds: float, side: int, trader_id: int, time: str) → list[source]

Handles a market order sent to the order book. Matches the market order if possible.

Parameters:
  • funds (float) – Size of the order.
  • side (int) – BUY or SELL, see :py:mod:´OrderBookRL.order_book.constants´
  • trader_id (int) – Id of the trader sending the order
  • time (str) –
Returns:

trades – If trades have occurred due the the message, all the trades are returned. Otherwise empty list

Return type:

list

cancel(order_id: int)[source]

Attempts to cancel a order by its order id

Parameters:order_id (int) – The order id of the order to be cancelled
update(order_id: int, size: float)[source]

Attempts to update a order by its order id

Parameters:
  • order_id (int) – The order id of the order to be updated
  • size (float) – The new size of the order
class orderbookmdp.order_book.order_books.PyOrderBook(price_level_type='cydeque', price_levels_type='cylist')[source]

Bases: orderbookmdp.order_book.order_books.OrderBook

An implementation of the abstract class OrderBook.

orders

dict – All current orders in the order book, key is the order id

order_id

int – The internal order id set by the order book. Is incremented for each order sent to the order book.

limit(price: int, side: int, size: float, trader_id: int, time: str) -> (<class 'list'>, <class 'tuple'>)[source]

Handles a limit order sent to the order book. Matches the limit order if possible, otherwise puts it in the order book.

Parameters:
  • price (float) – Price of the order.
  • side (int) – BUY or SELL, see :py:mod:´OrderBookRL.order_book.constants´
  • size (float) – Size of the order.
  • trader_id (int) – Id of the trader sending the order
  • time (str) –
Returns:

trades: list

If trades have occurred due the the message, all the trades are returned. Otherwise empty list

order_in_book: tuple

If a limit order has been placed which size has not fully been matched, the remaining order in book is returned. Otherwise -1.

Return type:

trades, order_in_book

cancel(order_id: int)[source]

Attempts to cancel a order by its order id

Parameters:order_id (int) – The order id of the order to be cancelled
update(order_id: int, size: float)[source]

Attempts to update a order by its order id

Parameters:
  • order_id (int) – The order id of the order to be updated
  • size (float) – The new size of the order
market_order(size: float, side: int, trader_id: int, time: str) → list[source]

Handles a market order sent to the order book. Matches the market order if possible.

Parameters:
  • side (int) – BUY or SELL, see :py:mod:´OrderBookRL.order_book.constants´
  • size (float) – Size of the order.
  • trader_id (int) – Id of the trader sending the order
  • time (str) –
Returns:

trades – If trades have occurred due the the message, all the trades are returned. Otherwise empty list

Return type:

list

market_order_funds(funds: float, side: int, trader_id: int, time: str) → list[source]

Handles a market order sent to the order book. Matches the market order if possible.

Parameters:
  • funds (float) – Size of the order.
  • side (int) – BUY or SELL, see :py:mod:´OrderBookRL.order_book.constants´
  • trader_id (int) – Id of the trader sending the order
  • time (str) –
Returns:

trades – If trades have occurred due the the message, all the trades are returned. Otherwise empty list

Return type:

list

orderbookmdp.order_book.order_types module

orderbookmdp.order_book.order_types.limit_message(side: int, size: float, price: float, trader_id: int) → types.SimpleNamespace[source]

Returns a limit order message

Parameters:
  • side (int) – BUY or SELL, see :py:mod:´OrderBookRL.order_book.constants´
  • size (float) – Size of the order.
  • price (float) – Price of the order
  • trader_id (int) – Id of the trader sending the order
Returns:

message – The limit order message

Return type:

SimpleNamespace

orderbookmdp.order_book.order_types.market_message(side: int, size: float, trader_id: int, funds=None)[source]

Returns a market order message

Parameters:
  • side (int) – BUY or SELL, see :py:mod:´OrderBookRL.order_book.constants´
  • size (float) – Size of the order.
  • trader_id (int) – Id of the trader sending the order
  • funds (float, optional) – If to use funds instead of size for the market order. Will be used if provided.
Returns:

message – The market order message

Return type:

SimpleNamespace

orderbookmdp.order_book.order_types.cancel_message(order_id: int) → types.SimpleNamespace[source]

Returns a cancellation message

Parameters:order_id (int) – The order id of the order to be cancelled
Returns:message – The cancellation message
Return type:SimpleNamespace
orderbookmdp.order_book.order_types.change_message(order_id: int, size: float) → types.SimpleNamespace[source]

Returns a order change message

Parameters:
  • order_id (int) – The order id of the order to be updated
  • size (float) – The new size of the order
Returns:

message – The order change message

Return type:

SimpleNamespace

orderbookmdp.order_book.price_level module

class orderbookmdp.order_book.price_level.PriceLevel[source]

Bases: object

A price level containing orders.

A price level contains orders with the same price. The total size of the level is the sum of all order sizes.

size

float – The total size of all orders in the price level.

orders

All the orders in the price level.

__init__()[source]

Initializes the size to 0.

append(order: list)[source]

Adds an order to the end of the price level and adds the size.

Parameters:order (list) – The order to be added.
delete(order: list)[source]

Deletes an order from the price level and removes the size.

Parameters:order (list) – The order to be added.
update(order, diff: float)[source]

Updates an order from the price level with an added difference.

Parameters:
  • diff
  • order (list) – The order to be added.
get_first()[source]

Returns the first order of the price level

Returns:The first order of the price level.
Return type:order
delete_first(order: list)[source]

Deletes the first order from the price level and removes the size.

Parameters:order (list) – The order to be removed.
is_not_empty() → bool[source]

Returns true if the price level is not empty

Returns:True if successful, False otherwise.
Return type:bool
get_last()[source]

Returns the last order of the price level

Returns:The last order of the price level.
Return type:order
delete_last(order: list)[source]

Deletes the last order from the price level and removes the size.

Parameters:order (list) – The order to be removed.
is_empty()[source]

Returns true if the price level is empty

Returns:True if successful, False otherwise.
Return type:bool
class orderbookmdp.order_book.price_level.OrderedDictLevel[source]

Bases: orderbookmdp.order_book.price_level.PriceLevel

A price level containing orders.

A price level contains orders with the same price. The total size of the level is the sum of all order sizes.

size

float – The total size of all orders in the price level.

orders

All the orders in the price level.

is_empty()[source]

Returns true if the price level is empty

Returns:True if successful, False otherwise.
Return type:bool
__init__()[source]

Initializes the size to 0.

is_not_empty() → bool[source]

Returns true if the price level is not empty

Returns:True if successful, False otherwise.
Return type:bool
get_first() → list[source]

Returns the first order of the price level

Returns:The first order of the price level.
Return type:order
get_last()[source]

Returns the last order of the price level

Returns:The last order of the price level.
Return type:order
class orderbookmdp.order_book.price_level.DequeLevel[source]

Bases: orderbookmdp.order_book.price_level.OrderedDictLevel

A price level containing orders.

A price level contains orders with the same price. The total size of the level is the sum of all order sizes.

size

float – The total size of all orders in the price level.

orders

All the orders in the price level.

__init__()[source]

Initializes the size to 0.

get_first()[source]

Returns the first order of the price level

Returns:The first order of the price level.
Return type:order
get_last()[source]

Returns the last order of the price level

Returns:The last order of the price level.
Return type:order
class orderbookmdp.order_book.price_level.SortedTradesLevel[source]

Bases: orderbookmdp.order_book.price_level.DequeLevel

A price level containing orders.

A price level contains orders with the same price. The total size of the level is the sum of all order sizes.

size

float – The total size of all orders in the price level.

orders

All the orders in the price level.

__init__()[source]

Initializes the size to 0.

orderbookmdp.order_book.price_levels module

orderbookmdp.order_book.price_levels.get_price_level(price_level_type: str) → orderbookmdp.order_book.price_level.PriceLevel[source]

Returns a price level based on parameter

Parameters:price_level_type (str) – The type of price level to be used
Returns:price_level
Return type:PriceLevel
class orderbookmdp.order_book.price_levels.PriceLevels(price_level_type)[source]

Bases: abc.ABC

Price levels containing order levels for different prices.

The speed of the price level is of importance. Removing orders, acessing price levels are important functions.

price_level_constructor

constructor – A constructor to create a price level

__init__(price_level_type)[source]

Sets the price level constructor.

Parameters:price_level_type (str) – The type of price level to be used
get_level(side: int, price: int) → orderbookmdp.order_book.price_level.PriceLevel[source]

Returns the price level for a given side and price. :param side: BUY or SELL, the side of the level to get. :type side: int :param price: The price of the price level to get. :type price: int

Returns:price_level
Return type:PriceLevel
remove_level(side: int, price: int)[source]

Removes a price level for a given side and price. :param side: BUY or SELL, the side of the level to remove. :type side: int :param price: The price of the price level to remove. :type price: int

add_order(side: int, price: int, size: float, trader_id: int, order_id: int) → list[source]

Adds a order to a price level.

Parameters:
  • side (int) – BUY or SELL, see :py:mod:´OrderBookRL.order_book.constants´
  • size (float) – Size of the order.
  • price (float) – Price of the order
  • trader_id (int) – Id of the trader sending the order
  • order_id (int) – Id of the order to be added
Returns:

order – The added order

Return type:

list

get_ask() → int[source]

Gets the lowest sell price. :returns: ask :rtype: int

get_bid() → int[source]

Gets the highest buy price. :returns: bid :rtype: int

get_quotes() → tuple[source]

Returns the a tuple of quotes, (ask, ask_v, bid, bid_v) :returns: quotes :rtype: tuple

get_snap() → dict[source]

Returns the current snapshot of the order book.

Returns:snap – Contains all the limit orders in the market. Format: {‘asks’:[order1, order2, …], ‘bids’: [order1, order2, …]
Return type:dict
exist_buy_orders() → bool[source]

Returns true if there exists buy orders

Returns:True if successful, False otherwise.
Return type:bool
exist_sell_orders() → bool[source]

Returns true if there exists sell orders

Returns:True if successful, False otherwise.
Return type:bool
get_prices(side: int) → int[source]

Yields all the prices of a given side.

Parameters:side (int) – BUY or SELL, which side to get prices for
Yields:price (int)
print_quotes(quotes)[source]
class orderbookmdp.order_book.price_levels.SortedDictPriceLevels(price_level_type, max_price=1500000, min_price=300000, **kwargs)[source]

Bases: orderbookmdp.order_book.price_levels.PriceLevels

__init__(price_level_type, max_price=1500000, min_price=300000, **kwargs)[source]

Sets the price level constructor.

Parameters:price_level_type (str) – The type of price level to be used
get_level(side: int, price: float) → orderbookmdp.order_book.price_level.PriceLevel[source]

Returns the price level for a given side and price. :param side: BUY or SELL, the side of the level to get. :type side: int :param price: The price of the price level to get. :type price: int

Returns:price_level
Return type:PriceLevel
add_order(side: int, price: float, size: float, trader_id: int, order_id: int) → list[source]

Adds a order to a price level.

Parameters:
  • side (int) – BUY or SELL, see :py:mod:´OrderBookRL.order_book.constants´
  • size (float) – Size of the order.
  • price (float) – Price of the order
  • trader_id (int) – Id of the trader sending the order
  • order_id (int) – Id of the order to be added
Returns:

order – The added order

Return type:

list

remove_level(side: int, price: float)[source]

Removes a price level for a given side and price. :param side: BUY or SELL, the side of the level to remove. :type side: int :param price: The price of the price level to remove. :type price: int

get_ask() → float[source]

Gets the lowest sell price. :returns: ask :rtype: int

get_bid() → float[source]

Gets the highest buy price. :returns: bid :rtype: int

get_snap() → dict[source]

Returns the current snapshot of the order book.

Returns:snap – Contains all the limit orders in the market. Format: {‘asks’:[order1, order2, …], ‘bids’: [order1, order2, …]
Return type:dict
exist_buy_orders() → bool[source]

Returns true if there exists buy orders

Returns:True if successful, False otherwise.
Return type:bool
exist_sell_orders() → bool[source]

Returns true if there exists sell orders

Returns:True if successful, False otherwise.
Return type:bool
get_prices(side: int) → int[source]

Yields all the prices of a given side.

Parameters:side (int) – BUY or SELL, which side to get prices for
Yields:price (int)
get_quotes() → list[source]

Returns the a tuple of quotes, (ask, ask_v, bid, bid_v) :returns: quotes :rtype: tuple

class orderbookmdp.order_book.price_levels.RBTreePriceLevels(price_level_type, **kwargs)[source]

Bases: orderbookmdp.order_book.price_levels.SortedDictPriceLevels

__init__(price_level_type, **kwargs)[source]

Sets the price level constructor.

Parameters:price_level_type (str) – The type of price level to be used
get_ask() → float[source]

Gets the lowest sell price. :returns: ask :rtype: int

get_bid() → float[source]

Gets the highest buy price. :returns: bid :rtype: int

class orderbookmdp.order_book.price_levels.AVLTreePriceLevels(price_level_type, **kwargs)[source]

Bases: orderbookmdp.order_book.price_levels.RBTreePriceLevels

__init__(price_level_type, **kwargs)[source]

Sets the price level constructor.

Parameters:price_level_type (str) – The type of price level to be used
class orderbookmdp.order_book.price_levels.ListPriceLevels(price_level_type, tick_size=0.01, max_price=1500000, min_price=300000)[source]

Bases: orderbookmdp.order_book.price_levels.PriceLevels

__init__(price_level_type, tick_size=0.01, max_price=1500000, min_price=300000)[source]

Sets the price level constructor.

Parameters:price_level_type (str) – The type of price level to be used
get_price_index(price: int) → int[source]
get_price(index: int) → float[source]
get_level(side: int, price: int) → orderbookmdp.order_book.price_level.PriceLevel[source]

Returns the price level for a given side and price. :param side: BUY or SELL, the side of the level to get. :type side: int :param price: The price of the price level to get. :type price: int

Returns:price_level
Return type:PriceLevel
is_empty(index)[source]
remove_level(side: int, price: int)[source]

Removes a price level for a given side and price. :param side: BUY or SELL, the side of the level to remove. :type side: int :param price: The price of the price level to remove. :type price: int

add_order(side: int, price: float, size: float, trader_id: int, order_id: int) → list[source]

Adds a order to a price level.

Parameters:
  • side (int) – BUY or SELL, see :py:mod:´OrderBookRL.order_book.constants´
  • size (float) – Size of the order.
  • price (float) – Price of the order
  • trader_id (int) – Id of the trader sending the order
  • order_id (int) – Id of the order to be added
Returns:

order – The added order

Return type:

list

get_ask() → int[source]

Gets the lowest sell price. :returns: ask :rtype: int

get_bid() → int[source]

Gets the highest buy price. :returns: bid :rtype: int

get_snap() → dict[source]

Returns the current snapshot of the order book.

Returns:snap – Contains all the limit orders in the market. Format: {‘asks’:[order1, order2, …], ‘bids’: [order1, order2, …]
Return type:dict
exist_buy_orders() → bool[source]

Returns true if there exists buy orders

Returns:True if successful, False otherwise.
Return type:bool
exist_sell_orders() → bool[source]

Returns true if there exists sell orders

Returns:True if successful, False otherwise.
Return type:bool
get_indexes(side: int)[source]
get_prices(side: int) → float[source]

Yields all the prices of a given side.

Parameters:side (int) – BUY or SELL, which side to get prices for
Yields:price (int)
get_quotes() → list[source]

Returns the a tuple of quotes, (ask, ask_v, bid, bid_v) :returns: quotes :rtype: tuple

orderbookmdp.order_book.utils module

orderbookmdp.order_book.utils.to_int(price: float, multiplier: int)[source]

Converts a price to an integer.

\(price_{int} = int(price_{float}*multiplier)\)

Parameters:
  • price (float) –
  • multiplier (int) –
Returns:

price

Return type:

int

orderbookmdp.order_book.utils.to_float(price: int, tick_dec: int, multiplier: int)[source]

Converts a price to an float.

\(price_{float} = price_{int}/multiplier\)

Parameters:
  • price (int) –
  • tick_dec (int) –
  • multiplier (int) –
Returns:

price

Return type:

float

Module contents