Skip to content

gwrxuk/HFTPerformance

Repository files navigation

High-Frequency Trading System

A performance-critical trading system implementation demonstrating expertise in low-latency C++ development, systems programming, and financial infrastructure.

πŸš€ Overview

This project implements a complete high-frequency trading (HFT) system stack including:

  • Matching Engine: Price-time priority order matching with sub-microsecond latency
  • Order Book: Lock-free, cache-optimized limit order book
  • Market Data Handler: Real-time market data processing with FIX and WebSocket support
  • Order Gateway: REST API with rate limiting and risk management
  • Performance Benchmarks: Comprehensive latency and throughput measurement tools

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        Order Gateway                             β”‚
β”‚                    (Rate Limiting, Risk)                         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚
                              β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      Matching Engine                             β”‚
β”‚              (Multi-instrument, Price-Time Priority)             β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚   BTC-USD Book  β”‚   ETH-USD Book  β”‚        ... more books       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚
                              β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Market Data Publisher                         β”‚
β”‚              (WebSocket, FIX, Execution Reports)                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

⚑ Performance Highlights

Component Metric Performance
Order Book Add Latency < 500 ns
Order Book Cancel Latency < 200 ns
Order Match Latency < 1 ΞΌs
SPSC Queue Throughput > 50M ops/sec
Memory Pool Allocation < 50 ns

πŸ”§ Key Technical Features

Low-Latency Optimizations

  • Lock-free Data Structures: SPSC/MPSC queues with cache-line padding
  • Custom Memory Pool: O(1) allocation without heap fragmentation
  • Cache-Aware Design: 64-byte aligned structures for cache efficiency
  • CPU Affinity: Thread pinning for deterministic performance
  • RDTSC Timing: Nanosecond-precision measurement with minimal overhead

Modern C++20 Features

  • Concepts and constraints
  • std::span for zero-copy views
  • constexpr compile-time computation
  • Structured bindings
  • std::optional and std::string_view

Protocol Support

  • FIX 4.4: Message parsing and generation
  • WebSocket: Real-time market data streaming
  • REST API: Order submission and management

πŸ“ Project Structure

.
β”œβ”€β”€ CMakeLists.txt          # Build configuration
β”œβ”€β”€ Dockerfile              # Development container
β”œβ”€β”€ docker-compose.yml      # Multi-service orchestration
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ core/               # Low-latency utilities
β”‚   β”‚   β”œβ”€β”€ lockfree_queue.hpp
β”‚   β”‚   β”œβ”€β”€ memory_pool.hpp
β”‚   β”‚   β”œβ”€β”€ spinlock.hpp
β”‚   β”‚   β”œβ”€β”€ timing.hpp
β”‚   β”‚   β”œβ”€β”€ cpu_affinity.hpp
β”‚   β”‚   └── types.hpp
β”‚   β”œβ”€β”€ matching/           # Matching engine
β”‚   β”‚   β”œβ”€β”€ order.hpp
β”‚   β”‚   β”œβ”€β”€ price_level.hpp
β”‚   β”‚   β”œβ”€β”€ order_book.hpp
β”‚   β”‚   └── matching_engine.hpp
β”‚   β”œβ”€β”€ protocol/           # Exchange protocols
β”‚   β”‚   β”œβ”€β”€ fix_message.hpp
β”‚   β”‚   β”œβ”€β”€ websocket_handler.hpp
β”‚   β”‚   └── rest_handler.hpp
β”‚   β”œβ”€β”€ marketdata/         # Market data handling
β”‚   β”‚   └── market_data_handler.hpp
β”‚   β”œβ”€β”€ benchmark/          # Performance tests
β”‚   β”‚   └── benchmark_main.cpp
β”‚   └── apps/               # Applications
β”‚       β”œβ”€β”€ matching_engine_main.cpp
β”‚       β”œβ”€β”€ market_data_feed_main.cpp
β”‚       └── order_gateway_main.cpp
└── tests/                  # Unit tests

🐳 Docker Setup

Quick Start

# Build and run the development container
docker-compose up -d hft-dev

# Enter the container
docker exec -it hft-trading-system bash

# Build the project
mkdir -p build && cd build
cmake -G Ninja -DCMAKE_BUILD_TYPE=Release ..
ninja

Run Components

# Start matching engine
docker-compose up matching-engine

# Start market data feed
docker-compose up market-data

# Run benchmarks
docker-compose --profile benchmark up benchmarks

