
A high-performance, low-latency C++ library for managing financial market order books. Optimized for microsecond-level performance in high-frequency trading systems.
โจ Features
- ๐ Blazing Fast: Sub-100ns L2 operations, sub-200ns L3 operations (p99)
- ๐ Dual Market Data: Level 2 (aggregated) and Level 3 (order-by-order) support
- ๐ฏ Multi-Symbol: Efficient management of thousands of instruments
- ๐ง Flexible Build: Compiled library (default) or header-only mode
- ๐งฉ Protocol Agnostic: Generic design works with any exchange feed
- โก Zero Allocations: Object pooling eliminates runtime allocations in hot paths
- ๐ฆ C++23: Leverages modern C++ features (
std::flat_map, concepts, etc.)
๐ Performance
All performance targets exceeded by 2-40x:
| Operation | Target | Actual (p99) | Status |
| L2 Add/Modify/Delete | <100ns | 21-33ns | โ
3-5x faster |
| L3 Add/Modify/Delete | <200ns | 59-490ns | โ
2-3x faster |
| Best Bid/Ask Query | <10ns | 0.25-0.33ns | โ
30-40x faster |
| Observer Notification | <50ns/observer | 2-3ns | โ
16-25x faster |
| Memory Footprint (L2) | <1KB/symbol | <1KB | โ
Meets target |
| Memory Footprint (L3) | <10KB/1000 orders | <10KB | โ
Meets target |
๐ Quick Start
Prerequisites
- C++23 compiler: GCC 14+, Clang 18+, or MSVC 2022+ (17.6+)
- CMake: 3.20 or higher
- Git: For cloning the repository
Installation
# Clone the repository
git clone https://github.com/SlickQuant/slick-orderbook.git
cd slick-orderbook
# Build (Release mode)
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
cmake --build . -j
# Run tests
ctest --output-on-failure
# Install (optional)
sudo cmake --install .
Basic Usage - Level 2 OrderBook
#include <iostream>
int main() {
book.updateLevel(Side::Buy, 10000, 100, 0, 0);
book.updateLevel(Side::Buy, 9950, 50, 0, 0);
book.updateLevel(Side::Sell, 10050, 75, 0, 0);
book.updateLevel(Side::Sell, 10100, 100, 0, 0);
auto tob = book.getTopOfBook();
std::cout << "Best Bid: " << tob.best_bid << " x " << tob.bid_quantity << "\n";
std::cout << "Best Ask: " << tob.best_ask << " x " << tob.ask_quantity << "\n";
std::cout << "Spread: " << tob.spread() << "\n";
std::cout << "Mid Price: " << tob.midPrice() << "\n";
return 0;
}
Level 2 OrderBook - aggregated price levels.
Output:
Best Bid: 10000 x 100
Best Ask: 10050 x 75
Spread: 50
Mid Price: 10025
Basic Usage - Level 3 OrderBook
int main() {
book.addOrModifyOrder(1001, Side::Buy, 10000, 100, 0, 0, 0);
book.addOrModifyOrder(1002, Side::Buy, 10000, 50, 0, 1, 0);
book.addOrModifyOrder(2001, Side::Sell, 10050, 75, 0, 0, 0);
book.addOrModifyOrder(1001, Side::Buy, 10000, 150, 0, 0, 0);
book.executeOrder(2001, 25, 0, 0);
book.deleteOrder(1002, 0);
auto bids = book.getLevelsL2(Side::Buy);
auto asks = book.getLevelsL2(Side::Sell);
return 0;
}
Level 3 OrderBook - Individual order tracking with price-time priority.
Multi-Symbol Management
#include <iostream>
int main() {
btc_book->updateLevel(Side::Buy, 4500000, 1000, 0, 0);
eth_book->updateLevel(Side::Buy, 250000, 5000, 0, 0);
sol_book->updateLevel(Side::Buy, 10000, 10000, 0, 0);
for (
SymbolId symbol_id : manager.getSymbols()) {
std::cout << "Symbol " << symbol_id << ": Best Bid=" << tob.best_bid << "\n";
}
return 0;
}
Multi-symbol OrderBook Manager.
const OrderBookT * getOrderBook(SymbolId symbol) const
Get existing orderbook (read-only access) Thread-safe: Uses shared lock for read-only access Lifetime...
OrderBookT * getOrCreateOrderBook(SymbolId symbol)
Get existing orderbook or create new one if it doesn't exist Thread-safe: Uses shared_mutex for symbo...
uint16_t SymbolId
Symbol identifier - can be hash or sequential ID.
Lifetime: raw pointers from getOrCreateOrderBook() / getOrderBook() are invalidated by removeOrderBook() / clear(), and by move-assigning another manager into this one. If symbols can be removed while other threads use them, use getOrCreateSharedOrderBook() / getSharedOrderBook(), whose std::shared_ptr keeps the orderbook alive after removal.
Observer Pattern - Real-Time Notifications
#include <iostream>
public:
std::cout << "Level updated: "
<< (update.
side == Side::Buy ?
"BID" :
"ASK")
}
void onTopOfBookUpdate(
const TopOfBook& tob)
override {
}
};
int main() {
auto observer = std::make_shared<MyObserver>();
book.addObserver(observer);
book.updateLevel(Side::Buy, 10000, 100, 0, 0);
return 0;
}
Base interface for orderbook observers Observers receive callbacks for orderbook events.
Level 2 price level update event Note: quantity = 0 implies the price level should be deleted.
Top-of-book snapshot - best bid and ask.
๐ Documentation
๐๏ธ Architecture Highlights
Cache-Optimized Data Structures
- FlatMap: Uses
std::flat_map (C++23) for cache-friendly sorted price level storage
- Intrusive List: Zero-allocation doubly-linked list for order queues
- Object Pool: Pre-allocated memory eliminates runtime allocations
- Cache Alignment: 64-byte alignment prevents false sharing in multi-symbol scenarios
Zero-Cost Abstractions
- Template-Based: No virtual dispatch in hot paths
- Concepts: C++23 concepts for compile-time type checking
- Inline Everything: Aggressive inlining for sub-nanosecond operations
๐ง CMake Options
# Build options
-DCMAKE_BUILD_TYPE=Release # Release, Debug, RelWithDebInfo
-DSLICK_ORDERBOOK_HEADER_ONLY=ON # Enable header-only mode (default: OFF)
-DSLICK_ORDERBOOK_BUILD_SHARED=ON # Build shared library (default: OFF)
-DSLICK_ORDERBOOK_BUILD_TESTS=ON # Build tests (default: ON)
-DSLICK_ORDERBOOK_BUILD_BENCHMARKS=ON # Build benchmarks (default: OFF)
-DSLICK_ORDERBOOK_BUILD_EXAMPLES=ON # Build examples (default: ON)
-DSLICK_ORDERBOOK_BUILD_COINBASE_EXAMPLE=ON # Build the Coinbase integration example (default: OFF)
-DSLICK_ORDERBOOK_ENABLE_LTO=OFF # LTO/IPO in Release builds (default: ON)
-DSLICK_ORDERBOOK_ENABLE_NATIVE_ARCH=OFF # -march=native in Release builds, GCC/Clang (default: ON)
The Coinbase example is opt-in: it fetches coinbase-advanced-cpp, which requires nlohmann-json, OpenSSL, Boost.Beast and jwt-cpp to be installed (e.g. via vcpkg) before configuring.
Shared Library
With SLICK_ORDERBOOK_BUILD_SHARED=ON the OrderBookL2/OrderBookL3 classes and the OrderBookManager<OrderBookL2>/OrderBookManager<OrderBookL3> instantiations are exported (__declspec(dllexport) on Windows, default visibility elsewhere). Linking slick::orderbook propagates SLICK_ORDERBOOK_SHARED so consumers import them. On Windows, the DLL must be on PATH or next to the executable at runtime. The library's SOVERSION is MAJOR.MINOR: ABI compatibility is only kept within a minor version.
๐ฆ Integration
Installed Package (find_package)
After cmake --install, the package config is installed to <prefix>/lib/cmake/slick-orderbook (for both compiled and header-only builds). All of the library's install rules belong to the slick-orderbook component, so cmake --install <build> --component slick-orderbook installs only this library, without anything fetched dependencies install:
find_package(slick-orderbook 1.1 REQUIRED)
target_link_libraries(your_target PRIVATE slick::orderbook)
Version matching follows the ABI policy: a compiled (static or shared) install only satisfies requests for the same MAJOR.MINOR (find_package(slick-orderbook 1.0) does not accept 1.1.x), while a header-only install satisfies any lower or equal version with the same major version.
Linking slick::orderbook also applies the required C++23 mode (cxx_std_23) to your_target.
CMake FetchContent
include(FetchContent)
FetchContent_Declare(
slick-orderbook
GIT_REPOSITORY https://github.com/SlickQuant/slick-orderbook.git
GIT_TAG main
)
FetchContent_MakeAvailable(slick-orderbook)
target_link_libraries(your_target PRIVATE slick::orderbook)
Header-Only Mode
# In your CMakeLists.txt
add_compile_definitions(SLICK_ORDERBOOK_HEADER_ONLY)
target_include_directories(your_target PRIVATE /path/to/slick-orderbook/include)
target_compile_features(your_target PRIVATE cxx_std_23)
include/slick/orderbook/version.hpp is generated by CMake at configure time from the project(VERSION ...) in CMakeLists.txt; change the version there, not in the header.
๐งช Testing
# Build and run all tests
cmake -B build -DCMAKE_BUILD_TYPE=Debug -DSLICK_ORDERBOOK_BUILD_TESTS=ON
cmake --build build -j
cd build && ctest --output-on-failure
# Run specific tests (all unit tests are in one GoogleTest executable)
./build/tests/slick_orderbook_tests --gtest_filter='OrderBookL2Test.*'
# With sanitizers
cmake -B build-asan -DCMAKE_CXX_FLAGS="-fsanitize=address -g"
cmake --build build-asan
cd build-asan && ctest
๐ Benchmarking
# Build benchmarks
cmake -B build -DCMAKE_BUILD_TYPE=Release -DSLICK_ORDERBOOK_BUILD_BENCHMARKS=ON
cmake --build build -j
# Run all benchmarks
cd build/benchmarks
./bench_orderbook_l2
./bench_orderbook_l3
./bench_market_replay
# Run specific benchmark with filter
./bench_orderbook_l2 --benchmark_filter="AddNewLevel/100"
# Output results to JSON
./bench_orderbook_l2 --benchmark_out=results.json --benchmark_out_format=json
๐ License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
๐ Acknowledgments
- Built with modern C++23 features
- Powered by Google Benchmark for performance testing
- Inspired by high-frequency trading systems and exchange matching engines
๐ง Contact
- Organization: Slick Quant
- GitHub Issues: For bug reports and feature requests
Made with โก by Slick Quant