This overlay strategy plots Bollinger Bands from a simple moving average and standard deviation, plus a separate moving-average line and a background color when that average sits outside the bands.
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
43 lines of Pine Script v4 from Advanced Bollinger Bands Strategy.pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.
Advanced Bollinger Bands Strategy.pine
//@version=4
//
strategy("Advanced Bollinger Bands Strategy", overlay=true)
//BB Values
wall1= input(defval=true,title="===BB Values===",type=input.bool)
source = input(defval=close,title="BB Source",type=input.source)
length = input(20,title="BB Length", minval=1)
mult = input(2.0,title="BB Multiplier",minval=0.001, maxval=50)
basis = sma(source, length)
dev = mult * stdev(source, length)
upper = basis + dev
lower = basis - dev
offset = input(0, " BB Offset", type = input.integer, minval = -500, maxval = 500)
plot(basis, "Basis", color=#872323, offset = offset)
p1 = plot(upper, "Upper", color=color.teal, offset = offset)
p2 = plot(lower, "Lower", color=color.teal, offset = offset)
fill(p1, p2, title = "Background", color=#198787, transp=95)
//Moving Average Values
wall2= input(defval=true,title="===MA Values===",type=input.bool)
nfl= input(defval=14,title="Moving Average Period",type=input.integer,minval=1,maxval=100)
source1= input(defval=close,title="Moving Average Source",type=input.source)
noisefilter= sma(source1,nfl)
plot(noisefilter,style=plot.style_line,linewidth=2,color=color.yellow,title=" Moving Average Filter")
bgcolor(noisefilter<lower?color.green:noisefilter>upper?color.red:na,title="Moving Average Filter")
//Strategy Conditions
wall3= input(defval=true,title="===Strategy Conditions===",type=input.bool)
bl= input(defval=false,title="Exit at Basis Line?",type=input.bool)
nflb= input(defval=false,title="Use Moving Average Filter?",type=input.bool)
//Strategy Condition
buyEntry = crossover(source, lower)
sellEntry = crossunder(source, upper)
if (nflb?(crossover(source,lower) and noisefilter<lower): crossover(source, lower))
strategy.entry("BBandLE", strategy.long, oca_name="BollingerBands", oca_type=strategy.oca.cancel, comment="BBandLE")
else
strategy.cancel(id="BBandLE")
if (nflb?(crossunder(source,lower) and noisefilter>upper): crossunder(source, lower))
strategy.entry("BBandSE", strategy.short, oca_name="BollingerBands", oca_type=strategy.oca.cancel, comment="BBandSE")
else
strategy.cancel(id="BBandSE")
strategy.close_all(when=bl?crossover(source,basis) or crossunder(source,basis):crossover(source,upper) or crossunder(source,lower))
Expand (45 lines)
Overview
Advanced Bollinger Bands Strategy opens a long when the source crosses above the lower band and a short when it crosses below the lower band, optionally requiring the moving average to be outside the bands. Positions close when price crosses the opposite band, or the basis if that exit option is on. There is no stop loss.
Bollinger Bands with adjustable length and multiplier
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
===BB Values===
bool
true
wall1
BB Source
source
close
source
BB Length
int
20
length
BB Multiplier
float
2.0
mult
BB Offset
int
0
offset
===MA Values===
bool
true
wall2
Moving Average Period
int
14
nfl
Moving Average Source
source
close
source1
===Strategy Conditions===
bool
true
wall3
Exit at Basis Line?
bool
false
bl
Use Moving Average Filter?
bool
false
nflb
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.
This strategy plots Bollinger Bands on the price chart with adjustable length, standard deviation, and offset. It defines buy and sell zones using candle size percentage and body size thresholds. Long positions enter when the lower zone is breached without con
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 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