Matching Engine
A limit order book written in C++20: the data structure at the centre of an exchange, which decides whose order trades, at what price, and in what sequence.
- Role
- Developer
- Language
- C++20
- Scope
- Limit order book with price-time priority
- Source
- Not public
Fig. 03 · Price-time priorityBrowser simulation
Trade tape
The problem
A matching engine takes a stream of orders and decides which ones trade, in what sequence and at what price. It has to give the same answer every time.
What it does
- Orders match on price-time priority: the best price trades first, and at the same price the order that arrived first goes first.
- Market orders take whatever liquidity is resting. Limit orders trade up to their price and leave any remainder on the book.
- Resting orders can be cancelled or replaced.
- Orders come in and events go out through single-producer/single-consumer ring buffers, so the matching thread never waits on a lock.
About the demo above
The order book on this page is a small JavaScript simulation I wrote to show the matching rule in motion. It follows the same price-time priority logic, but it is not the C++ engine and its numbers are random.
No latency or throughput figures appear here, because I haven't published measured ones.