A transparent C++20 central limit order book paired with a Python implementation of the Almgren–Chriss execution experiment.
- Integer tick prices and quantities.
- Price-time priority.
- Limit and market orders.
- Partial fills and cancellations.
- Best bid/ask, spread, top-level depth, and imbalance.
- Deterministic event simulation and CSV export.
- TWAP and Almgren–Chriss liquidation schedules.
- Monte Carlo implementation-shortfall comparison using common random numbers.
cmake -S . -B build
cmake --build build
ctest --test-dir build --output-on-failure
./build/order_book results
uv sync
uv run pytest
uv run python analysis.pyThe final command creates results/execution_metrics.csv and results/microstructure_and_execution.png. The committed summary is RESULTS.md; both stages are seeded, so the table reproduces exactly.
Almgren–Chriss cut the standard deviation of implementation shortfall by 29.1% relative to TWAP, for 0.85 additional basis points of expected cost. That is the intended trade-off of the model, not evidence that one schedule dominates.
The C++ suite checks price-time priority, non-crossing limits, execution at the resting price, cancellation, top-level imbalance, and rejection of malformed or duplicate orders. It deliberately avoids assert, because a release build defines NDEBUG and would strip every assertion, leaving a binary that reports success unconditionally. Checks are always compiled in and report the failing expression and line, and the test target is built with -UNDEBUG so the engine's internal invariant assertions stay live regardless of build type.
The Python suite covers the execution layer: the schedule liquidates exactly the parent order, zero risk aversion reduces to TWAP, higher risk aversion front-loads execution monotonically, degenerate parameters are rejected, and the Monte Carlo comparison actually exhibits the variance-for-cost trade-off the model predicts.
The matching engine uses standard containers and linear cancellation lookup. This is intentional: the project demonstrates correct matching and measurable invariants rather than making unsupported nanosecond-latency claims. A production venue would require indexed cancellation, persistence, recovery, concurrency controls, and much more extensive testing.
The generated order flow is synthetic. It is useful for verifying mechanics but cannot establish empirical facts about a real exchange. The execution parameters are explicit assumptions and are not presented as market calibrations.
- Almgren, R. and Chriss, N. (2001). Optimal execution of portfolio transactions. Journal of Risk.
- Cont, R., Kukanov, A. and Stoikov, S. (2014). The price impact of order book events. Journal of Financial Econometrics.
- Gould, M. et al. (2013). Limit order books. Quantitative Finance.