Quant from Scratch by QuantWright

Introduction to Quant Finance · Lesson 16

Costs

How much of a trade's average profit is left after paying to trade?

Know the basics? Read the lesson notes

Every trade comes with a bill

A backtest replays a trading rule on past data. Real trading adds something a backtest can leave out: what you pay just to make a trade. It has three parts; together these are the transaction costs. Tap each one.

The fee your broker, the firm that places your trades, charges for each trade.

At any moment, the price you can buy at is a little higher than the price you can sell at. That gap is the spread. Buy and sell straight away, and you have lost the gap.

The price you actually get is often a little worse than the price you saw when you decided, because the price moves before your order goes through.

All three are paid on every trade, whether it wins or loses. In this lesson they come to $6 a trade together. That is an assumed number, and "one trade" means getting in and getting out again.

One trade, before costs

Here is a simulated trade: made up by a computer, not taken from a market. It risks $100 to make $100, so a win adds $100 and a loss takes $100. It wins 52% of the time. That is its win rate, the share of trades that win.

On average, what does one trade make?

$52 is what the wins bring in per trade (0.52 × $100), but the losses take 0.48 × $100 = $48 back. The average is +$4.
That reads the gap between 52% and 50% as dollars. Work it through: 0.52 × $100 − 0.48 × $100 = +$4.
The wins bring in 0.52 × $100 = $52 per trade on average, and the losses take 0.48 × $100 = $48. That leaves +$4.
+$4 a trade is this rule's edge: the average profit a trade makes before costs.

A guess before the bill arrives

The edge is +$4 a trade. How much can one trade cost before that edge is gone, so the average trade makes nothing?

Keep that guess in mind. On the next screen you'll find the answer with a slider.

Add the cost

Drag the cost of one trade up from $0. It comes off every trade: a win keeps less than $100, and a loss costs more than $100. The lower chart shows where a $10,000 account ends up after 1,000 such trades on average: $10,000 plus 1,000 times the average trade.

One simulated trade: the edge, the cost, what is left

Dollars per trade. Assumed: risk $100 to make $100, 52% wins. The cost bar starts where the edge ends and reaches left by the cost.

A win keeps$100
A loss costs$100
Average trade+$4
Average account after 1,000 trades$14,000

At $4 a trade, the cost equals the edge: the average trade makes $0 and the average account ends where it started. Past $4, the average trade loses money.

At the $6 this lesson assumes, a win keeps $94, a loss costs $106, and the average trade is +$4 − $6 = −$2. Over 1,000 trades, the average account goes from $10,000 to $8,000 instead of $14,000.

For the preciseAverage trade after costs = p × W − (1 − p) × L − c, with win rate p = 0.52, win W = $100, loss L = $100 and cost c. The edge vanishes at c = p × W − (1 − p) × L = $4. In R, with 1R = the $100 at risk: edge +0.04R, cost 0.06R, after costs −0.02R. The average trade is also called the expectancy; a result before costs is called gross, and after costs net.

One trader, 1,000 trades

Now a simulated trader starts with $10,000 and makes 1,000 of these trades in a year. Each trade has a 52% chance to win, and each one costs $6. Press play and watch two lines: the account before costs (grey) and after costs (coloured).

One simulated trader: the same trades, before and after costs

Account in dollars. Start $10,000. Every trade: +$100 or −$100, then −$6.

Trades0
Wins0
Before costs$10,000
After costs$10,000
Costs paid$0

This trader won 514 of 1,000 trades. Before costs the account rose $2,800: 514 wins and 486 losses is 28 more wins than losses, at $100 each. After costs it fell $3,200. The last screen's −$2,000 was the average of many traders; this one fell $1,200 further, by bad luck (the theory step measures luck's size).

The gap is 1,000 × $6 = $6,000. It grows by exactly $6 on every trade, win or lose, so the gap between the two lines widens at a steady pace, whatever the trades do.

