Tutorial

Pine Script Strategy Commission and Slippage Settings

Set commission_type, commission_value and slippage in a Pine Script v6 strategy, learn what ticks mean, and stress-test your costs.

•9 min read

Your strategy shows a smooth equity curve in the Strategy Tester. Then you open the Properties tab and notice that commission is 0 and slippage is 0. Every fill in that report happened at the exact price your code asked for, at no cost. Real orders do not work that way, and the two settings that model the difference are only a few characters each in the strategy() declaration.

This guide covers what commission_type, commission_value, and slippage actually do in Pine Script v6, how to pick values that are realistic for the market you test on, where the settings stop being accurate, and how to use them as a stress test instead of a one-time checkbox. If you want the conceptual side first, why costs matter so much to a backtest, the post on how slippage and commission eat your returns covers it. This one is about the code.

Where the cost settings live

Both settings are arguments of the strategy() declaration at the top of the script. They set the defaults, and the Properties tab in the Strategy Tester settings shows the same fields so a user can change them without editing code. If you hand a script to someone else, the values in your declaration are what they will see first, so it is worth setting them deliberately instead of leaving them at zero.

pine
//@version=6
strategy("Costed crossover", overlay = true,
     initial_capital = 10000,
     default_qty_type = strategy.percent_of_equity,
     default_qty_value = 10,
     commission_type = strategy.commission.percent,
     commission_value = 0.1,
     slippage = 2)

fastLen = input.int(10, "Fast length", minval = 1)
slowLen = input.int(30, "Slow length", minval = 2)

fast = ta.sma(close, fastLen)
slow = ta.sma(close, slowLen)

if ta.crossover(fast, slow)
    strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
    strategy.close("Long")

plot(fast, "Fast", color.new(color.teal, 0))
plot(slow, "Slow", color.new(color.orange, 0))

Three arguments do the work here. commission_type chooses how a fee is calculated, commission_value is the size of that fee, and slippage is a worse fill, measured in ticks. The rest of this post takes each in turn.

The three commission types

commission_type accepts three constants, and commission_value means something different for each one. Mixing them up is the most common mistake, because the number looks plausible in every case.

commission_typeWhat commission_value meansTypical use
strategy.commission.percentPercent of the order's valueCrypto exchanges, many brokers
strategy.commission.cash_per_orderA flat amount in account currency per orderBrokers with a fixed ticket fee
strategy.commission.cash_per_contractAn amount per contract, share, or lotFutures and per-share stock pricing

With the percent type, a value of 0.1 means 0.1 percent, not 0.1 as a fraction. On a 1,000 dollar order that is one dollar. The fee is charged on every order, so an entry and a later exit are two separate charges. A trade that opens and closes at roughly the same size pays about 0.2 percent of its value in total, and a strategy that trades 200 times a year pays that 200 times.

The cash types are simpler to reason about. With cash_per_order and a value of 2, each entry and each exit costs 2 in your account currency, whatever its size. That makes small orders proportionally expensive and large orders cheap. With cash_per_contract and a value of 0.005, a 1,000 share order costs 5 each way. This type scales with quantity rather than price, which is how many per-share and futures fee schedules are written.

Which one is right depends on how the venue you actually use charges. Read the fee schedule, convert it to one of these three shapes, and use that. If a venue has tiers or maker and taker rates, pick the rate that matches how your orders would execute. A market order typically pays the taker rate, a resting limit order often pays the maker rate, and Pine Script applies one number to everything. When in doubt, use the higher rate so the test errs on the cautious side.

Slippage is measured in ticks

The slippage argument is an integer count of ticks. It is added against you on every fill the model treats as slipped: a long entry fills a few ticks above the requested price, a long exit fills a few ticks below, and the reverse for shorts. TradingView applies it to market and stop orders. A limit order is not slipped, because a limit order by definition fills at its price or better.

The word that trips people up is tick. A tick is the smallest price increment of the symbol, and Pine Script exposes it as syminfo.mintick. Two ticks on a symbol with a minimum tick of 0.01 is 0.02 in price. Two ticks on a symbol with a minimum tick of 0.5 is a full point. The same slippage = 2 therefore models very different amounts of money on different markets, so always convert it back to price before deciding it is reasonable.

pine
//@version=6
indicator("Slippage in price terms", overlay = true)

slipTicks = input.int(2, "Slippage (ticks)", minval = 0)
slipPrice = slipTicks * syminfo.mintick
slipPercent = close > 0 ? slipPrice / close * 100 : na

var table t = table.new(position.top_right, 2, 3,
     bgcolor = color.new(color.gray, 85))

if barstate.islast
    table.cell(t, 0, 0, "Min tick")
    table.cell(t, 1, 0, str.tostring(syminfo.mintick))
    table.cell(t, 0, 1, "Slippage in price")
    table.cell(t, 1, 1, str.tostring(slipPrice))
    table.cell(t, 0, 2, "Slippage % of close")
    table.cell(t, 1, 2, str.format("{0,number,#.####}", slipPercent))

Drop this on the chart you plan to test and it shows what your tick count means in price and as a share of the current close. If the percentage is far below the market's typical spread, your setting is too optimistic. If it is several times larger than a normal spread, you are modeling a thin or fast market, which can be a fair choice for a stress test but should be a decision, not an accident.

Two real limits of the model

