01 · C++20 · Matching engine
OrderBook
A price-time-priority limit order-book matching engine that replays a ClickHouse-backed trade tape and streams every fill as JSON, with a live browser GUI for the resulting depth ladder.
Built during a summer stint researching high-frequency backtesting infrastructure for A-share statistical arbitrage, where a hash-map-optimized order book with near-O(1) lookups and a ClickHouse-based columnar store were the two pieces of plumbing everything else depended on. This project is that order book, built out as a standalone matching engine.
The orderbook executable streams a configured ClickHouse table in chronological order,
runs each row through the matching engine, and writes every generated trade to standard output as one
JSON object per line. Because the source order IDs restart on each trading date, the executable runs
one matching engine per date — completed session engines stay in memory, so orders from different
dates can never match each other.
Matching
MatchingEngine accepts owned orders and keeps every buy and sell in its
OrderBook, including filled and cancelled orders. Each execution is appended to
Trades, so a partially filled order can appear in more than one trade record. Recalling
an order marks it CANCELLED without removing its queue entry — matching and the GUI
depth ladder ignore cancelled orders, and stale queue entries are discarded lazily when matching
reaches them.
Live view
The executable opens a local browser GUI that polls for updates every 100 ms. Trades are grouped into 100 ms intervals using their ClickHouse market timestamps; the chart keeps every sample from program startup rather than rolling forward, so the first point stays visible. A classic depth ladder shows five asks above the spread and five bids below it, best prices nearest the center, with the total quantity at each level.
Backtest commissions
A second executable, backtest_commissions, reads commission rows through ClickHouse's
HTTP interface using HTTP Basic auth, with connection details kept outside the source. Output is JSON
Lines with values represented as strings, so ClickHouse decimal values don't lose precision on the
way out; nullable values come through as JSON null.