Introduction to Quant Finance · Lesson 12
Markets and prices
What a price is, how a candle squeezes an hour of trades into four numbers, and why there are always two prices.
Know the basics? Read the lesson notesA market, one trade at a time
A market is a place where buyers and sellers trade the same thing. This one is simulated, made up by a computer program: it trades the shares of a made-up company. A share is one small piece of a company, and here it is the thing being traded.
A trade happens when one buyer and one seller agree on a price, and the seller's shares go to the buyer at that price. The market's price is simply the price of the last trade. Press Next trade to watch the first ten trades of the hour.
The first trades of the hour (simulated)
Each dot is one trade: when it happened (minutes:seconds into the hour), and at what price. The line holds the last price until the next trade.
The price is now $99.95: the price of the tenth trade. Look at the dots. So far every trade was at either $99.95 or $100.05, back and forth. A few screens on, you'll see why.
An hour in one candle
This market makes 120 trades in its hour, too many to read one by one. So a chart cuts time into stretches of equal length, each called a time window, and draws each window as one shape, a candle. Play the hour and watch its candle grow on the right.
One hour of trades, and the candle they make (simulated)
Left: every trade. Right: the candle so far. Dashed lines: the highest and lowest trade yet, which the candle's thin lines reach.
A candle keeps four prices from its window. The open is the first trade's price, $99.95. The high is the highest trade, $100.25, and the low is the lowest, $99.75. The close is the last trade's price, $99.90.
The box is the candle's body: it runs from the open to the close. The thin lines above and below it are its wicks, reaching up to the high and down to the low. This hour closed below where it opened, so its body is filled. A candle that closes above its open is drawn empty.
Pick the candle
Now read it the other way round. Another made-up share, another hour: it opened at $50.20, fell to $49.60, climbed to $50.80, and closed at $50.50. Which candle is that hour?
Four candles, one price scale
Empty body: closed above its open. Filled body: closed below it.
Shorter windows, more candles
A window can be any length: a minute, five minutes, an hour, a day. Here is the same hour, the same 120 trades, cut into windows of different lengths. Try 15 minutes, then 5.
The same hour, three ways (simulated)
Faint dots: the trades. Each candle covers one window and is built from the trades inside it.
Shorter windows keep more of the path: the 5-minute candles show the price climbing early in the hour and sliding after. Each candle still keeps only four prices, however many trades it holds.
The windows fit together. The hour's candle is the four 15-minute candles joined up: it opens at the first one's open, closes at the last one's close, and its high and low are the highest high and the lowest low of the four.
Why the price jumps back and forth
Back to the first ten minutes. The price keeps jumping up and down by 10 cents, again and again. The company isn't changing its worth every few seconds, so something else is going on.
Not everyone trades at once. Some people post a price and wait: waiting buyers name the most they'll pay, waiting sellers the least they'll accept. Show them.
The first ten minutes, with the people waiting (simulated)
Dots: trades. After you press the button, the upper line is the lowest price a waiting seller accepts and the lower line the highest price a waiting buyer pays.
The highest price a waiting buyer will pay is called the bid. The lowest price a waiting seller will accept is the ask. To buy right now, you pay the ask. To sell right now, you get the bid. In these ten minutes, 9 of the 21 trades were buyers paying the ask and 12 were sellers taking the bid.
So the last price hops between the bid and the ask, depending on who traded last. The gap between them is the spread: here $100.05 − $99.95 = $0.10.
A guess: buy, then sell at once
Right now the bid is $99.95 and the ask is $100.05. You buy 100 shares and, one second later, sell all 100. In that second nothing changes: not the company, not the bid, not the ask. What happens to your money?
Buy, then sell
Make the trades. Buy 100 shares, then sell them straight back. Then do it twice more.
Buying and selling 100 shares at once (simulated)
The waiting prices stay put the whole time: ask above, bid below, and the halfway point dashed.
Each buy paid the ask: 100 × $100.05 = $10,005. Each sell got the bid: 100 × $99.95 = $9,995. That's $10 lost every time, though nothing moved: the spread of $0.10 on each of 100 shares. It is what the waiting buyers and sellers earn for being there when you wanted to trade at once.
A buy and the sell that undoes it are together called a round trip. Halfway between the bid and the ask is the mid, $100.00. Buying, you paid $0.05 above it; selling, you got $0.05 below it.
Every round trip pays the spread.
A candle is four numbers
Its textbook name is an OHLC bar (open, high, low, close), or a candlestick when drawn with a body and wicks. Take the prices of the trades in one window, in time order:
H = max(p1, …, pn)
L = min(p1, …, pn)
C = pn
- n
- the number of trades in the window
- p1 … pn
- their prices, first to last: p1 is the first trade's price, pn the last one's
- max, min
- the highest and the lowest of the prices in the brackets
- O, H, L, C
- the window's open, high, low and close
Joining windows: O of the first, C of the last, H = highest of the highs, L = lowest of the lows. The body is empty if C is above O, filled if below, and a flat line if C = O.
n = 120 trades. The first was at $99.95, so O = $99.95. The highest was $100.25 and the lowest $99.75, so H = $100.25 and L = $99.75. The last was at $99.90, so C = $99.90. C is below O, so the body is filled. Joining its four 15-minute candles gives the same four numbers: the first one's open, the last one's close, the highest high and the lowest low.
- the window has at least one trade;
- the trades are taken in the order they happened;
- every trade is from this one market and this one window.
A window with no trades has no candle, and data sources deal with that gap in different ways. The four numbers don't say whether the high or the low came first.
Try it on new numbers. In a 5-minute window the trades were, in order: $20.10, $20.40, $19.80, $20.00, $20.30. Which candle is it?
The spread and the mid
The gap between the ask and the bid is called the bid-ask spread, and the point halfway between them the mid price. To buy and sell straight back, a round trip, you pay the ask and get the bid, so you lose the spread on every share. Buying costs s ÷ 2 above the mid; selling gets s ÷ 2 below it.
m = (a + b) ÷ 2
round-trip cost = s × q
- a
- the ask: the lowest price a waiting seller accepts, which you pay to buy now
- b
- the bid: the highest price a waiting buyer pays, which you get to sell now
- s
- the spread, per share
- m
- the mid, halfway between the bid and the ask
- q
- the number of shares bought and then sold
a = $100.05 and b = $99.95, so s = $100.05 − $99.95 = $0.10 and m = ($100.05 + $99.95) ÷ 2 = $100.00, so each buy paid s ÷ 2 = $0.05 above the mid and each sell got $0.05 below it. With q = 100 shares, a round trip costs $0.10 × 100 = $10, and three cost $30, as your figure showed.
- you buy and sell before the bid and the ask move;
- you trade few enough shares that the people waiting at the bid and the ask can take them all.
The spread changes: it widens in fast or quiet markets, and over many trades it adds up with the other costs of trading. Trading many shares at once uses up the best waiting prices, and you get worse prices.
Try it on new numbers. Another made-up share: bid $9.98, ask $10.02. You buy 500 shares and sell them straight back. How many dollars do you lose?
What this means for a backtest
A backtest runs a trading rule (a fixed recipe for when to buy and when to sell) over past prices to see what it would have done. The past prices it runs on are usually candles. Three things follow. Turn them over one at a time.
- A backtest usually sees candles, not trades.
For each window it knows four prices, not every trade inside. Your hour of 120 trades reaches an hourly backtest as $99.95, $100.25, $99.75 and $99.90.
- A candle doesn't say what came first.
Say a rule bought near $100.00 and sells as soon as the price reaches $100.20 or falls to $99.80. Both happened in this hour, but its candle can't say which came first. One exit is a gain and the other a loss, so which came first changes the result.
- You buy at the ask and sell at the bid.
Most charts show last-trade prices, and some data shows mids. A backtest that buys and sells at those prices leaves out the spread that every real round trip pays (lesson A16, Costs).
A friend's month of round trips
How many dollars did the spread cost that month?
Hint: one round trip costs the spread times the number of shares. Then count the round trips.
Four quarters into one hour
- first: O $30.00, H $30.40, L $29.90, C $30.20
- second: O $30.20, H $30.30, L $29.70, C $29.80
- third: O $29.80, H $30.10, L $29.60, C $30.00
- fourth: O $30.00, H $30.60, L $29.95, C $30.50
Which is the hour's candle?
A backtest that trades at the mid
Paying the ask to buy and getting the bid to sell, with everything else the same, what would a round trip average per share?
Lesson done
A price is the price of the last trade. A candle squeezes a window of trades into four numbers: open, high, low and close. Behind every trade there are two prices, the bid and the ask, so buying and selling straight back always pays the spread.
Key ideas
- Market
- a place where buyers and sellers trade the same thing
- Trade
- a buyer and a seller agree on a price, and the seller's shares go to the buyer at it
- Price
- the price of the last trade
- Time window
- a stretch of time of fixed length: a minute, an hour, a day
- Candle (OHLC bar)
- one window's open (first trade), high, low and close (last trade); body from open to close, wicks to the high and the low; empty if it closed higher, filled if lower
- Bid
- the highest price a waiting buyer pays: what you get to sell now
- Ask
- the lowest price a waiting seller accepts: what you pay to buy now
- Spread
- the ask minus the bid
- Mid
- halfway between the bid and the ask
- Round trip
- a buy and the sell that undoes it; at once, it costs the spread on every share
Formulas
O = p1; H = max(p1, …, pn); L = min(p1, …, pn); C = pn
Joining windows: O of the first, C of the last, H = highest of the highs, L = lowest of the lows.
s = a − b; m = (a + b) ÷ 2; round-trip cost = s × q
Buying costs s ÷ 2 above the mid; selling gets s ÷ 2 below it.