Both settings are flat. Commission is one rate, and slippage is the same number of ticks on every fill. Real execution is not flat. Slippage grows when volume is thin, when volatility spikes, and when your order is large compared with what the book can absorb. A strategy that trades during a news candle will pay more than the same strategy at midday, and a fixed slippage = 2 cannot tell the difference.

The second limit is order size. Pine Script does not model market impact. Backtesting a ten thousand dollar account and a ten million dollar account with the same code produces the same slippage per unit, which is only plausible while your orders are small compared with the market's depth.

Neither limit means the settings are useless. It means they give you a floor, a minimum cost of doing business, rather than a forecast. Treat a result that survives realistic costs as having cleared one bar, not as a prediction of anything.

How fills interact with costs

Costs are applied at the moment the Strategy Tester fills an order, so anything that changes when or at what price orders fill changes what costs apply to. Two settings matter most.

By default a strategy evaluates on bar close and fills the resulting order at the next bar's open. That gap is where slippage is added. If you switch on process_orders_on_close, orders fill at the close of the signal bar instead, which looks more flattering because it removes the overnight or intrabar move between the trigger and the fill. It is also harder to justify, since you cannot know the close until the bar is over, and your real order would go out after it. If you use it, keep slippage on and say so in your notes.

The other is intrabar resolution. Stop and limit orders are checked against a bar's high and low, and the order of events inside a bar is an assumption the tester has to make. The post on the bar magnifier explains how a finer timeframe changes those assumptions, and calc_on_every_tick covers how realtime calculation changes when orders are evaluated. Either one can move results by more than a tick of slippage, so know which you are using before attributing a difference to costs.

Read what the costs actually were

Once the settings are in, check that they are doing what you expect. The Strategy Tester shows the total commission paid in the performance summary, but it is also useful to read the figure from code so a script can show it next to its own state. strategy.closedtrades.commission(trade_index) returns the commission paid on one closed trade, covering both its entry and its exit.

pine
//@version=6
strategy("Commission readout", overlay = true,
     initial_capital = 10000,
     default_qty_type = strategy.percent_of_equity,
     default_qty_value = 10,
     commission_type = strategy.commission.percent,
     commission_value = 0.1,
     slippage = 2)

fast = ta.ema(close, 12)
slow = ta.ema(close, 26)

if ta.crossover(fast, slow)
    strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
    strategy.close("Long")

float totalFees = 0.0
if barstate.islast
    for i = 0 to strategy.closedtrades - 1
        totalFees += strategy.closedtrades.commission(i)
    label.new(bar_index, high,
         "Closed trades: " + str.tostring(strategy.closedtrades) +
         "\nCommission paid: " + str.tostring(totalFees, "#.##"),
         style = label.style_label_down)

The loop runs only on the last bar, so it costs one pass over the closed trades rather than one per bar. If the commission total looks too small or too large, check your commission_type first. A percent value typed into a cash_per_order setting, or the reverse, is easy to miss in the declaration and obvious in this readout.

Use costs as a stress test, not a setting

The most useful habit is to run the same strategy several times with different cost assumptions and see how the numbers move. Start with zero costs as a reference, then your realistic estimate, then roughly double it. Everything else stays the same: same symbol, same timeframe, same date range.

What you are looking for is how quickly the result degrades. A strategy whose trade count is low and whose average trade is large will barely notice doubled costs. A strategy that trades often with small average moves can go from looking acceptable to clearly negative, because costs are charged per trade while the moves they are measured against are tiny. The average trade in the Strategy Tester summary compared with your per-trade cost is the quickest check. If the two are the same order of magnitude, the strategy is mostly measuring its own fees.

The Strategy Tester metrics guide explains how to read the rest of the report once costs are in, and the post on why backtest results differ from live trading covers the other gaps a cost model does not close. Position size matters here too, because a percent-of-equity order and a fixed-quantity order pay costs differently, as covered in position size in Pine Script.

This is analysis of how a piece of code behaves on historical data. It says nothing about what any strategy will do in future, and costs in a backtest are always an approximation of costs you will actually pay.

A short checklist before you trust the report

Before reading any strategy result, confirm four things. The commission type matches how your venue charges. The commission value is in the right unit for that type. The slippage tick count, converted to price, is not smaller than the typical spread. And you know whether fills happen at the next open or at the signal bar's close. Those four checks catch most of the cost mistakes that quietly flatter a backtest.

If your current script ignores costs and you would rather not edit the declaration by hand, PineScripter is a Pine Script v6 editor where you can describe the change in plain English, for example "add 0.1 percent commission and 2 ticks of slippage to this strategy", then review exactly which lines changed in the Code Changes view. It checks the script for compile errors the way TradingView does. It does not run strategy backtests itself, so you paste the result into TradingView and read the numbers in the Strategy Tester. The free Pine Script and trading calculators run in the browser with no account.

PineScripter is an independent coding tool and is not affiliated with TradingView. Nothing here is financial advice, a trading recommendation, or a prediction of results. Backtest figures describe past data only, and you are responsible for your own trading decisions.

Back to Blog

Describe your indicator in plain English and get Pine Script that compiles →


Disclaimer: PineScripter is a coding tool for Pine Script development. It does not provide financial advice and does not guarantee trading profits. Always backtest strategies thoroughly and understand the risks before live trading.