One trader could be luck. Try 10,000.

Here are 10,000 simulated traders with the same rule, each with their own 1,000 trades. A trader is ahead if the account finished above the $10,000 it started with. Before costs, 88.8% are ahead. Not all: luck still hands some traders a losing run even though the average trade is +$4. Guess what share is still ahead after the $6 cost, then add it.

10,000 simulated traders: where each account finished after 1,000 trades

Each bar counts the traders who finished at that amount. Coloured: ahead. Once the cost is added, the dotted outline shows where they finished before costs.

Cost of one trade$0
Costs paid by each trader$0
Ahead88.8%

Every trader kept the same wins and losses and paid the same $6,000, so the whole picture slid $6,000 to the left and kept its shape. A quarter are still ahead, but only by luck: the average trade now loses $2, but a lucky run of wins can still put some traders ahead.

A busy rule pays the bill more often

Same traders, same 52%, same $6 a trade. Now change how many trades they make in a year. Each trade is partly luck. With few trades, luck can beat a small average. With many, the sure part, the number of trades × the average trade, grows faster than luck, which grows only with √n, the square root of the number of trades (lesson A5, The bell curve; more on the theory step), so the average decides: +$4 a trade before costs, −$2 after. If the edge is bigger than the cost, more trades help you. If it's smaller, they hurt. Slide from 250 trades (1 a day, over 250 trading days a year) to 4,000 (16 a day), and watch who is ahead. The Before costs / After costs switch shows both.

More trades in a year, the same cost per trade

Lines: 200 of the simulated traders (coloured if ahead at the end). Dashed: the average of all 10,000 traders. Percentages count all 10,000 traders. Accounts can go below $0 here because the simulation never stops them; a real account would stop at $0.

Show
Costs for the year$1,500
Ahead before costs70.9%
Ahead after costs37.3%

After costs: 37.3% ahead at 1 trade a day, 25.2% at 4 a day, 9.7% at 16 a day. Before costs it goes the other way: 70.9%, 88.8%, 99.4%.

After costs at 4,000 trades, the dashed average ends at $1,928, not exactly $10,000 − 4,000 × $2 = $2,000: even an average of 10,000 traders keeps a little luck.

That is luck evening out as the trades add up. When the average trade after costs is below zero, every extra trade makes a loss more likely.

The finish line moves

With no cost, a trade that risks $100 to make $100 breaks even, making $0 on average, at a 50% win rate. With a $6 cost, what win rate does it need just to break even?

A win keeps $94 and a loss costs $106. You break even when the wins bring in exactly what the losses take. Check it on 100 trades at 53%: 53 wins keep 53 × $94 = $4,982, and 47 losses cost 47 × $106 = $4,982. They cancel.
At 50%, the wins keep $94 each and the losses cost $106 each, so the average trade is −$6.
That adds 6 points for $6. Each dollar of cost moves this line by half a point.
Break-even means the wins bring in what the losses take. With win rate p: p × $94 = (1 − p) × $106, so p = 106 ÷ (94 + 106) = 53%. Before costs, our 52% cleared the line by 2 points. The cost moved the line 3 points, so 52% is now one point short. An edge has to be bigger than the bill.
Theory

What is left after costs, and why more trades make it certain

A result before transaction costs is gross; after them, net. Each trade's net is its gross minus its cost, and the cost is paid on every trade, so the costs grow in step with the number of trades. The cost that leaves the average trade at exactly $0, the break-even cost, is the gross edge itself.

Luck spreads each trader's total around that average. From lesson A5 (The bell curve), its typical size, the standard deviation, grows only with √n: four times the trades brings four times the average net total but only twice the luck. So with enough trades the average outgrows the luck: nearly everyone ends ahead if the edge is bigger than the cost, and nearly everyone behind if it is smaller.

