C++ / Trading Systems Project

C++ Matching Engine

A deterministic limit order book and matching engine in C++ that models price-time priority, partial fills, cancellations, seeded order flow, and measurable engine throughput.

Limit order book system overview

Key Features

  • Price-time priority matching with best price first
  • FIFO execution within each price level
  • Partial fills across resting liquidity
  • Cancellation support for resting orders
  • Deterministic simulation using a fixed random seed
Matching engine order flow diagram

Matching Flow

  • Incoming buy orders cross against the best ask while price permits
  • Incoming sell orders cross against the best bid while price permits
  • Fill quantity is the minimum of incoming and resting quantity
  • Fully filled resting orders are removed from the front of the queue
  • Unfilled incoming quantity rests at its limit price

Architecture

  • Bid side uses descending ordered price levels
  • Ask side uses ascending ordered price levels
  • Each price level stores orders in FIFO queues
  • Simulator generates seeded add and cancel events
  • Engine reports trade count, volume, spread, average trade size, and throughput
Example Engine Output
=== SIMULATION CONFIG ===

Seed:                         42
Events:                       100000

=== SIMULATION SUMMARY ===
Add Orders Processed:         80103
Cancel Attempts:              19897
Successful Cancels:           5009

=== TOP OF BOOK ===
Best Bid:                     95
Best Ask:                     96
Final Spread:                 1

=== ENGINE METRICS ===
Trades Executed:              58189
Total Traded Volume:          177043
Average Trade Size:           3.042551

Elapsed Time:                 0.051799s
Throughput:                   1,930,535.47 events/sec

Goal: model core trading-engine behavior while keeping correctness,
determinism, and measurable system performance visible.

Highlights

  • Built deterministic C++ limit order book with price-time priority matching
  • Implemented partial fills, resting order insertion, and cancellation support
  • Modeled bid and ask books with sorted price levels and FIFO queues
  • Simulated 100K seeded order events for reproducible benchmark runs
  • Tracked trades executed, traded volume, final spread, average trade size, and throughput
  • Separated matching logic from simulation flow for cleaner system structure
  • Documented future paths for indexed cancellation, cache-friendly storage, and multi-instrument support

Tech

C++17 CMake Order Book Simulation Performance
Execution model icon

Execution Model

  • Best price is matched first
  • FIFO preserves time priority
  • Partial fills carry remaining quantity forward
  • Spread is derived from best ask minus best bid
Learning icon

What I Learned

  • Correct matching rules matter before raw speed
  • Deterministic seeds make performance easier to reason about
  • Cancellation lookup becomes a real scaling bottleneck
  • Data structure choices shape both clarity and throughput
Tech stack icon

Tech Stack

  • C++17
  • CMake
  • std::map and std::deque
  • chrono benchmark timing
  • Seeded random simulation