Run Individual Services

# Matching Engine Server (port 8080)
./build/bin/matching_engine

# Market Data Feed Simulator (port 9090)
./build/bin/market_data_feed

# Order Gateway (port 9000)
./build/bin/order_gateway

# Benchmark Suite
./build/bin/benchmark_suite

πŸ”Œ API Reference

Matching Engine REST API

# Health check
curl http://localhost:8080/health

# Get order book depth
curl http://localhost:8080/api/v1/depth/BTC-USD

# Get quote
curl http://localhost:8080/api/v1/quote/BTC-USD

# Submit order
curl -X POST http://localhost:8080/api/v1/order \
  -H "Content-Type: application/json" \
  -d '{"symbol":"BTC-USD","side":"BUY","type":"LIMIT","price":50000.0,"quantity":1.0}'

# Cancel order
curl -X DELETE http://localhost:8080/api/v1/order/BTC-USD/12345

# Get stats
curl http://localhost:8080/api/v1/stats

Order Gateway REST API

# Submit order with risk checks
curl -X POST http://localhost:9000/api/v1/order \
  -H "Content-Type: application/json" \
  -d '{"symbol":"BTC-USD","side":"BUY","type":"LIMIT","price":50000.0,"quantity":1.0}'

# Get position
curl http://localhost:9000/api/v1/position/BTC-USD

# Get gateway stats (includes latency percentiles)
curl http://localhost:9000/api/v1/stats

πŸ“Š Benchmark Results

Run the benchmark suite to measure system performance:

./build/bin/benchmark_suite

Example output:

╔══════════════════════════════════════════════════════════════╗
β•‘           HFT Trading System - Performance Benchmark          β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

System Information:
  CPU Cores:      16
  Cache Line:     64 bytes
  Order Size:     64 bytes
  TSC Frequency:  3.20 GHz

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
SPSC Queue Benchmark
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  Push Latency:  mean=15.2ns, p99=45ns
  Pop Latency:   mean=12.8ns, p99=38ns
  Throughput:    52.3M ops/sec

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Order Book Benchmark
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  Add Order:     mean=425ns, p99=1.2ΞΌs
  Cancel Order:  mean=180ns, p99=450ns
  Match Order:   mean=890ns, p99=2.1ΞΌs

πŸ§ͺ Running Tests

cd build
./bin/unit_tests

πŸ”’ Performance Tuning

Linux Kernel Parameters

# Disable CPU frequency scaling
echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

# Increase network buffer sizes
sudo sysctl -w net.core.rmem_max=16777216
sudo sysctl -w net.core.wmem_max=16777216

# Disable swap
sudo swapoff -a

# Set real-time scheduling limits
echo '* soft rtprio 99' | sudo tee -a /etc/security/limits.conf
echo '* hard rtprio 99' | sudo tee -a /etc/security/limits.conf

CPU Isolation

# Add to kernel parameters (GRUB_CMDLINE_LINUX)
isolcpus=2,3 nohz_full=2,3 rcu_nocbs=2,3

πŸ“š Technical Deep Dive

Lock-Free SPSC Queue

The SPSC queue implementation uses:

  • Power-of-2 sizing for branchless modulo
  • Cache-line separated head/tail indices
  • Local caching of remote indices to reduce cache coherence traffic
  • Acquire-release memory ordering for minimal synchronization

Memory Pool

The custom memory pool provides:

  • O(1) allocation and deallocation
  • Zero heap fragmentation
  • Cache-aligned blocks
  • Thread-local pools for lock-free operation

Order Book

The order book uses:

  • std::map for price levels (O(log N) access)
  • Intrusive linked lists at each level (O(1) order ops)
  • Separate bid/ask structures for cache locality

πŸ› οΈ Development

Prerequisites

  • C++20 compatible compiler (GCC 12+, Clang 14+)
  • CMake 3.20+
  • Boost 1.74+
  • OpenSSL
  • Linux (for full feature support)

Building without Docker

mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make -j$(nproc)

πŸ“„ License

MIT License - See LICENSE file for details.

πŸ™ Acknowledgments

Inspired by real-world trading systems and low-latency programming techniques from:

  • "Trading and Exchanges" by Larry Harris
  • "Market Microstructure in Practice" by Lehalle and Laruelle
  • Various open-source trading system implementations

About

A performance-critical trading system implementation demonstrating expertise in low-latency C++ development, systems programming, and financial infrastructure.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages