A C++20 order book and paper market maker that reads market data. It's for research only and never sends an order to a broker.
Live trades arrive over a Finnhub WebSocket, get parsed once, and pass through a lock-free ring buffer. A recorded session comes from a binary event log through a replay engine. Both feed the same pipeline: order book, market metrics, strategy, risk checks, paper execution, then P&L and a report.
Live and replay share that one pipeline, and the symbol is a runtime flag, so nothing is tied to a single stock.
I'm keeping this list on purpose.
My first README said a 5-million-event replay ran at 10.0 to 10.8 million events a second, with a book of about 40 levels. Both were wrong. The synthetic generator never deleted stale levels, so its book was crossed after 99.9991% of events and grew past 2,600 levels. I was benchmarking nonsense.
I rewrote the generator to keep a consistent book and added a test that checks, over 400,000 events, that the book stays uncrossed and under 30 levels. Every number below comes from the fixed generator.
A flat vector for the book, not std::map
std::map was 3 to 7% faster at applying an update. The flat vector was about twice as fast to read. This pipeline mostly reads, so the vector won. I picked it by measuring.
A lock-free ring buffer between I/O and the pipeline
The I/O thread should never wait on a lock the consumer holds. The cost is a fixed size, dropping data on overflow, and exactly one producer and one consumer. Head and tail sit on separate cache lines: 128 bytes on Apple Silicon, whose real line size is 128 even though the standard library says 64.
Replay is deterministic
Events are 50-byte binary records, processed in file order using only their own timestamps, with a seeded random generator. Results are identical from run to run on one toolchain, and the speed flag only changes pacing. Tests check that the final state matches across two runs and across speeds.
All measured on an Apple M4. The system timer ticks at about 41.7 ns, so treat these as rough, and rerun them on your own machine.