Projects

C++ Exchange Matching Engine

How Hamza Memon built a C++20 exchange simulator with price-time priority, order amendments, and deterministic journal replay.

View source on GitHub

What happens when a market sell arrives?

Suppose 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 sequence

Design decisions

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.

Amendments and immediate execution

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.

Replayable commands

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.

Testing the matching rules

The repository compares randomized and bounded exhaustive command sequences against an independent reference model. It also includes sanitizer checks and command fuzzing.

Scope and validation

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.