Who Should Fill First?
Put orders into one deterministic queue and observe price priority, time priority, resting prices, and partial fills.
- Explain price-time priority in an order queue
- Predict why a large order can fill only partly
- Separate a limit, an execution, and released frozen assets
- PREVIOUSCourse introduction
- CURRENTWho Should Fill First?
Put orders into one deterministic queue and observe price priority, time priority, resting prices, and partial fills.
- NEXTWhat Does a Market Order Consume?
Think about these questions first.
Choose an answer before opening the explanation. You can add anything unexpected to your review list.
Q01I submitted first. Why can someone else fill first?+
Time priority applies only within one price level. A better-priced order jumps ahead, and a large order may fill only partially while the remainder keeps its place.
An order book is not just a sorted price table. After every state change, a matching engine must still answer who fills first, how much fills, at which price, and whether the remaining assets wait or are released.
Read the best bid and ask
The virtual pair is LAB / credits. Asks sort from low to high; bids sort from high to low:
- Best ask: the lowest current sell price.
- Best bid: the highest current buy price.
- Spread: ask minus bid, the space an immediate trade must cross.
As long as best bid is below best ask, the book is not crossed. A new limit order can wait or bring its price into the book and trigger matching.
LAB / credits
Limit orders enter the queue; only a crossed book turns them into fills.
Read the book first: best ask is the lowest sell, best bid is the highest buy. Matching starts only when buy price reaches sell price.
- 01
Book created: best ask 102 分(两个价位相同's 订单),best bid 98 .price and time priority must be preserved。
Use the slider to return to an earlier step. Continuing from there replaces the later history with a new sequence.
Price priority comes before time priority
Two asks rest at 102: Maker A entered 2 LAB first, Maker B entered 3 later. When you submit BUY 6 @104, the engine fills A's 2 before B's 3. It does not randomize equal-price orders.
That is price-time priority: choose the most favorable price first, then the earliest sequence at that price. Stable sorting and event sequences matter because identical inputs should not produce different balances or settlements.
A large order may fill only partly
The BUY 6 @104 can access only 5 LAB of asks priced at or below 104. The engine records two fills and leaves 1 LAB resting at 104. This separates intention from fact: requested 6, filled 5, remaining 1. Cancelling releases only the frozen credits for the remaining unit.
Expand: how this step works
while bestBid.price >= bestAsk.price:
quantity = min(bestBid.remaining, bestAsk.remaining)
maker = earlier(bestBid, bestAsk)
price = maker.price
settle(buyer, seller, price, quantity)
bestBid.remaining -= quantity
bestAsk.remaining -= quantity
append(trade, price, quantity, maker)
Production engines add self-trade prevention, tick sizes, batch matching, limits, and circuit breakers. The lab keeps the minimum rule set but exposes each consequence in state, events, and invariants.
Why does BUY 6 @104 fill 5 LAB while 1 LAB remains at 104?
Five questions for a matching engine
Ask what the best bid, ask, and spread are; why equal-price orders have an order; why the resting order sets price; how orders, balances, and frozen assets change after a partial fill; and why cancellation cannot erase history. This is the shared skeleton of exchange books, onchain order books, and prediction markets.