This repository contains a simple implementation of a Limit Order Book (LOB) in C++, with automatic matching logic for buy and sell orders. It is my first hands-on project in the trading domain, and I intend to keep improving it step by step.
At the moment this project demonstrates the core matching workflow used by exchanges:
- Add limit orders
- Match aggressive orders against the opposite side
- Keep price-time priority (FIFO inside each price level)
- Cancel and modify existing orders
- Inspect top-of-book and spread
This project steams from my curiosity about algorithmic trading and financial systems. It is my very first practical contact with building an order book, and as such, I consider it a work-in-progress prototype. My goal is to learn the fundamentals of order matching, efficient data management, and gradually incorporate features found in real-world trading engines.
I am aware that there is plenty of room for optimization and refinement, but that's precisely what makes this project exciting: the chance to experiment, make mistakes, and learn along the way. Any feedback, suggestions, or contributions are more than welcome!
The implementation is in lob.cc and uses:
std::mapfor price levels- Bids: descending prices (
greater<ll>) - Asks: ascending prices
- Bids: descending prices (
std::list<Order>per price level to preserve FIFO orderstd::unordered_map<orderId, OrderEntry>for O(1)-style order lookup on cancel/modify
Each order contains:
id: unique order identifierprice: limit pricequantity: remaining quantityside:BuyorSell
When a new order arrives:
- It tries to match the best prices on the opposite side.
- Trades are executed while prices cross and quantity remains.
- If quantity is still left, the order is inserted into its own side of the book.
Matching is done with price-time priority:
- Best price first
- Oldest order first within that price
-
addOrder(Order)- Rejects duplicate IDs
- Matches immediately if possible
- Inserts residual quantity if not fully filled
-
cancelOrder(orderId)- Removes an existing order from the book
-
modifyOrder(orderId, newQuantity)- Updates quantity for an existing order
- If
newQuantity == 0, it cancels the order
-
display()- Prints aggregated quantity at each price level
-
getBestBid(),getBestAsk(),getSpread(),empty()- Basic top-of-book queries
g++ -std=c++17 -O2 -Wall -Wextra -o lob lob.cc
./lobThe demo sequence does the following:
- Places initial bid/ask orders
- Sends an aggressive buy order that matches existing asks
- Modifies one remaining ask
- Cancels that ask
- Attempts to add a duplicate order ID (error path)
- Prints best bid/ask and spread
Since this is my first approach to the domain, I have planned several enhancements to deepen my understanding and make the system more realistic.
Current limitations:
- No decimal/fixed-point support.
- Only limit orders are implemented.
- Single-threaded; no concurrency control.
- No persistence (book state is lost after program ends).
- Limited testing (no automated unit tests yet).
Planned Enhancements (Roadmap):
- Support for decimal prices (fixed-point) and tick sizes.
- Implementation of market orders, stop-loss, and iceberg orders.
- Real-time market depth and statistics.
- Partial cancel (reduce quantity).
- Trade history storage.
- Input-driven simulator (from file/socket).
- Unit tests for matching/cancel/modify edge cases.
- Performance benchmarks and profiling.
- Python binding for visualization becktesting.
If you're interested in any of these areas or would like to collaborate, feel free to reach out!
This project is a learning journey, so all suggestions, bug reports, and pull requests are appreciated. Please open an issue or submit a PR if you'd like to help.
This repository is provided for educational use.