Strategy · Pine Script v4 · On the chart

MTF Bollindger Bands Strategy [Alorse]

by Alorse

This strategy plots multi-timeframe Bollinger Bands on the price chart and trades mean reversion around them.

View on GitHub

Repaints

GitHub stars330on the repository
Pine versionv4older, still runs
Lines of code81
Settings15inputs
LicenceNone statedfile: MPL-2.0
Last update2 months ago25 Aug 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 strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

81 lines of Pine Script v4 from strategies/MTF BB.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

MTF BB.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@author Alorse
//@version=4
strategy("MTF Bollindger Bands Strategy [Alorse]", shorttitle="MTF BB Strategy [Alorse]", overlay=true, pyramiding=0, initial_capital=1000, currency=currency.USD, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.01) 

txtVer = "1.0.3"
version = input(title="Version", type=input.string, defval=txtVer, options=[txtVer], tooltip="This is informational only, nothing will change.")
res = input(title="Multi Timeframe", type=input.resolution, defval="60", tooltip="Timeframe recommended  is 5 minutes, if you want to use the strategy in higher timeframes, please also increase Multi Timeframe.\nEg: If you want to use the strategy in 1 hour, put the Multi Timeframe in 4 hours.")

// MTF Bollinger Bands
bbGroup = "MTF Bollindger Bands"
src = input(title="Source", type=input.source, defval=close, group=bbGroup)
length = input(20, title="Length", group=bbGroup)
mult = input(2.0, title="StdDev", minval=0.001, maxval=5, group=bbGroup)

basis = sma(src, length)
dev = mult * stdev(src, length)
upper = basis + dev
lower = basis - dev

mtfBasis = security(syminfo.tickerid, res, basis)
mtfUpper = security(syminfo.tickerid, res, upper)
mtfLower = security(syminfo.tickerid, res, lower)

pb1 = plot(mtfUpper, color=color.new(color.teal, 50), title="Upper")
pb2 = plot(mtfLower, color=color.new(color.teal, 50), title="Lower")
fill(pb1, pb2, color=color.new(color.teal, 98), title="Background")
plot(mtfBasis, "Basis", color=color.new(color.orange, 60))

// MTF EMA
maGroup = "MTF Moving Average Filter"
hasMa = input(false, title="╔══════   Use MA   ══════╗", group=maGroup, tooltip="If Moving Average Filter is enabled, strategy only shows Long entries when the price is above the MA and Short entries when the price is below the MA.")
mtfType = input(title="MultiTimeframe MA Type", type=input.string, defval="EMA", options=["EMA", "SMA"], group=maGroup)
emaLen = input(title='MA Periods', minval=1, defval=200, group=maGroup)
ema = security(syminfo.tickerid, res, mtfType == "EMA" ? ema(close, emaLen) : sma(close, emaLen))

plot(hasMa ? ema : na, title="Moving Average", linewidth=1, color=color.new(color.blue, 10))
buyMaFilter = hasMa ? close > ema : true
sellMaFilter = hasMa ? close < ema : true

//Strategy
stratGroup = "Strategy"
showLong = input(true, title="Long entries", group=stratGroup)
showShort = input(false, title="Short entries", group=stratGroup)

buy = close < mtfLower and buyMaFilter
sell = close > mtfUpper and sellMaFilter

// Stop Loss
slGroup = "Stop Loss"
useSL = input(true, title="╔══════   Enable   ══════╗", group=slGroup, tooltip="If you are using this strategy for Scalping or Futures market, we do not recommend using Stop Loss.")
SLbased = input(title="Based on", type=input.string, defval="Percent", options=["ATR", "Percent"], group=slGroup, tooltip="ATR: Average True Range\nPercent: eg. 5%.")
multiATR = input(6.0, title="ATR   Mult", type=input.float, group=slGroup, inline="atr")
lengthATR = input(14, title="Length", type=input.integer, group=slGroup, inline="atr")
SLPercent = input(6, title="Percent", type=input.float, group=slGroup) * 0.01

longStop = 0.0
shortStop = 0.0

if SLbased == "ATR"
    longStop := valuewhen(buy, low, 0) - (valuewhen(buy, rma(tr(true), lengthATR), 0) * multiATR)
    longStopPrev = nz(longStop[1], longStop)
    longStop := close[1] > longStopPrev ? max(longStop, longStopPrev) : longStop

    shortStop := (valuewhen(sell, rma(tr(true), lengthATR), 0) * multiATR) + valuewhen(sell, high, 0)
    shortStopPrev = nz(shortStop[1], shortStop)
    shortStop := close[1] > shortStopPrev ? max(shortStop, shortStopPrev) : shortStop
