TradingView Backtest & Robustness Lab

How to Model Slippage, Spread and Fees in a Backtest: Cost Sensitivity and Stress Testing

How to Model Slippage, Spread and Fees in a Backtest | SG Group

Backtest Learning · BT09

How to Model Slippage, Spread and Fees in a Backtest: Cost Sensitivity and Stress Testing

A gross backtest built on idealized fills shrinks sharply once you subtract real commission, spread, slippage and holding costs, and that friction compounds the more often a strategy trades. This guide shows how to subtract costs in a consistent unit to reach a net result, then confirm how robust that conclusion is with sensitivity analysis and stress grids.

  • How to classify commission, spread and slippage and keep their units consistent
  • How to avoid double counting by separating per side from round trip and per fill from per trade
  • How cost drag widens the gap between gross and net equity curves
  • How sensitivity analysis and a two-dimensional stress grid test robustness
About 13 min read Updated 13 July 2026 For short-term FX, CFD, futures and crypto strategies

Key takeaways

  • Compare strategies on net P&L after subtracting at least commission, spread and slippage in a consistent unit, not on gross.
  • Separate per side from round trip and per fill from per trade so you avoid double counting or undercounting.
  • Cost drag accumulates with turnover, so the gap between gross and net equity curves widens the more you trade.
  • Run baseline, 1.25x, 1.5x and 2x cost-sensitivity scenarios to see at which multiple you cross break-even.
  • Use a two-dimensional stress grid such as slippage by commission to check that a conclusion does not rest on one assumption.
Contents

This guide explains how to move a strategy’s idealized, gross backtest result from a tool such as TradingView toward a realistic net result that reflects commission, spread, slippage and holding costs. It starts with the answer, then works through cost classification, the per-side versus round-trip distinction, how to think about the TradingView settings, spread modeling, a gross-versus-net comparison, sensitivity analysis, stress grids and finally an estimator where you can try your own numbers. If you want the wider context first, the complete TradingView backtesting workflow frames where this lesson fits.

The answer in 30 seconds

The conclusion is simple. Compare strategies on net P&L after costs, not on gross, idealized P&L. At minimum, subtract three items one by one in the same unit: commission, the bid/ask spread and slippage. For high-turnover strategies, small per-trade costs stack up and quietly push expectancy down. A curve that looked attractive on a gross basis can turn flat, or even slope downward, once it is measured net.

Throughout this article we use a single set of fictional, illustrative data: gross average P&L of 5.0 cost units per trade and an assumed round-trip cost total of 3.0 cost units. Here a “cost unit” is a relative value expressed in your account currency; it can stand for an amount, pips or a percentage, as long as every input uses the same unit. In this example, net expectancy per trade is 5.0 − 3.0 = 2.0 units, and this after-cost figure matters more as the number of trades grows. For how to read expectancy and profit factor, see how to read backtest results.

Terminology: gross is the idealized result before costs such as commission are removed; net is the result after they are subtracted. Every number in this article is fictional, illustrative data, not a real result or a recommended value.

Classifying costs: what to subtract

Managing costs by type prevents both double counting and omissions. Your analysis goal decides how far you go.

  • Commission: the explicit fee paid to the broker. Units vary: per fill, as a percentage of trade value, or per contract.
  • Bid/ask spread: the gap between the bid and the ask. A known cost that arises structurally whenever you cross with a market order, and it varies by instrument and time of day.
  • Slippage: the difference between your assumed price and the actual fill price. An uncertain cost that widens in thin books, on sharp moves and with larger order sizes.
  • Swap / financing: a cost incurred by holding a position overnight, including FX and CFD swaps, futures rollover and crypto funding rates.
  • Borrow fee: the cost of borrowing stock to sell short, which moves with supply and demand for the symbol.
  • Taxes: dependent on the regime and your circumstances. This article does not give tax advice, so manage them separately.

The table below organizes this classification by when the cost occurs and how easy it is to estimate. The minimum three — commission, spread and slippage — occur on almost every fill, so subtract those first.

Table 1: Trading cost classification (conceptual, based on fictional illustrative data)
Cost typeWhen it occursEase of estimationInclude as minimum?
CommissionPer fillHigh (specified)Include
SpreadPer fillMedium to highInclude
SlippagePer fillLow (use a distribution)Include
Swap / financingWhile heldMediumInclude if holding
Borrow feeWhile short is heldMediumOnly when relevant
TaxesOn realization, etc.Regime-dependentManage separately
Per-trade cost decomposition waterfall From a gross average of 5.0 cost units, round-trip commission 0.8, spread 1.2, slippage 0.6 and swap 0.4 are subtracted in turn to reach a net of 2.0 cost units. Values are fictional illustrative data. Per-trade cost composition (fictional illustrative data, cost units) 5 3 0 Gross avg 5.0 −Commission 0.8 −Spread 1.2 −Slippage 0.6 −Swap 0.4 Net 2.0
Figure 1: Per-trade cost decomposition waterfall. From a gross average of 5.0 units, round-trip commission 0.8, spread 1.2, slippage 0.6 and swap 0.4 are subtracted in turn to reach a net of 2.0 units. Alongside color, each step is labeled with its value. All numbers are fictional, illustrative data.

Per side vs round trip: avoiding unit errors

The most common accident when applying costs is a unit mismatch. Because one trade splits into an entry fill and an exit fill, costs are, as a rule, counted round trip. Use a per-side figure as a round-trip figure and you undercount; layer a round-trip figure onto each fill and you double count.

  • Per side vs round trip: if commission is per fill, one trade carries two charges (round trip). In our example, per-side commission 0.4 × 2 = round-trip 0.8 units.
  • Per order vs per fill vs per contract: when one order is split into several fills, charges levied per fill rise with the number of fills. Charges proportional to quantity scale with size.
  • Percentage of trade value: percentage-type commission is proportional to position value. Do not mix it with a fixed-amount type, and state clearly which unit applies.

The spread works the same way. Estimating 1.2 units round trip and assigning 0.6 units each to entry and exit is consistent; adding the full 1.2 to each fill on top of that double counts. If you want to review this alongside exit timing and entry efficiency, MFE and MAE exit analysis is a useful companion.

Per-side and round-trip cost flow The entry fill and the exit fill each incur per-side commission, spread and slippage, which combine into the round-trip cost. Values are fictional illustrative data. Per side × 2 = round-trip cost (fictional illustrative data, cost units) Entry fill (per side) Commission 0.4 Spread 0.6 Slippage 0.3 Subtotal 1.3 Exit fill (per side) Commission 0.4 Spread 0.6 Slippage 0.3 Subtotal 1.3 Round-trip cost Commission 0.8 Spread 1.2 Slippage 0.6 Total 2.6 Swap 0.4 is incurred separately while held; with round-trip 2.6 it makes 3.0 units per trade
Figure 2: Per-side cost flow. Entry and exit each incur 1.3 units per side, so the round trip totals commission 0.8, spread 1.2 and slippage 0.6 = 2.6 units. Adding the swap of 0.4 incurred while held brings one trade to 3.0 units. Numbers are fictional, illustrative data; check actual fees against your broker’s specifications.

TradingView commission and slippage

There are two main places to apply costs in TradingView. One is the commission and slippage fields in the strategy Properties panel; the other is the arguments of the strategy() function in Pine Script. Values entered in the Properties panel are reflected in the backtest immediately, while specifying them in the script fixes the assumptions when you publish or share it.

Commission units

Commission is commonly chosen from units such as a fixed amount, a percentage or per contract. What matters here is understanding whether the chosen unit is per side or round trip. In many cases the value is charged per fill, so one trade carries two charges. The round-trip figure of 0.8 units above is the result of charging 0.4 per side on each fill.

Slippage units

Slippage is commonly added in ticks. The assumed market fill price is shifted by the configured number of ticks in the unfavorable direction. Because real slippage changes with conditions, do not rely on a single fixed value; give it a range in the sensitivity analysis described below.

Please re-verify: the field names, available units and defaults in TradingView can change. Before configuring, confirm the current item names and units in Strategy Properties (official) and the Pine Script declaration statements (official). SG Group does not claim any partnership with or endorsement by TradingView.

Modeling the spread

The spread is a known cost, but it is not constant. How precisely you model it depends on the nature of the strategy. Since no single universal setting exists, check whether your conclusion holds across several models.

  • Fixed spread: the simplest approximation, using one constant value across the whole period. Manageable for liquid symbols traded in a narrow window, but it diverges from reality when a strategy spans fast markets or thin sessions.
  • Session-based spread: vary the spread by session (Tokyo, London, New York and so on) or around the open and close. This improves accuracy for strategies that cross time zones.
  • Volatility-linked: widen the spread according to a volatility measure. This prevents underestimating cost for strategies that include news releases or fast-moving conditions.

In practice, first get a sense of net performance with a fixed spread; if the result is thin, refine it to a session-based or volatility-linked model and confirm the conclusion does not change. To read the market regime from an outside vantage point, an environment-awareness tool such as the Macro Research Workbench can also help.

Gross vs net equity curves

The impact of costs can look small per trade, but once it stacks up over the number of trades it is no longer negligible. Here we use the earlier settings (gross average 5.0, round-trip plus holding cost total 3.0 units) over 400 trades of fictional, illustrative data to compare the gross and net equity curves.

Cumulative gross P&L is 400 × 5.0 = 2,000 units, the cumulative round-trip and holding cost is 400 × 3.0 = 1,200 units, and net is the difference, 800 units. Indexed, gross rises from a start of 100 to 180 while net rises from 100 to 132, and the gap widens the more trades accumulate. This is cost drag, and the difference in slope bites harder the more often a high-frequency strategy trades.

Gross vs net equity curve comparison Equity index over 400 trades. Gross rises from a start of 100 to 180, net rises from 100 to 132, and the gap widens with more trades. Values are fictional illustrative data. Gross vs net equity (fictional illustrative data, index base 100) 180 130 100 0 200 trades 400 trades Cost gap 180 132 Gross (idealized fills) Net (after costs)
Figure 3: Gross and net equity curves. The solid gross line reaches an index of 180 and the dashed net line reaches 132, with the red vertical line at the right marking the cost gap. Line style and labels distinguish the two. Numbers are fictional, illustrative data and do not indicate future results.

A strategy whose after-cost slope is close to flat sinks below expectancy on even a small overshoot in costs. To also read the impact on drawdown depth and recovery time, see maximum drawdown analysis.

Cost sensitivity: baseline to 2x

Real costs move day to day and are not captured by an average alone. So set a baseline cost and raise it in stages to observe where net performance breaks down. There is no universally correct number of steps, but increments such as baseline, 1.25x, 1.5x and 2x make the point where you cross break-even easy to see.

The table below applies those multipliers to the round-trip and holding cost (baseline 3.0 units × 400 trades = 1,200 units) on fictional, illustrative data with gross gross profit of 5,000 and gross gross loss of 3,000 (gross profit factor 1.67). Costs are added to the loss side as a simple treatment.

Table 2: Cost sensitivity scenarios (fictional illustrative data, 400 trades, gross net profit 2,000 units)
ScenarioCost multiplierCost per tradeCumulative costNet profitNet expectancy/tradeNet PFStatus
Baseline1.00×3.012008002.01.19Headroom
Slightly worse1.25×3.7515005001.251.11Headroom
Worse1.50×4.518002000.51.04Near break-even
Much worse2.00×6.02400-400-1.00.93Below break-even

