Indicator · Pine Script v4 · On the chart

Bollinger Bands Squeeze / Trend + EMAs

by krosenfeld7

This indicator plots multi-timeframe Exponential Moving Averages alongside double Bollinger Bands directly on the price chart.

View on GitHub

GitHub stars5on the repository
Pine versionv4older, still runs
Lines of code110
Settings17inputs
LicenceApache-2.0
Last update3 years ago12 May 2023

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

110 lines of Pine Script v4 from BB Squeeze + Trend + EMAs/BB_Squeeze_Trend+EMAs.txt, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

BB_Squeeze_Trend+EMAs.txt
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © krosenfeld7

//@version=4
study(shorttitle="EMAs + BB Squeeze + Trend", title="Bollinger Bands Squeeze / Trend + EMAs", overlay=true)

// EMAs
trendModeStr   = input(title = "Trend or Squeeze",     type = input.string,     defval = "Trend", options = ["Trend", "Squeeze"])
ema1Length     = input(title = "EMA Short Length",     type = input.integer,    defval = 20, minval = 1)
ema1Input      = input(title = "EMA Short Source",     type = input.source,     defval = close)
ema1Resolution = input(title = "EMA Short Resolution", type = input.resolution, defval = "D")
ema2Length     = input(title = "EMA Med Length",       type = input.integer,    defval = 50, minval = 1)
ema2Input      = input(title = "EMA Med Source",       type = input.source,     defval = close)
ema2Resolution = input(title = "EMA Med Resolution",   type = input.resolution, defval = "D")
ema3Length     = input(title = "EMA Long Length",      type = input.integer,    defval = 100, minval = 1)
ema3Input      = input(title = "EMA Long Source",      type = input.source,     defval = close)
ema3Resolution = input(title = "EMA Long Resolution",  type = input.resolution, defval = "D")

ema1 = ema(ema1Input, ema1Length)
ema2 = ema(ema2Input, ema2Length)
ema3 = ema(ema3Input, ema3Length)

trendMode = trendModeStr == "Trend"

ema1Daily = security(syminfo.tickerid, ema1Resolution, ema1[barstate.isrealtime ? 1 : 0], barmerge.gaps_off, barmerge.lookahead_off)
ema2Daily = security(syminfo.tickerid, ema2Resolution, ema2[barstate.isrealtime ? 1 : 0], barmerge.gaps_off, barmerge.lookahead_off)
ema3Daily = security(syminfo.tickerid, ema3Resolution, ema3[barstate.isrealtime ? 1 : 0], barmerge.gaps_off, barmerge.lookahead_off)

plot(ema1Daily, "EMA Short", color = color.new(color.white, transp = trendMode ? 0 : 100), linewidth = 2)
plot(ema2Daily, "EMA Med",   color = color.new(color.blue, transp = trendMode ? 0 : 100),   linewidth = 2)
plot(ema3Daily, "EMA Long",  color = color.new(color.yellow, transp = trendMode ? 0 : 100), linewidth = 2)

emaLong  = ema1Daily > ema2Daily and ema2Daily > ema3Daily
emaShort = ema1Daily < ema2Daily and ema2Daily < ema3Daily

// Bollinger Bands
length1 = input(title = "BB MA Length", type = input.integer, defval = 20, minval = 1)
src1    = input(title = "BB MA Source", type = input.source,  defval = hlc3)
maType  = input(title = "BB MA Type",   type = input.string,  defval = "SMA", options = ["SMA", "EMA"])
mult1   = input(title = "BB 1 Std Dev Multiplier", type = input.float, defval = 2.0, minval = 0.001, maxval = 50)
mult2   = input(title = "BB 2 Std Dev Multiplier", type = input.float, defval = 1.0, minval = 0.001, maxval = 50)

bbLookback    = input(title = "BB Lookback",    type = input.integer, defval = 10, minval = 1)
displayLabels = input(title = "Display Labels", type = input.bool,    defval = true)

bbMa = sma(src1, length1)

if maType == "EMA"
    bbMa := ema(src1, length1)

plot(bbMa, "Basis", color = color.new(color.white, transp = 50), linewidth = 2)

// BB One
dev1 = mult1 * stdev(src1, length1)
upper1 = bbMa + dev1
lower1 = bbMa - dev1
p1 = plot(upper1, "Upper", color = color.new(color.green, transp = 50), linewidth = 2)
p2 = plot(lower1, "Lower", color = color.new(color.orange, transp = 50), linewidth = 2)

