Skip to content

aarav-agn/hft-matching-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

⚡ High-Frequency Trading (HFT) Matching Engine

License C++ React

A deterministic, ultra-low latency Limit Order Book (LOB) matching engine written in C++17. Designed to simulate high-frequency trading environments, it processes orders with sub-microsecond latency and streams real-time market data to a React-based dashboard via WebSockets.


🚀 Key Performance Metrics

Component Metric Benchmark Result
Core Matching Engine Throughput 3,500,000+ orders/sec
Core Matching Engine Latency ~286 nanoseconds (avg)
Network Stack (TCP) Throughput 20,000+ orders/sec (WebSocket)
Memory Allocation Runtime Zero (O(1) Object Pool)

Benchmarks run on a standard Linux Desktop (Ryzen 5 / Intel i7 equivalent).


🛠️ Architecture & Design

1. C++ Backend (trading-engine)

  • Zero Garbage Collection: Implements a custom ObjectPool to pre-allocate memory for millions of orders at startup. This eliminates new/delete latency spikes during trading sessions.
  • Data Structures: Uses Red-Black Trees (std::map) for the Order Book to guarantee Price-Time priority matching with $O(\log N)$ efficiency.
  • Networking: Built on uWebSockets, an event-driven, non-blocking WebSocket library optimized for high throughput.
  • Pointer Stability: The Object Pool uses a std::vector backing store that is sized at startup to ensure memory locality and prevent pointer invalidation (segfaults).

2. React Frontend (trading-ui)

  • Real-Time Visualization: Connects via WebSockets (ws://localhost:9001) to render the "Market Tape" live.
  • Optimized Rendering: Uses a circular buffer to display the latest 25 trades without bogging down the DOM.
  • Styling: Designed with Tailwind CSS for a dark-mode "Bloomberg Terminal" aesthetic.

📂 Project Structure

hft-matching-engine/
├── trading-engine/           # C++ Backend
│   ├── include/              # Header files (Order.h, OrderBook.h, ObjectPool.h)
│   ├── src/                  # Source code (main.cpp)
│   ├── scripts/              # Python Load Testing Bots
│   └── libs/                 # Dependencies (uSockets, uWebSockets, json)
│
└── trading-ui/               # React Frontend
    ├── src/                  # React Components
    └── tailwind.config.js    # Styling Configuration
⚡ How to Run
Prerequisites
C++ Compiler: GCC or Clang (supporting C++17)

Build System: CMake (v3.10+)

Runtime: Node.js (for Frontend) & Python 3 (for Benchmarks)

1. Build & Start the Engine
Bash

cd trading-engine
mkdir build && cd build
cmake ..
make
./engine
Expected Output: Starting HFT Engine on port 9001...

2. Launch the Dashboard
Open a new terminal:

Bash

cd trading-ui
npm install
npm run dev
Open http://localhost:5173 in your browser.

3. Run the High-Frequency Bot
Open a third terminal to flood the engine with 1 Million orders:

Bash

cd trading-engine
python scripts/benchmark_bot.py
🔮 Future Improvements
Binary Protocol: Replace JSON with SBE (Simple Binary Encoding) or FIX for lower network overhead.

UDP Multicast: Implement a UDP feed handler for market data distribution (bypassing TCP ACKs).

Lock-Free Queue: Replace std::mutex with a Ring Buffer (LMAX Disruptor pattern) for thread-safe communication.

👨‍💻 Author
Aarav Built as a high-performance systems engineering project.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors