Contents Lesson 9 of 19

10 min read · professional

Transaction costs, and why the default is on

By the end of this lesson you will have a test that pins costs to every position change, compounding, two new numbers under the strategy — what the costs took as a share of starting equity and of the gross return — and one row of net returns at 0, 5, 10 and 25 basis points.

Unit 1 put a cost line on your first chart while it was still flat. This lesson gives it a number and shows what it does to a strategy that trades.

What the number stands for

costBps: 10 is one tenth of one percent, charged per trade — so a round trip costs twenty. It is not a commission; it is a bundle of everything that makes your realised price worse than the price on the chart:

  • the broker's fee, whatever it is on your account;
  • half the bid-ask spread, because you buy at the ask and the chart plots the close;
  • exchange and regulatory fees;
  • and, on anything but the smallest order, the fact that your own buying moves the price.

Ten basis points is a defensible retail default for liquid US large caps and it is what this course uses. It is a choice, not a fact. On a thin small cap it is optimistic by a factor of several, and on FX majors it is pessimistic. The honest move is to state it in the report rather than to find the perfect number.

The arithmetic that kills most strategies

Cost scales with turnover, and turnover is the thing beginners never count.

A rule trading 50 round trips a year at 10 bps each way pays 50 × 20 = 1000 bps of notional turnover cost, before it has made anything. A rule doing a round trip every day pays roughly 250 of them — 5000 bps.

Those are notional figures, added up. Applied properly they compound against a shrinking balance, so the actual equity drag is slightly less: 1 − 0.999¹⁰⁰ = 9.5% and 1 − 0.999⁵⁰⁰ = 39.4%. The difference is not the point — the point is that both numbers are larger than any equity edge you are likely to find.

This is why the cost line went on the chart before the strategy existed. The gap between the gross and net curves is not a detail at the end; on most rules it is the whole result.

Where costs enter the code

At the position change, not at the end:

if (position.units !== previous.units) {
  equity *= 1 - params.costBps / 10_000;
}

Two mistakes to avoid. Do not subtract a total at the end — costs compound against a shrinking balance and the order matters. And do not charge for holding; a position held for a year costs the same to open as one held for a day, which is precisely why holding longer is how most rules survive costs at all.

Print the number that makes it real

Alongside net return, print total cost as a percentage of starting equity and cost as a fraction of gross return.

That second one is the sentence that ends arguments: "the strategy made 14% gross and paid 11% of it in costs." A person looking at a net number alone will keep believing the edge is real and the costs are a nuisance. The fraction tells them which is which.

The finance behind it

What a trade costs in practice, itemised, before you decide what to put in costBps: What does a trade really cost?

Try it now

Run your strategy at 0, 5, 10 and 25 bps and put the four net returns in a row. If the result flips sign somewhere in that range, you do not have a strategy — you have a cost assumption, and the honest report says so.

The prompt

Mock mode on, plan mode on:

Plan first. Read how runStrategy in src/lib/backtest/strategy.ts
charges costs and tell me, before changing anything, whether it
charges at every position change, compounding, and never for holding
or as a total taken off at the end. Then write tests/costs.test.ts
first: on flat prices, with the positions given directly (the 20/50 rule
never signals on flat prices), four position changes at 10 bps leave
0.999 to
the fourth power; a position held a hundred flat bars stays at 0.999
from its entry to its exit; the forced close on the last bar pays
like any exit. Fix the code only where a test fails. Under the
strategy, print what the costs took as a share of starting equity and
as a fraction of the gross return (n/a when the gross return is not
positive; keep the word gross on the page for it), and add a row of net
returns at 0, 5, 10 and 25 bps on the
same cached bars, shown and not logged. No request. If runStrategy
cannot take positions directly, split out the part that does and tell
me. Run npm test, then
show me the diff.

A good answer reads before it writes and names the line where the charge sits, adds the test, changes the page — and strategy.ts only if a test went red.

Check it yourself

  1. The agent's reading named the line that charges the cost, and it sits at a position change.
  2. npm test is green, the four-change case and the hundred-bar hold among the passes.
  3. In mock mode the two new numbers sit under the strategy. Write your defaults' sentence in PLAN.md: made this much gross, and paid this share of it in costs.
  4. The sweep row reads left to right as cost rises. If its sign flips anywhere, the report will say you have a cost assumption, not a strategy.

Break it on purpose

Move the charge to the end — take twice the trade count times costBps off the final return — and run npm test. The four-change test goes red, missing the compounding by a hair, and so does the hundred-bar hold, which now shows no cost at all until the end. Put it back: green.

Compare with ours

Our cost charge and the number that settles the argument
// src/lib/backtest/strategy.ts
const fee = 1 - params.costBps / 10_000;
for (let i = 1; i < bars.length; i++) {
  gross *= 1 + position[i] * r[i];
  net *= 1 + position[i] * r[i];
  if (position[i] !== position[i - 1]) net *= fee;   // at the change, compounding
}
if (position.at(-1) === 1) net *= fee;               // the forced close pays like any exit
const paid = grossEnd - netEnd;                        // in units of starting equity
const shareOfGross = grossReturn > 0 ? paid / grossReturn : null;

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 "Costs at every position change, compounding, and the share of gross they took"

Then answer this lesson's questions.