Skip to content

ranjan2829/High-Frequency-Trading-Exchange-Engine

Repository files navigation

Nanosecond High-Frequency Trading Exchange Engine

Production-grade C++ trading system processing with 89ns latency.

System Architecture

┌─────────────────────────────────────────────────────────────────┐
│                    EXCHANGE MATCHING ENGINE                      │
│  ┌────────────────┐  ┌──────────────┐  ┌───────────────────┐   │
│  │ Order Server   │  │   Matching   │  │ Market Data       │   │
│  │ (TCP:12345)    │→→│   Engine     │→→│ Publisher (UDP)   │   │
│  │ 38ns latency   │  │ 45ns latency │  │ 42ns latency      │   │
│  └────────────────┘  └──────────────┘  └───────────────────┘   │
│           ↑                  ↓                      ↓            │
│    Lock-Free Queue    Lock-Free Queue      Multicast UDP        │
└───────────┬─────────────────┬────────────────────┬──────────────┘
            │                 │                    │
            │                 │                    │
┌───────────┴─────────────────┴────────────────────┴──────────────┐
│                     TRADING CLIENTS (1-10)                       │
│  ┌────────────────┐  ┌──────────────┐  ┌───────────────────┐   │
│  │ Order Gateway  │  │ Trade Engine │  │ Market Data       │   │
│  │ (TCP Client)   │←←│ (Algorithms) │←←│ Consumer (UDP)    │   │
│  └────────────────┘  └──────────────┘  └───────────────────┘   │
│                              ↓                                   │
│                    ┌─────────────────┐                          │
│                    │ Risk Manager    │                          │
│                    │ Position Keeper │                          │
│                    └─────────────────┘                          │
└──────────────────────────────────────────────────────────────────┘

Performance Metrics

Metric Value Status
Average Latency 89 nanoseconds Target: <100ns
P99 Latency 145 nanoseconds Sub-microsecond
Orders Processed 20,000+ verified
Crashes 0 Zero-error

Critical Optimizations

Fixed Race Conditions

  • Lock-free queues with atomic operations
  • Memory-order guarantees (acquire/release semantics)
  • 20,000+ orders processed without corruption

Thread-Safe Memory Management

  • Lock-free memory pool allocation
  • Exponential backoff on contention
  • Zero memory errors during execution

Nanosecond Precision

  • Event-driven architecture (zero sleep calls)
  • High-resolution timing infrastructure
  • Cache-aligned data structures (64-byte)

Price-Time Priority Matching

  • FIFO order book implementation
  • Correct BUY/SELL matching logic
  • 8 tickers with real price discovery

Build

mkdir cmake-build-release && cd cmake-build-release
cmake -DCMAKE_BUILD_TYPE=Release ..
make -j8

Run

# Exchange
./exchange_main &

# Trading clients
./trading_main 1 RANDOM 100 0.5 1000 5000 100 &

Technology Stack

  • Language: C++17 with -O3 optimization
  • Concurrency: Lock-free queues, atomic operations
  • Networking: TCP (order routing), Multicast UDP (market data)
  • Memory: Custom pools, zero allocations in hot paths
  • Timing: High-resolution clock (nanosecond precision)

Verified Execution

  • 10 parallel trading clients
  • 8 instruments with price discovery
  • 20,000+ orders through fixed lock-free queues
  • 25+ MB of execution logs
  • Zero crashes, zero errors

See METRICS.json for complete performance data.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages