0
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. $\endgroup$
    – Community Bot
    Oct 13, 2023 at 20:42

1 Answer 1

5
$\begingroup$

The Queue Reactive (QR) model (by Huang, L and Rosenbaum) is probably the most reliable to simulate order book dynamics. See this answer Order Book Dynamics for specific details.

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:

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:

  1. look for the price level
    • if it does not exist create it, assign its side and add the order to the list
  2. 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
  3. 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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.