Does anyone know where to find an example of LOB modeling in python? I would like to create machine learning model to predict mid-price.
Let just say that the principle of the model is
- Events (trade, insert, cancel) on each price limite (ie queue) have an intensity (i.e. probability to occur by unit of time) that is a function of the shape of the orderbook.
- You can maintain the number of limits that you want in this model, knowing that the original paper says that empirically it seems enough to maintain the queues just before and just after each one (for instance: the best bid is influenced by the second bid and the best ask only)
- if you do not simulate "large ticks" (ie when the bid-ask spread is between 1 and 2 ticks), you will have to maintain a lot of queues.
This is not very difficult to implement the QR model (I can help if you try). But maybe what you are looking for is a python code to simulate a Matching Engine. They are some outside:
- https://github.com/gavincyi/LightMatchingEngine - 300 stars, last update 2 years ago
- https://github.com/dyn4mik3/OrderBook - 325 stars, last update 4 years ago
- https://github.com/tigeryant/order-matching-engine - 14 stars, last update 2 years ago
- https://github.com/pgaref/orderbook - 57 stars, last updates 4 years ago
- https://github.com/ridulfo/Order-Matching-Engine - 42 stars, last update 3 years ago
- https://github.com/chintai-platform/OrderBookMatchingEngine - 4 stars, created less that 1 year ago
They are a lot because it is not very difficult to implement a Matching Engine (ME); you need
- a dictionary for price levels
- each price level has only one side (bid or ask)
- each price level contains the ordered list of orders (for time priority reasons)
- an order is its side, its ID and the ID of its owner, and potentially some properties (like time in force).
See Market Microstructure in Practice by L and Laruelle, 2nd edition for details.
Each time the ME receives a message:
- look for the price level
- if it does not exist create it, assign its side and add the order to the list
- if the price level exist: look if it has the same side than the order
- if yes add the new order at the end of the list
- if the sides are opposite
- generate a trade, tell it to order owners, and remove them from the list.
This question is related to implementation details: Red Black Trees for Limit Order Book
The difficulty is when you want to plug it on a FIX engine, to backtest the code of your real algorithms in front of it.