Contents Lesson 16 of 19

9 min read · professional

In-sample, out-of-sample, and the honest way to tune

By the end of this lesson you will have a split date fixed in the code before any tuning, in-sample results by default and out-of-sample only on request, every out-of-sample look counted in the run log, and your prediction written into PLAN.md before the one run that answers it.

Unit 2 warned that tuning parameters until the curve looks good is fitting the answer to the sample. This lesson is the discipline that makes tuning legitimate.

Split before you tune, not after

Divide the history by date — the first 70% is in-sample, the rest is out-of-sample — and then do not look at the second part.

Tune in-sample as much as you like. Try fifty parameter pairs; that is what the in-sample period is for. Then run the winner once on the out-of-sample period, and whatever it produces is your result.

The discipline is entirely in the word once. Look at the out-of-sample result, go back, adjust, and look again, and you have converted it into a second in-sample period — just a smaller and noisier one. Nothing in the code stops you; only the log from unit 2 records that you did.

What you expect to see

Out-of-sample performance is worse. Always, more or less. A rule that returns 18% in-sample and 6% out is normal and possibly useful. A rule that returns 18% and 17% is either a rare genuine effect or a leak you have not found yet — and after unit 3, the prior should be leak.

A rule that returns 18% in-sample and −4% out has told you something valuable: the 18% was noise, and you learned that for the price of one run.

Walking forward

One split uses one out-of-sample period, which is one sample. Walk-forward gives you several: tune on years 1–3, test on year 4; tune on 2–4, test on 5; and so on. String the test periods together and that concatenated curve is a much more honest picture, because every point in it was produced by parameters chosen without seeing it.

It is also the first thing in this course that needs real history. Which is the next lesson.

On a free plan's year of daily bars there is room for one split and no walk. Do the single 70/30 split below, on the year you have or on the mock year, and read walk-forward as the picture a longer history buys rather than something you can run yet.

Two cheap checks that catch most overfitting

The neighbour test, from unit 2's closing exercise: vary each parameter slightly and plot the results. You want a plateau, not a spike. A rule that works at 20/50 and fails at 21/50 has found a coincidence, and no amount of out-of-sample testing rescues that.

Count your attempts. If you tried forty parameter sets, the best of forty is expected to look good on noise alone. Report the number of attempts next to the result. It costs nothing, it is in your run log already, and it is the single most informative line in an honest report.

The finance behind it

How much evidence a run of good results actually is: How many years before a track record means anything?

Try it now

Split your data 70/30 before touching anything, tune only on the first part, and write your prediction for the out-of-sample return down before you run it. Comparing your prediction to the outcome calibrates your judgement, which is worth more than the strategy.

The prompt

Mock mode on, plan mode on:

Plan first. Add SPLIT_DATE to src/lib/backtest/split.ts: the first
date 70% of the way through my history in .cache/eod/, or through the
mock year if I have no cache, worked out once now and written into
the code as a string, not recomputed when the history grows. Tell me
the date. The backtest page runs on the bars before it
by default. With oos=1 in the address it also runs the same parameters
from that date on, letting the averages warm up on the bars just
before it, and shows the two results side by side. Every live
out-of-sample run goes into the run log marked oos, even a repeat;
mock runs stay out of the log, as before. The page
prints how many parameter sets the log holds and how many times I
looked out of sample. No request. Tests first in tests/split.test.ts.
Run npm test, then show me the diff.

A good answer names the date before writing it, adds split.ts and the test, changes the page and runlog.ts, and reads the date off the cache without a request.

Check it yourself

  1. SPLIT_DATE is a literal in the code and the agent told you what it is. Copy it into PLAN.md.
  2. Without oos=1 the page shows in-sample results only. With a history cached from a live load on your own free key, switch mock off — the cache answers, so no call — and tune as much as you like; each new set adds a line to the log. On mock nothing is logged by design and the page reads "The log holds 0 parameter sets; I have looked out of sample 0 times": you can still split, predict and compare, but the counters only count live runs.
  3. Before you look out of sample, your prediction for its return is in PLAN.md.
  4. Then add ?oos=1 once. With a cached live history the log holds exactly one out-of-sample line and the page says one look; on mock the counters stay at 0, as above.
  5. Prediction beside outcome: worse out of sample is normal; nearly identical is a leak until proven otherwise.

Compare with ours

Our split
// src/lib/backtest/split.ts
// 70% through the year of mock bars; yours is 70% through your own history.
export const SPLIT_DATE = "2026-07-16";

export function outOfSample(bars: Bar[], params: Params) {
  const at = bars.findIndex((b) => b.date >= SPLIT_DATE);
  const from = Math.max(0, at - (params.slow - 1));  // warm-up reads the past: fine
  return rebase(runStrategy(bars.slice(from), params), at - from);
}

Your date may differ from ours by a day depending on how you round "70% of the way"; any date near 70% is fine if it was written before you tuned. The parameters were chosen in-sample; the averages on the first out-of-sample bars read the last in-sample prices, which is the past and fair. The full state at the end of this unit is on the starter's branch ref/backtest-strategies/u4.

Checkpoint

npm test && npm run typecheck
git status   # only the files this step touched, no .env*
git add -A
git commit -m "A split date fixed before tuning, and every look out of sample counted"

Then answer this lesson's questions.