1 of 4
Cross an order into a live book, price what the sweep costs, and cancel out of one in constant time.
Raise the order size until the buy stops filling at the best offer, and watch what the average fill price does as each level runs out.
Limit Order Book & Market Impact Simulator
This one has no automatic check. Work through it above, then carry on.
Market microstructure and order books· 2 of 3
Market microstructure and order books
Limit order book
Build an in-memory limit order book with price-time priority, O(1) cancellations, and crossing market order execution.
25 minute read
A limit order book tracks all unexecuted limit orders submitted by market participants.
To handle high message throughput, matching engines organize bids and asks in sorted price levels, where each price level maintains a FIFO queue of resting orders.
Representation of order book state
An order book maintains two sides:
- Bids: Buy orders sorted in descending order of price. The highest bid is the best bid.
- Asks: Sell orders sorted in ascending order of price. The lowest ask is the best ask.
When a new buy order arrives with a price equal to or higher than the best ask, the matching engine does not add it to the bid queue. Instead, it immediately crosses the book, matching against resting ask orders in price-time sequence until either the incoming buy quantity is satisfied or no acceptable asks remain. Any remaining unfilled quantity is then posted to the bid queue.
Fast order cancellation
In live trading, quote updates and cancellations vastly outnumber executed trades. If cancelling an order requires scanning every queue in the order book, the search is $O(N)$ in the number of open orders.
Production matching systems track active orders in a hash map from order_id to order reference. Cancelling an order updates its active status or sets its quantity to zero in $O(1)$ time. When the matching engine encounters a zero-quantity order at the head of a queue during a fill pass, it discards the dead node lazily.
An ID set preserves every submitted identifier, so duplicate submissions remain a permanent error. A fully filled order is terminal, while a partially filled taker remains resting until its remainder is gone.
Exercise: build an order book
In this exercise, you will implement an in-memory OrderBook with FIFO price-time priority.
Your implementation must support:
- Inserting limit orders with automatic crossing execution when prices cross.
- Executing market orders against resting book liquidity and reporting any unfilled quantity.
- Cancelling existing orders in $O(1)$ time.
- Computing the best bid, best ask, midpoint price, and quoted spread.
A reused order ID always raises ValueError. A partially filled taker still has unfilled remainder, so it remains resting and cancellable; a fully filled maker cannot be cancelled or reused.
Market microstructure and order books
Limit order book
Build an in-memory limit order book with price-time priority, O(1) cancellations, and crossing market order execution.
25 minute read
Implement your solution in the editor on the right or work in your local IDE. All unit tests run against your code in real-time.
Exercise
limit-order-book
8 tests, graded on your machine in about a second. Stuck for 35 minutes? The desktop app will offer you the reference solution.