Indicator · Pine Script v6 · On the chart

Range Filtered Trend Signals

by ivansoft· code credited to AlgoAlpha

An indicator on the price chart that smooths price with a Kalman filter, builds a band around it from a weighted average of bar ranges multiplied by a deviation setting, and runs a SuperTrend on the filtered line.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code111
Settings8inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago1 Mar 2025

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

111 lines of Pine Script v6 from examples/range_filter_trend.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

range_filter_trend.pine
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AlgoAlpha

//@version=6
indicator("Range Filtered Trend Signals [AlgoAlpha]", "AlgoAlpha - Range Filtered", true)

groupKalman = "Kalman Filter"
kalmanAlpha = input.float(0.01, title="Kalman Alpha", tooltip="The Alpha parameter controls the smoothing factor of the Kalman filter. A smaller value results in more smoothing, while a larger value makes the filter more responsive to price changes.", group=groupKalman)
kalmanBeta = input.float(0.1, title="Kalman Beta", tooltip="The Beta parameter influences the rate of change in the Kalman filter. It adjusts the filter's sensitivity to trend changes.", group=groupKalman)
kalmanPeriod = input.int(77, title="Kalman Period", tooltip="The Period defines the number of bars used in the Kalman filter calculation, affecting the filter's responsiveness to market movements.", group=groupKalman)
dev = input.float(1.2, title="Deviation", tooltip="The Deviation parameter sets the multiplier for the deviation from the trend line, affecting the width of the trend band.", group=groupKalman)

groupSupertrend = "Supertrend"
supertrendFactor = input.float(0.7, title="Supertrend Factor", tooltip="This Factor determines the multiplier for the ATR in the Supertrend calculation, affecting the distance of the trend line from the price.", group=groupSupertrend)
supertrendAtrPeriod = input.int(7, title="ATR Period", tooltip="The ATR Period specifies the number of bars used to calculate the Average True Range, which is a component of the Supertrend indicator.", group=groupSupertrend)

groupColors = "Colors"
green = input.color(#00ffbb, title="Bullish Color", tooltip="This color is used to indicate a bullish trend in the chart.", group=groupColors)
red = input.color(#ff1100, title="Bearish Color", tooltip="This color is used to indicate a bearish trend in the chart.", group=groupColors)

kalman(a, b, alpha, beta) =>
    var float v1 = na
    var float v2 = 1.0
    var float v3 = alpha * b
    var float v4 = 0.0
    var float v5 = na

    if na(v1)
        v1 := a[1]

    v5 := v1
    v4 := v2 / (v2 + v3)
    v1 := v5 + v4 * (a - v5)
    v2 := (1 - v4) * v2 + beta / b

    v1

pine_supertrend(k, factor, atrPeriod) =>
    src = k
    atr = ta.atr(atrPeriod)
    upperBand = src + factor * atr
    lowerBand = src - factor * atr
    prevLowerBand = nz(lowerBand[1])
    prevUpperBand = nz(upperBand[1])

    lowerBand := lowerBand > prevLowerBand or k[1] < prevLowerBand ? lowerBand : prevLowerBand
    upperBand := upperBand < prevUpperBand or k[1] > prevUpperBand ? upperBand : prevUpperBand
    int _direction = na
    float superTrend = na
    prevSuperTrend = superTrend[1]
    if na(atr[1])
        _direction := 1
    else if prevSuperTrend == prevUpperBand
        _direction := k > upperBand ? -1 : 1
    else
        _direction := k < lowerBand ? 1 : -1
    superTrend := _direction == -1 ? lowerBand : upperBand
    [superTrend, _direction]

k = kalman(close, kalmanPeriod, kalmanAlpha, kalmanBeta)

[supertrend, direction] = pine_supertrend(k, supertrendFactor, supertrendAtrPeriod)

vola = ta.wma(high-low, 200)

upper = k+vola*dev
lower = k-vola*dev

midbody = math.avg(close, open)

var trend = 0
if close > upper
    trend := 1
else if close < lower
    trend := -1

ktrend = 0
if direction < 0
    ktrend := 1
else if direction > 0
    ktrend := -1

t = 70
t_ = 20

p1 = plot(ktrend * trend == 1 ? k : na, color=color.gray, style = plot.style_linebr, linewidth = 3)

m = plot(midbody, color=color.gray, display = display.none)

up = plot(trend == -1 or ktrend * trend == -1 ? upper : na, color=ktrend * trend == -1 ? color.gray : ktrend == -1 ? color.new(red, t) : color.gray, style = plot.style_circles)

lo = plot(trend == 1 or ktrend * trend == -1 ? lower : na, color=ktrend * trend == -1 ? color.gray : ktrend == 1 ? color.new(green, t) : color.gray, style = plot.style_circles)

plotchar(ta.crossover(ktrend * trend, 0) ? k : na, location = location.absolute, color=trend == 1 ? green : red, char="◉", size=size.tiny)

x = color.new(chart.bg_color, 80)
x_ = color.new(trend == -1 ? red : green, t)

fill(p1, m, k, midbody, x, x_)

fill(up, lo, ktrend * trend == -1 ? color.new(color.gray, 90) : na, "Range")

barcolor(ktrend * trend == -1 ? color.new(color.gray, t_) : trend == 1 ? color.new(green, t_) : color.new(red, t_))

////////////////////////Alerts
alertcondition(ta.crossover(ktrend * trend, 0), title="Market Trending", message="The market is trending {{trend == 1 ? 'up' : 'down'}}")
alertcondition(ta.crossunder(ktrend * trend, 0), title="Market Ranging", message="The market is ranging")
alertcondition(ta.crossover(trend, 0), title="Market Short Term Bullish", message="The market is bullish short term")
alertcondition(ta.crossunder(trend, 0), title="Market Short Term Bearish", message="The market is bearish short term")
alertcondition(ta.crossover(ktrend, 0), title="Market Long Term Bullish", message="The market is bullish long term")
alertcondition(ta.crossunder(ktrend, 0), title="Market Long Term Bearish", message="The market is bearish long term")

Overview

Trend is set when the close moves outside the band, and the filtered line is coloured bullish or bearish when the band trend and the SuperTrend agree. Settings cover the Kalman alpha, beta and period, the deviation, the SuperTrend factor and ATR period, and colours.

Details

Published by

ivansoft on GitHub

Code credit

AlgoAlpha, as written in the script's header

Type

Indicator, Pine Script v6

Draws

On the price chart

Category

Trend, Volatility & bands

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

pinescripttradingvew

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Kalman Alphafloat0.01kalmanAlpha
Kalman Betafloat0.1kalmanBeta
Kalman Periodint77kalmanPeriod
Deviationfloat1.2dev
Supertrend Factorfloat0.7supertrendFactor
ATR Periodint7supertrendAtrPeriod
Bullish Colorcolor#00ffbbgreen
Bearish Colorcolor#ff1100red
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

Chandelier Exit

by everget
948 stars

This indicator plots an ATR-based Chandelier Exit trailing stop on the price chart. It tracks a long stop under recent highs and a short stop above recent lows, then switches direction when price crosses the opposite stop. Buy and sell labels mark those flips.

Indicator v6 Volatility & bandsTrend Repaints
Updated 11 days ago · GPL-3.0 GitHub repository of Chandelier Exit

The Momentum Setup is a multi-indicator visual tool designed for trend and momentum analysis directly on the price chart. It combines Relative Strength Index (RSI), Money Flow Index (MFI), Commodity Channel Index (CCI), Supertrend, and a 250-bar percentage cha

Indicator v5 Oscillators & momentumTrend
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup - Ankush Bajaj Momentum Investing Setup
330 stars

Range Filter [Alorse] is a technical indicator overlay for TradingView designed to filter market volatility and identify price trends. It calculates a smoothed range based on historical volatility using a configurable sampling period and multiplier applied to

Indicator v5 TrendVolatility & bands
Updated 2 months ago · None stated GitHub repository of Range Filter [Alorse]
318 stars

This indicator calculates an ATR trailing stop line derived from the highest high minus an average true range multiple over a specified lookback period. It plots the trailing stop line directly on the price chart and colors price bars based on whether the clos

Indicator v5 TrendVolatility & bands
Updated 2 months ago · MIT GitHub repository of ATR Trailing Stoploss

NVDA Trend Ribbon

by longsurf-ai
277 stars

This indicator plots three exponential moving averages and ATR volatility envelopes on the price chart. It compares the fast and slow EMAs to shade a trend ribbon and marks confirmed upward and downward crossovers with triangle labels. Settings change the thre

Indicator v6 TrendMoving averages
Updated 2 days ago · MIT GitHub repository of NVDA Trend Ribbon

[RS]Donchian Fans

by ricardosantos79
261 stars

This indicator plots Donchian Channel boundary lines along with dynamic fan projections directly on the price chart. It calculates the highest high, lowest low, and median price over a customizable lookback period. Whenever price establishes a new channel high

Indicator v4 Support & resistanceTrend
Updated 4 years ago · None stated GitHub repository of [RS]Donchian Fans

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers