Skip to content

K-Finger/liborderbook

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

liborderbook

A high-performance C++20 limit order book matching engine with sub-100ns add-order latency.

Build License: MIT

Features

  • Sub-100ns latency on the critical path
  • Order types: GTC, Market, IOC, FOK
  • Price-time priority matching (FIFO)
  • Strong types for compile-time safety
  • Slab allocator for cache locality
  • Zero dependencies beyond STL

Quick Start

Build

cmake -S . -B build
cmake --build build --config Release

Integrate

add_subdirectory(liborderbook)
target_link_libraries(your_app PRIVATE orderbook::orderbook)

Use

#include "OrderBook.h"

using namespace orderbook;

OrderBook book;

// Place limit orders
book.addOrder(OrderBuilder{}.id(OrderId{1}).buy().goodTillCancel()
    .price(Price{100}).quantity(Quantity{10}).build());
book.addOrder(OrderBuilder{}.id(OrderId{2}).sell().goodTillCancel()
    .price(Price{101}).quantity(Quantity{20}).build());

// Market order crosses the spread
auto trades = book.addOrder(OrderBuilder{}.id(OrderId{3}).buy().market()
    .quantity(Quantity{5}).build());

book.cancelOrder(OrderId{1});
book.printBook();

Output:

=== ASKS ===
101 | 15 (1)
-------------
=== BIDS ===

API

OrderBuilder

OrderBuilder{}
    .id(OrderId{42})
    .buy()              // or .sell()
    .goodTillCancel()   // or .market() / .fillOrKill() / .immediateOrCancel()
    .price(Price{100})  // omit for market orders
    .quantity(Quantity{10})
    .build();

OrderBook

std::vector<Trade>  addOrder(Order order);   // Match + rest remainder
bool                cancelOrder(OrderId id); // Remove resting order
std::size_t         size() const;            // Live order count
OrderBookLevelInfos getLevelInfos() const;   // Aggregated depth
const std::vector<Trade>& getTrades() const; // Trade history
void                printBook();             // Debug output

Benchmarks

Single thread, MSVC Release with IPO and AVX2:

Operation Latency
Add, no match 67 ns
Add, single match 143 ns
Add into 10k levels 58 ns
Cancel 75 ns
./build/Release/orderbook_bench_all

See BENCHMARKS.md for history.

Tests

cmake --build build --target orderbook_tests
./build/Release/orderbook_tests

Documentation

Document Description
Introduction Architecture and design
Installation Build and integration
Guide Core concepts and operations
Examples Usage patterns
Support FAQ, contributing, license

References

License

MIT

About

A C++20 limit order book library with sub-100ns operations. Supports GTC, market, IOC, and FOK orders

Topics

Resources

License

Stars

2 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors