strategy · on price · open source

Range Filter Strategy

Deepwick · @deepwickv1Updated 2 Oct 2026▲ 0 users
Running…
Loading chart…

About

Range Filter trend strategy based on a smoothed range channel. Long when price breaks above the upper smoothed boundary; exit on the reverse break.

  • The smoothed boundaries are plotted on the chart for visual confirmation.
  • Combine with a higher-timeframe trend filter for cleaner entries.
  • Use the Length and Smooth Length to tune sensitivity.

Source code

//@version=5
strategy("Range Filter Strategy", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.05)
len = input.int(20, "Range Length", minval=5, maxval=50, step=1)
smoothLen = input.int(14, "Smooth Length", minval=5, maxval=50, step=1)
stopPct = input.float(2, "Stop Loss %", minval=0.5, maxval=10, step=0.1)
tpPct = input.float(6, "Take Profit %", minval=0, maxval=20, step=0.1)
hh = ta.highest(high, len)
ll = ta.lowest(low, len)
range = hh - ll
upperBand = close + range * 0.5
lowerBand = close - range * 0.5
smoothUpper = ta.sma(upperBand, smoothLen)
smoothLower = ta.sma(lowerBand, smoothLen)
longSignal = strategy.position_size == 0 and ta.crossover(close, smoothUpper)
exitSignal = strategy.position_size > 0 and ta.crossunder(close, smoothLower)
if longSignal
    strategy.entry("Long", strategy.long)
if strategy.position_size > 0
    strategy.exit("Risk", stop=stopPct > 0 ? strategy.position_avg_price * (1 - stopPct / 100) : na, limit=tpPct > 0 ? strategy.position_avg_price * (1 + tpPct / 100) : na)
if exitSignal
    strategy.close("Long", comment="Range break down")
p_Smooth_Upper = plot(smoothUpper, "Smooth Upper", color=color.green, linewidth=1)
p_Smooth_Lower = plot(smoothLower, "Smooth Lower", color=color.red, linewidth=1)
plotshape(longSignal, "Long", style=shape.triangleup, location=location.belowbar, color=color.green, text="L")
plotshape(exitSignal, "Exit", style=shape.triangledown, location=location.abovebar, color=color.red, text="X")
alertcondition(longSignal, "Range filter long entry", "Price broke above the smoothed upper band")
alertcondition(exitSignal, "Range filter exit", "Price broke below the smoothed lower band")

Comments

No comments yet.

Sign in to comment.