Skip to content

SiddNan/HFT-Orderbook

Repository files navigation

🚀 High-Performance Order Book

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.

✨ Features

  • 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

🎯 Performance Highlights

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)

🧪 Testing Methods

This project includes multiple testing approaches to validate functionality and measure performance:


1. ✅ Basic Functionality Test (Fastest)

Validates core order book operations.

make test

What 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


2. 🎨 Interactive Performance Demo (Most Visual)

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  # Linux

Expected 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


3. ⚡ Performance Benchmarks (Most Comprehensive)

Detailed performance analysis with statistical breakdowns.

make benchmark

What 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


4. 🔧 Advanced Testing Options

Build all executables without running:

make all

Run specific benchmark modes:

./safe_test --latency        # Latency-only benchmark
./safe_test --order-types    # Order type comparison
./safe_test --market-data    # Market data queries

Multi-threaded stress test:

./performance_test           # Uses all CPU cores
./performance_test --benchmark

Debug build:

make debug

Clean build artifacts:

make clean

📋 Quick Reference

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

🎯 Recommended Testing Workflow

  1. First run: make test for quick validation
  2. Performance analysis: make demo for HTML report with charts
  3. Detailed metrics: make benchmark for comprehensive statistics

About

Writing an order-book to fulfill stock orders using C++.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •