Strategy · Pine Script v5 · On the chart

Bollinger Band Squeeze - Volatility Breakout

by Basant1Saini· code credited to TradingTools

This strategy detects low-volatility consolidation by identifying when Bollinger Bands contract inside Keltner Channels.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code81
Settings11inputs
LicenceNone statedfile: MPL-2.0
Last update7 months ago4 Mar 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 v5 from 8_Bollinger_Squeeze/8_Bollinger_Squeeze.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

8_Bollinger_Squeeze.pine
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0
// © TradingTools

//@version=5
strategy("Bollinger Band Squeeze - Volatility Breakout", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)

// ─── Inputs ───
bbLength        = input.int(20, "BB Length", group="Bollinger Bands")
bbMult          = input.float(2.0, "BB StdDev Multiplier", step=0.1, group="Bollinger Bands")
kcLength        = input.int(20, "Keltner Channel Length", group="Keltner Channel")
kcMult          = input.float(1.5, "KC ATR Multiplier", step=0.1, group="Keltner Channel")
momLength       = input.int(12, "Momentum Length", group="Momentum")
useVolConfirm   = input.bool(true, "Require Volume Confirmation", group="Filters")
volLength       = input.int(20, "Volume MA Length", group="Filters")
volMult         = input.float(1.2, "Volume Multiplier", step=0.1, group="Filters")
atrLength       = input.int(14, "ATR Length", group="Risk Management")
atrMult         = input.float(2.0, "ATR Multiplier for SL", step=0.1, group="Risk Management")
rrRatio         = input.float(2.5, "Risk:Reward Ratio", step=0.1, group="Risk Management")

// ─── Bollinger Bands ───
bbBasis = ta.sma(close, bbLength)
bbDev   = ta.stdev(close, bbLength) * bbMult
bbUpper = bbBasis + bbDev
bbLower = bbBasis - bbDev

// ─── Keltner Channel ───
kcBasis = ta.ema(close, kcLength)
kcAtr   = ta.atr(kcLength)
kcUpper = kcBasis + kcAtr * kcMult
kcLower = kcBasis - kcAtr * kcMult

// ─── Squeeze Detection ───
// Squeeze = BB inside KC (low volatility)
squeeze     = bbLower > kcLower and bbUpper < kcUpper
noSqueeze   = not squeeze
squeezeFire = noSqueeze and squeeze[1]  // squeeze just released

// ─── Momentum (linear regression) ───
mom = ta.linreg(close - bbBasis, momLength, 0)
momUp   = mom > 0 and mom > mom[1]
momDown = mom < 0 and mom < mom[1]

// ─── Volume ───
volMA   = ta.sma(volume, volLength)
volOK   = useVolConfirm ? volume > volMA * volMult : true

// ─── ATR ───
atrVal  = ta.atr(atrLength)

// ─── Entry Conditions ───
longCondition  = squeezeFire and momUp and volOK
shortCondition = squeezeFire and momDown and volOK

// ─── Execute Trades ───
if longCondition and strategy.position_size == 0
    sl = close - atrVal * atrMult
    tp = close + (close - sl) * rrRatio
    strategy.entry("Squeeze Long", strategy.long)
    strategy.exit("Squeeze Long Exit", "Squeeze Long", stop=sl, limit=tp)

if shortCondition and strategy.position_size == 0
    sl = close + atrVal * atrMult
    tp = close - (sl - close) * rrRatio
    strategy.entry("Squeeze Short", strategy.short)
    strategy.exit("Squeeze Short Exit", "Squeeze Short", stop=sl, limit=tp)

// ─── Plotting ───
plot(bbUpper, "BB Upper", color=color.new(color.blue, 30))
plot(bbBasis, "BB Basis", color=color.new(color.blue, 60))
plot(bbLower, "BB Lower", color=color.new(color.blue, 30))

// Squeeze dots on the zero line
bgcolor(squeeze ? color.new(color.orange, 85) : na, title="Squeeze Active")

plotshape(longCondition, title="Squeeze Long", location=location.belowbar, style=shape.diamond, color=color.lime, size=size.small, text="SQZ")
plotshape(shortCondition, title="Squeeze Short", location=location.abovebar, style=shape.diamond, color=color.red, size=size.small, text="SQZ")

// ─── Alerts ───
alertcondition(squeeze and not squeeze[1], title="Squeeze Started", message="BB Squeeze: Volatility compression detected")
alertcondition(longCondition, title="Squeeze Long Breakout", message="BB Squeeze: Bullish breakout from squeeze")
alertcondition(shortCondition, title="Squeeze Short Breakout", message="BB Squeeze: Bearish breakout from squeeze")

Overview

When the squeeze releases, it evaluates linear regression momentum and optional volume spikes to confirm breakouts. Long signals occur on rising positive momentum, while short signals trigger on falling negative momentum. Positions are managed with ATR-based stop losses and configurable risk-to-reward profit targets, plotting Bollinger Bands and trade markers on the price chart.

Details

Published by

Basant1Saini on GitHub

Code credit

TradingTools, as written in the script's header

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Volatility & bands, Breakout, Oscillators & momentum, Moving averages

Markets

Stocks

Timeframes

M15-H1, H4 and above

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
BB Lengthint20bbLength
BB StdDev Multiplierfloat2.0bbMult
Keltner Channel Lengthint20kcLength
KC ATR Multiplierfloat1.5kcMult
Momentum Lengthint12momLength
Require Volume ConfirmationbooltrueuseVolConfirm
Volume MA Lengthint20volLength
Volume Multiplierfloat1.2volMult
ATR Lengthint14atrLength
ATR Multiplier for SLfloat2.0atrMult
Risk:Reward Ratiofloat2.5rrRatio
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

106 stars

Calculates a momentum squeeze using Bollinger Bands and Keltner Channels alongside a trend-filtering moving average. It generates long and short entries based on squeeze momentum bar conditions and an optional pre-squeeze dot count filter. Exits can trigger on

Strategy v5 Oscillators & momentumVolatility & bands
Updated 2 years ago · GPL-3.0 GitHub repository of Athena Momentum Squeeze - Autoview

Bollinger bands examples

by tradingstrategy-ai
53 stars

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

Strategy v5 Volatility & bandsOscillators & momentum
Updated 3 years ago · None stated GitHub repository of Bollinger bands examples

VAMA Bands(BreakOut)+!

by ymlee1104
1 stars

This strategy trades breakouts using VAMA Bands on the price chart. It supports long and short entries with filters from VAMA, MACD, DI, ADX, OBV and optional entry limits. Positions exit on opposite trend, force close periods or flip signals. Main settings ad

Strategy v5 BreakoutMoving averages
Updated 7 months ago · None stated GitHub repository of VAMA Bands(BreakOut)+!
330 stars

This strategy plots MACD line, signal line, Bollinger Bands, and RSI on the price chart. It signals long entries on MACD crossover above signal with RSI below 50 and price below BB basis, plus optional DMI filter. Short entries are available but disabled by de

Strategy v5 Oscillators & momentumVolatility & bands
Updated 2 months ago · None stated GitHub repository of MACD + BB + RSI [Alorse]

Bollinger Breakout

by hackingthemarkets
189 stars

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

Strategy v4 Volatility & bandsBreakout
Updated 3 years ago · None stated GitHub repository of Bollinger Breakout

均线突破交易系统

by tangly1024
76 stars

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

Strategy v5 TrendMoving averages
Updated 4 months ago · None stated GitHub repository of 均线突破交易系统

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