A high-performance limit order matching engine implemented in modern C++, designed to replicate core functionality found in stock exchanges (NSE, NASDAQ, etc.). This project demonstrates fundamental concepts in financial systems architecture and real-time order processing.
This matching engine processes buy and sell orders for securities using the Price-Time Priority matching algorithm:
- Price Priority: Best prices are matched first
- Highest buy prices paired with lowest sell prices
- Time Priority: Orders at the same price level are matched in FIFO (First-In-First-Out) order
- Earlier orders execute before later ones at identical price levels
make runThis compiles the project and executes ./orderbook. The output displays:
- Order insertions into the order book
- Real-time trade executions and matched pairs
orderbook/
├── src/
│ ├── Order.h # Order data structure (ID, side, price, quantity)
│ ├── OrderBook.h # Order matching engine class definition
│ ├── OrderBook.cpp # Core matching algorithm implementation
│ └── main.cpp # Demo and test harness
├── Makefile # Build configuration
└── README.md # This file
Order Data Structure
- Unique order ID
- Side (BUY or SELL)
- Limit price
- Order quantity Order Book Management
- Buy orders stored in
std::mapwith descending price order (highest first) - Sell orders stored in
std::mapwith ascending price order (lowest first) - Enables O(log n) order insertion and retrieval Matching Algorithm
- When a new order arrives,
matchOrders()checks for executable counterparty orders - Orders are matched at the price level of the resting order (maker price)
- When quantities match:
- Both orders' quantities are decremented
- Fully filled orders are removed from the book
- Trade execution is recorded
- Partially filled orders remain in the book
NEW BUY ORDER (price P, qty Q)
├─ Check best SELL orders (lowest price first)
├─ If SELL price ≤ P: MATCH at SELL price
├─ Decrement both order quantities
└─ Repeat until no more matches or BUY quantity = 0
NEW SELL ORDER (price P, qty Q)
├─ Check best BUY orders (highest price first)
├─ If BUY price ≥ P: MATCH at BUY price
├─ Decrement both order quantities
└─ Repeat until no more matches or SELL quantity = 0
✅ Price-Time Priority Matching — Compliant with standard exchange rules
✅ Efficient Data Structures — O(log n) operations using std::map
✅ Order Book Visualization — Clear output of book state and trades
✅ Modular Design — Clean separation of concerns for easy extension
✅ Memory Efficient — Automatic cleanup of fully filled orders
OrderBook book;
// Add orders
book.addOrder(Order(1, OrderSide::BUY, 100.50, 100)); // Buy 100 @ 100.50
book.addOrder(Order(2, OrderSide::SELL, 100.75, 50)); // Sell 50 @ 100.75
book.addOrder(Order(3, OrderSide::SELL, 100.50, 50)); // Sell 50 @ 100.50
// Output:
// Trade: BUY Order 1 matched with SELL Order 3 @ 100.50 for 50 units
// Remaining: 50 units of Order 1 in bookThis project intentionally maintains simplicity to serve as a foundation for progressive enhancements. The following extensions are recommended to build a production-grade system and develop skills relevant to quantitative trading and HFT roles:
- Order Cancellation & Modification
- Implement cancel/modify requests
- Update order quantities or remove orders entirely
- Track order state transitions
- Order Expiration
- Time-based order validity (Good-Till-Cancel vs. Good-For-Day)
- Automatic order cleanup on expiration
- Multi-threaded Architecture
- Order input thread (producer)
- Matching engine thread (consumer)
- Use
std::thread,std::mutex,std::condition_variable - Learn thread safety and synchronization primitives
- Build understanding for concurrent trading systems
- Latency Benchmarking
- Measure order-to-execution latency using
std::chrono - Track matching engine throughput (orders/second)
- Profile critical code paths
- Essential for HFT system optimization
- Measure order-to-execution latency using
- TCP Socket Interface
- Accept orders from external clients (e.g., Python scripts)
- Send trade confirmations and market data updates
- Implement binary protocol for low-latency communication
- Learn socket programming and network protocols
- Market Data Feed
- Implement real-time order book snapshot export
- Generate market data messages (trades, best bid/ask)
- Support multiple subscribers (pub-sub pattern)
- Unit Tests
- Google Test (GTest) framework integration
- Test matching logic with various order scenarios
- Stress testing with high order volumes
- Edge case validation (partial fills, price improvement, etc.)
- Simulation Engine
- Realistic order flow generator
- Market microstructure simulation
- Performance regression testing
- Multiple Asset Classes
- Support for different instruments
- Cross-product order flow
- Advanced Order Types
- Iceberg orders (hidden quantity)
- Pegged orders (dynamic price adjustment)
- Market orders with smart execution
- Risk Management
- Position limits
- Exposure monitoring
- Circuit breakers
This project covers fundamental concepts essential for roles in:
- Quantitative Trading — Order matching, market microstructure
- Financial Technology (FinTech) — Exchange infrastructure, trading systems
- High-Frequency Trading (HFT) — Low-latency optimization, system design
- Systems Engineering — Data structures, concurrency, performance optimization
- Standard Library:
std::map,std::vector,std::deque - OOP: Class design, encapsulation, separation of concerns
- Memory Management: Smart pointers, RAII principles
- Modern C++: C++17 features, structured bindings
- Algorithms: Binary search (via std::map), FIFO matching
- C++17 or later compiler (g++, clang++)
- Make build system
# Clean and rebuild
make clean && make
# Run with default test cases
make run
# Run with verbose output
./orderbook- Compiler: GCC 7+, Clang 5+, or MSVC 2017+
- Memory: Minimal (< 50MB for typical workloads)
- CPU: Single-core sufficient for demonstration; multi-core for concurrency phase
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Add Order | O(log n) | O(1) per order |
| Match Orders | O(k log n) | O(k) for k matches |
| Cancel Order | O(log n) | O(1) |
| Book Snapshot | O(n) | O(n) |
n = total orders in book, k = number of matched orders
- Naming: Clear, self-documenting variable and function names
- Comments: Technical explanations for complex matching logic
- Structure: Modular design enabling easy testing and extension
- Style: Consistent indentation and formatting (4-space standard)
Once extended, this engine can integrate with:
- Risk Management System — Position monitoring, limit enforcement
- Market Data Publisher — Real-time feed distribution
- Analytics Engine — Trade analysis, market statistics
- Backtesting Framework — Historical simulation
- Exchange Order Books: Nasdaq Technical Documentation
- Matching Algorithms: CME Group matching rules
- Low-Latency Systems: "Systems Performance" by Brendan Gregg
- C++ Concurrency: "C++ Concurrency in Action" by Anthony Williams
This project is provided as-is for educational and portfolio purposes.
Shubham Mehta
- GitHub: @Shubhammehta2008
- LinkedIn: Shubham Mehta