g = p × W − (1 − p) × L
Average net total = n × (g − c)
σtotal = σ × √n
p* = (L + c) ÷ (W + L)
n
the number of trades
g
the gross edge: the average profit of one trade before costs
p
the win rate: the share of trades that win
c
the cost of one trade, paid win or lose. At c = g the net is $0: the break-even cost
σtotal
luck: the standard deviation of a trader's total, how far it typically lands from the average net total
σ
the standard deviation (sigma) of one trade's result. For a trade that wins or loses the same amount, σ is about that amount (here $100)
W, L
what a win makes and a loss takes, before costs
p*
the break-even win rate. After costs a win makes W − c and a loss takes L + c. Break-even: p × (W − c) = (1 − p) × (L + c); the c's cancel in the sum, (W − c) + (L + c) = W + L, so p* = (L + c) ÷ (W + L)
Worked example: this page

g = 0.52 × $100 − 0.48 × $100 = $4 and c = $6, so each trade nets $4 − $6 = −$2. Over n = 1,000 trades: 1,000 × (−$2) = −$2,000, which is why the average account finished near $8,000 ($7,992 in the simulation). The bill alone was 1,000 × $6 = $6,000. One trader is not the average: card 5's trader finished at −$3,200, not −$2,000. The difference is luck.

With σ ≈ $100, σtotal ≈ $100 × √1,000 ≈ $3,160. To end ahead, a trader needed luck worth more than $2,000, a z-score (how many standard deviations a result sits from the average, lesson A5) of $2,000 ÷ $3,160 ≈ 0.63: less than one standard deviation. Luck clears one standard deviation about 1 time in 6 (about 68 in 100 land within one standard deviation either side, lesson A5, so about 16 in 100 land above it), so this lower bar was cleared more often: by 25.2%.

At 4,000 trades the average is 4,000 × (−$2) = −$8,000, but luck only doubles (√4,000 = 2 × √1,000), to about $6,300. Now a trader needs more than one standard deviation of luck, so fewer than 1 in 6 are ahead: 9.7%.

Break-even win rate: p* = (100 + 6) ÷ (100 + 100) = 53%, card 8's answer.

Holds when
  • every trade pays the same cost (here $6);
  • the trades are independent (one trade's result doesn't change the next) and alike, which the √n rule needs (lesson A5);
  • g is the rule's true average, not one backtest's lucky estimate.
Breaks when

Big orders move the price against you, so the cost per trade rises with order size (lesson D15, Liquidity and market impact). A gross edge picked as the best of many tries is often luck (The best of many (lesson B7)).

Try it on new numbers. A backtest makes +$10 a trade before costs, over 800 trades. Its costs come to $7 a trade. What is its net total, in dollars?

Net per trade: $10 − $7 = $3. Net total: 800 × $3 = $2,400. The bill took 800 × $7 = $5,600 of the $8,000 gross.
Take the cost off every trade first: $10 − $7 = $3 a trade. Then 800 × $3 = $2,400. Equivalently, $8,000 gross minus an 800 × $7 = $5,600 bill.
Its break-even cost is $10, its gross edge: past $10 a trade, every extra trade loses on average.

What this means for your backtest

Three habits follow from everything above. Turn them over one at a time.

  1. Find the bill.

    Every backtest assumes some cost per trade, sometimes zero. Look for commission, spread and slippage in its settings or notes. With all three at zero, you are seeing results before costs.

Practice 1 of 3

The course seller's result

The situationA trading course sells a rule that "made $6,000 last year". It made 2,000 trades, each risking $100, and the $6,000 is before any transaction costs. You expect commission, spread and slippage together to cost you about $5 a trade.

Before you pay, what does the rule look like after your costs?

$5 is small next to $100, but the edge isn't $100. It is $6,000 / 2,000 = +$3 a trade, and $5 is bigger than that: −$2 a trade, about $4,000 down over 2,000 trades.
$6,000 over 2,000 trades is +$3 a trade. Take $5 off each one and the average trade is −$2, so 2,000 trades lose about $4,000.
The bill is paid on every trade: 2,000 × $5 = $10,000, not $5,000. That leaves $6,000 − $10,000 = about $4,000 down, or −$2 a trade.
The useful question for any backtest without costs: how big is the average trade, and how big is the cost of one trade?
Practice 2 of 3

