Markets, lesson 1
What an exchange is
Buyers and sellers wait at their prices, kept in one order book.
Every stock exchange keeps a list of who wants to buy, who wants to sell, and at what price. This lesson shows you how to read that list and what trading against it costs. You need only arithmetic, plus a little Python for the code at the end.
By the end, you can:
Part 1. Bids, asks and the order book
Read an order book and tell when a buyer and a seller can trade.
Nobody agrees a price in advance on an exchange. Buyers say the most they'll pay, sellers say the least they'll take, and their orders wait in a list until someone on the other side agrees. Here is that list for GULL, a practice stock.
Example 1.1
Reading GULL's book
GULL's sellers wait with 300 shares at 101, 400 at 102, 500 at 103 and 600 at 104. Its buyers wait with 300 at 99, 300 at 98 and 600 at 97. What's the most any buyer will pay, and the least any seller will take? A new buyer arrives who'll pay up to 102. Does a trade happen?
- Find the highest buyer99
Of 99, 98 and 97, a seller would rather get 99.
- Find the lowest seller101
Of 101 to 104, a buyer would rather pay 101.
- Test the new buyer102 ≥ 101
The buyer will pay at least what the cheapest seller asks, so both are happy to trade.
Answer99 and 101. The new buyer at 102 trades.
A trade needs a buyer who'll pay at least what a seller asks. So in a book where nothing is trading, every buy price sits below every sell price, with a gap between them. An order that reaches across the gap trades at once.
Order book, bid and ask
Every waiting order, sorted by price, is the order book. Waiting buy orders are bids and waiting sell orders are asks. The highest bid is the best bid and the lowest ask is the best ask.
a trade can happen when bid ≥ ask
A new buyer at 100 trades with nobody, because no seller takes 100. That order waits in the book as the new best bid, and the gap shrinks to 1. Equal prices are enough, too: a buyer at 101 meets the seller at 101 and trades.
Your turn
The best bid is 99. A new seller arrives who'll take 98. Does a trade happen?
Part 2. The spread and the mid
Work out the spread and the mid, and what trading straight away costs.
Say you buy 100 GULL shares straight away, change your mind, and sell them straight back. Nothing in the book moves in between.
Example 2.1
A round trip
GULL's best bid is 99 and its best ask is 101. You buy 100 shares now and sell them straight back. What did the round trip cost, and how wide is the gap between the two best prices?
- Buy 100 now100 × 101 = 10,100
Buying now means paying the best ask.
- Sell them straight back100 × 99 = 9,900
Selling now means taking the best bid.
- Find the cost10,100 − 9,900 = 200
2 a share, though no price in the book changed.
- Measure the gap101 − 99 = 2
The round trip cost exactly this gap on every share. Halfway across it is (99 + 101) / 2 = 100.
Answer200, or 2 a share: the gap between the best bid and the best ask.
You crossed the gap twice. Measured from 100, the point halfway, you paid 1 too much going in and got 1 too little coming out: half the gap each way. That's the price of trading now. A narrow gap makes trading cheap, and a wide one makes it dear.
Spread and mid
The spread is the best ask minus the best bid. The mid is halfway between them. The mid is a fair middle for valuing what you hold, but trading straight away happens at the bid or the ask.
spread = 101 − 99 = 2; mid = (99 + 101) / 2 = 100
You don't have to cross. Post a bid at 100 and wait for a seller to come to you. If one comes, you saved 1 a share. If none does, you don't trade at all. Your bid also shrank the spread to 1 for everyone else.
Trade now and pay the spread, or wait for a better price and maybe not trade: that choice runs through everything that follows.
Your turn
A stock's best bid is 49.95 and its best ask is 50.05. You buy 1,000 shares and sell them straight back before anything changes. What did the round trip cost?
Part 3. Big orders walk the book
Price an order bigger than the best price's size, and measure its slippage.
Each price holds a limited number of shares. GULL has 300 at 101, then 400 at 102. Buy more than 300 at once and the rest has to come from further up the book, at worse prices.
Example 3.1
Buy 1,000 at once
GULL's asks are 300 at 101, 400 at 102, 500 at 103 and 600 at 104. You buy 1,000 shares straight away. What do they cost, and what's your average price?
- Take the best ask300 × 101 = 30,300
Only 300 wait at 101. 700 to go.
- Move up a price400 × 102 = 40,800
That makes 700 shares.
- Finish at 103300 × 103 = 30,900
300 of the 500 waiting there. The other 200 stay in the book.
- Add up and average102,000 ÷ 1,000 = 102.00
30,300 + 40,800 + 30,900 = 102,000 for the lot.
Answer102,000, an average of 102.00 a share: 1.00 above the best ask.
The book sells you the cheapest shares first, and each price runs out. So every share past the first 300 costs more than the best ask, and the bigger the order, the further up it reaches.
Depth and slippage
Depth is how many shares wait at and near the best prices. Slippage is how much worse your average price was than the best price when you started.
slippage = 102.00 − 101 = 1.00 a share
Now sell the 1,000 straight back. The bids walk down the same way: 300 × 99 + 300 × 98 + 400 × 97 = 97,900. The round trip cost 102,000 − 97,900 = 4,100, or 4.10 a share. For 100 shares it cost 2, the spread. Size more than doubled the cost of each share.
That's why a fund with a big order cuts it into small pieces and sends them over time. Each piece pays close to the spread instead of walking the book.
Your turn
The asks are 200 at 50, 300 at 51 and 500 at 52. You buy 600 shares straight away. What's your average price?
Where you'll meet it
Nasdaq and the London Stock Exchange run order books like GULL's, and both serve the best price first. Nasdaq's rulebook gives its own example: a bid at $10.00 is ranked ahead of a bid at $9.99. GULL's prices are simulated. The rules are the real ones.
"Write an order book" is a classic coding question for quant developer roles: add an order, cancel one, report the best bid and ask. Expect quick sums too: the spread, the mid, or what 1,000 shares cost against a book you're shown.
from collections import defaultdict
# (side, shares, price): GULL's orders, in the order they arrived
orders = [("buy", 200, 99), ("sell", 300, 101), ("buy", 300, 98),
("sell", 400, 102), ("buy", 100, 99), ("sell", 500, 103),
("buy", 600, 97), ("sell", 600, 104)]
bids = defaultdict(int) # price -> shares waiting
asks = defaultdict(int)
for side, shares, price in orders:
book = bids if side == "buy" else asks
book[price] += shares
for price in sorted(asks, reverse=True):
print(f" {price} {asks[price]}")
for price in sorted(bids, reverse=True):
print(f" {bids[price]:>3} {price}")
best_bid, best_ask = max(bids), min(asks)
print("spread", best_ask - best_bid, "mid", (best_bid + best_ask) / 2)
def cost_to_buy(asks, shares):
total = 0
for price in sorted(asks): # cheapest first: walk the book
take = min(shares, asks[price])
total += take * price
shares -= take
if shares == 0:
return total
cost = cost_to_buy(asks, 1000)
print("1,000 shares cost", cost, "average", cost / 1000)
# Output:
# 104 600
# 103 500
# 102 400
# 101 300
# 300 99
# 300 98
# 600 97
# spread 2 mid 100.0
# 1,000 shares cost 102000 average 102.0Two dictionaries from price to shares are enough to read a book. "Matching a trade" adds the line of orders at each price, and orders that trade the moment they arrive.
Check yourself
Three questions from across the lesson. Try each one before you look back.
1
The book is bid 99, ask 101. Someone posts a new buy order at 100. What's the spread now?
2
One exchange's book shows a best bid of 102 and a best ask of 101, and nothing is trading. What's wrong?
3
The asks are 500 at 10.02 and 500 at 10.03. The bids are 500 at 10.00 and 500 at 9.99. You buy 1,000 shares and sell them straight back. What did the round trip cost?
What to remember
1Bids, asks and the order book
A trade happens when a buyer pays at least what a seller asks, so in a book at rest the best bid (99) sits below the best ask (101).
2The spread and the mid
The spread is the best ask minus the best bid, 101 − 99 = 2, and buying then selling straight back costs it on every share.
3Big orders walk the book
A big order walks the book: 1,000 GULL shares averaged 102.00 against a best ask of 101, and the round trip cost 4.10 a share instead of 2.
Markets overviewSpotted a mistake? Tell us.