Introduction
Every stock exchange is built around an order book that matches buyers with sellers. The rules are simple but strict: the best price trades first and, at the same price, the earliest order trades first. Implementing them efficiently is a great lesson in choosing containers: sorted maps for price levels, linked lists for queues and a hash index for cancellation. (This is a simulation for learning; it does not connect to any market.)
Problem statement
Objectives
- Choose containers for performance: map with a custom comparator, list and unordered_map
- Implement price-time priority matching
- Keep an index of iterators for O(1) removal
- Use exact integer prices
Features
- Limit and market orders, partial fills
- Price-time priority, trades at the resting price
- Cancel and reduce (keeping queue position)
- Depth view with spread; VWAP statistics
Concepts demonstrated
- Order books
- Priority rules
- Iterator stability
- Custom comparators
- Member templates
- Fixed-point prices
System architecture
OrderBook keeps bids_ as std::map<price, std::list<Order>, std::greater<>> (best = highest) and asks_ as an ascending map (best = lowest), so begin() is always the best level. match is a member template used for both sides: it walks the best opposite level's FIFO list, filling against the front order until the incoming order is done or prices stop crossing, and erases empty levels. Remaining limit quantity is appended to its level and its list iterator stored in index_, which makes cancel a direct erase. modify only allows reductions, so the order keeps its place.
Folder structure
Complete source code
Makefile
What each file does
- src/order_book.h
- Order, Trade and the
OrderBookinterface with its containers. - src/order_book.cpp
- Matching (a member template for both sides), resting orders, cancel, reduce, depth and best prices.
- src/main.cpp
- Command parsing, exact price parsing, trade reporting, the depth display and statistics.
- Makefile
- Build automation:
makebuilds./app,make testruns the sample session and compares it with the expected output,make cleanremoves build files.
How to run it
Build
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
sell S1 100 101.50 sell S2 50 101.00 sell S3 70 101.00 sell S4 200 102.25 buy B1 80 100.00 buy B2 40 99.75 buy B3 30 100.00 book buy B4 60 101.00 book buy B5 150 101.50 book sell S5 100 market reduce S4 120 reduce S4 500 cancel B2 cancel B2 buy B6 10 abc buy B1 10 99.00 buy B7 300 market book stats quit
Testing
- Compare
./app < tests/sample-input.txtwithtests/expected-output.txt. - B4 buys 60 at 101.00: S2 (earlier) fills 50, then S3 fills 10.
- The market sell S5 hits B1 (80) before B3 (20) because B1 arrived first at 100.00.
- The market buy B7 takes all remaining asks (130) and cancels the unfilled 170.
- VWAP = 44,540.00 / 440 = 101.23.
Common errors
Cancel crashes or removes the wrong order
Store std::list iterators (stable) in the index, not std::vector indices or deque iterators, which shift when other orders are removed.
Trades print prices like 100.99999
Represent prices as integer cents; never use floating point for money.
Possible improvements
- Add stop orders that trigger at a price
- Support several instruments (one book per symbol)
- Record trades to a CSV file and replay them
Additional challenges
- 1Add immediate-or-cancel and fill-or-kill order types.
- 2Measure throughput with a million random orders.
- 3Show a time-and-sales tape of the last 10 trades.
Built and tested it yourself? Mark it to update your dashboard.