Quant from Scratch by QuantWright

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 notes

A 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.

Trades so far0
Time of the last trade–
Price now–

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.

Trades0 of 120
Open–
High–
Low–
Close–

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.

A has the right box, but it is filled: it would have opened at $50.50 and closed lower, at $50.20. This hour went up from $50.20 to $50.50, so its body is empty. C is the one.
B's body runs from the low to the high. The body runs from the open ($50.20) to the close ($50.50); the low and the high are where the wicks end. C is the one.
Body from the open, $50.20, up to the close, $50.50, drawn empty because it closed higher. Wicks down to the low, $49.60, and up to the high, $50.80.
D has no wick above its body, so its high would be its close, $50.50. This hour reached $50.80, so its top wick must reach $50.80. C is the one.

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.

Window
Candles1
Trades in each candle120

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.

Trades21
Buyers who paid the upper price–
Sellers who took the lower price–

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?

Keep your guess in mind. On the next screen you make the trades.

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.

Round trips0
Paid$0
Got back$0
Lost$0

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.

Theory

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:

O = p1
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.

Worked example: your hour

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.

Holds when
  • 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.
Breaks when

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?

O = p1 = $20.10 and C = p5 = $20.30. H and L are the highest and lowest of all five: $20.40 and $19.80.
$20.00 is the fourth trade. The close is the last one, p5 = $20.30, so A is the candle.
$19.80 is the lowest trade, not the first. The open is p1 = $20.10, so A is the candle.
Theory

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.

s = a − b
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
Worked example: your round trips

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.

Holds when
  • 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.
Breaks when

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?

s = $10.02 − $9.98 = $0.04, so the round trip costs $0.04 × 500 = $20.
s = $10.02 − $9.98 = $0.04, so the round trip costs $0.04 × 500 = $20. No price has to move: buying at the ask and selling at the bid is enough.

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.

  1. 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.

Practice 1 of 3

A friend's month of round trips

The situationA friend's rule buys and sells back within minutes. Last month it made 40 round trips of 200 shares each, in a made-up market whose spread was $0.03 all month.

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.

One round trip: $0.03 × 200 = $6. Forty of them: 40 × $6 = $240.
One round trip costs $0.03 × 200 = $6, and there were 40: 40 × $6 = $240. The spread is paid on every share of every round trip.
Practice 2 of 3

Four quarters into one hour

The situationYour data has 15-minute candles, and your rule needs hourly ones. One hour's four candles, in order:
  • 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?

That is the fourth quarter's own candle. The hour's low is the lowest of all four lows: $29.60, in the third quarter.
The open from the first quarter ($30.00), the close from the last ($30.50), the highest of the four highs ($30.60) and the lowest of the four lows ($29.60).
That is the first quarter's own candle. The hour closes with the last quarter's close, $30.50, and its high and low come from all four.
Practice 3 of 3

A backtest that trades at the mid

The situationAn ad for a trading course shows a backtest of a rule that buys and sells back within minutes. It assumes every buy and every sell happens at the mid, and it averages +$0.04 a share per round trip. In that market the spread is usually $0.06.

Paying the ask to buy and getting the bid to sell, with everything else the same, what would a round trip average per share?

Trading at once never happens at the mid: you pay the ask, $0.03 above it, and get the bid, $0.03 below it. That takes off the whole $0.06: $0.04 − $0.06 = −$0.02.
Half the spread comes off the buy and the other half off the sell, so the whole spread comes off a round trip: $0.04 − $0.06 = −$0.02.
Buying at the ask costs half the spread above the mid, and selling at the bid half below it: $0.06 a round trip. $0.04 − $0.06 = −$0.02 a share.
When a rule's average per round trip is smaller than the spread, paying the spread turns it negative.
✓

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.

Lesson notes

A market's price is the price of its last trade. A candle squeezes the trades in a stretch of time into four prices. Behind every trade are two prices, the bid and the ask, so buying and selling straight back loses the gap between them.

Trades and the price

A market is a place where buyers and sellers trade the same thing. This one is simulated and trades the shares of a made-up company. A trade happens when a buyer and a seller agree on a price, and the market's price is the price of the last trade. In the first ten trades the price took only two values, $99.95 and $100.05, back and forth.

An hour in one candle

The hour held 120 trades. A chart cuts time into time windows (stretches of equal length) and draws each as a candle, which keeps four prices: the open (the first trade's price), the high (the highest), the low (the lowest) and the close (the last trade's price). The lesson's hour gave $99.95, $100.25, $99.75 and $99.90.

The candle's box is its body, from open to close. Its thin lines, the wicks, reach the high and the low. The lesson draws a candle empty if it closes above its open, filled if below. The hour closed at $99.90, below its open of $99.95, so its body is filled.

Shorter windows, more candles

The same 120 trades make 4 candles in 15-minute windows and 12 in 5-minute ones. The windows join up: the hour takes the first quarter's open, the last one's close, the highest high and the lowest low.

Why there are two prices

Some people post a price and wait. The highest price a waiting buyer will pay is the bid; the lowest a waiting seller will accept is the ask. To buy right now you pay the ask, and to sell right now you get the bid. In the first ten minutes every trade happened at one of the two. Of those (21 trades), 9 were buyers paying the ask and 12 were sellers taking the bid, so the last price hops between them. The gap is the spread: $100.05 − $99.95 = $0.10.

