Parameters, defaults, and the number you will regret
By the end of this lesson you will have the rule's numbers in one Params object, the backtest page reading them from its address, a run log that gains one line for each new parameter set, and the defaults you chose from theory written into PLAN.md before any tuning.
Your rule has a 20 and a 50 in it. Both are decisions, both are currently invisible, and both have a price.
Hard-coded numbers are invisible assumptions
function signal(bars) { return sma(bars, 20) > sma(bars, 50); } // two hidden decisions
Lift them out:
type Params = { fast: number; slow: number; costBps: number };
const DEFAULTS: Params = { fast: 20, slow: 50, costBps: 10 };
Three benefits and one trap.
The benefits: the assumptions are now listed in one place where a reviewer can see them; the report you export in unit 4 can print them beside the result, which is what makes it reproducible; and you can vary one without editing the strategy.
The trap, which is the point of this lesson
The moment parameters are easy to change, you will change them until the curve looks good. That is not research, it is fitting the answer to the sample, and it is the most common way an honest person produces a dishonest backtest.
It does not feel like cheating. It feels like tuning. You try 20/50, then 10/30, then 15/40, keep the best, and report it — without reporting the other eleven you tried. The number you publish is then the maximum of twelve draws, not a typical result, and the gap between those two is the entire subject of unit 4.
Two rules that cost nothing and help enormously
Log every run. Append parameters plus result to a file, automatically, every time. Not to be tidy — so that when you report a result you can count how many attempts it took to find it. Twelve attempts is fine and normal; twelve attempts reported as one is the problem.
Set defaults before you look. Write down the parameters you would have chosen on theory alone, run those first, and keep that number. It is your honest baseline, and everything found afterwards gets compared to it rather than replacing it.
Cost is a parameter too
Note costBps sits in the same struct as fast and slow. That is deliberate: it makes cost a stated assumption of the run rather than a setting buried somewhere, and it means the exported report shows what you assumed.
Ten basis points — 0.10% per trade — is a defensible retail default for liquid US equities and is what this course uses. It is a choice, not a fact, and unit 3 explains what it stands in for.
Try it now
Add the run log before you tune anything, and make it automatic — a manual log is a log you stop keeping on the day it matters. Then write, yourself, the defaults you would have chosen on theory alone in PLAN.md under a heading of their own, where you cannot quietly replace them (the 20/50 crossover and 10 basis points above are such a choice: the textbook pair and the retail default). The agent does not do this step. The rule gets positions in the next lesson, and its first result goes beside them there.
The prompt
Mock mode on, plan mode on:
Plan first. Lift the rule's numbers into src/lib/backtest/params.ts: a
Params type with fast, slow and costBps, DEFAULTS of 20, 50 and 10,
and parseParams(query), which checks fast, slow and cost from the
address - whole numbers, fast at least 2 and below slow, slow at most
200, cost from 0 to 100 - drops a bad value, runs the default in its
place, and names what it dropped. The backtest page uses them in
place of the cost it held until now (a constant, or a parameter if
it already reads one from the address) and prints them, and says plainly that fast and slow do not change the
curve yet (the next lesson draws the rule). Add logRun() in src/lib/backtest/runlog.ts:
each live run appends one JSON line to .cache/runs.jsonl with the
date, the symbol, the first and last bar, the parameters and the
result so far - the share of all bars in the series, warm-up
included, on which the signal is on - unless a line
for the same parameters on the same bars is already there. Mock runs
are not logged. No request. Tests first. Run npm test, then show me
the diff.
A good answer adds params.ts, runlog.ts and a test for each, changes the page, and says the page makes no new request: parameters only change arithmetic on bars already cached.
Check it yourself
localhost:3000/backtest?fast=10&slow=30prints 10 and 30 (the curve does not change yet, and the page says so);?fast=60&slow=50names the pair it dropped and runs the defaults.- This step needs a history cached from one live load (your own free key;
SPYanswers 403 on the shareddemokey). With mock off and the cache answering, so no call: open the defaults and reload three times..cache/runs.jsonlholds one line. Try 10 and 30: two lines. Without a cache, mock runs write nothing by design and.cache/runs.jsonldoes not exist: rely ontests/runlog.test.tsfor this step. - In mock mode, the log does not grow.
PLAN.mdholds your theory defaults under a heading of their own, written before any other set was tried.
Compare with ours
Our parameters and the line that makes a reload not an attempt
// src/lib/backtest/params.ts
export type Params = { fast: number; slow: number; costBps: number };
export const DEFAULTS: Params = { fast: 20, slow: 50, costBps: 10 };
// src/lib/backtest/runlog.ts
const key = (l: RunLine) => JSON.stringify([l.symbol, l.from, l.to, l.params]);
if (logged.some((l) => key(l) === key(entry))) return; // same set, same bars
await appendFile(LOG, JSON.stringify(entry) + "\n");
The log sits in .cache/, beside the history, and stays out of git with it; the count it holds goes into the report in unit 4. 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 "Parameters in one place, every new set logged, defaults written down first"
Then answer this lesson's questions.