Skip to content

atharvajoshi01/Atlas

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

28 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Atlas

Sub-Nanosecond Order Book Engine in C++20

CI C++20 MIT License

Add Order: 16ns Get BBO: 0.7ns ITCH Parse: 3.9ns Throughput: 62M ops/s


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.

Benchmark Results (Apple M1 Pro)

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_benchmarks to reproduce. Full results in docs/benchmark_results.txt

Quick Start

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

Architecture

                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚                     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   β”‚
                                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Core Components

Order Book (include/atlas/core/order_book.hpp)

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 levels

Design decisions:

  • std::map for 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

Memory Pool (include/atlas/memory/pool_allocator.hpp)

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 list

Why it matters: new/delete costs ~25ns+ with potential syscalls. Pool allocation is 15x faster and deterministic.

Ring Buffer (include/atlas/feed/ring_buffer.hpp)

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 */ }

ITCH 5.0 Parser (include/atlas/feed/itch_parser.hpp)

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).

Matching Engine (include/atlas/matching/matching_engine.hpp)

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);

Project Structure

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

Performance Techniques

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

Python Integration

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)

Feature Engineering

from atlas.features import FeaturePipeline

pipeline = FeaturePipeline.default()  # 50+ features
features = pipeline.compute(order_book_state)  # < 1ms

Backtesting

from 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%}")

Requirements

  • 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)

References

  • Harris, L. (2003). Trading and Exchanges
  • Cartea, A., et al. (2015). Algorithmic and High-Frequency Trading

Live Demo

Interactive dashboard: atlas-dashboard.hf.space

Author

Atharva Joshi - GitHub | LinkedIn

License

MIT

About

Low-Latency Order Book Engine with Predictive Execution - C++ core with Python ML pipeline

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages