Sub topic 5
How a Buyer and a Seller Are Matched
Prerequisites and the order book
This chapter assumes you know what a share represents, what bid and ask prices mean, and how to place an order in a brokerage app.
A trading venue matches compatible buy and sell orders. Its central record is the order book, a list of available orders that have not executed yet.
- A bid is a buy order. The highest bid is the most a waiting buyer offers.
- An ask, or offer, is a sell order. The lowest ask is the least a waiting seller accepts.
- The difference between the lowest ask and highest bid is the bid-ask spread.
A limit order can wait in the book. A buy limit order states the highest price the buyer will pay. A sell limit order states the lowest price the seller will accept. A market order accepts the best available prices until its quantity is filled.
For example, suppose the book contains:
| Side | Price | Shares |
|---|---|---|
| Ask | $10.01 | 50 |
| Ask | $10.02 | 100 |
| Bid | $10.00 | 80 |
A buy limit order for 120 shares at $10.02 can match both asks. It first buys 50 shares at $10.01, then 70 shares at $10.02. Its $10.02 limit is a maximum, not a required execution price.
Price-time priority
Most continuous order books use price-time priority. Price priority means the most competitive orders go first:
- Higher-priced bids have priority over lower-priced bids.
- Lower-priced asks have priority over higher-priced asks.
Time priority applies when orders have the same price. The order submitted first is normally matched first. This rule gives traders an incentive to post competitive prices and join a queue rather than bypassing earlier orders at the same price.
An incoming order “crosses” the book when it can trade against an available order. A buy order crosses when its price is at least the lowest ask. A sell order crosses when its price is at most the highest bid.
The trade usually executes at the price of the resting order, meaning the order already waiting in the book. This explains why a buyer with a $10.02 limit can receive a fill at $10.01. The buyer agreed to pay up to $10.02, while the seller had already offered shares at $10.01.
import heapq
from itertools import count
seq = count()
bids = [] # entries: (-price, arrival_seq, qty); highest price first
asks = [] # entries: (price, arrival_seq, qty); lowest price first
def submit(side, price, qty):
"""Match a limit order using price-time priority."""
fills = []
order_seq = next(seq)
if side == "buy":
while qty and asks and asks[0][0] <= price:
ask_price, ask_seq, ask_qty = heapq.heappop(asks)
traded = min(qty, ask_qty)
fills.append((ask_price, traded))
qty -= traded
if ask_qty > traded:
heapq.heappush(asks, (ask_price, ask_seq, ask_qty - traded))
if qty:
heapq.heappush(bids, (-price, order_seq, qty))
else:
while qty and bids and -bids[0][0] >= price:
neg_price, bid_seq, bid_qty = heapq.heappop(bids)
traded = min(qty, bid_qty)
fills.append((-neg_price, traded))
qty -= traded
if bid_qty > traded:
heapq.heappush(bids, (neg_price, bid_seq, bid_qty - traded))
if qty:
heapq.heappush(asks, (price, order_seq, qty))
return fills
submit("sell", 10.02, 100)
submit("sell", 10.01, 50)
print(submit("buy", 10.02, 120))
# [(10.01, 50), (10.02, 70)]Partial fills and market orders
An order may receive a partial fill when the book does not contain enough matching shares at an acceptable price. The remaining quantity of a limit order can stay in the book. A market order may continue through several price levels, producing a different price for each portion. This effect is called price slippage.
Exercise
The lowest ask is $25.00 for 40 shares, and the next ask is $25.05 for 100 shares. What happens to a buy limit order for 70 shares at $25.03?
Show answer
It buys 40 shares at $25.00. The remaining 30 shares cannot match because $25.05 exceeds the $25.03 limit, so they rest in the book as a bid at $25.03.