Modeling slippage and commission
A backtest with zero costs is a fantasy, and optimizing against a fantasy tunes your strategy to exploit free trading. The moment you add realistic commission and slippage, many high-frequency, thin-edge settings collapse. Better to find that out now than with real money.
Why a costless backtest lies
Every real trade pays a spread, a commission, and some slippage between the price you wanted and the price you got. Those costs are tiny per trade and enormous in aggregate: a strategy that takes 500 trades a year at a quarter percent round-trip cost pays 125 percent of capital in friction before it makes a cent. Optimize without costs and the search gravitates toward exactly these high-churn settings, because in a frictionless world more trades just means more profit.
Set the Strategy Tester properties
TradingView's Strategy Tester reads commission and slippage from the strategy's Properties, and you can set sensible defaults right in the strategy() call so every backtest starts honest. Commission can be a percent of the trade value or a fixed cash amount per order; slippage is set in ticks. Tune these to your broker and instrument, not to whatever makes the curve look best.
Realistic costs in strategy() (Pine v5)
Pine v5Commission as a fixed cash-per-order charge plus a few ticks of slippage. Adjust to your broker; never set costs to zero to flatter a result.
//@version=5
strategy("Cost-Aware Strategy", overlay=true,
commission_type=strategy.commission.cash_per_order,
commission_value=1.0, // 1.00 per order, both sides
slippage=3, // 3 ticks of slippage per fill
default_qty_type=strategy.percent_of_equity,
default_qty_value=10) Note
Defaults by instrument
Liquid large-cap stocks and major FX pairs slip little; small caps, crypto alts, and low-volume futures slip a lot. When unsure, model costs on the pessimistic side and let the strategy earn its keep.
The break-even win rate shifts
Costs raise the win rate you need just to break even. If your average winner and loser are the same size, you break even at 50 percent before costs, but every dollar of round-trip cost pushes that threshold up. The formula below makes the shift concrete, and it is why a strategy that looks profitable at zero cost can be a loser once you model reality.
Break-even win rate after costs
PythonThe win rate needed to break even given average win, average loss, and round-trip cost per trade. Rising costs raise the bar.
# Break-even win rate given average win, average loss, and per-trade cost.
def break_even_win_rate(avg_win, avg_loss, cost):
net_win = avg_win - cost
net_loss = avg_loss + cost
return net_loss / (net_win + net_loss)
break_even_win_rate(100, 100, 0) # 0.50 (no costs)
break_even_win_rate(100, 100, 10) # 0.55 (10 per round trip) Always optimize with costs on
Set your costs first, then optimize. If you tune on a frictionless backtest and add costs afterward, the settings you chose were selected for the wrong objective and the realistic result will disappoint. With costs on from the start, the optimizer naturally prefers fewer, higher-conviction trades, which is what you want to trade anyway. This is one of the common backtesting mistakes precisely because it is so easy to skip.
Key idea
The one rule
Model commission and slippage before you optimize, on the pessimistic side, so the search rewards a real edge instead of free churn.
Key takeaways
- Zero-cost backtests reward high-churn settings that lose money live.
- Set commission and slippage in the Strategy Tester properties.
- Costs raise the win rate you need just to break even.
- Optimize with costs on from the start, not bolted on after.
Frequently asked questions
What slippage and commission should I use for backtesting?
Match your broker and instrument. Liquid large-cap stocks and major FX pairs need only a tick or two of slippage and small commission; illiquid small caps, crypto alts, and thin futures need much more. When unsure, model costs pessimistically.
Should I add costs before or after optimizing?
Before. If you optimize on a costless backtest and add costs afterward, the settings were chosen for the wrong objective. With costs on from the start, the optimizer prefers fewer, higher-conviction trades.
Why does a profitable backtest become a loss after adding costs?
Thin-edge, high-frequency settings make money only in a frictionless world. Real commission and slippage are small per trade but huge in aggregate, and they raise the break-even win rate above what the strategy actually achieves.
Related guides
- Beginner
Reading backtest metrics
How to read the backtest metrics TradingTune surfaces (net profit, win rate, profit factor, Sharpe, drawdown, trade count) and weigh them together, not one alone.
Read the guide - Intermediate
Common backtesting mistakes
Overfitting, too few trades, ignored costs, and lookahead bias: the most common TradingView strategy backtesting mistakes and how to avoid each one.
Read the guide - Intermediate
How many trades is enough?
How many trades a TradingView backtest needs before its metrics mean anything: sample size, statistical noise, and why too few make optimization a coin flip.
Read the guide
Put it into practice
Install TradingTune and optimize any TradingView strategy right in your browser. Free tier, no API keys. A free account is required to run.
Add to Chrome, it's free