Price levels and FIFO queues
Ordered maps keep prices sorted. A FIFO queue at each level gives earlier orders priority when prices are equal. An index by order ID keeps cancellation and amendment from requiring a scan of the book.
How Hamza Memon built a C++20 exchange simulator with price-time priority, order amendments, and deterministic journal replay.
View source on GitHubSuppose two buy orders rest at 100. Order A arrived first for 10 units, followed by order B for 8. A market sell for 2 fills against A at 100. A remains at the front with 8 units, and B still has all 8. If A then increases its remaining quantity at the same price, it goes behind B.
See the runnable order sequenceOrdered maps keep prices sorted. A FIFO queue at each level gives earlier orders priority when prices are equal. An index by order ID keeps cancellation and amendment from requiring a scan of the book.
Reducing quantity at the same price keeps priority. Increasing it moves the order to the back of its price level. Changing price re-enters matching. Market and immediate-or-cancel orders expire any unfilled remainder.
A versioned append-only journal can rebuild the book and its reserved IDs by replaying commands. The journal is an educational intent log. Flushing it does not guarantee disk persistence.
The repository compares randomized and bounded exhaustive command sequences against an independent reference model. It also includes sanitizer checks and command fuzzing.
The engine is a local, single-threaded simulator for one instrument. Benchmarks use synthetic workloads. It has no live exchange connection or real-money trading.