A high-performance, ultra-low latency C++17 trading infrastructure project that parses raw binary exchange data (NASDAQ ITCH 5.0) and reconstructs real-time Limit Order Books (LOB). Built from the ground up for maximum throughput, utilizing Zero-Copy Memory-Mapped I/O, Object Pooling, and Lock-Free Multi-Threading.
The architecture is explicitly designed to bypass operating system bottlenecks and eliminate dynamic heap allocations on the hot path.
flowchart LR
A[(Raw ITCH 5.0 Data)] -->|Zero-Copy mmap| B(Producer Thread)
B -->|Parse Big-Endian| C{Lock-Free SPSC Ring Buffer}
C -->|Memory Barriers| D(Consumer Thread)
subgraph Order Book Engine
D -->|O 1 lookup| E[(AAPL LOB)]
D -->|O 1 lookup| F[(MSFT LOB)]
D -->|O 1 lookup| G[(TSLA LOB)]
end
Traditional file I/O (fread) suffers from system call overhead and kernel-to-user memory copying. This engine maps the ITCH binary file directly into the virtual address space using OS page cache, allowing the CPU to perform zero-copy pointer arithmetic over the data.
To decouple parsing from Order Book construction without lock contention, the threads communicate over a circular ring buffer. It uses std::atomic<size_t> with strict memory_order_release and memory_order_acquire semantics, padded to alignas(64) to completely prevent false sharing between CPU cores.
The Limit Order Book reconstructs millions of active orders. Using new and delete causes severe lock contention in the global heap allocator and fragments the CPU cache. The engine uses a custom MemoryPool<Order, 4096> that pre-allocates contiguous memory blocks, turning allocations and deallocations into
The engine can be run in a Single-Threaded baseline or Multi-Threaded SPSC mode.
Benchmark: 1,000,000 Message ITCH Dataset (sample_1m.itch)
| Metric | Single-Threaded | Multi-Threaded (SPSC) |
|---|---|---|
| Total Processed | 7,498,324 messages | 7,498,324 messages |
| Elapsed Time | 1,205.8 ms | 1,080.1 ms |
| Throughput | 6.21 Million msgs/sec | 6.94 Million msgs/sec |
| Avg Latency | 160.8 ns / msg | 144.1 ns / msg |
Full-Day Stress Test: 11.2 GB ITCH Dataset (01302019.NASDAQ_ITCH50)
(Data sourced from NASDAQ ITCH Sample Data)
| Metric | Single-Threaded | Multi-Threaded (SPSC) |
|---|---|---|
| Total Processed | 368,366,634 messages | 368,366,634 messages |
| Elapsed Time | 537.8 sec (8.9 min) | 800.5 sec (13.3 min) |
| Throughput | 684,934 msgs/sec | 460,162 msgs/sec |
| Avg Latency | 1,460.0 ns / msg | 2,173.1 ns / msg |
Why is Multi-Threading slower on the 11.2 GB file? On smaller samples (1M messages), the SPSC queue provides a ~15% throughput boost. However, a full-day dataset contains over 9,000 unique equities. The Consumer thread acts as a bottleneck because algorithmic maintenance (
std::maprebalancing) for 9,000 distinct order books is heavy. The ultra-fast Producer thread instantly fills the lock-free queue and enters a spin-wait state, causing CPU cache-thrashing, which ultimately degrades performance. This intentionally highlights the hardware realities and limits of lock-free concurrency.
Note: The engine tracks and computes the Best Bid, Best Ask, Spread, and Mid Price for all equities instantly after every single message, maintaining strictly sorted Red-Black Trees (std::map).
While tools like Valgrind are traditionally used for leak detection, this engine guarantees memory safety by design. The successful parsing of the 11.2 GB full-day NASDAQ feed (368.3 million messages) without an Out-Of-Memory (OOM) crash mathematically proves that the custom allocator perfectly freed over 160 million orders.
-
RAII Object Lifecycles: The custom
MemoryPoolowns all chunks. When theBookManagerfalls out of scope, the pool's destructor automatically cascades and returns all block memory to the OS. -
Zero Orphaned Orders: The
$O(1)$ order tracking hash-map guarantees that cancellations ('X') and deletions ('D') perfectly track back to the memory pool without leaks. - Deterministic Memory Footprint: Because the system uses arena allocation, the application's memory usage plateaus and remains perfectly flat, avoiding OOM issues during massive market volume spikes.
This project is built as a command-line interface (CLI) tool. It requires a C++17 compliant compiler (g++ or clang++).
make clean
makeThe CLI requires the path to an uncompressed ITCH 5.0 binary file. By default, it runs the Single-Threaded engine. You can pass optional flags to change the execution mode:
# 1. Run Single-Threaded Mode (Default)
./build/market_parser data/sample_1m.itch
# 2. Run Lock-Free Multi-Threaded Mode
./build/market_parser data/sample_1m.itch --multi
# 3. Run BOTH modes sequentially to compare performance
./build/market_parser data/sample_1m.itch --compareThe CLI does not generate external files. It prints its results directly to stdout in your terminal. After rapidly parsing the dataset, the engine instantly outputs:
- Execution Statistics: Total messages processed, explicitly categorized by message type (Adds, Cancels, Executions, Deletes, etc.).
- Performance Metrics: Total elapsed time, throughput (msgs/sec), and average latency per message.
- Order Book Snapshot: A live, sorted tabular snapshot of the top equities, displaying their Best Bid, Best Ask, Spread, and Mid Price exactly as they stood at the end of the file.
While designed as a showcase for high-performance C++ concepts, this infrastructure is directly applicable to real-world quantitative trading:
- Historical Backtesting: Rapidly reconstruct full order books from historical exchange data to simulate and test high-frequency trading (HFT) strategies.
- Latency Benchmarking: Evaluate OS-level bottlenecks and hardware cache efficiency by profiling single vs. multi-threaded throughput.
- Market Microstructure Analysis: Extract tick-level insights, spread dynamics, and order flow imbalances from raw, unaggregated LOB data.
A- Add OrderX- Order CancelD- Order DeleteE- Order ExecutedU- Order ReplaceP- Non-Cross TradeS- System Event