A high-performance C++20 limit order book matching engine with sub-100ns add-order latency.
- 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
cmake -S . -B build
cmake --build build --config Releaseadd_subdirectory(liborderbook)
target_link_libraries(your_app PRIVATE orderbook::orderbook)#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 ===
OrderBuilder{}
.id(OrderId{42})
.buy() // or .sell()
.goodTillCancel() // or .market() / .fillOrKill() / .immediateOrCancel()
.price(Price{100}) // omit for market orders
.quantity(Quantity{10})
.build();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 outputSingle 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_allSee BENCHMARKS.md for history.
cmake --build build --target orderbook_tests
./build/Release/orderbook_tests| Document | Description |
|---|---|
| Introduction | Architecture and design |
| Installation | Build and integration |
| Guide | Core concepts and operations |
| Examples | Usage patterns |
| Support | FAQ, contributing, license |
- How to Build a Fast Limit Order Book
- Limit Order Book in C++ — Abosi
- brprojects/Limit-Order-Book
- Strong types with CRTP — Fluent C++
- TomaszRewak/cpp-allocator — slab allocator
MIT