Draw the baseline first, with costs already visible
By the end of this lesson you will have a /backtest page that draws buy and hold for SPY with its net-of-cost line already on, three numbers under the chart — total return with its dates, maximum drawdown and one trade — and a screenshot that every later strategy is compared against.
Before any strategy: buy and hold, plotted. It is the control, and without it you have no way to know whether a result is skill or a rising market.
The control comes first for a reason
If you build the strategy first, you will compare it to nothing and conclude it works. If you build the control first, every later result arrives with a comparison already attached.
Buy and hold is a single line: compound the return series from lesson 3 and plot it. That is the whole implementation, and it takes ten minutes with an assistant.
Costs are on from this line, not added later
Here is a decision this course makes for you: your equity curve renders a cost line from the very first chart. The mechanics of commissions and slippage come in unit 3; the habit starts now.
Set a default and make it visible on the chart even when it is zero. Buy and hold has almost no costs — one purchase — so the two lines sit on top of each other and the pair looks pointless. That is exactly the point. When the strategy trades two hundred times, the gap between the lines is the first thing you see, and you will not have to remember to look for it.
Nothing in this course ever shows a cost-free equity curve as an achievement. A curve without costs is not a simpler version of the truth; it is a different and more flattering claim.
Three numbers under the chart
Not a dashboard. Three, because more invites cherry-picking:
- Total return over the window, and the window's dates. A percentage without dates is meaningless.
- Maximum drawdown — worst peak-to-trough. Lesson 13 does this properly; a rough version now is enough to stop you reading the curve as if it went straight up.
- Number of trades — one for buy and hold, and the number that makes costs matter later.
Ask your assistant for the control, then read it
This is the adult loop from Build Your First Tool applied here. Ask for the buy-and-hold curve and read the diff for four specific things:
- Does it compound by multiplying, or does it sum returns anywhere?
- Which close column does it use?
- Does the first bar produce a return? It cannot — there is nothing before it. An off-by-one here shifts your entire curve by a day, which is a look-ahead bug in miniature and unit 3's whole subject.
- What does it draw when the series is empty or one bar long?
Assistants are reliably good at plotting and reliably careless at the first bar. That is where to spend your attention.
Try it now
Plot buy and hold for one symbol over the longest window your plan gives you, with the cost line switched on at zero. Screenshot it. Every strategy you build for the rest of this course gets compared against this picture, and having it on screen makes cherry-picking harder. If the PLAN.md from the screener course says "Not building: charts", change that line in the same commit.
The prompt
Mock mode on, plan mode on:
Plan first. Add a page at src/app/backtest/page.tsx and a Backtest link
next to the existing page links (create a small shared nav if the pages
have none). The page loads SPY on the US exchange with
loadCandles and draws buy and hold: buyAndHold(bars, costBps) in
src/lib/backtest/baseline.ts compounds adjusted returns from 1 into a
gross curve, and a net one that pays costBps on the purchase and on the
sale at the end. Draw both as one server-rendered SVG in
src/components/EquityChart.tsx, no chart library, and draw the net line
even at 0 bps, which is where the page starts. Under the chart: total
return with the first and last dates, maximum drawdown against the
running peak in src/lib/backtest/metrics.ts, and the trade count (buy and
hold is one round trip, so 1). Show the net total return with the gross
one beside it. Fewer
than two bars shows a sentence, not a chart. Tell me what the page costs
live before you wire it. Tests first in tests/baseline.test.ts. Run npm
test and npm run check:leak, then show me the diff.
A good answer states the cost — one call on the first live load, none while the cache holds — and adds the page, the chart, baseline.ts, metrics.ts and the test, with one link added beside the others.
Check it yourself
- Read the diff for the four things above: returns multiplied and never summed,
adjusted_close, no return for the first bar — the gross curve starts at exactly 1 on the first date — and a sentence rather than a chart for a series too short to draw. - In mock mode
localhost:3000/backtestshows a curve from 2025-10-31 to 2026-10-30 (the 251 bars of the fixture; the page prints dates, not a count), two lines lying on top of each other, and one trade. - Set the page's cost to 10 for a moment: the net line drops just under the gross one, a purchase and a sale.
tests/baseline.test.tsalready says by how much. Set it back to 0. - Go live once, with your own free key (
SPYanswers 403 on the shareddemokey; on mock ordemoskip this step). Switch mock off, read the meter on the page, open it: one call. Reload: the meter does not move, because the cache answered, and the line under the chart says when the history was pulled. Take the screenshot now.
Compare with ours
Our control
// src/lib/backtest/baseline.ts
const gross = compound(dailyReturns(bars, "adjusted_close").map((x) => x.r));
const fee = 1 - costBps / 10_000;
// paid on the purchase, and again on the sale at the end
const net = gross.map((v, i) => v * fee * (i === gross.length - 1 && i > 0 ? fee : 1));
return { dates: bars.map((b) => b.date), gross, net, trades: 1 };
The page holds const COST_BPS = 0: the habit starts before the number does, and the next unit turns it into a parameter. The full state at the end of this unit is on the starter's branch ref/backtest-strategies/u1.

Checkpoint
npm test && npm run typecheck
git status # only the files this step touched, no .env*
git add -A
git commit -m "Draw the control first: buy and hold, with the cost line already on"
Keep the screenshot where you will see it. Then answer this lesson's questions.