Skip to content

Pakkuu/Order-Book

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

High-Performance C++ Order Book

A production-grade limit order book implementation in C++17 with comprehensive metrics collection and sub-microsecond latency.

Features

  • Price-Time Priority Matching: FIFO matching algorithm
  • High Performance: Sub-100ns latency, 11M+ ops/sec throughput
  • Comprehensive Metrics: Latency tracking (P50, P95, P99), throughput measurements
  • Zero-Copy Design: Intrusive linked lists for order management
  • Well-Tested: Complete unit test suite and performance benchmarks

Architecture

Core Components

  • Order: Individual buy/sell orders with quantity and price
  • PriceLevel: Manages all orders at a specific price using intrusive doubly-linked list
  • OrderBook: Main matching engine with separate bid/ask sides
  • Metrics: Lock-free performance metrics collection

Data Structures

  • std::map<Price, PriceLevel> for price level management (O(log n) operations)
  • std::unordered_map<OrderId, Order> for O(1) order lookup
  • Intrusive linked lists within price levels for zero-allocation management

Building

Prerequisites

# macOS
brew install cmake googletest google-benchmark

# Ubuntu/Debian
sudo apt-get install cmake libgtest-dev libbenchmark-dev

Build Instructions

mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make -j$(nproc)

Usage

Basic Example

#include "OrderBook.hpp"

using namespace orderbook;

int main() {
    OrderBook book;
    
    // Set up trade callback
    book.set_trade_callback([](const Trade& trade) {
        std::cout << "Trade: " << trade.quantity 
                  << " @ $" << trade.price / 100.0 << "\n";
    });
    
    // Add limit orders
    book.add_limit_order(1, Side::BUY, 10000, 100);   // Buy 100 @ $100.00
    book.add_limit_order(2, Side::SELL, 10050, 50);   // Sell 50 @ $100.50
    
    // Add market order (executes immediately)
    auto filled = book.add_market_order(3, Side::BUY, 50);
    
    // Cancel order
    book.cancel_order(1);
    
    // View metrics
    std::cout << book.metrics().get_summary();
    
    return 0;
}

Running Examples

# Run demo program
./build/simple_trading

# Run unit tests (if Google Test available)
./build/order_book_tests

# Run performance benchmarks (if Google Benchmark available)
./build/order_book_benchmark

Performance Results

Measured on 10,000+ order workload:

Latency (nanoseconds)

Operation Average P50 P95 P99 Max
Add Order 88.97 83 84 167 22,917
Cancel Order 250 250 250 250 250
Match Order 5,896 7,708 7,708 7,708 7,708

Throughput

  • 11,239,349 operations/second
  • Total test operations: 10,011 (10,008 adds, 1 cancel, 2 matches)
  • Total volume traded: 300 shares

Complexity

Operation Average Worst Case
Add Order O(log n) O(log n)
Cancel Order O(log n) O(log n)
Match Order O(m) O(m)
Best Bid/Ask O(1) O(1)

where n = number of price levels, m = number of orders matched

API Reference

OrderBook Methods

// Add limit order to book
bool add_limit_order(OrderId id, Side side, Price price, Quantity qty);

// Add market order (executes immediately)
Quantity add_market_order(OrderId id, Side side, Quantity qty);

// Cancel existing order
bool cancel_order(OrderId id);

// Query best prices
std::optional<Price> best_bid() const;
std::optional<Price> best_ask() const;
std::optional<Price> spread() const;

// Query volume
Quantity get_bid_volume(Price price) const;
Quantity get_ask_volume(Price price) const;

// Get metrics
const Metrics& metrics() const;

Metrics Methods

// Get operation counts
uint64_t total_orders() const;
uint64_t total_cancels() const;
uint64_t total_matches() const;
uint64_t total_volume_traded() const;

// Get latency percentiles (in nanoseconds)
int64_t get_add_percentile(double percentile) const;
int64_t get_cancel_percentile(double percentile) const;
int64_t get_match_percentile(double percentile) const;

// Get formatted summary
std::string get_summary() const;

Design Decisions

Intrusive Lists

Intrusive linked lists avoid separate allocations for list nodes, reducing memory overhead and improving cache locality. Orders contain next_ and prev_ pointers directly.

Map-Based Price Levels

std::map provides automatic ordering, O(log n) operations, and standard library reliability. Good enough for typical market depth (dozens to hundreds of price levels).

Thread Safety

The current implementation is not thread-safe. For multi-threaded use, add external synchronization (e.g., mutex per order book).

Testing

Unit Tests

Run the test suite:

./build/order_book_tests

Tests cover:

  • Order lifecycle (add, cancel, modify)
  • Matching logic correctness
  • Price-time priority verification
  • Edge cases (empty book, crossing orders)

Benchmarks

Run performance benchmarks:

./build/order_book_benchmark

Benchmarks measure:

  • Individual operation latencies
  • Mixed workload throughput
  • Scalability with order book depth

About

High Performance C++ Orderbook

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors