Skip to content
All library documents

Queue-Reactive Models and Limit Order Book Simulation

Article Quant Q&A · Author: Milosz

Summary

The discussion distinguishes modeling order book dynamics from implementing a matching engine. It introduces the Queue Reactive model as a way to simulate a limit order book: trade, insertion, and cancellation events at each price queue occur with intensities that depend on the book’s shape. A model can track a selected set of queues, with neighboring levels influencing one another; the amount of depth to represent depends in part on tick size and spread conditions. This provides a framework for studying book evolution and potentially generating data for mid-price prediction.

The text also outlines basic matching-engine structures and operations: price levels hold ordered queues of orders, incoming orders may join a same-side queue or execute against the opposite side, and trades remove matched orders. It notes that connecting a simulator to a FIX engine for algorithm backtesting is a more difficult integration task. The post offers no Python implementation, predictive evaluation, calibration procedure, or benchmark. It is primarily a conceptual pointer, and model usefulness depends on realistic event intensities, market data, and execution assumptions.

Key ideas

  • The Queue Reactive model makes order event intensities depend on the current order book shape.
  • Trade, insertion, and cancellation events can be modeled at individual price queues.
  • The number of queues to simulate depends on spread and tick-size conditions.
  • A matching engine organizes orders by price and time priority and processes incoming orders against the book.
  • Connecting a simulator to a FIX engine is an additional challenge for backtesting live algorithm logic.

Tags

Full text
# Limit Order Book modeling


# Limit Order Book modeling












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.

## Answer by lehalle (score 5)

https://quant.stackexchange.com/a/77009

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:

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

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.