insideout Get Pro

02 · Price-time priority

Free

Markets, lesson 2

Price-time priority

Best price first, then first come, first served at each price.

When several buyers wait at the same price, the exchange needs a rule for who trades first. That rule decides how often a waiting order fills, so trading systems are built around it. You need the order book from "What an exchange is": bids, asks and the shares waiting at each price.

By the end, you can:

  1. Work out which waiting orders fill first under price-time priority.
  2. Count the shares ahead of you, and work out how much of a trade reaches you.
  3. Share a trade pro-rata, and see why it rewards bigger orders.

Part 1. Best price, then first in line

Work out which waiting orders fill first under price-time priority.

A sell order arrives, and several buy orders could take it. Four of them bid the same price. The exchange needs a rule that always gives the same answer.

Example 1.1

Who gets the shares

Bids wait at 100 from A for 300 shares, B for 200, C for 500 and you for 200, in that order of arrival. An older bid for 1,000 waits at 99. A sell for 600 arrives that will take any price down to 99. Who gets what?

  1. Pick the best price100 > 99

    The seller gets more at 100, so the bids at 100 go first, however long the bid at 99 has waited.

  2. Fill the oldest order600 − 300 = 300 left

    A arrived first at 100, so A fills in full.

  3. Fill the next in line300 − 200 = 100 left

    B fills in full.

  4. Give C the rest100 of C's 500

    C keeps 400 waiting, still at the front. Nothing reaches you.

AnswerA 300, B 200, C 100. You and the bid at 99 get nothing.

sell 600back of the linetrades from the front100A300B200C500you200991,000waiting longestFilled: A 300, B 200, C 100 of its 500Not reached: you, and the bid at 99
Figure 1The bids at 100 form a line, oldest at the front. The sell fills from the front: A and B in full, then 100 of C. The bid at 99 has waited longest, but its price is worse.

Price comes first because the seller wants the most money, and because it rewards anyone who improves their price. Time breaks ties: whoever committed to a price first is served first. A part-filled order keeps its place, as C did, with 400 still waiting at the front.

Price-time priority

Best price first. At the same price, the order that arrived first trades first. The line at each price is first in, first out, or FIFO.

rank by price, then by time of arrival

Size counts for nothing under this rule. A bid for 10,000 shares that arrived a second late waits behind a bid for 100. And the rule ranks only the orders that are waiting. The order that arrives and trades takes what it finds.

Your turn

A bid at 99 has waited an hour. A bid at 100 arrived a second ago. A seller sells 100 shares at the best price. Who gets them?

The seller gets 100 a share from one and 99 from the other, so the better price wins however long the other waited. If waiting could beat a better price, nobody would bother improving their price.

Part 2. Your place in the queue

Count the shares ahead of you, and work out how much of a trade reaches you.

Once you join the line at a price, one number decides when you trade: how many shares wait ahead of you. Follow it as the market moves.

Example 2.1

Waiting at 100

You bid for 200 shares at 100, behind 1,000 shares bid earlier. Then three things happen: sellers sell 400 at 100, an order for 300 ahead of you is cancelled, and sellers sell 450 at 100. How much of your order fills?

  1. Count what's ahead1,000 ahead

    Every share bid at 100 before yours.

  2. A trade of 4001,000 − 400 = 600 ahead

    It fills the front of the line, not you.

  3. A cancel ahead of you600 − 300 = 300 ahead

    Orders that leave move you up too.

  4. A trade of 450450 − 300 = 150 to you

    The first 300 clear the line. The last 150 reach you.

Answer150 of your 200 fill. Your last 50 now wait at the front.

The shares ahead of you only ever go down, as orders ahead of you trade or leave. Orders that join after you wait behind you and never count. Nothing you do at the same price moves you forward.

Queue position

Your queue position is how many shares wait ahead of you at your price. A trade reaches you only after it has filled all of them.

your fill = trade − shares ahead, between 0 and your size

A cancel only helps if it was ahead of you. One behind you changes nothing. And at a busy price the back of the line may wait so long that the price moves away first. That's one reason trading firms pay to reach the exchange sooner, which "Latency and speed races" comes back to.

Your turn

You're in the queue at 100 with 1,000 shares ahead of you. Sellers sell 1,050 shares at 100. How much of your 100-share order fills?

The first 1,000 go to the orders ahead of you and the last 50 reach you. You're part-filled, with 50 left, and now first in line.

Part 3. Splitting by size: pro-rata

Share a trade pro-rata, and see why it rewards bigger orders.

Time isn't the only way to break a tie. Some markets share each trade among every order at the best price, in proportion to how many shares each one bid for. Run the queue from part 1 through that rule.

Example 3.1

The same queue, shared by size

Bids wait at 100 from A for 300, B for 200, C for 500 and you for 200: 1,200 shares in all. A sell for 600 arrives. How much does each order get if the trade is shared by size? Then say you'd bid for 1,000 instead of 200.

  1. Find the fraction600 ÷ 1,200 = 0.5

    The trade covers half of what's waiting.

  2. Give every order that fraction150, 100, 250 and 100

    Half of each order, whenever it arrived. Under price-time you got nothing.

  3. Bid 1,000 instead600 ÷ 2,000 = 0.3

    Now 2,000 shares wait, so every order gets 30% of its size.

  4. Share it again90, 60, 150 and 300

    Your slice triples, and it comes out of everyone else's.

