Skip to content

A single-threaded C++ limit order matching engine supporting market, limit, and cancel events with price–time priority. Features deterministic feed replay and a benchmarking harness measuring throughput and tail latency (p50/p95/p99) across synthetic market workloads with varying price dispersion, liquidity, and cancellation pressure.

Notifications You must be signed in to change notification settings

Sjk4824/cpp_order_matching_engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Limit Order Book

A price–time priority limit order book implemented in modern C++.
The engine supports order matching, partial fills, cancellations, deterministic replay, and unit testing.

This project focuses on correctness, clarity, and systems-level design, rather than premature optimisation or networking concerns.


Features

  • Price–time priority matching
  • Limit and market orders
  • Partial and full fills
  • Order cancellation by ID
  • Trade generation
  • Deterministic execution
  • CSV-driven input
  • Unit-tested using Catch2

Core Data Structures

std::map<double, std::deque<Order>, std::greater<double>> bids;
std::map<double, std::deque<Order>> asks;
std::unordered_map<uint64_t, std::pair<Side, double>> orderIndex;

std::map enforces price priority std::deque enforces FIFO (time priority) at each price level orderIndex enables fast lookup for cancellation


Matching Logic Overview

For each incoming order:

  1. Validate order
  2. While the book is crossed:
  3. Match against the top-of-book
  4. Generate trades
  5. Handle partial fills
  6. Remove filled orders
  7. Insert the remaining quantity if the order is a limit order

Trade price is always the resting order price, following standard market convention.

Correctness Guarantee

The engine enforces the following invariants:

  1. No negative quantities
  2. No zero-quantity orders remain in the book
  3. No crossed book after matching
  4. FIFO ordering is preserved within each price level
  5. Deterministic behaviour for identical input sequences

Input Format (CSV)

Orders are read from a CSV file passed via the command line:

./bench data/{fileName of data you want to test}

Various data files are provided for reference, differing in order book depth, price range, and inclusion of cancellation orders.

Performance Notes

This implementation prioritises correctness and clarity.

  1. Tree-based price levels (std::map) are used intentionally
  2. Sensitivity to price-level cardinality is acknowledged
  3. No premature optimisation or lock-free structures

The design is suitable for:

  1. Learning
  2. Deterministic simulation
  3. Interview and portfolio use

Build Instructions:

  1. C++17 or newer
  2. CMake
  3. Catch2 (for tests)
mkdir build
cd build
cmake ..
make
cd ..
./bench orders.csv

About

A single-threaded C++ limit order matching engine supporting market, limit, and cancel events with price–time priority. Features deterministic feed replay and a benchmarking harness measuring throughput and tail latency (p50/p95/p99) across synthetic market workloads with varying price dispersion, liquidity, and cancellation pressure.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages