Compare against buy-and-hold, honestly
By the end of this lesson you will have both curves starting on the same bar with the same costs, four numbers under each — total return, time in market, trades and maximum drawdown — and a table of your chosen parameters beside four neighbours.
You have a strategy curve and the control from unit 1. Putting them on one chart is easy; comparing them fairly is not.
Same window, same costs, same data
Three ways a comparison quietly cheats, all of them accidental:
Different windows. The strategy starts at the first bar where its slow average exists — bar 50, if the slow window is 50. The control started at bar 1. Those are different periods, and if bars 1 to 50 fell, you just handed the strategy a free head start. Start both at the same bar.
Costs on one side. The strategy trades and pays; buy and hold trades once. If costs are off for the control they should be off for both, and they should be off for neither.
Different columns. Both must use adjusted close. Mixing them makes the comparison meaningless in a way that produces a plausible-looking chart.
Time in the market is not the same as risk
A strategy that is flat half the time carries about half the market exposure, so a lower return is expected rather than a failure — and a higher return with half the exposure is a genuinely interesting result, not just a bigger number.
So print time in market as a percentage beside the return. Without it, "beat the benchmark by 3%" is uninterpretable: it might be skill, or it might be leverage in disguise.
The comparison that actually means something
Not "did it beat buy and hold" — over a single window, with one rule, that question is nearly noise. The useful questions:
- Did it beat the control after costs, in the window where both were live?
- What did it give up to do it — how much time out of the market, how many trades, how deep a drawdown?
- Is the gap larger than the gap you would get by changing one parameter slightly? If a 20/50 crossover wins and 21/50 loses, you found noise.
That last one is a test you can run in a minute, and it is the cheapest reality check in this course.
Two curves, one chart, four numbers
Plot both lines from the same start. Under them, four numbers per line: total return, time in market, number of trades, max drawdown. Nothing else yet — unit 4 adds the rest, and a wall of statistics this early just gives you more places to find something flattering.
Try it now
Run your strategy at its chosen parameters and at four neighbours — fast ±2, slow ±5. Put the five results side by side. If the chosen one is a spike among losers rather than the middle of a plateau, you have learned something important before unit 3 even starts.
The prompt
Mock mode on, plan mode on:
Plan first. On the backtest page, start buy and hold on the bar where
the slow average first exists, which is where the strategy starts,
rebase both curves to 1 there, charge costBps on both, and use
adjusted closes for both. Under each curve print four numbers: total
return, time in market as a share of bars, number of trades, and
maximum drawdown; add timeInMarket to src/lib/backtest/metrics.ts,
the share of bars on which a position is held, so buy and hold,
holding on every bar from its first, shows 100%. Add
a neighbour table: the chosen pair and four neighbours (fast 2 either
side, slow 5 either side), on the same cached bars, all measured from
the latest start among the five, with the same four numbers (print that
start date above the table, and say that the chosen pair's row can differ
from the numbers under its own curve because their start dates differ). The
neighbours are shown, not logged: you did not choose them. No request.
Tests first in tests/benchmark.test.ts. Run npm test, then show me the
diff.
A good answer changes the page and metrics.ts, adds the test, and makes no request: five runs are arithmetic on one cached history.
Check it yourself
- Both curves start on the same date, printed under the chart: for a slow window of 50, the fiftieth bar of your history.
- Buy and hold shows 100% in the market and one trade; the strategy shows its own share, below 100%, and its own trade count.
- The neighbour table has five rows from one start date, and the chosen pair's row may differ a little from the numbers under its own curve: the page says why. With a history cached from a live load, the run log holds no line for any of the four neighbours (on mock nothing is logged at all).
- Read the table as the exercise above says — a plateau, or a spike among losers — and write which it is in
PLAN.mdbeside your result.
Compare with ours
Our shared start and time in market
// src/app/backtest/page.tsx
const from = params.slow - 1; // the slow average's first bar
const control = buyAndHold(bars.slice(from), params.costBps);
const strategy = rebase(runStrategy(bars, params), from); // both at 1.0 on that bar
// src/lib/backtest/metrics.ts
export const timeInMarket = (position: (0 | 1)[]) =>
position.reduce((held: number, p) => held + p, 0) / position.length;
The full state at the end of this unit is on the starter's branch ref/backtest-strategies/u2.

Checkpoint
npm test && npm run typecheck
git status # only the files this step touched, no .env*
git add -A
git commit -m "Same start and same costs for both curves, and the neighbours beside the chosen pair"
Then answer this lesson's questions.