Sub topic 2
How a Buyer and a Seller Are Matched
This chapter assumes you know what a bid and an ask are, can read a limit price in a brokerage app, and can read basic Python.
A trading venue keeps an order book, a list of resting buy orders (bids) and sell orders (asks). When a new order arrives, the venue tries to match it against the opposite side. Matching follows price-time priority: the best price fills first, and among equal prices the earliest order fills first.
Matching a limit order
A buy limit order trades only against asks at or below its limit price. Each trade executes at the resting order's price, so a buyer with a limit of 10.02 can fill at 10.01 when a seller rests there. This is price improvement.
import heapq
from itertools import count
seq = count()
bids = [] # (-price, arrival_seq, qty): highest price first
asks = [] # (price, arrival_seq, qty): lowest price first
def submit(side, price, qty):
"""Match a limit order using price-time priority. Return the fills."""
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)]The unfilled 30 shares rest on the ask side at 10.02.
Execution reports and confirmations
After each fill, the venue sends an execution report to your broker. It includes the price, quantity, time, and a trade identifier. The broker then confirms the trade to you. The trade date is the day the match occurs.
Clearing: netting and the central counterparty
A clearing house sits between the two sides of a trade. In US equities, the National Securities Clearing Corporation (NSCC) acts as the central counterparty (CCP). The CCP becomes the buyer to every seller and the seller to every buyer, so you face the CCP rather than the stranger who took the other side. The CCP also nets each member's trades in a stock, so a day of buying 100 shares and selling 40 requires delivery of only the net 60.
Settlement: when cash and shares change hands
Settlement is the final transfer of shares and cash. US equities settle on T+1, meaning one business day after the trade date. A Friday trade settles on Monday. Settlement uses delivery versus payment, so shares and cash move together or not at all.
Exercise
A trade executes on Wednesday, 2026-11-18, under T+1. On what date does it settle, and which party does the CCP face after novation?
Show answer
It settles on Thursday, 2026-11-19. After novation, the CCP faces both the buyer and the seller.