Buy, then sell

Buy 100 shares at the ask and sell them straight back at the bid, nothing moving: 100 × $100.05 = $10,005 paid, 100 × $99.95 = $9,995 back, $10 lost. A buy and the sell that undoes it are a round trip. The mid, halfway between the bid and the ask, is $100.00: you paid $0.05 above it and got $0.05 below it. Every round trip pays the spread.

What this means for a backtest

A backtest runs a trading rule over past prices to see what it would have done. It usually sees candles, not trades. A candle doesn't say what came first: a rule that bought near $100.00 and sells at $100.20 or $99.80 has a problem: the hour ranged from $99.75 to $100.25, so both $100.20 (a gain) and $99.80 (a loss) were reached, and the candle can't say which came first. And a backtest that trades at last prices or mids leaves out the spread (lesson A16, Costs).

Formula: a candle is four numbers

O = p1
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.

Worked example: the lesson's hour

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.

Holds whenBreaks when

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.

Formula: the spread and the mid

s = a − b
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

Buying costs s ÷ 2 above the mid; selling gets s ÷ 2 below it.

Worked example: the lesson's round trips

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.

Holds whenBreaks when

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.

One more worked example

Made-up round numbers

A 5-minute window's trades, in order: $8.40, $8.55, $8.30, $8.35, $8.60, $8.50. Open: the first, $8.40. Close: the last, $8.50. High: the largest, $8.60. Low: the smallest, $8.30. The close is above the open, so the body is empty.

Now a bid of $24.90 and an ask of $25.10, and you buy 300 shares and sell them straight back. Spread: $25.10 − $24.90 = $0.20. Mid: ($25.10 + $24.90) ÷ 2 = $25.00. The buy pays 300 × $25.10 = $7,530, the sell gets 300 × $24.90 = $7,470, and the difference is $60. The formula agrees: $0.20 × 300 = $60.

Common mistakes

Check yourself

  1. A window's trades, in order: $12.00, $12.30, $11.90, $12.10. What is the candle, and is the body empty or filled?
    Answer

    Open $12.00 (first), high $12.30, low $11.90, close $12.10 (last). The close is above the open, so the body is empty.

  2. Bid $19.95, ask $20.05. You buy 400 shares and sell them straight back. How much do you lose?
    Answer

    Spread $20.05 − $19.95 = $0.10. $0.10 × 400 = $40.

  3. Two 30-minute candles: open $5.00, high $5.20, low $4.90, close $5.10; then open $5.10, high $5.30, low $5.00, close $4.95. What is the hour's candle?
    Answer

    Open $5.00 (first), close $4.95 (last), high $5.30 (the larger high), low $4.90 (the smaller low). The close is below the open, so the body is filled.

  4. A rule averages +$0.03 a share per round trip at the mid, and the spread is $0.05. What does it average when it pays the spread?
    Answer

    $0.03 − $0.05 = −$0.02 a share.

Try it yourself

Run the simulation. Every trade and number in this lesson comes from one Python script with a fixed random seed (below). Change the spread, the number of trades or how often the mid moves, and see what changes.

Build a candle by hand. Start at $100.00 and flip a coin 20 times: heads, the price goes up 5 cents; tails, down 5 cents. Write each price down. Your candle's open is the first price, its close the last, and its high and low the largest and smallest you wrote. Split the 20 into four groups of 5 and you have four shorter candles that join up into the same one.

The code behind every number (Python, seed 2026110911)
Loading…

Going further: why last prices zigzag

In the first ten trades the price hopped between $99.95 and $100.05 though the bid and the ask never moved. This is called bid-ask bounce. It makes last-trade prices look jumpier than the market underneath: in our hour, a trade-to-trade price change had a standard deviation of 7.63 cents, while the mid's change, over the same steps, had 2.47. It also makes a fall more often followed by a rise than by another fall: after a drop from the ask to the bid, the next trade can only stay at the bid or go back up to the ask, unless the mid moves.

Richard Roll turned that pattern into a way to estimate the spread from trades alone: the spread is about 2 × √(−cov), where cov is the covariance of each price change with the one before it (negative when falls tend to follow rises). On our 120 trades it gives $0.11, against the true $0.10. The same pattern can fool a backtest: a rule that buys after a fall and sells after a rise, run on last-trade prices, collects the bounce on paper and pays the spread in real trading.

For the preciseTrades: 120 at distinct whole seconds of the hour, sorted. Before each trade the mid moves −5, 0 or +5 cents with chances 0.15, 0.70, 0.15 (29 of the 120 steps moved it); each trade is a buy at the ask (mid + 5 cents) or a sell at the bid (mid − 5 cents), 50–50 (58 buys, 62 sells). Roll's estimate: s ≈ 2√(−cov(Δpt, Δpt−1)), with Δp the change from one trade's price to the next; here cov = −30.3 cents², so s ≈ 2 × √30.3 ≈ 11 cents. Roll's formula assumes the mid's own changes don't follow each other in any pattern and that buys and sells are equally likely and independent. The spread as a share of the price is s ÷ m = $0.10 ÷ $100.00 = 0.1% here, the form in which costs are often quoted.

Honest notes

Where this comes from