Your own rule's finish line

The situationYour own rule risks $200 to make $200 on every trade. Your costs come to $10 a trade, win or lose.

What win rate does it need just to break even? Type a percentage.

Hint: after costs, a win keeps $200 − $10 and a loss costs $200 + $10. Break-even is the loss divided by the win plus the loss.

A win keeps $190 and a loss costs $210. Break-even is 210 / (190 + 210) = 210 / 400 = 52.5%, two and a half points above 50%.
A win keeps $200 − $10 = $190 and a loss costs $200 + $10 = $210. The wins have to bring in what the losses take: 210 / (190 + 210) = 210 / 400 = 52.5%.
Practice 3 of 3

Two versions of one idea

The situationYou backtested two versions of an idea, without costs. Version A: 1,200 trades a year, +$5 a trade on average, so $6,000. Version B: 150 trades a year, +$30 a trade on average, so $4,500. Your costs are $6 a trade.

Which version has more left after costs?

Version A made more before costs, yes. But A's average trade, +$5, is smaller than the $6 cost, so each trade loses $1 after costs: 1,200 × −$1 = −$1,200. B keeps +$24 a trade: 150 × $24 = +$3,600.
Both pay $6 a trade, but A pays it 1,200 times ($7,200) and B 150 times ($900). After costs A is at −$1,200 and B at +$3,600.
B keeps $30 − $6 = $24 a trade: 150 × $24 = +$3,600. A's +$5 is smaller than the $6 cost, so it loses $1 a trade: 1,200 × −$1 = −$1,200.
✓

Lesson done

Every trade pays transaction costs, win or lose: commission, the spread and slippage. A backtest's average trade has to be bigger than those costs, and a busy rule whose edge is below its cost loses more surely.

Key ideas

Transaction costs
what one trade costs, win or lose: commission, the spread and slippage
Commission
the broker's fee for each trade
Spread
the gap between the price you can buy at and the price you can sell at
Slippage
getting a worse price than the one you saw when you decided
Edge (gross)
the average profit of one trade before costs
Net
what is left after costs
Break-even cost
the cost that leaves the average trade at $0; it equals the gross edge
Break-even win rate
the win rate at which the average trade after costs is $0

Formulas

Net total = n × (g − c)  ·  σtotal = σ × √n

Break-even win rate = loss after costs ÷ (win after costs + loss after costs)

Lesson notes

Every trade pays a bill, win or lose. A rule's edge is the average profit of one trade before costs, and it survives only if it is bigger than the cost of one trade. A busy rule with edge below cost loses more surely with every trade.

The bill on every trade

A backtest, a replay of a rule on past data, can leave out the transaction costs. There are three. Commission is the broker's fee for each trade (the broker places your trades). The spread is the gap between the price you can buy at and the lower price you can sell at. Slippage is getting a worse price than the one you saw, because the price moves before your order goes through. All three are paid win or lose. The lesson assumes $6 a trade in total, where one trade means getting in and getting out.

The edge before costs

The lesson's simulated trade risks $100 to make $100 and has a 52% win rate, the share of trades that win. Its average is 0.52 × $100 − 0.48 × $100 = +$4 a trade. That is its edge, g in the formula below. A cost of $4 would leave the average at $0: the break-even cost.

What $6 does

At $6 a win keeps $94, a loss costs $106, and the average trade is $4 − $6 = −$2. Over 1,000 trades the average account goes from $10,000 to $8,000 instead of $14,000.

One simulated trader won 514 of 1,000 trades: 28 more wins than losses, so before costs the account rose $2,800. The bill of 1,000 × $6 = $6,000 took it to −$3,200. The average was −$2,000; this trader was $1,200 worse by luck.

Ten thousand traders, and busier rules

In the lesson's simulation of 10,000 traders, a trader is ahead if the account finished above the $10,000 it started with. After 1,000 trades, 88.8% are ahead before costs and 25.2% after, by luck alone, since the average trade now loses $2.

Luck's size (the standard deviation: how far a typical total lands from its average) grows only with the square root of the number of trades, while the sure part grows with the number itself. With 250, 1,000 and 4,000 trades, the share ahead after costs falls to 37.3%, 25.2% and 9.7%, while before costs it climbs to 70.9%, 88.8% and 99.4%. When the average trade after costs is below zero, every extra trade makes a loss more likely. (The simulation never stops an account at $0; a real one would stop.)

The finish line moves

With no cost, risking $100 to make $100 breaks even at 50%. With $6, a win keeps $94 and a loss costs $106, so break-even is p × $94 = (1 − p) × $106, giving p = 106 ÷ (94 + 106) = 53%. The rule's 52% is one point short. An edge has to be bigger than the bill. When it is bigger, more trades help. Two habits follow: find the cost a backtest assumes in its settings (all three at zero means results before costs), and re-run it at a higher cost, because an edge that survives only at the cheapest cost is fragile.

Formula: net total, luck and break-even win rate

A result before costs is gross; after them, net. Each trade's net is its gross minus its cost.

g = p × W − (1 − p) × L
Average net total = n × (g − c)
σtotal = σ × √n
p* = (L + c) ÷ (W + L)
n
the number of trades
g
the gross edge: the average profit of one trade before costs
p
the win rate: the share of trades that win
c
the cost of one trade, paid win or lose. At c = g the net is $0: the break-even cost
σtotal
luck: the standard deviation of a trader's total, how far it typically lands from the average net total
σ
the standard deviation (sigma) of one trade's result. For a trade that wins or loses the same amount, σ is about that amount (here $100)
W, L
what a win makes and a loss takes, before costs
p*
the break-even win rate. After costs a win makes W − c and a loss takes L + c. Break-even: p × (W − c) = (1 − p) × (L + c); the c's cancel in the sum, (W − c) + (L + c) = W + L, so p* = (L + c) ÷ (W + L)
Worked example: the lesson's trades

g = 0.52 × $100 − 0.48 × $100 = $4 and c = $6, so each trade nets $4 − $6 = −$2. Over n = 1,000 trades: 1,000 × (−$2) = −$2,000, which is why the average account finished near $8,000 ($7,992 in the simulation). The bill alone was 1,000 × $6 = $6,000. One trader is not the average: the single simulated trader above finished at −$3,200, not −$2,000. The difference is luck.

With σ ≈ $100, σtotal ≈ $100 × √1,000 ≈ $3,160. To end ahead, a trader needed luck worth more than $2,000, a z-score (how many standard deviations a result sits from the average, lesson A5) of $2,000 ÷ $3,160 ≈ 0.63: less than one standard deviation. Luck clears one standard deviation about 1 time in 6 (about 68 in 100 land within one standard deviation either side, lesson A5, so about 16 in 100 land above it), so this lower bar was cleared more often. The 25.2% is the simulation's share of traders ahead, for a bar 0.63 standard deviations away, not the 1-in-6 rule's figure.

At 4,000 trades the average is 4,000 × (−$2) = −$8,000, but luck only doubles (√4,000 = 2 × √1,000), to about $6,300. Now a trader needs more than one standard deviation of luck, so fewer than 1 in 6 are ahead: 9.7%. The average account ends near $1,928 in the simulation (10,000 − 8,072).

Break-even win rate: p* = (100 + 6) ÷ (100 + 100) = 53%.

Holds whenBreaks when

Big orders move the price against you, so the cost per trade rises with order size (lesson D15, Liquidity and market impact). A gross edge picked as the best of many tries is often luck (lesson B7, The best of many).