In this example, when costs double from baseline the net profit falls to −400 units and net profit factor sinks to 0.93, so expectancy turns negative. Already at 1.5x, net profit thins to 200 units, showing the break-even point is close. It is important not to judge from the baseline alone and conclude there is headroom, but to raise costs to the multiples that could plausibly occur. Choose the multipliers and increments from measurements of your instrument and your broker’s specifications.

Multidimensional stress grids

Costs do not always move on one axis alone. To see compound stress — slippage and commission worsening at once, or a wider spread overlapping with poorer fills — a two-axis grid is effective. Below is a heatmap of fictional, illustrative data that combines a commission multiplier (columns) and a slippage multiplier (rows) and shows net profit (units) with both color and number. Spread 1.2 and swap 0.4 are held fixed.

Figure 4: Two-dimensional slippage-by-commission stress grid. Cells are net profit over 400 trades (units), shown with color (green = higher, blue = middle, amber = lower) as well as numbers. Profit thins in stages from 800 at the top left (each 1.0x) to 400 at the bottom right (commission 1.5x, slippage 2.0x) as stress compounds. Numbers are fictional, illustrative data.

Across this grid’s range net profit stays positive, but if you widen the stress further — to something like commission 2.0x — it crosses break-even as Table 2 shows. The grid is a tool for seeing the whole surface of which combinations thin the result and by how much, so you can confirm a conclusion does not depend on a single optimistic scenario. Combined with walk-forward methods that reveal regime-by-regime degradation, you can test robustness on both the time axis and the cost axis. For the design, see walk-forward and out-of-sample analysis. The cost impact when several strategies are combined is covered in multi-strategy portfolio backtesting.

This heatmap is a static, in-browser example. In the Lab, Pro handles use cases such as cost-assumption sensitivity and cost decomposition, while Premium handles stress grids that vary several axes at once and batch reports for broader validation. Check features and scope on the plans page.

Try the cost estimator

Enter trade count, gross average P&L per trade, per-side commission, round-trip spread equivalent and slippage equivalent, and this estimates total cost, net expectancy and break-even cost. Values are calculated only in your browser and are never sent anywhere. Enter everything in the same unit (an amount, a pips equivalent or a percentage). If JavaScript is off, see the static worked example directly below.

Static worked example (JavaScript off): with trade count 400, gross average 5.0, per-side commission 0.4, round-trip spread equivalent 1.2 and slippage equivalent 0.6, round-trip commission is 0.4 × 2 = 0.8, and total cost per trade (commission + spread + slippage) is 0.8 + 1.2 + 0.6 = 2.6 units. Total cost is 400 × 2.6 = 1,040 units, net expectancy is 5.0 − 2.6 = 2.4 units per trade, and net total P&L is 400 × 2.4 = 960 units. The break-even cost (the per-trade cost at which net reaches zero) equals the gross average, 5.0 units, so the current 2.6 units is about 52% of it. For a holding strategy, add the swap of 0.4 separately.

Trade cost estimator

Total trades in the evaluation period
Average before costs (keep unit consistent)
Per fill; round trip is x2 automatically
Spread for one trade (round trip)
Assumed slippage for one trade

Enter values above and press Estimate to see total cost, net expectancy and break-even cost.

This estimate is for illustrating the concept and does not guarantee performance or profit. Mixing units makes the result meaningless, so keep everything in one of an amount, a pips equivalent or a percentage. Add swaps, borrow fees and taxes separately as your goal requires.

Practical checklist

  • Are you comparing strategies on net rather than gross P&L, and did you include the minimum three of commission, spread and slippage?
  • Did you keep per side vs round trip, per fill vs per trade, and amount vs percentage straight, avoiding double counting and undercounting?
  • Did you confirm in the official documentation whether TradingView’s commission and slippage units are per side or round trip?
  • Did you test the spread with session-based and volatility-linked models, not just a fixed value, and confirm the conclusion holds?
  • For holding strategies, did you include swap, financing and borrow fees, and manage taxes separately?
  • Did you run baseline, 1.25x, 1.5x and 2x cost sensitivity and identify at which multiple you cross break-even?
  • Did you use a two-dimensional grid such as slippage by commission to check robustness to compound stress across the surface?
  • Did you align the included costs and unit assumptions across the strategies you compare?