// BB Two
dev2 = mult2 * stdev(src1, length1)
upper2 = bbMa + dev2
lower2 = bbMa - dev2
p3 = plot(upper2, "Upper", color = color.new(color.green, transp = 50), linewidth = 2)
p4 = plot(lower2, "Lower", color = color.new(color.orange, transp = 50), linewidth = 2)

fill(p1, p3, title = "Background", color=color.new(color.green, transp = 75))
fill(p3, p4, title = "Background", color=color.new(color.white, transp = 100))
fill(p4, p2, title = "Background", color=color.new(color.orange, transp = 75))

bb1Upper = ema(upper1, bbLookback)
bb1Lower = ema(lower1, bbLookback)
bb2Upper = ema(upper2, bbLookback)
bb2Lower = ema(lower2, bbLookback)

bb1Contraction = upper1 < bb1Upper and lower1 > bb1Lower
bb2Contraction = upper2 < bb2Upper and lower2 > bb2Lower
 
bbLong  = close > upper1
bbShort = close < lower1

// Net Volume
higherVolume = volume > volume[1] and volume[1] > volume[2]

squeezeLong  = bb1Contraction and higherVolume and bbLong
squeezeShort = bb1Contraction and higherVolume and bbShort

trendLong  = emaLong  and bbLong
trendShort = emaShort and bbShort

if trendLong and displayLabels and trendMode
    label.new(x = bar_index, y = na, text = "Trend Long", yloc = yloc.abovebar, color = color.green)
if trendShort and displayLabels and trendMode
    label.new(x = bar_index, y = na, text = "Trend Short", yloc = yloc.abovebar, color = color.red)
if squeezeLong and displayLabels and not trendMode
    label.new(x = bar_index, y = na, text = "Squeeze Long", yloc = yloc.abovebar, color = color.green)
if squeezeShort and displayLabels and not trendMode
    label.new(x = bar_index, y = na, text = "Squeeze Short", yloc = yloc.abovebar, color = color.red)

if barstate.isrealtime and displayLabels
    label.new(x = bar_index, y = upper1, text = "+" + tostring(mult1), style = label.style_none, textcolor = color.white)
    label.new(x = bar_index, y = upper2, text = "+" + tostring(mult2), style = label.style_none, textcolor = color.white)
    label.new(x = bar_index, y = lower2, text = "-" + tostring(mult2), style = label.style_none, textcolor = color.white)
    label.new(x = bar_index, y = lower1, text = "-" + tostring(mult1), style = label.style_none, textcolor = color.white)

// Alerts
alertcondition(trendLong,   title = "Trend Long Signal",    message = "Long @ {{timenow}}: {{ticker}} -> {{interval}}: {{close}}")
alertcondition(trendShort,  title = "Trend Short Signal",   message = "Short @ {{timenow}}: {{ticker}} -> {{interval}}: {{close}}")
alertcondition(squeezeLong, title = "Squeeze Long Signal",  message = "Long @ {{timenow}}: {{ticker}} -> {{interval}}: {{close}}")
alertcondition(squeezeShort,title = "Squeeze Short Signal", message = "Short @ {{timenow}}: {{ticker}} -> {{interval}}: {{close}}")

Overview

Bollinger Bands Squeeze / Trend + EMAs calculates three EMAs (short, medium, and long term) fetched from configurable higher timeframes, alongside two sets of Bollinger Bands using either SMA or EMA basis with customizable standard deviation multipliers. Users can switch between Trend and Squeeze modes to toggle visibility and tailor visual analysis.

Details

Published by

krosenfeld7 on GitHub

Type

Indicator, Pine Script v4

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
Trend or SqueezestringTrendtrendModeStr
EMA Short Lengthint20ema1Length
EMA Short Sourcesourcecloseema1Input
EMA Short ResolutiontimeframeDema1Resolution
EMA Med Lengthint50ema2Length
EMA Med Sourcesourcecloseema2Input
EMA Med ResolutiontimeframeDema2Resolution
EMA Long Lengthint100ema3Length
EMA Long Sourcesourcecloseema3Input
EMA Long ResolutiontimeframeDema3Resolution
BB MA Lengthint20length1
BB MA Sourcesourcehlc3src1
Show all 17 settings
SettingTypeDefaultVariable
BB MA TypestringSMAmaType
BB 1 Std Dev Multiplierfloat2.0mult1
BB 2 Std Dev Multiplierfloat1.0mult2
BB Lookbackint10bbLookback
Display LabelsbooltruedisplayLabels
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