Skip to content

kunalsinghdadhwal/fast-cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Low-Latency C++ System

Purpose

This project demonstrates low-latency, high-performance system design in C++ for real-time data processing, such as high-frequency trading (HFT). It focuses on:

  • Lock-free data structures
  • NUMA-aware memory management
  • Thread affinity and concurrency
  • Performance benchmarking

Key Components

  • Market Data Pipeline (src/market_data.cpp, src/market_data.h):
    • CSV replay from data/mock_market_data.csv
    • Replay modes: max speed, fixed rate, realtime
    • Sequence gap, duplicate, and out-of-order detection
    • Lock-free queue and memory pool for minimal latency
    • Thread affinity for NUMA optimization
  • Limit Order Book (src/order_book.h):
    • Maintains L1/L2/L3 bid and ask levels per symbol
    • Handles ADD, MODIFY, CANCEL, TRADE, QUOTE, and SNAPSHOT events
    • Tracks best bid/ask, spread, mid-price, and depth imbalance
  • Feature Engine (src/feature_engine.h):
    • Computes microprice, rolling VWAP, rolling volatility, trade intensity, signed volume, and N-tick returns
    • Uses fixed-size rings for O(1) rolling updates
  • Strategy Sandbox (src/strategy.h):
    • Simulates market making, momentum, mean reversion, and imbalance-driven orders
    • Supports latency-aware backtest timestamps before risk checks
  • Risk Layer (src/risk.h):
    • Enforces kill switch, max position, per-symbol position, max order size, max notional, and stale-data rejection
  • Order Gateway Simulator (src/order_gateway.h):
    • Simulates NewOrder, Ack, PartialFill, Fill, Cancel, Reject, and Replace lifecycle events
    • Models exchange latency and deterministic queue position
  • Latency Metrics (src/latency_metrics.h):
    • Reports min, mean, p50, p95, p99, p999, and max for feed, queue, strategy-decision, order-send, and exchange-ACK latency
  • Lock-Free Queue (src/lock_free_queue.h):
    • Single-producer, single-consumer lock-free queue
    • Batch operations for throughput
  • Memory Pool (src/memory_pool.h):
    • NUMA-aware, fast allocation for MarketData
  • Thread Affinity (src/thread_affinity.cpp, src/thread_affinity.h):
    • Pin threads to CPU cores for cache/NUMA locality
  • Logger (src/logger.h):
    • Async spdlog-backed file/console logger
  • Benchmarking (src/benchmark.cpp):
    • Compares mutex queue, current SPSC queue, MoodyCamel MPSC, bounded MPSC, and allocation paths
  • Types (src/types.h):
    • Shared data structures (e.g., MarketData)

Project Structure

low-latency-cpp/
├── CMakeLists.txt
├── README.md
├── LICENSE
├── .gitignore
├── data/
│   └── mock_market_data.csv
├── docs/
│   └── concurrency.md
├── src/
│   ├── benchmark.cpp
│   ├── lock_free_queue.h
│   ├── logger.h
│   ├── main.cpp
│   ├── feature_engine.h
│   ├── market_data.cpp
│   ├── market_data.h
│   ├── memory_pool.h
│   ├── order_book.h
│   ├── order_gateway.h
│   ├── latency_metrics.h
│   ├── risk.h
│   ├── strategy.h
│   ├── thread_affinity.cpp
│   ├── thread_affinity.h
└───└── types.h

Building

mkdir build && cd build
cmake ..
make

Dependencies:

  • C++20 compiler (e.g., GCC 13.3)
  • libnuma-dev for thread affinity
  • libspdlog-dev for async logging

Running

./build/hft_system
./build/hft_system data/mock_market_data.csv max
./build/hft_system data/mock_market_data.csv fixed 100000
./build/hft_system data/mock_market_data.csv realtime
./build/hft_system data/mock_market_data.csv max 100000 verbose
  • Replays CSV market data through the SPSC queue
  • Logs output to hft_system.log
  • verbose enables sampled gateway lifecycle logging without changing default hot-path behavior

Benchmarking

./build/benchmark
  • Compares mutex queue, current SPSC ring, Boost SPSC if installed, vendored MoodyCamel MPSC, bounded MPSC ring, memory pool, and standard allocation

Further Improvements

  • Error Handling & Robustness:
    • Use custom exceptions and RAII for resource management
    • Ensure all resources are released on error
  • Thread Management:
    • Use RAII wrappers (e.g., std::jthread in C++20)
    • Prefer condition variables or atomics over sleep_for for synchronization
  • Lock-Free Queue:
    • Review atomic memory ordering
    • Benchmark against established libraries (e.g., MoodyCamel’s ConcurrentQueue)
  • Memory Pool:
    • Profile NUMA benefits; consider std::pmr for flexibility
  • Logging:
    • Consider lock-free or buffered logging for high-frequency events
    • Allow runtime log level configuration
  • Benchmarking:
    • Add multi-threaded benchmarks
    • Output results in machine-readable formats (CSV, JSON)
  • Modern C++:
    • Use constexpr, noexcept, [[nodiscard]], and smart pointers
    • Use std::span for batch operations (C++20)
  • Documentation & Testing:
    • Add unit/integration tests (e.g., Google Test)
    • Expand API and threading documentation
  • Build System:
    • Add sanitizer options and CI integration

License

MIT License

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors