Lesson 0111 min
PRICE · TIME · QUEUE

Who Should Fill First?

Put orders into one deterministic queue and observe price priority, time priority, resting prices, and partial fills.

ObjectivesAfter this lesson, you should be able to:
  • 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
COURSE POSITIONStep 1 of 4
  1. PREVIOUSCourse introduction
  2. CURRENTWho Should Fill First?

    Put orders into one deterministic queue and observe price priority, time priority, resting prices, and partial fills.

  3. NEXTWhat Does a Market Order Consume?
BEFORE YOU READ

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?
SHORT ANSWER

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.

Compared with your prediction:

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.

ENGINE RUNNINGLAB / credits
ORDERBOOK 001 · PRICE / TIME PRIORITY

LAB / credits

Limit orders enter the queue; only a crossed book turns them into fills.

Last tradecredits / LAB
Best bid98highest buy
Best ask102lowest sell
Spread4ticks
Traded0LAB
Live order bookPrice / remaining / source
Asks (SELL)Bids (BUY)
1022A
1023B
1055A
983B
965A
Empty
Best ask: 102Best bid: 98
Coach

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.

Engine events1 EVENTS
  1. 01

    Book created: best ask 102 分(两个价位相同's 订单),best bid 98 .price and time priority must be preserved。

ACTION HISTORYExperiment timeline
1 state snapshots

Use the slider to return to an earlier step. Continuing from there replaces the later history with a new sequence.

Initial state
View experiment records →

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.

KNOWLEDGE CHECK

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.

LESSON RECAPComplete the exercise and knowledge check first