Skip to content

About

C++20 limit order book prototype exploring price-time priority, SPSC queues, and pooled order storage.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C++20 Matching Engine

A C++20 limit order book prototype exploring price-time priority, pooled order storage, and a producer/consumer queue.

Architecture

  • Ordered bid and ask price levels (std::map), with linked orders within each level.
  • An order-ID index (std::unordered_map) for cancellation lookup. Unlinking within a level is constant-time; cancellation also performs a price-level tree lookup.
  • An SPSC ring buffer between a synthetic producer and the matching thread.
  • Preallocated order objects with a mutex-protected free list. The maps and free-list container can allocate; this is not a zero-allocation or wholly lock-free engine.

Benchmarks and scope

The repository includes a historical latency plot from an Apple M-series machine. The exact chip, compiler configuration, and raw run data are not recorded here, so the plot is illustrative rather than a reproducible performance guarantee.

Historical matching-call latency distribution

The two benchmark programs measure different things:

  • apps/main.cpp generates one million synthetic orders. Its per-order samples time book.add_order after dequeue, including timer overhead; they do not measure producer-to-consumer latency. The printed Avg Latency is total consumer elapsed time divided by processed orders, an amortized time per order. Its reciprocal estimates throughput for that run, not a latency percentile.
  • benchmarks/EngineBench.cpp uses Google Benchmark. Each timed iteration allocates and submits two crossing orders, so its iteration time is not a per-order figure.

scripts/make_plot.py computes mean, median, and percentiles from build/latencies.csv. These are distinct statistics; the histogram omits the top 0.1% of samples for display while computing statistics over the full sample.

The demo uses synthetic traffic, with no network gateway, persistence, exchange protocol, or live-trading integration. Before comparing performance, record the chip, OS, compiler, build flags, workload, and raw output, and repeat the run.

Project structure

apps/main.cpp              Synthetic producer/consumer demo
benchmarks/EngineBench.cpp Google Benchmark crossing-order workload
src/                       Order book, price levels, pool, and ring buffer
tests/OrderBookTest.cpp     GoogleTest tests
scripts/make_plot.py       Latency visualization
plots/                     Historical plot
CMakeLists.txt             Build configuration

Build and run

Requires a C++20 compiler and CMake 3.15+. CMake downloads GoogleTest and Google Benchmark.

From the repository root:

cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j4
./build/unit_tests
./build/benchmarks
cd build
./hft_app

To regenerate the plot, return to the repository root, install pandas, numpy, and matplotlib, then run:

python scripts/make_plot.py

This writes latency_distribution.png in the current directory using build/latencies.csv.

About

C++20 limit order book prototype exploring price-time priority, SPSC queues, and pooled order storage.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages