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.
- 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
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
For each incoming order:
- Validate order
- While the book is crossed:
- Match against the top-of-book
- Generate trades
- Handle partial fills
- Remove filled orders
- Insert the remaining quantity if the order is a limit order
Trade price is always the resting order price, following standard market convention.
The engine enforces the following invariants:
- No negative quantities
- No zero-quantity orders remain in the book
- No crossed book after matching
- FIFO ordering is preserved within each price level
- Deterministic behaviour for identical input sequences
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.
This implementation prioritises correctness and clarity.
- Tree-based price levels (std::map) are used intentionally
- Sensitivity to price-level cardinality is acknowledged
- No premature optimisation or lock-free structures
The design is suitable for:
- Learning
- Deterministic simulation
- Interview and portfolio use
- C++17 or newer
- CMake
- Catch2 (for tests)
mkdir build
cd build
cmake ..
make
cd ..
./bench orders.csv