AnswerYou get 100, where price-time gave you nothing. Bid for 1,000 and you get 300.

Price-time: from the frontyou get 0A300B200C100you0Pro-rata: half of every orderyou get 100A150B100C250you100Pro-rata, you bid 1,000you get 300A90B60C150you300
Figure 2One queue, one sell for 600, shared three ways. Price-time fills from the front. Pro-rata fills every order by the same fraction, so a bigger bid takes a bigger share.

Pro-rata ignores the clock, so arriving early earns you nothing. A bigger order earns a bigger slice. So a trader who wants 200 shares may bid for 1,000, betting they'll rarely be filled in full. The risk is a big seller who fills them for far more than they wanted.

Pro-rata

Splitting a trade among the orders at the best price in proportion to their size.

your fill = trade × your size ÷ all shares at the price

Slices rarely come out whole. A sell for 100 into three bids of 100 gives each 33.3 shares. A pro-rata market has to round, to 33 each here, and needs a second rule for the share left over.

Your turn

Three bids wait at 50: 100 shares, then 300, then 600, in order of arrival. A sell for 500 arrives. How much does the 300-share bid get under pro-rata, and how much under price-time?

Pro-rata: 500 ÷ 1,000 = 0.5, so the 300 gets 150. Price-time: the 100 fills first, then all 300, and the last 100 go to the 600. 167 splits the 500 equally three ways, which neither rule does.

Where you'll meet it

On a real exchange

Nasdaq and the London Stock Exchange both rank by price, then shown orders ahead of hidden ones, then time. CME, a futures exchange, runs several rules on its Globex platform. Its SOFR futures, on a US interest rate, share trades pro-rata, after first filling the one order that improved the best price. Slices are rounded down, a slice under 2 contracts becomes 0, and leftovers go by time. GULL's prices are simulated. The rules are the real ones.

In interviews

"Implement an order book" almost always means price-time priority: a sorted map from price to a first-in, first-out queue at each price. A common follow-up asks what you'd change for pro-rata. The code below runs one price level both ways.

One price level, two ways to share, in Python
from collections import deque

# The queue at 100, oldest first. Each entry is [order, shares].
level = deque([["A", 300], ["B", 200], ["C", 500], ["you", 200]])

def sell_into(level, size):
    fills = []
    while size > 0 and level:
        order, want = level[0]
        take = min(size, want)
        fills.append((order, take))
        size -= take
        if take == want:
            level.popleft()       # filled: leaves the queue
        else:
            level[0][1] -= take   # part-filled: keeps its place
    return fills

print(sell_into(level, 600))
print(list(level))

def pro_rata(level, size):
    total = sum(want for _, want in level)
    return [(order, size * want // total) for order, want in level]

print(pro_rata([["A", 300], ["B", 200], ["C", 500], ["you", 200]], 600))
print(pro_rata([["A", 300], ["B", 200], ["C", 500], ["you", 1000]], 600))

# Output:
# [('A', 300), ('B', 200), ('C', 100)]
# [['C', 400], ['you', 200]]
# [('A', 150), ('B', 100), ('C', 250), ('you', 100)]
# [('A', 90), ('B', 60), ('C', 150), ('you', 300)]

// rounds each slice down. With these numbers nothing is left over. Try a sell for 100 into three bids of 100 and one share goes unassigned.

Check yourself

Three questions from across the lesson. Try each one before you look back.

1

In a pro-rata market, what's the obvious way to get more than your fair share?

Your slice grows with your size, so traders pad their orders. That's why pro-rata markets need extra rules. Under time priority the game is different: be early.

2

Bids at 100: 500 shares that arrived first, then your 300. A bid for 200 at 101 arrives after both. A sell for 600 arrives that will go down to 100. How much do you get?

The bid at 101 is the best price, so it fills first though it came last: 200. The other 400 go to the oldest order at 100, which wanted 500. Nothing reaches you.

3

You bid at 100 with 800 shares ahead of you and 400 behind. An order for 300 behind you is cancelled, then 900 shares trade at 100. Your order is for 200. How much fills?

Only shares ahead of you count, and the cancel was behind you, so 800 are still ahead. 900 − 800 = 100 reach you. Had the cancel been ahead of you, all 200 would have filled.

What to remember

  1. 1Best price, then first in line

    Best price first, then the oldest order at that price: a sell for 600 into A 300, B 200, C 500 and you at 100 fills A, B and 100 of C.

  2. 2Your place in the queue

    A trade reaches you only after every share ahead of you: with 300 ahead, a trade of 450 gives you 150.

  3. 3Splitting by size: pro-rata

    Pro-rata gives every order the same fraction of its size, 600 ÷ 1,200 = half each, so a bigger bid takes a bigger slice.

Markets overviewSpotted a mistake? Tell us.