Back to Projects
🟒 LIVE

Limit Order Book Simulator

A from-scratch C++ matching engine implementing price-time priority order matching, compiled to WebAssembly for a live in-browser demo β€” trade against a synthetic market maker as either a Market Taker or Market Maker and track real PnL against a persistent account.

Open Live App View on GitHub

TECH STACK

C++WebAssemblyData StructuresMarket MicrostructureAlgorithmsNext.js

Deep Dive

A from-scratch implementation of a Central Limit Order Book (CLOB) with price-time priority matching. Supports limit orders, market orders, order cancellation, and cancel-newest self-trade prevention. Price levels are a std::map of std::list FIFO queues, with O(1)-average cancellation via a handle table storing each resting order's list iterator directly.

Correctness is checked by a differential fuzzer that drives both the production book and a deliberately naive O(n) reference implementation with identical random operation sequences, asserting exact agreement (trades, resting orders, best bid/ask) after every single operation β€” clean across 200k+ iterations and three independent seeds. A Google Benchmark suite backs the performance claim with real measured numbers: insert and cancel both empirically fit O(1) across book depths from 10 to 100,000 (not the O(log n) a plain "sorted map" description might suggest for cancel specifically), and match cost scales with price levels crossed, not total book depth. Full methodology and results in the repo's BENCHMARKS.md.

The matching engine compiles to WebAssembly via Emscripten and runs entirely client-side β€” no backend β€” powering a live order book ladder, cumulative depth chart, trade tape, and price history chart, with a synthetic market maker keeping the book moving so there's always liquidity to trade against.

On top of the engine, a persistent account layer tracks cash, position, weighted-average entry price, and realized/unrealized PnL against a real order history β€” with a toggle between playing as a Market Taker (instant fills against the book) or a Market Maker (quoting your own bid/ask spread), both sharing one account so a self-crossing order between your own quote and your own taker order is correctly blocked by the engine's self-trade prevention. Go negative and you're bankrupt β€” the game resets from scratch.

Live Preview

Live app β€” if it doesn't load, open it directly β†—