A production-grade order matching engine built in C++ for high-frequency trading applications. Features sub-microsecond latency, institutional-scale throughput (1M+ orders/sec), and comprehensive order type support.
- Lightning Fast: Sub-microsecond order processing for shallow books
- Scalable: O(log N) complexity using red-black tree implementation
- Full Order Type Support: GTC, IOC, FOK, and GFD time-in-force options
- Thread-Safe: Mutex-based concurrency with atomic performance counters
- Price-Time Priority: FIFO matching with self-match prevention and Level-2 market data
- Comprehensive Testing: Multiple test suites with performance visualizations
| Metric | Value |
|---|---|
| Average Latency | < 1 μs (shallow books) |
| Throughput | 1M+ orders/sec |
| Scalability | O(log N) |
| Order Types | Limit, Market (GTC, IOC, FOK, GFD) |
| Thread Safety | Yes (mutex + atomics) |
This project includes multiple testing approaches to validate functionality and measure performance:
Validates core order book operations.
make testWhat it tests:
- GTC, IOC, FOK order types
- Order matching and fill generation
- Order cancellation
- Market data queries (best bid/ask, level-2 data)
When to use: First-time setup validation or after code changes
Runtime: ~2 seconds
Tests order book performance across different scales with web visualization.
# Complete demo workflow
make demo
# Or step-by-step:
make generate_test_orders
./generate_test_orders
make web_demo
./web_demo
# Open the report
open performance_report.html # macOS
xdg-open performance_report.html # LinuxExpected Results:
The demo processes three test scenarios with reproducible results:
| Test Size | Orders | Avg Latency | Throughput | Book Depth |
|---|---|---|---|---|
| Small | 1,000 | ~900 ns | 1M+ ops/sec | Shallow |
| Medium | 10,000 | ~5 μs | 200K+ ops/sec | Moderate |
| Large | 100,000 | ~20 μs | 50K+ ops/sec | Deep |
Performance Characteristics:
- ✅ Sub-microsecond for shallow books (typical trading scenarios)
- ✅ Sub-5-microsecond for moderate depth (institutional scale)
- ✅ Sub-25-microsecond for extreme depth (stress test)
- ✅ O(log N) scalability as expected from red-black tree implementation
The latency increase with volume demonstrates realistic trading behavior where deeper order books require more price level traversals. Even at 100K orders, performance remains institutional-grade.
Report includes:
- 📊 Side-by-side performance comparison
- 📈 Latency metrics (Average, Median, P95, P99)
- ⚡ Throughput analysis
- 🎯 Fill rate statistics
Uses: Fixed-seed random orders (reproducible results), CSV-based test data
Runtime: ~30 seconds
Detailed performance analysis with statistical breakdowns.
make benchmarkWhat it tests:
- Single-threaded latency (100K orders)
- Order type performance comparison (GTC, IOC, FOK)
- Market data query performance
- Detailed latency distribution (Min, Max, Median, P95, P99)
When to use: Performance optimization or profiling
Uses: True random orders (non-deterministic, simulates real trading)
Runtime: ~2 minutes
Build all executables without running:
make allRun specific benchmark modes:
./safe_test --latency # Latency-only benchmark
./safe_test --order-types # Order type comparison
./safe_test --market-data # Market data queriesMulti-threaded stress test:
./performance_test # Uses all CPU cores
./performance_test --benchmarkDebug build:
make debugClean build artifacts:
make clean| Command | Purpose | Runtime | Output |
|---|---|---|---|
make test |
Functionality validation | ~2s | Console |
make demo |
Visual performance report | ~30s | HTML file |
make benchmark |
Detailed performance analysis | ~2min | Console |
make all |
Build everything | ~10s | Executables |
- First run:
make testfor quick validation - Performance analysis:
make demofor HTML report with charts - Detailed metrics:
make benchmarkfor comprehensive statistics