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
70 lines of Pine Script v4 from Bollinger Bands Breakout Strategy.pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.
Bollinger Bands Breakout Strategy.pine
//@version=4
//┌───── •••• ─────┐//
// TradeChartist //
//└───── •••• ─────┘//
//Bollinger Bands is a classic indicator that uses a simple moving average of 20 periods along with upper and lower bands that are 2 standard deviations away from the basis line.
//These bands help visualize price volatility and trend based on where the price is in relation to the bands.
//This Bollinger Bands filter plots a long signal when price closes above the upper band and plots a short signal when price closes below the lower band.
//It doesn't take into account any other parameters such as Volume/RSI/fundamentals etc, so user must use discretion based on confirmations from another indicator or based on fundamentals.
//This filter's default is 55 SMA and 1 standard deviation, but can be changed based on asset type
//It is definitely worth reading the 22 rules of Bollinger Bands written by John Bollinger.
strategy(shorttitle="BB Breakout Strategy", title="Bollinger Bands Filter", overlay=true,
pyramiding=1, currency=currency.NONE ,
initial_capital = 100000, default_qty_type = strategy.percent_of_equity,
default_qty_value=100)
src = input(close, title = "Source")
length = input(55, minval=1, title = "SMA length")// 20 for classis Bollinger Bands SMA line (basis)
mult = input(1., minval=0.236, maxval=2, title="Standard Deviation")//2 for Classic Bollinger Bands //Maxval = 2 as higher the deviation, higher the risk
basis = sma(src, length)
dev = mult * stdev(src,length)
Show = input("Both", options = ["Longs Only", "Shorts Only", "Both"], title = "Trade Type")
CC = input(true, "Color Bars")
upper = basis + dev
lower = basis - dev
//Conditions for Long and Short - Extra filter condition can be used such as RSI or CCI etc.
short = src<lower// and rsi(close,14)<40
long = src>upper// and rsi(close,14)>60
L1 = barssince(long)
S1 = barssince(short)
longSignal = L1<S1 and not (L1<S1)[1]
shortSignal = S1<L1 and not (S1<L1)[1]
//Plots and Fills
////Long/Short shapes with text
// plotshape(S1<L1 and not (S1<L1)[1]?close:na, text = "sᴇʟʟ", textcolor=#ff0100, color=#ff0100, style=shape.triangledown, size=size.small, location=location.abovebar, transp=0, title = "SELL", editable = true)
// plotshape(L1<S1 and not (L1<S1)[1]?close:na, text = "ʙᴜʏ", textcolor = #008000, color=#008000, style=shape.triangleup, size=size.small, location=location.belowbar, transp=0, title = "BUY", editable = true)
// plotshape(shortSignal?close:na, color=#ff0100, style=shape.triangledown, size=size.small, location=location.abovebar, transp=0, title = "Short Signal", editable = true)
// plotshape(longSignal?close:na, color=#008000, style=shape.triangleup, size=size.small, location=location.belowbar, transp=0, title = "Long Signal", editable = true)
p1 = plot(upper, color=#ff0000, display=display.all, transp=75, title = "Upper Band")
p2 = plot(lower, color=#008000, display=display.all, transp=75, title = "Lower Band")
p = plot(basis, color=L1<S1?#008000:S1<L1?#ff0000:na, linewidth=2, editable=false, title="Basis")
fill(p,p1, color=color.teal, transp=85, title = "Top Fill") //fill for basis-upper
fill(p,p2, color=color.orange, transp=85, title = "Bottom Fill")//fill for basis-lower
//Barcolor
bcol = src>upper?color.new(#8ceb07,0):
src<lower?color.new(#ff0000,0):
src>basis?color.green:
src<basis?color.red:na
barcolor(CC?bcol:na, editable=false, title = "Color Bars")
// //Alerts ---- // Use 'Once per bar close'
// alertcondition(condition=longSignal, title="Long - BB Filter", message='BB Filter Long @ {{close}}') // Use 'Once per bar close'
// alertcondition(condition=shortSignal, title="Short - BB Filter", message='BB Filter Short @ {{close}}') // Use 'Once per bar close'
Notestart1 = input(true, "╔═══ Time Range to BackTest ═══╗")
// === INPUT BACKTEST RANGE ===
FromMonth = input(defval=1, title="From Month", minval=1, maxval=12)
FromDay = input(defval=1, title="From Day", minval=1, maxval=31)
FromYear = input(defval=2018, title="From Year", minval=2015)
ToMonth = input(defval=1, title="To Month", minval=1, maxval=12)
ToDay = input(defval=1, title="To Day", minval=1, maxval=31)
ToYear = input(defval=9999, title="To Year", minval=2010)
// === FUNCTION EXAMPLE ===
start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window
finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window
window() =>
time >= start and time <= finish ? true : false
if(window() and (Show == "Longs Only" or Show == "Both"))
strategy.entry("Long", long=true, when = longSignal)
strategy.close("Long", when = shortSignal, comment = "Close Long")
if(window() and (Show == "Shorts Only" or Show == "Both"))
strategy.entry("Short", long=false, when = shortSignal)
strategy.close("Short", when = longSignal, comment = "Close Short")
Expand (116 lines)
Overview
Bollinger Bands Filter treats a close above the upper band as a long condition and a close below the lower band as a short condition, then colors the basis by which condition happened more recently. Length and multiplier change the basis period and band width, and the trade-type input limits activity to longs, shorts, or both. A date range restricts the backtest window, and optional bar coloring is available. No stop loss or take profit is defined.
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
Source
source
close
src
SMA length
int
55
length
Standard Deviation
input
1.
mult
Trade Type
string
Both
Show
Color Bars
bool
true
CC
╔═══ Time Range to BackTest ═══╗
bool
true
Notestart1
From Month
int
1
FromMonth
From Day
int
1
FromDay
From Year
int
2018
FromYear
To Month
int
1
ToMonth
To Day
int
1
ToDay
To Year
int
9999
ToYear
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.
Calculates standard Bollinger Bands and identifies band expansion breakouts where upper and lower bands move in opposite directions. It enters long positions when price breaks above the upper band with a high candle body percentage, and enters short positions
This strategy plots VWMA-based Bollinger bands on the 1-minute ES futures price chart for volatility analysis and potential breakout identification. It calculates dynamic long and short stop loss and take profit levels using percentage inputs relative to entry
This strategy detects price breakouts above rolling highest highs or below lowest lows over a customizable lookback period. It includes optional volume spike and ATR volatility threshold filters, along with bar confirmation rules. Positions are taken upon conf
This price-chart strategy plots a simple moving average and upper and lower bands built from the standard deviation of close, scaled by separate offset inputs. It enters a fixed quantity long when close crosses above the upper band and a fixed quantity short w
This strategy trades long breakouts of a fast and a slow moving average on the price chart. It opens a long position when price crosses above either average, using a percentage of equity as size. It reduces the position by a set share when the high reaches the
This strategy plots exponential-moving-average Bollinger bands on the price chart and calculates RSI. It opens a long position when close crosses above the upper band while RSI is at or above a threshold, the date filter allows trading, and no position is open