Exchange-grade CLOB matching engine + ITCH-style market data replay + microstructure analytics in modern C++20.
π Live Interactive Dashboard β 3D order book surface, Kyle's lambda landscape, spread decomposition, stylized facts.
A complete market microstructure laboratory: from order entry to trade print, from raw event feeds to empirical spread decomposition, built with the rigor of production exchange systems and the analytical depth of graduate-level financial economics.
3D Limit Order Book Surface - Bid (blue) and ask (red) depth across price levels over time:
3D Price Impact Surface - Kyle's lambda: impact increases with volume (concave, square-root law) and amplifies with directional imbalance:
Spread Decomposition - Effective spread decomposed into realized spread (MM revenue) and price impact (adverse selection β 68%):
Stylized Facts - Fat-tailed returns (ΞΊ β 12 vs Gaussian) and volatility clustering (positive ACF of |returns|):
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β MicroExchange Architecture β
β β
β ββββββββββββββββ ββββββββββββββββββββ ββββββββββββββββββββ β
β β Simulation βββββΆβ Matching Engine βββββΆβ Market Data Feed β β
β β (Hawkes / β β (CLOB + FIFO) β β (ITCH-style) β β
β β ZI agents) β β β β β β
β ββββββββββββββββ β β’ Limit/Market β β β’ Incremental β β
β β β’ IOC / FOK β β β’ Snapshots β β
β ββββββββββββββββ β β’ Amend/Cancel β β β’ Trade prints β β
β β ITCH Replay βββββΆβ β’ Partial fills β ββββββββββ¬βββββββββββ β
β β (historical β ββββββββββββββββββββ β β
β β data) β βΌ β
β ββββββββββββββββ βββββββββββββββββββββββ
β β Analytics ββ
β β β’ Spread decomp ββ
β β β’ Price impact ββ
β β β’ Kyle's Ξ» ββ
β β β’ Order imbalance ββ
β β β’ Stylized facts ββ
β βββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Bid side (blue) and ask side (red) form the characteristic valley around the midpoint. Depth clusters at key levels and shifts with the price drift.
Price impact as a function of trade volume and order flow imbalance. The concave shape demonstrates the square-root law of impact (Bouchaud et al., 2018) β larger trades have diminishing marginal impact, amplified by directional imbalance.
Effective spread decomposed into realized spread (market maker revenue) and price impact (adverse selection). The adverse selection component dominates at ~68%.
Left: return distribution vs Gaussian β heavy tails from Hawkes-driven clustering. Right: autocorrelation of |returns| showing slow decay characteristic of ARCH effects.
| Domain | Concept | Implementation |
|---|---|---|
| Market Structure | Price-time priority (FIFO) | core/OrderBook with deterministic sequencing |
| Market Structure | Queue position tracking | Per-level FIFO queues with sequence numbers |
| Liquidity | Quoted spread | Real-time BBO tracking in analytics/SpreadAnalyzer |
| Liquidity | Effective spread | Trade-midpoint deviation analysis |
| Liquidity | Depth & resilience | Post-trade book recovery metrics |
| Price Formation | Realized spread | 5-second post-trade midpoint reversion |
| Price Formation | Price impact (permanent) | Effective β Realized spread decomposition |
| Information | Order flow imbalance (OFI) | Signed volume aggregation β return prediction |
| Information | Kyle's Ξ» | Regression: ΞP = Ξ» Β· signed_volume + Ξ΅ |
| Adverse Selection | Glosten-Milgrom intuition | Spread widens with information asymmetry in simulation |
| Inventory | Ho-Stoll / Avellaneda-Stoikov | Quote skewing under inventory risk in MM agent |
| Stylized Facts | Fat tails, vol clustering | Hawkes arrival process + empirical verification |
| Stylized Facts | Spread under stress | Endogenous widening with order imbalance |
Most GitHub "matching engines" are toy implementations β a sorted map, a match loop, and a README. This project bridges three disciplines:
- Systems engineering β Lock-free queues, arena allocation, cache-aligned structures, deterministic replay, property-based invariant testing
- Financial economics β Spread decomposition, adverse selection models, information-based trading theory (Glosten-Milgrom, Kyle, Ho-Stoll)
- Quantitative research β Reproducible empirical analysis, stylized fact generation, microstructure model calibration
-
Volatility clustering is weak: The AC(|r|) at lag 1 is ~0.02, well below the empirical 0.15-0.40 range. The Hawkes process generates clustered arrivals but the ZI agents don't modulate aggressiveness with volatility. A regime-switching model or agents that condition on recent returns would help.
-
Kyle's lambda RΒ² is near zero: The midprice indexing uses event count rather than wall clock time, so the interval bucketing doesn't align properly. Needs timestamp-based aggregation.
-
FeedPublisher overwrites OrderBook callbacks: The
attach()method callsbook.set_trade_callback()which clobbers the engine's internal routing. Needs a multi-subscriber pattern (vector of callbacks, or an event bus). Disabled in main.cpp for now. -
Arena allocator never frees: Orders accumulate in the arena for the lifetime of the process. Fine for simulation (it exits) but would need periodic cleanup or epoch-based reclamation for production.
-
No proper order tracking per agent: The cancellation logic in the simulator is approximate β agents don't track their own outstanding orders, so cancel rates are estimates.
Requires C++20 and CMake 3.20+.
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j$(nproc)Or without CMake:
g++ -std=c++20 -O2 -I core/include -I md/include -I sim/include -I analytics/include \
src/main.cpp -o build/micro_exchange# Generate 1M orders via Hawkes process, match, and compute analytics
./bin/micro_exchange --mode simulate --orders 1000000 --symbol AAPL
# Replay ITCH-format historical data
./bin/micro_exchange --mode replay --file data/sample_itch.bin
# Run full analytics pipeline
./bin/micro_exchange --mode analyze --input results/trades.csv./bin/test_matching_engine # Property-based invariant tests
./bin/test_fuzz_orders # Fuzz random order sequences
./bin/bench_throughput # Single-thread matching throughput
./bin/bench_latency # Latency histogram (p50/p95/p99/p999)Single-thread matching throughput: 2.24M orders/sec (1M order run)
Median latency: 255 ns
P95 latency: 654 ns
P99 latency: 876 ns
P99.9 latency: 1,371 ns
590K orders β 210K trades
Metric Value (ticks)
βββββββββββββββββββββββββββββββββββββ
Quoted spread 1.06
Effective spread 2.51
Realized spread 1.73
Price impact 0.56
Adverse selection % 22.3%
Excess kurtosis: 78.5 (benchmark: > 0) β
AC(|r|, lag=1): 0.02 (benchmark: 0.15+) β (see Known Issues)
AC(|r|, lag=10): 0.03 (benchmark: > 0) β
- Intrusive doubly-linked list for price levels β O(1) insert/remove at known position; avoids
std::mapoverhead and heap fragmentation - Arena allocator for Order objects β Pre-allocated slab; zero malloc on the hot path; deterministic deallocation
- SPSC lock-free ring buffer for MD feed β Single-producer/single-consumer between matching thread and feed handler; no mutex contention
- Compile-time order type dispatch β
if constexpreliminates branch misprediction for known order types - Contiguous price level array β Cache-friendly iteration for BBO updates and book snapshots
- Sequence numbers on every event β Enables deterministic replay, gap detection, and recovery
| Test Category | What It Verifies |
|---|---|
| Invariant: No crossed book | After every match cycle, best bid < best ask |
| Invariant: FIFO preserved | Orders at same price fill in arrival order |
| Invariant: Deterministic | Same input stream β identical output on every run |
| Fuzz: Random sequences | 10M random order events with invariant checks |
| Replay consistency | Reconstructed book from incremental feed matches snapshot |
| Metric cross-check | Effective spread = quoted spread (for market orders at BBO) |
| Conservation | Total filled quantity = sum of both sides of every trade |
MicroExchange/
βββ core/ # Matching engine
β βββ include/
β β βββ Order.h # Order types, side, TIF
β β βββ OrderBook.h # CLOB with price-time priority
β β βββ MatchingEngine.h # Multi-symbol engine facade
β β βββ PriceLevel.h # Intrusive linked-list level
β β βββ ArenaAllocator.h # Slab allocator for orders
β βββ tests/
β βββ test_invariants.cpp # Property-based + fuzz tests
βββ md/ # Market data feed
β βββ include/
β βββ FeedMessage.h # ITCH-style wire protocol
β βββ FeedPublisher.h # Incremental + snapshot publisher
β βββ SPSCRingBuffer.h # Lock-free SPSC queue
βββ sim/ # Event-driven simulation
β βββ include/
β βββ HawkesProcess.h # Clustered arrivals
β βββ ZIAgent.h # Zero-intelligence trader
β βββ Simulator.h # Orchestrator (unused, see main.cpp)
βββ analytics/ # Microstructure metrics
β βββ include/
β βββ SpreadAnalyzer.h # Huang-Stoll decomposition
β βββ ImpactAnalyzer.h # Kyle's lambda
β βββ ImbalanceAnalyzer.h # OFI analysis
β βββ StylizedFacts.h # Fat tails, vol clustering
βββ src/
β βββ main.cpp # CLI entry point
βββ bench/
β βββ bench_throughput.cpp # Performance benchmarks
βββ research/
β βββ microstructure_paper.md # Theory + empirical writeup
βββ output/ # Generated by simulation
β βββ trades.csv
β βββ midprices.csv
β βββ spreads.csv
β βββ report.txt
βββ docs/
β βββ visualizations.html # Interactive charts
βββ CMakeLists.txt
βββ CHANGELOG.md
βββ .gitignore
βββ LICENSE
βββ README.md
See research/microstructure_paper.md for a 12-page writeup covering:
- Price formation theory (Glosten-Milgrom, Kyle, Ho-Stoll)
- Spread decomposition methodology (Huang-Stoll, realized spread)
- Empirical results from simulation
- Stylized fact reproduction and model calibration
- Limitations and extensions
- Glosten, L. & Milgrom, P. (1985). Bid, ask and transaction prices in a specialist market with heterogeneously informed traders.
- Kyle, A. (1985). Continuous auctions and insider trading.
- Ho, T. & Stoll, H. (1981). Optimal dealer pricing under transactions and return uncertainty.
- Avellaneda, M. & Stoikov, S. (2008). High-frequency trading in a limit order book.
- Hasbrouck, J. (2007). Empirical Market Microstructure.
- Bouchaud, J.-P. et al. (2018). Trades, Quotes and Prices: Financial Markets Under the Microscope.
- Hawkes, A. (1971). Spectra of some self-exciting and mutually exciting point processes.
MIT License. See LICENSE.