Frequently asked questions

Which costs should every backtest include?
At minimum, include three: commission, the bid/ask spread and slippage. These occur on almost every fill and quietly erode gross P&L, especially for high-turnover strategies. Subtract all three in a consistent unit to reach a net result first, then add swaps, financing, borrow fees or taxes as your analysis goal requires. Record which costs you included and keep the same assumptions across any strategies you compare.
Are spread and slippage the same thing?
No. The spread is the gap between the bid and the ask, a known cost that arises structurally every time you cross the book with a market order. Slippage is the difference between your assumed price and the actual fill price, an uncertain cost that widens in thin books, around news and during fast markets. Spread is relatively easy to estimate, whereas slippage is best handled as a distribution with a range rather than a single fixed value.
Where do I set commission in TradingView?
Set it in the strategy Properties panel for commission and slippage, or through the arguments of the strategy() function in Pine Script. Commission can usually be expressed per contract, as a percentage of value or as a fixed amount, while slippage is commonly added in ticks. Field names and available units can change, so confirm the current items in TradingView’s official documentation before you configure anything.
Is commission charged per side or round trip?
Think in round trips, because one trade contains an entry fill and an exit fill. When a tool charges commission per fill, a single trade automatically carries two charges. Treating a per-side figure as if it were the full round-trip cost causes undercounting, while stacking a round-trip figure onto each fill causes double counting, so verify every time whether your input is per side or round trip and per fill or per trade.
Is a fixed spread assumption enough?
It depends on the strategy and the instrument. For liquid symbols traded in a narrow window, a fixed-spread approximation rarely misses by much, but strategies that span news releases, thin sessions or high-volatility regimes will diverge from a single fixed value. Test fixed, session-based and volatility-linked models to see whether your conclusion holds, and avoid treating any one setting as a universal answer.
Should financing or swap costs be included?
Include them when holding periods are long. FX and CFD swaps, futures rollover, crypto funding rates and stock borrow fees accumulate the longer a position is held. Their impact is small for intraday trades that do not carry overnight, but they are material for swing and carry-style strategies. Taxes are regime-dependent and this article does not give tax advice, so manage them separately.
How many cost-sensitivity scenarios should I run?
There is no universally correct number of steps. A clear approach is to set a baseline cost and raise it in stages such as 1.25x, 1.5x and 2x to see where net performance breaks down. What matters more than the count is covering the range that could plausibly occur and identifying at which multiple the strategy crosses break-even. Choose the multipliers and increments from broker specifications and measurements of your instrument.
Is a strategy unusable if costs remove its profit?
Not necessarily, but a strategy whose expectancy falls to around zero or turns negative under realistic costs is hard to sustain in live trading. Ways to improve include trading less often so each trade carries a thicker edge, shifting toward lower-cost sessions or instruments, and improving exit efficiency. Confirm any judgment with this article’s cost estimator or real data in the Lab, and do not conclude from a single number.
Disclaimer: this article and the tool provide descriptive aggregation, diagnostics and visualization of loaded backtest data as educational and informational content. They are not investment advice, a buy/sell signal, a forecast of future prices or a guarantee of profit. All numbers and figures shown are fictional, illustrative data and do not represent real results, recommended parameters or recommended cost settings. Backtest results do not guarantee future performance and change with assumptions, data, costs and market regime. Verify actual commission, spread, slippage and swap against your broker’s or exchange’s specifications, and taxes against the applicable regime. Final decisions are your own responsibility. TradingView’s interface names and specifications can change, so confirm the latest information on the official pages. SG Group does not claim any partnership with or endorsement by TradingView.