1Copy the scriptPress Copy script above, or the copy button on the code below.
2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
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.
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
Version
string
txtVer
version
Multi Timeframe
timeframe
60
res
Source
source
close
src
Length
int
20
length
StdDev
float
2.0
mult
╔══════ Use MA ══════╗
bool
false
hasMa
MultiTimeframe MA Type
string
EMA
mtfType
MA Periods
int
200
emaLen
Long entries
bool
true
showLong
Short entries
bool
false
showShort
╔══════ Enable ══════╗
bool
true
useSL
Based on
string
Percent
SLbased
Show all 15 settings
Setting
Type
Default
Variable
ATR Mult
float
6.0
multiATR
Length
int
14
lengthATR
Percent
float
6
SLPercent
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.
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
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
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
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
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
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