Skip to content

albystack/LOB-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

⚡ Limit Order Book Matching Engine

A high-performance limit order book simulator written in modern C++.
Implements millisecond-precision event handling, O(log n) inserts/cancels, and realistic market mechanics.
Designed for quantitative trading, backtesting, and microstructure analysis.


🧠 Overview

This engine maintains separate bid/ask trees using std::map (red–black trees) for price levels
and std::list for FIFO queues within each level, guaranteeing deterministic ordering and
accurate time priority. The simulator supports LIMIT, MARKET, and CANCEL orders with
complete trade and depth-of-book CSV logging.


⚙️ Build

mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
cmake --build . -j

▶️ Run Demo

./orderbook_cli
# writes trades.csv and depth.csv in the working directory

🧪 Run Tests

ctest --output-on-failure

🔥 Benchmark

./bench
# prints throughput and writes bench_depth.csv

Benchmark results (Release, Apple M3 Pro):
50 000 orders / second with >1 million total messages processed.


🧩 Design Notes

  • Price maps
    • Bids: std::map<Price, PriceLevel, Desc> (descending order).
    • Asks: std::map<Price, PriceLevel> (ascending order).
  • FIFO Queues
    • std::list<Order> per price level for strict time priority.
    • O(1) cancels via id → iterator hash map.
  • Integer arithmetic
    • All prices/quantities in integer ticks for exactness
      (price = 1000 ⇒ 100.0 if tick = 0.1).
  • Execution rules
    • Market orders cross only; limit orders cross then rest remainder.
  • Outputs
    • trades.csv — execution log.
    • depth.csv — level 2 snapshot for each event.

📦 Repository Structure

src/
 ├── orderbook.hpp / orderbook.cpp   # Core matching logic
 ├── types.hpp                       # Price, Qty, Order structs
 ├── cli.cpp                         # Demo executable
 ├── bench.cpp                       # Benchmark tool
 └── tests/                          # GoogleTest unit tests

🧰 Technologies

Stack Purpose
C++17 / STL core data structures
CMake build system
GoogleTest unit testing
CSV I/O data logging for analysis

🧑‍💻 Author

Alberto Rescigno
Durham University — MSci Computer Science & Mathematics
GitHub • LinkedIn


“Precision and determinism are the true benchmarks of a matching engine.”

LOB-engine

About

New repo

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published