Pine Script Slippage & Spread Guard
Adds per-contract commission and tick slippage to the strategy declaration — plus a wide-bar skip — so backtests match live prop firm fills.
//@version=6
// Commission and slippage live in the strategy() declaration.
// slippage is in TICKS; commission here is $ per contract per side.
// Pine requires literal values here — inputs are not allowed.
strategy("Slippage & Spread Guard", overlay=true, calc_on_every_tick=false,
commission_type=strategy.commission.cash_per_contract, commission_value=0.62,
slippage=1)
// ── Inputs ──────────────────────────────────────────────
maxRangeTicks = input.int(40, "Max Bar Range (ticks)", minval=1,
tooltip="Skip entries on bars wider than this — wide bars mean bad fills.")
// ── Wide-range skip ──────────────────────────────────────
barRangeTicks = (high - low) / syminfo.mintick
rangeOK = barRangeTicks <= maxRangeTicks
// ── Entry logic (replace with your own conditions) ───────
longCondition = ta.crossover(ta.ema(close, 9), ta.ema(close, 21)) and rangeOK
shortCondition = ta.crossunder(ta.ema(close, 9), ta.ema(close, 21)) and rangeOK
if longCondition
strategy.entry("Long", strategy.long)
strategy.exit("Long Exit", "Long", profit=20, loss=10)
if shortCondition
strategy.entry("Short", strategy.short)
strategy.exit("Short Exit", "Short", profit=20, loss=10)
// Visual
bgcolor(not rangeOK ? color.new(color.purple, 90) : na, title="Wide Bar — Skipped")
Settings
commission_value— $ per contract per side, set in the declaration. Default 0.62 (typical micro all-in).slippage— ticks lost per fill, set in the declaration. Default 1.maxRangeTicks— skip entries on bars wider than this. Default 40.
How to use
- Copy the
strategy()declaration parameters into your own script — commission and slippage must be literals there, not inputs. - Set
commission_valueto your broker’s all-in round-turn cost divided by two. - Replace the EMA-crossover placeholder with your own entry conditions.
- Re-run your backtest after adding costs; if the edge disappears, it was never there.
Frequently Asked Questions
How do I add slippage to a Pine Script backtest?
Set the slippage parameter in the strategy() declaration — it is measured in ticks and applied against you on every entry and exit. Add commission_type=strategy.commission.cash_per_contract with your per-side cost as commission_value. Both must be literal values in the declaration; Pine does not allow inputs there.
Why does my backtest differ from live fills?
Zero-cost backtests assume you get the exact print of the signal bar close. Live, you pay the spread, commissions, and occasional slippage on fast bars. One tick of slippage plus commissions can flip a marginal scalping system from profitable to losing — which is why every strategy should be validated with these costs on before it touches an eval.
Related
Want a complete eval-ready strategy backtested with real costs?
This snippet is a building block. Our paid scripts are complete strategies — entries, filters, kill switch, and EOD flatten, all backtested with commission and slippage on 3 years of MES/NQ data. Instant delivery from $50.
View Plans — From $50