if SLbased == "Percent"
    longStop  := strategy.position_avg_price * (1 - SLPercent)
    shortStop := strategy.position_avg_price * (1 + SLPercent)

if showLong
    strategy.entry("Long", true, when=buy)
    strategy.close("Long", when=close > mtfUpper)
    if useSL
        strategy.exit("SL", "Long", stop=longStop)
if showShort
    strategy.entry("Short", false, when=sell)
    strategy.close("Short", when=close < mtfLower)
    if useSL
        strategy.exit("SL", "Short", stop=shortStop)

Overview

MTF Bollindger Bands Strategy [Alorse] enters long when the close is below the higher-timeframe lower band and, if enabled, short when the close is above the upper band. An optional higher-timeframe EMA or SMA can require price to sit on the matching side of that average. Stops default to a percentage of price, or an ATR multiple, and can be switched off. Size is a fixed percent of equity. There is no take-profit rule. Higher-timeframe values update before that bar closes, so bands and signals can change.

Details

Published by

Alorse on GitHub

Type

Strategy, Pine Script v4

Draws

On the price chart

Category

Volatility & bands, Mean reversion, Moving averages

Markets

Any symbol

Timeframes

M1-M5, M15-H1

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)

Topics

pinescriptpinescript-indicatorspinescript-strategiestrading-strategiestradingviewtradingview-alerttradingview-strategy

Settings

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

SettingTypeDefaultVariable
VersionstringtxtVerversion
Multi Timeframetimeframe60res
Sourcesourceclosesrc
Lengthint20length
StdDevfloat2.0mult
╔══════   Use MA   ══════╗boolfalsehasMa
MultiTimeframe MA TypestringEMAmtfType
MA Periodsint200emaLen
Long entriesbooltrueshowLong
Short entriesboolfalseshowShort
╔══════   Enable   ══════╗booltrueuseSL
Based onstringPercentSLbased
Show all 15 settings
SettingTypeDefaultVariable
ATR   Multfloat6.0multiATR
Lengthint14lengthATR
Percentfloat6SLPercent
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

218 stars

The Mean Reversion Strategy Template calculates a baseline mean using SMA, EMA, or VWAP alongside upper and lower deviation bands based on standard deviation, ATR, or percentage offsets. It generates long and short entries when price touches, bounces from, or

Strategy v6 Mean reversionMoving averages
Updated 3 months ago · None stated GitHub repository of Mean Reversion Strategy Template
106 stars

This strategy backtests a long Bollinger Band deal on the price chart for 3Commas-style bots. It plots short and long simple moving averages, offset bands, and the average entry price of the open deal. A long starts when price crosses the lower band inside a c

Strategy v4 Volatility & bandsMoving averages
Updated 2 years ago · GPL-3.0 GitHub repository of 3Commas Bollinger Strategy

Bollinger Bands Strategy

by cklose2000
105 stars

This strategy generates buy and sell signals based on Bollinger Band crossovers combined with a simple moving average trend filter. It enters a long position when price crosses above the lower band while trading above the SMA filter line, and enters a short po

Strategy v5 Volatility & bandsMoving averages
Updated 1 year ago · None stated GitHub repository of Bollinger Bands Strategy

Bollinger Bands Strategy

by TWODS-CAPITAL
37 stars

This strategy executes trades based on price interactions with standard Bollinger Bands plotted on the price chart. It calculates a simple moving average basis line along with upper and lower standard deviation bands. Long entries are triggered when price cros

Strategy v5 Volatility & bandsMean reversion
Updated 3 years ago · MIT GitHub repository of Bollinger Bands Strategy
9 stars

This strategy scaffold calculates a standard Volume Weighted Average Price (VWAP) alongside upper and lower volatility bands set 1.5 ATRs away. It generates long entry signals when a confirmed bar closes below the lower band and short entry signals when a conf

Strategy v5 Mean reversionMoving averages
Updated 12 days ago · Custom licence GitHub repository of Nexural VWAP Reversion Scaffold
5 stars

Greeffer's Scalping Strategy combines multi-timeframe Exponential Moving Averages, Bollinger Bands, and Donchian Channels to execute dip-buying trades directly on the price chart. It triggers long entries when price dips below both the lower Donchian Channel a

Strategy v5 Moving averagesVolatility & bands
Updated 4 years ago · None stated GitHub repository of Greeffer's Scalping Strategy

More strategys

Trade Your Charts With a Regulated Broker

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

Compare brokers