Indicator · Pine Script v5 · On the chart

MTF Bollinger Bands Trend Stop

by TamaraKGreen· code credited to notprofessorgreen

This multi-timeframe indicator calculates an adaptive trailing stop line based on Bollinger Bands evaluated on a user-selected resolution.

View on GitHub

GitHub stars46on the repository
Pine versionv5runs as is
Lines of code85
Settings4inputs
LicenceNone statedfile: MPL-2.0
Last update7 weeks ago4 Sep 2026

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

85 lines of Pine Script v5 from MTF Bollinger Bands Trend Stop.txt, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

MTF Bollinger Bands Trend Stop.txt
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © notprofessorgreen

//@version=5
indicator("MTF Bollinger Bands Trend Stop", overlay=true)

// --- Input settings
tf_input = input.timeframe("", "Resolution", group="Timeframe Settings")
Length = input.int(20, 'Period', group = 'Settings')
Deviation = input.int(2, 'Standard Deviation', group = 'Settings')
MoneyRisk = input.float(1.00, 'Money Risk', group = 'Settings')

// --- Security function calls for MTF
htf_close = request.security(syminfo.tickerid, tf_input, close)
htf_high = request.security(syminfo.tickerid, tf_input, high)
htf_low = request.security(syminfo.tickerid, tf_input, low)

// --- Calculations on higher timeframe
htf_avg = request.security(syminfo.tickerid, tf_input, ta.sma(close, Length))
htf_dev = request.security(syminfo.tickerid, tf_input, ta.stdev(close, Length)) * Deviation
htf_smax = htf_avg + htf_dev
htf_smin = htf_avg - htf_dev

var trend = 0

if (htf_close > htf_smax[1])
    trend := 1
else if (htf_close < htf_smin[1])
    trend := -1

// Ensure bands don't retrace against trend
if (trend > 0 and htf_smin < htf_smin[1])
    htf_smin := htf_smin[1]
if (trend < 0 and htf_smax > htf_smax[1])
    htf_smax := htf_smax[1]

// Calculate buffer bands with money risk
htf_bsmax = htf_smax + 0.5 * (MoneyRisk - 1) * (htf_smax - htf_smin)
htf_bsmin = htf_smin - 0.5 * (MoneyRisk - 1) * (htf_smax - htf_smin)

if (trend > 0 and htf_bsmin < htf_bsmin[1])
    htf_bsmin := htf_bsmin[1]
if (trend < 0 and htf_bsmax > htf_bsmax[1])
    htf_bsmax := htf_bsmax[1]

var float TrendLine = na
var color r = na

if (trend > 0)
    TrendLine := htf_bsmin
    r := color.rgb(7, 124, 235)

if (trend < 0)
    TrendLine := htf_bsmax
    r := color.rgb(172, 12, 221)

// Trend calculations
var bool down = bool(na)
var bool up = bool(na)
stop_line = float(na)
line_color = color(na)

crossover_1 = ta.crossover(htf_high, htf_bsmax)
crossunder_1 = ta.crossunder(htf_low, htf_bsmin)

if crossover_1
    up := true
    down := false
    down
    
if crossunder_1
    up := false
    down := true
    down

change_up = down[1] and up
change_down = up[1] and down
stop_line := up ? htf_bsmin : down ? htf_bsmax : na

// Plots
line_color := stop_line == htf_bsmax ? color.rgb(172, 12, 221) : color.rgb(7, 124, 235)
p1 = plot(stop_line, style=plot.style_cross, linewidth=1, color=line_color)

plotshape(change_down ? htf_bsmax : na, style=shape.circle, location=location.absolute, size=size.tiny, color=color.rgb(172, 12, 221), transp=20)
plotshape(change_up ? htf_bsmin : na, style=shape.circle, location=location.absolute, size=size.tiny, color=color.rgb(7, 124, 235), transp=20)

Overview

MTF Bollinger Bands Trend Stop calculates standard deviation and simple moving average bands from the higher timeframe data, tracking trend shifts when price breaks out of the outer bands. The stop line trails the trend without retracing and adjusts according to a configurable risk multiplier, displaying colored crosses and trend reversal circles directly on the price chart.

Details

Published by

TamaraKGreen on GitHub

Code credit

notprofessorgreen, as written in the script's header

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Trend, Volatility & bands, Moving averages

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.

Settings

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

SettingTypeDefaultVariable
Resolutiontimeframetf_input
Periodint20Length
Standard Deviationint2Deviation
Money Riskfloat1.00MoneyRisk
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

318 stars

This indicator plots four customizable moving averages directly on the main price chart using default lengths of 3, 8, 21, and 50 periods. It fills the space between adjacent averages in green or maroon depending on trend alignment. Additionally, it computes B

Indicator v5 Moving averagesTrend
Updated 2 months ago · MIT GitHub repository of Multiple Moving Averages

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 yesterday · MIT GitHub repository of NVDA Trend Ribbon

Bollinger Bands Force

by harryguiacorn
219 stars

This indicator modifies standard Bollinger Bands to measure directional trend force based on line alignment. It calculates a simple moving average basis along with upper and lower volatility bands. When all three lines move upward or downward simultaneously, i

Indicator v5 Volatility & bandsTrend
Updated 1 year ago · None stated GitHub repository of Bollinger Bands Force

Adaptive MA Slope

by capissimo
114 stars

This indicator calculates Kaufman's Adaptive Moving Average (KAMA) on a chosen price source and determines trend direction based on the slope across a configurable lookback window. Signals can be filtered by short-term volatility expansion using ATR comparison

Indicator v4 Moving averagesTrend
Updated 1 year ago · None stated GitHub repository of Adaptive MA Slope

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