Backtest a trading strategy against a deterministic order book
Train a reinforcement learning agent in a custom matching environment
Replay historical order flow and inspect resulting fills
Plain cargo project; cargo run --example simple_order_flow gives an immediate working demo.
lob is a small Rust library that implements a limit order book, the data structure at the heart of every stock exchange or crypto matching system. Bids, the prices buyers are willing to pay, and asks, the prices sellers are willing to accept, are stored sorted by price, and when a buy and a sell cross at the same price the book matches them and produces a fill. The author positions the project as a research tool for experimenting with order matching behaviour and order flow, rather than a production exchange. The default matching rule is price time priority. Orders are sorted first by best price, then within a price level by who arrived first, using a FIFO queue per level. The code is laid out so that the matching engine is a separable component behind a strategy enum, with the current execution layer wired to the price time priority plan. Listed use cases include testing different matching algorithms, building reinforcement learning environments, backtesting trading strategies, building custom books, and replaying historical order flow. Usage looks like creating a LimitOrderBook with the PriceTimePriority strategy, adding buy and sell orders built from the utils helpers with rust_decimal amounts, then calling tick to match the book and read back a list of fills. Two example programs come with the repository. simple_order_flow adds synthetic orders and ticks the book, and depth_snapshot exports the market depth as JSON for plotting in any chart tool. Both are launched through cargo run --example. Benchmarks live in the benches directory and run with cargo bench, using the criterion crate. The current numbers reported in the README are a median 30.866 milliseconds to build a fresh book and insert 10,000 resting orders, which works out to about 323,990 input orders per second, and a median 480.75 milliseconds to fully match and drain a crossed book of 2,000 input orders, about 4,160 input orders per second. The author is candid about current limits. Only limit orders are supported, so market orders, stop losses, and similar types are not yet there. The execution engine assumes the price time priority plan, so plugging in a very different algorithm needs execution layer changes too. Calculating volume at a price level walks the whole queue at that level, and cancellation within a level is also a linear scan.
Generated 2026-05-22 · Model: sonnet-4-6 · Verify against the repo before relying on details.