A high-performance limit order book and matching engine implementation in C++23, featuring a custom slab allocator and price-time priority matching.
# Build using CMake
mkdir build && cd build
cmake ..
make -j$(nproc)
# Run the example
./examples/example_basic
# Run benchmarks
cd benchmarks && ./benchmarksOr use the manual build script:
./build.sh
./build_manual/example_basic- Price-Time Priority Matching: Orders matched by price first, then time (FIFO)
- Custom Slab Allocator: Pre-allocated memory pools for zero-allocation order management
- Multiple Order Types: Limit, Market, IOC (Immediate or Cancel), FOK (Fill or Kill)
- High Performance: Optimized for low-latency trading systems
- C++23 Features:
std::print/std::printlnfor formatted outputstd::optionalmonadic operations (.transform(),.and_then(),.or_else())std::ranges::tofor range-to-container conversionstd::source_locationfor better error tracking
- Testing & Benchmarks: Unit tests (Catch2) and performance benchmarks (Google Benchmark)
-
Order Types (
include/types.hpp)- Order structure with price, quantity, side, type, and status
- Trade structure for executed trades
- Timestamp tracking for price-time priority
-
Slab Allocator (
include/allocator/slab_allocator.hpp)- Pre-allocates memory pools (slabs)
- O(1) allocation/deallocation with free list reuse
- Lock-free for maximum performance
-
Order Book (
include/order_book.hpp)- Maintains bid/ask price levels using
std::map(O(log n) lookup) - Price levels contain linked lists of orders (FIFO, O(1) insertion)
- O(1) order lookup via
std::unordered_mapfor cancellation/modification - Market depth queries
- Maintains bid/ask price levels using
-
Matching Engine (
include/matching_engine.hpp)- Processes incoming orders and matches based on price-time priority
- Generates trades and supports all order types
- C++23 compatible compiler (GCC 13+, Clang 16+, MSVC 19.30+)
- CMake 3.20+ (optional - manual build script available)
Using CMake (recommended):
mkdir build && cd build
cmake ..
make -j$(nproc)Manual Build:
./build.sh- Order Insertion: O(log n) price level lookup + O(1) order insertion
- Order Cancellation: O(1) hash map lookup
- Best Bid/Ask Lookup: O(1) constant time
- Matching: O(k) where k is number of price levels matched
On modern hardware, typical performance:
- Allocator: ~34 ns allocation, ~1.5 ns deallocation
- Order Book: ~123 ns add order, ~13 ns best bid/ask lookup, ~47 ns cancel
- Matching: ~8-15M orders/sec throughput across order types
- Scalability: Constant-time best bid/ask lookup even with 10,000+ orders
Run benchmarks for detailed results:
cd build/benchmarks && ./benchmarksOrders matched using:
- Price Priority: Best price wins (highest bid, lowest ask)
- Time Priority: Within same price, first-in-first-out (FIFO)
- Zero-allocation order creation after initial setup
- Cache-friendly memory layout
- Fast deallocation via free list
- Memory reuse for better performance
- Price Levels:
std::mapfor O(log n) price lookup - Orders per Level: Doubly-linked list for O(1) insertion and FIFO ordering
- Order Lookup:
std::unordered_mapfor O(1) cancellation/modification
Run tests with CMake:
cd build
ctest --output-on-failureTests cover order book operations, matching logic, order types, allocator functionality, and edge cases.
- Fix IOC/FOK implementation and benchmarks.
- More order types (stop orders, trailing stops)
- Order book visualization
- Network protocol for order submission
- More sophisticated matching algorithms
This is a portfolio project, but suggestions and improvements are welcome!
This implementation demonstrates:
- Low-latency C++ programming
- Memory management (custom allocators)
- Algorithms and data structures
- Trading systems and market microstructure
- Performance optimization
- Modern C++ (C++23)