A thread-safe, multi-order type OrderBook implementation in C++ designed for low-latency trading systems.
- Multiple Order Types: Good Till Cancel (GTC), Fill And Kill (FAK), Fill Or Kill (FOK), Market, Good For Day (GFD)
- Thread-Safe Operations: Concurrent order processing with proper synchronization
- Price-Time Priority: FIFO matching within price levels
- Automatic Order Management: Background thread for Good For Day order pruning
- Comprehensive Testing: Full test suite with Google Test framework
- CMake 3.28+
- C++20 compatible compiler (GCC 10+, Clang 10+, MSVC 2019+)
- Git (for dependency management)
Option 1: Quick Build Scripts (Recommended)
# Clone the repository
git clone https://github.com/daronoh/OrderBook.git
cd OrderBook
# For development and debugging
./build-debug.sh
# For performance testing and benchmarks
./build-release.shOption 2: Manual CMake Build
# Create build directory
mkdir -p build
cd build
# Configure (choose one)
cmake -DCMAKE_BUILD_TYPE=Debug .. # For debugging
cmake -DCMAKE_BUILD_TYPE=Release .. # For performance
# Build
make -j$(nproc)Debug Build:
./build-debug/unit_tests # Run tests
./build-debug/main_app # Demo application
./build-debug/benchmark # Performance benchmark (slower)Release Build:
./build-release/unit_tests # Run tests
./build-release/main_app # Demo application
./build-release/benchmark # Performance benchmark (optimized)#include "OrderBook.h"
int main() {
Orderbook orderbook;
// Add a buy order
auto buyOrder = std::make_shared<Order>(
OrderType::GoodTillCancel, 1, Side::Buy, 100, 10);
orderbook.AddOrder(buyOrder);
// Add a matching sell order
auto sellOrder = std::make_shared<Order>(
OrderType::GoodTillCancel, 2, Side::Sell, 100, 5);
auto trades = orderbook.AddOrder(sellOrder);
// Check if trade occurred
std::cout << "Trades executed: " << trades.size() << std::endl;
return 0;
}- OrderBook: Main engine managing orders and matching
- Order: Individual order representation with various types
- Trade: Result of successful order matching
- PriceLevelInfo: Market data aggregation by price level
- Bids:
std::map<Price, OrderPointers, std::greater<Price>>(descending) - Asks:
std::map<Price, OrderPointers, std::less<Price>>(ascending) - Order Registry:
std::unordered_map<OrderId, OrderEntry>for O(1) lookup
| Build Type | Optimization | Debug Info | Use Case | Expected Performance |
|---|---|---|---|---|
| Debug | -O0 |
Full (-g) |
Development, debugging | ~10x slower |
| Release | -O3 -march=native |
None | Benchmarking, production | Maximum speed |
- Add Order: O(log n) time complexity
- Cancel Order: O(1) time complexity
- Modify Order: O(log n) time complexity
- Memory Usage: O(n) where n = number of active orders
Actual Benchmark Results:
- Average Latency: 211-326 nanoseconds per operation
- Peak Throughput: 4.35M operations per second
- Minimum Latency: 38 nanoseconds (best case)
- Memory: ~100 bytes per active order
Benchmark Summary:
| Test | Avg Latency | Throughput |
|---|---|---|
| Mixed Operations | 211 ns | 4.35M ops/sec |
| Add Orders | 270-326 ns | 2.69-3.2M ops/sec |
Note: Results from Release build (-O3 -march=native) on modern x86_64 CPU
The project includes comprehensive test coverage:
# Run all tests
./build/unit_tests
# Test scenarios include:
# - Basic order matching
# - Market order execution
# - Fill And Kill behavior
# - Fill Or Kill scenarios
# - Order cancellation
# - Order modificationRun performance benchmarks to measure OrderBook latency and throughput:
# Build optimized version first
./build-release.sh
# Run benchmark suite
./build-release/benchmarkLatest Results (Release Build):
=== Add 10,000 Orders ===
Average Latency: 326 ns
Min Latency: 106 ns
Max Latency: 24,284 ns
Throughput: 2.69M ops/sec
=== Mixed Operations (5,000) ===
Average Latency: 211 ns
Min Latency: 38 ns
Max Latency: 15,802 ns
Throughput: 4.35M ops/sec
| Type | Description | Behavior |
|---|---|---|
| GoodTillCancel | Remains active until cancelled | Standard limit order |
| FillAndKill | Fill immediately, cancel remainder | Immediate execution |
| FillOrKill | Fill completely or cancel entirely | All-or-nothing |
| Market | Execute at best available price | Converted to FAK |
| GoodForDay | Auto-cancel at market close | Time-based expiry |
- Lock-free data structures - Eliminate mutex contention
- Memory pool allocation - Reduce allocation overhead
- Multi-threaded pre-processing - Parallel order validation
- SPSC/MPSC Queues - Efficient order pipeline
- CPU Cache Optimization - Improve memory access patterns
- Branch Prediction Optimization - Reduce conditional overhead
- SIMD Instructions - Vectorized operations
- Template Metaprogramming - Compile-time optimizations
- Technical Report - Comprehensive analysis and performance metrics