Sub-Nanosecond Order Book Engine in C++20
A production-grade limit order book engine achieving sub-20 nanosecond order insertion with zero heap allocations in the hot path. Built for HFT research and quantitative trading systems.
Order Book Operations:
BM_OrderBook_AddOrder 16.3 ns 62M ops/s β Core operation
BM_OrderBook_CancelOrder 50.8 ns 20M ops/s
BM_OrderBook_GetBBO 0.72 ns 1.4B ops/s β Sub-nanosecond
BM_OrderBook_BestBid 0.28 ns 3.5B ops/s
BM_OrderBook_MidPrice 0.67 ns 1.5B ops/s
BM_OrderBook_GetDepth/10 41.0 ns 24M ops/s
ITCH 5.0 Protocol Parsing:
BM_ITCH_ParseAddOrder 3.9 ns 258M msg/s β Single message
BM_ITCH_ParseMessageStream ~4 GB/s 148M msg/s β Bulk parsing
BM_ITCH_Be64ToHost 0.24 ns 4.2B ops/s β Endianness
Infrastructure:
BM_PoolAllocator_Allocate 1.7 ns 588M ops/s
BM_RingBuffer_PushPop 3.2 ns 620M ops/s
BM_MatchingEngine_Cancel 2.8 ns 354M ops/s
Run
./build/atlas_benchmarksto reproduce. Full results indocs/benchmark_results.txt
git clone https://github.com/atharvajoshi01/Atlas.git
cd Atlas
# Build (requires CMake 3.16+, C++20 compiler)
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make -j$(nproc)
# Run benchmarks
./atlas_benchmarks
# Run tests
./atlas_tests βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β ATLAS ENGINE β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββββ βββββββββββββββββ βββββββββββββββββ
β MARKET DATA β β ORDER BOOK β β MATCHING β
β HANDLER β β (CORE) β β ENGINE β
βββββββββββββββββ€ βββββββββββββββββ€ βββββββββββββββββ€
β Ring Buffer β βββββββββββββββΆβ Price Levels βββββββββββββββββΆβ Price-Time β
β (SPSC, 3ns) β β (std::map) β β Priority β
β β β β β β
β ITCH Parser β β Order Index β β IOC/FOK/GTC β
β (3.9ns/msg) β β (hash map) β β Support β
β β β β β β
β Feed Handler β β BBO Cache β β Trade β
β Interface β β (0.7ns) β β Callbacks β
βββββββββββββββββ βββββββββββββββββ βββββββββββββββββ
β β β
βββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββ
β MEMORY POOL β
βββββββββββββββββ€
β 64-byte align β
β Zero malloc β
β 1.7ns alloc β
βββββββββββββββββ
The heart of the engine. Maintains bid/ask sides with price-time priority.
#include "atlas/core/order_book.hpp"
atlas::OrderBook book;
// Add orders - 16ns per operation
book.add_order(/*id=*/1, /*price=*/to_price(100.00), /*qty=*/100, Side::Buy);
book.add_order(/*id=*/2, /*price=*/to_price(100.01), /*qty=*/50, Side::Sell);
// Get BBO - 0.7ns (cached)
auto bbo = book.get_bbo();
std::cout << "Spread: " << from_price(bbo.spread()) << std::endl;
// Get market depth
std::vector<DepthLevel> bids, asks;
book.get_depth(bids, asks, /*levels=*/10); // 41ns for 10 levelsDesign decisions:
std::mapfor price levels - O(log N) with N < 100 typical levels- Intrusive doubly-linked list for orders at each level - O(1) cancel
- Cached BBO invalidated only on best-level changes
Zero-allocation order management via pre-allocated memory pool.
#include "atlas/memory/pool_allocator.hpp"
// Pre-allocate 1M order slots, 64-byte aligned
atlas::PoolAllocator<Order, 1000000> pool;
Order* order = pool.allocate(); // 1.7ns - no syscall
pool.deallocate(order); // O(1) return to free listWhy it matters: new/delete costs ~25ns+ with potential syscalls. Pool allocation is 15x faster and deterministic.
Lock-free SPSC queue for market data ingestion.
#include "atlas/feed/ring_buffer.hpp"
atlas::RingBuffer<MarketMessage, 65536> buffer; // Power of 2 for fast modulo
// Producer (market data thread)
buffer.push(message); // 3.2ns
// Consumer (order book thread)
MarketMessage msg;
if (buffer.pop(msg)) { /* process */ }Production-grade NASDAQ ITCH 5.0 binary protocol parser.
#include "atlas/feed/itch_parser.hpp"
#include "atlas/feed/itch_handler.hpp"
// Low-level parser with callbacks
atlas::itch::Parser parser;
parser.on_add_order([](const atlas::itch::AddOrder& msg) {
std::cout << "Order: " << msg.order_ref
<< " @ " << msg.price_double() << "\n";
});
// Parse binary ITCH data - 3.9ns per message
size_t consumed = parser.parse_messages(data, len);
// High-level handler integrates with OrderBook
atlas::ITCHHandler handler;
handler.set_symbol_filter("AAPL");
handler.on_trade([](const auto& trade) {
std::cout << "Trade: " << trade.quantity << " @ " << trade.price << "\n";
});
handler.initialize();
handler.process(itch_data, len);
auto* book = handler.get_order_book("AAPL");Supported message types: Add Order (A/F), Order Executed (E/C), Order Cancel (X), Order Delete (D), Order Replace (U), Trade (P/Q), System Event (S), Stock Directory (R).
Full matching with IOC, FOK, GTC order types.
#include "atlas/matching/matching_engine.hpp"
atlas::MatchingEngine engine;
// Register trade callback
engine.set_trade_callback([](const Trade& trade) {
std::cout << "Trade: " << trade.quantity << " @ " << trade.price << "\n";
});
// Submit order - matches immediately if crosses spread
engine.submit_order(order);Atlas/
βββ include/atlas/
β βββ core/
β β βββ types.hpp # Price, Quantity, OrderId (64-byte aligned)
β β βββ order.hpp # Order struct with intrusive list pointers
β β βββ price_level.hpp # Doubly-linked order list at price
β β βββ order_book.hpp # Main order book class
β βββ memory/
β β βββ pool_allocator.hpp # Lock-free memory pool
β βββ matching/
β β βββ matching_engine.hpp # Price-time priority matching
β βββ feed/
β βββ ring_buffer.hpp # SPSC lock-free queue
β βββ itch_parser.hpp # NASDAQ ITCH 5.0 parser
β βββ itch_handler.hpp # ITCH to OrderBook bridge
β βββ market_data.hpp # Message definitions
β βββ feed_handler.hpp # Feed handler interface
βββ tests/cpp/
β βββ test_order_book.cpp
β βββ test_matching_engine.cpp
β βββ test_ring_buffer.cpp
β βββ test_itch_parser.cpp
β βββ benchmark/
β βββ bench_order_book.cpp
β βββ bench_matching.cpp
β βββ bench_itch.cpp
βββ atlas/ # Python ML pipeline
β βββ features/ # 50+ order book features (Numba JIT)
β βββ signals/ # Alpha generation
β βββ backtest/ # Execution simulation
βββ dashboard/ # Streamlit visualization
| Technique | Impact | Location |
|---|---|---|
| Cache-line alignment | Prevents false sharing | types.hpp - 64-byte aligned structs |
| Memory pooling | Zero malloc in hot path | pool_allocator.hpp |
| BBO caching | 0.7ns best price access | order_book.hpp |
| Intrusive lists | O(1) order removal | price_level.hpp |
| Power-of-2 sizing | Fast modulo via bitmask | ring_buffer.hpp |
| Zero-copy parsing | 3.9ns per ITCH message | itch_parser.hpp |
| Branch prediction hints | [[likely]]/[[unlikely]] |
Throughout |
The C++ engine exposes Python bindings for research:
from atlas import OrderBook, Side, to_price
book = OrderBook()
book.add_order(id=1, price=to_price(100.0), quantity=100, side=Side.Buy)
# Get depth as NumPy array (zero-copy)
depth = book.get_depth_array(levels=10) # Shape: (10, 4)from atlas.features import FeaturePipeline
pipeline = FeaturePipeline.default() # 50+ features
features = pipeline.compute(order_book_state) # < 1msfrom atlas.backtest import BacktestEngine
engine = BacktestEngine(initial_capital=100000)
result = engine.run(strategy, market_data)
print(f"Sharpe: {result.sharpe_ratio:.2f}")
print(f"Max DD: {result.max_drawdown:.2%}")- C++20 compiler (GCC 10+, Clang 12+, MSVC 2019+)
- CMake 3.16+
- Google Benchmark (fetched automatically)
- Google Test (fetched automatically)
- Python 3.8+ (optional, for ML pipeline)
- Harris, L. (2003). Trading and Exchanges
- Cartea, A., et al. (2015). Algorithmic and High-Frequency Trading
Interactive dashboard: atlas-dashboard.hf.space
Atharva Joshi - GitHub | LinkedIn
MIT