Contents Lesson 10 of 19

10 min read · professional

Slippage, or the price you did not get

By the end of this lesson you will have fills at the next open as a stated parameter, slippage that always goes against you, a test that every printed fill lies inside its bar's range, and the median overnight gap on your traded bars printed beside your cost assumption.

Costs are what you are charged. Slippage is the difference between the price your backtest assumed and the price you would actually have received, and it is the part beginners model as zero without noticing they modelled it at all.

Execution is an assumption, and you already made one

Your backtester fills at some price. Whatever that is, it is a claim about how the world works:

  • Fill at the close of the signal bar — what unit 2 did, and impossible in practice: you did not know the close until it happened, so you could not have traded at it.
  • Fill at the next open — plausible, and the honest default. It is the first price at which a decision made after yesterday's close can act.
  • Fill at the next close — a different assumption, not a safer one. It is what a market-on-close order gets, and the close is about as often better than the open as worse, so it buys you no margin.
  • Fill at the best price in the next bar — this is not conservative or aggressive, it is fantasy. If you see Math.min(low, ...) in fill logic, that is a bug wearing an optimisation's clothes.

Pick one, write it in the report, and make it a parameter so it can be changed and re-run.

The overnight gap is not slippage — and it is bigger

A signal computed at Monday's close, filled at Tuesday's open, absorbs the whole overnight move. That is not a modelling error; it is what actually happens. But it means the price you get is systematically different from the price you saw, and on gappy instruments that difference dwarfs your ten basis points.

Measure it on your own data rather than guessing: for the bars where your strategy traded, compute the median absolute gap between the previous close and the next open, in basis points. If that number is larger than costBps, your cost model is dominated by the thing it does not include.

A slippage model you can defend

Keep it simple and stated:

const fill = nextOpen * (1 + direction * params.slippageBps / 10_000);

Always against you — worse when buying, worse when selling. A symmetric random slippage averages out over many trades and therefore models nothing; the point of the assumption is to be conservative, not realistic.

The test for this one

Same craft discipline as unit 2: turn the check into a test.

Assert that every fill's printed price, before slippage, lies within that bar's low-to-high range. A fill outside the day's range is impossible, so this catches off-by-one bars, adjusted prices mixed with raw ones, and fills taken from the wrong day — whenever the wrong price happens to land outside the range. A neighbouring day with an overlapping range slips through, so treat it as a cheap net rather than a proof.

Six lines. Run it on every backtest.

Try it now

Measure the median overnight gap on the bars where your strategy traded, and compare it to your costBps. Then re-run with fills at the next open instead of the signal close. Expect a difference, not necessarily a drop: over one year the next open can land on either side of the close, and on the mock year the next-open run came out marginally ahead. Either way, the gap between the two runs is the value of a fact you did not have yesterday. Changing the default fill and slippage moves every result you wrote in PLAN.md since the benchmark lesson, so write the new numbers beside the old ones, labelled, instead of overwriting them.

The prompt

Mock mode on, plan mode on:

Plan first. Add two parameters to Params, read from the address like
the others: fill, "next-open" or "signal-close", defaulting to
next-open, and slippageBps, defaulting to 5 and always against me: a
buy fills higher, a sale lower. Fills use raw prices; a next-open fill
trades at the open of the bar after the signal. Returns still come
from adjusted closes, with that open scaled by its bar's adjusted to
raw ratio. Record each fill's bar and its printed price before
slippage, and write tests/fills.test.ts first: every printed fill
lies inside its bar's raw low-to-high. Print the median absolute
overnight gap, previous close to next open in basis points, over the
bars where the strategy traded, beside costBps. Apply the same fill and
slippage to buy and hold, the neighbour table and the cost row, so every
comparison stays like for like. No request. Run npm
test, then show me the diff.

A good answer changes params.ts, strategy.ts and the page, adds the test, and makes no request.

Check it yourself

  1. In mock mode, ?fill=signal-close and ?fill=next-open give two results. The difference is the value of a fact you did not have yesterday; write it in PLAN.md.
  2. npm test is green, and the range test reads the printed price, not the slipped one.
  3. The median gap sits beside costBps. If it is the larger of the two, your cost model is dominated by the thing it does not include.
  4. Nothing in the fill code reads a bar's low or high: grep -rn "\.low\|\.high" src/lib/backtest prints at most the loader's check that each bar has its seven fields, and no line in the code that picks a fill price.

Break it on purpose

Point the next-open fill at the wrong day, the open two bars ahead, and run npm test. One fill outside its bar is enough to turn it red, and most likely one is. Count how many of the wrong-day prices still landed inside their bar: that is the size of the hole in the net, and why the lesson calls it a net rather than a proof. Put it back: green.

Compare with ours

Our fill and our range test
// src/lib/backtest/strategy.ts — i is the bar whose signal changed
const at = params.fill === "next-open" ? i + 1 : i;
const printed = params.fill === "next-open" ? bars[at].open : bars[at].close;
const price = printed * (1 + side * params.slippageBps / 10_000); // side: +1 buy, -1 sell
fills.push({ bar: at, printed, price });
// tests/fills.test.ts
for (const f of run.fills) {
  expect(f.printed).toBeGreaterThanOrEqual(bars[f.bar].low);
  expect(f.printed).toBeLessThanOrEqual(bars[f.bar].high);
}

The test reads the printed price on purpose. On a day that opened at its high, a buy 5 bps worse lands just above the range, and that is the assumption working, not a bug. The full state at the end of this unit is on the starter's branch ref/backtest-strategies/u3.

Checkpoint

npm test && npm run typecheck
git status   # only the files this step touched, no .env*
git add -A
git commit -m "Fills at the next open, slippage against me, every printed fill inside its bar"

Then answer this lesson's questions.