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:
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?
- 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.
- Fill the oldest order600 − 300 = 300 left
A arrived first at 100, so A fills in full.
- Fill the next in line300 − 200 = 100 left
B fills in full.
- 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.
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?
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?
- Count what's ahead1,000 ahead
Every share bid at 100 before yours.
- A trade of 4001,000 − 400 = 600 ahead
It fills the front of the line, not you.
- A cancel ahead of you600 − 300 = 300 ahead
Orders that leave move you up too.
- 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?
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.
- Find the fraction600 ÷ 1,200 = 0.5
The trade covers half of what's waiting.
- Give every order that fraction150, 100, 250 and 100
Half of each order, whenever it arrived. Under price-time you got nothing.
- Bid 1,000 instead600 ÷ 2,000 = 0.3
Now 2,000 shares wait, so every order gets 30% of its size.
- 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.
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?
Where you'll meet it
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.
"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.
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?
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?
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?
What to remember
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.
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.
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.