One more worked example

Made-up round numbers

A backtest makes 600 trades, each risking $50 to make $50, with a 54% win rate and costs of $3 a trade.

Edge: g = 0.54 × $50 − 0.46 × $50 = $27 − $23 = $4.

Net per trade: $4 − $3 = $1. Net total: 600 × $1 = $600.

Break-even win rate: p* = (50 + 3) ÷ (50 + 50) = 53%, so 54% clears it by one point.

Luck: σtotal ≈ $50 × √600 ≈ $50 × 24.5 ≈ $1,225. The average of +$600 is about half of that, so a trader can easily finish behind: the edge is real on average, but small against luck.

Common mistakes

Check yourself

  1. A rule risks $200 to make $200 on every trade, with costs of $10 a trade. What win rate does it need to break even?
    AnswerA win keeps 200 − 10 = $190 and a loss costs 200 + 10 = $210. p* = (L + c) ÷ (W + L) = 210 ÷ (200 + 200) = 52.5%.
  2. A backtest makes +$12 a trade before costs over 500 trades, and costs come to $9 a trade. What is its net total?
    Answern × (g − c) = 500 × (12 − 9) = $1,500.
  3. Version A makes 1,200 trades at +$5 each on average, version B 150 trades at +$30, before costs of $6 a trade. Which has more left?
    AnswerVersion B: 150 × (30 − 6) = +$3,600. A nets 1,200 × (5 − 6) = −$1,200.
  4. A rule's average trade after costs is below zero. Why does doing more trades make losing surer?
    AnswerThe average net total, n × (g − c), grows with n, but luck, σ × √n, grows only with √n, so the sure loss outgrows it.

Try it yourself

Run the simulation. Every line and number in this lesson comes from one Python script with a fixed random seed (below). Change the win rate, the cost or the number of trades and see what moves.

Do it by hand. Take any backtest's list of trades. Subtract your cost from every trade, wins and losses alike, or take the number of trades times the cost off the total. Divide what is left by the number of trades: that is the average trade after costs.

About QuantWright

QuantWright, which publishes this course, sells backtesting software. It backtests with costs: commission, slippage and next-bar fills. Every result states its data window, data source and costs, so you can see which bill a result paid. quantwright.com

The code behind every number (Python, seed 2026110505)
Loading…

As a 129-second video: Backtesting from Scratch, Part 4, "$4,000 Edge, $6,000 Fees". Same 10,000 simulated traders, same numbers.

Going further: why more trades make the bill certain

The theory card showed why: the sure part, the number of trades × the average trade, grows in step with the trades, luck only with their square root (about $1,580 after 250 trades, $3,160 after 1,000 and $6,319 after 4,000). So once the average trade after costs is below zero, the loss wins in the end; the only question is how many trades it takes. The figure shows how fast, at each cost.

The chance a trader is ahead, by trades made

Line: exact, from the 52% win rate. Dots: the 10,000 simulated traders.

Cost of one trade
Ahead after 4,000 trades, exact–
Simulated–

Below $4, the cost leaves part of the edge and the share ahead climbs toward everyone. At $4, the cost equals the edge and the share stays just under half (finishing at exactly $10,000 doesn't count as ahead). Above $4 it sinks toward no one.

For the preciseAfter n trades with k wins, the account is $10,000 + $100 × (2k − n) − c × n, so a trader is ahead when k > n × (100 + c) / 200, with k ~ binomial(n, 0.52). Exact: n = 1,000 at $6, 25.3% (simulated 25.2%); n = 4,000 at $6, 10.0% (simulated 9.7%). Luck's size is the standard deviation of the result before costs, 2 × $100 × √(n × 0.52 × 0.48). Break-even win rate for a trade that wins W or loses L at cost c: (L + c) / (W + L); here (100 + 6) / 200 = 53%.

Honest notes

Where this comes from