C++20 limit order book and matching engine with price-time priority. Supports multiple order types, O(1) cancel via stored level iterators, optional Good-For-Day pruning, and a Google Benchmark / GoogleTest harness.
This is a correctness-first educational matcher (map-backed price levels, shared_ptr orders, optional mutex). Numbers below are single-threaded, in-memory and are not exchange wire-to-book latency.
- Order types: GTC, FAK, FOK, GFD, Market
- Price-time priority matching with partial fills and execution reports
- Cancel / modify with O(1) erase from a price level (list iterator in the order id map)
- Optional mutex-backed API locking and GFD prune thread (
OrderbookConfig) OrderPoolto reuseOrderstorage and cut heap churn on hot paths- Scenario + invariant tests, Google Benchmark targets
- CMake 3.20+
- C++20 compiler (GCC/MinGW 14+, Clang, or MSVC)
- Git (FetchContent pulls GoogleTest and Google Benchmark)
Visual Studio solution (Orderbook.sln) remains available; CMake is the recommended path for tests and benches.
cmake -G "MinGW Makefiles" -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=g++
cmake --build build -j| Target | Description |
|---|---|
orderbook |
Core library |
orderbook_demo |
Small smoke demo |
orderbook_tests |
Unit / scenario tests |
orderbook_bench |
Throughput and latency benchmarks |
cmake -B build -DORDERBOOK_BUILD_TESTS=OFF -DORDERBOOK_BUILD_BENCH=OFF./build/orderbook_testsCovers 7 file scenarios (GTC/FAK/FOK/Market/Cancel/Modify) plus invariants: price-time FIFO, FOK miss leaves book unchanged, cancel of missing id, partial-fill size/qty consistency, and order pool reuse (12 tests).
./build/orderbook_benchMeasured on a modern desktop (Release, prune thread off, locking off, OrderPool, in-memory):
| Path | Approx. rate / latency |
|---|---|
| Resting GTC add | ~1.9M ops/s |
| Cancel by id | ~7.6M ops/s |
| Aggressive match (crossed) | ~1.3M ops/s |
| Add + cancel | median ~400 ns, p99 ~700 ns |
Orderbook.{h,cpp} matching engine
Order.h / OrderPool.h order + preallocated pool
OrderbookTest/ original MSVC scenario files
tests/ GoogleTest (invariants + scenarios)
bench/ Google Benchmark
See LICENSE.