nr.dev
← work
// project

Cryptocurrency Order Book & Matching Engine

Thread-safe C++20 limit order book with price-time priority matching

2025 · live
[ STACK ]
C++20 SQLite CMake Google Test
[ LINKS ]

A C++20 limit order book and matching engine that mirrors how real exchanges work. Buy and sell orders are organized by price into sorted maps, with time-priority queues at each price level so orders fill in the right order. All internal prices and quantities are stored as int64_t scaled by 10^8, which avoids the floating-point precision errors that show up when financial code accumulates rounding across thousands of trades. A producer-consumer simulator drives realistic order flow using live BTC prices fetched from the CoinMarketCap API, and every executed trade gets persisted to SQLite through prepared statements.

The order book is thread-safe through a shared mutex, so concurrent reads (best bid, best ask, depth checks) don’t block each other while writes still take an exclusive lock. Rolling market statistics like VWAP, spread, and volatility are computed live using Welford’s algorithm in a ring buffer. The benchmarking suite measures p50 and p99 latency for addOrder, executeTrade, and full market simulation from one to one hundred thousand orders, with the matching engine and price fetcher both built behind interfaces so unit tests can inject mocks rather than touch the network or the database.

← back to work