Strategy · Pine Script v5 · On the chart

Bollinger Squeeze Breakout

by LePotoRico2105

Calculates standard Bollinger Bands and identifies volatility contraction when band width drops below a percentage of its moving average.

View on GitHub

GitHub stars4on the repository
Pine versionv5runs as is
Lines of code51
Settings4inputs
LicenceMIT
Last update6 months ago3 Apr 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

51 lines of Pine Script v5 from strategies/bollinger_squeeze.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

bollinger_squeeze.pine
// This Pine Script v5 strategy is licensed under the MIT License
// Tested with Backtrex (https://backtrex.com) on 10 years of M1 data
// Anti-repainting: all references use close[1]

//@version=5
strategy("Bollinger Squeeze Breakout", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=2)

// Inputs
bbLen = input.int(20, "Bollinger Band Length")
bbMult = input.float(2.0, "Bollinger Band Multiplier")
squeezeFactor = input.float(0.75, "Squeeze Threshold (% of avg width)")
lookback = input.int(20, "Squeeze Lookback Period")

// Bollinger Bands (using close[1] to prevent repainting)
basis = ta.sma(close[1], bbLen)
dev = bbMult * ta.stdev(close[1], bbLen)
upper = basis + dev
lower = basis - dev

// Band width and squeeze detection
bbWidth = upper - lower
avgWidth = ta.sma(bbWidth, lookback)
isSqueeze = bbWidth < avgWidth * squeezeFactor
wasSqueeze = isSqueeze[1]

// Breakout signals (squeeze must have occurred in recent bars)
recentSqueeze = ta.barssince(isSqueeze) < 5
longSignal = recentSqueeze and close[1] > upper and close[2] <= upper[1]
shortSignal = recentSqueeze and close[1] < lower and close[2] >= lower[1]

// Risk calculation
distToMiddle = math.abs(close - basis)

// Execute trades
if longSignal
    sl = basis
    tp = close + distToMiddle * 2
    strategy.entry("Long", strategy.long)
    strategy.exit("Long Exit", "Long", stop=sl, limit=tp)

if shortSignal
    sl = basis
    tp = close - distToMiddle * 2
    strategy.entry("Short", strategy.short)
    strategy.exit("Short Exit", "Short", stop=sl, limit=tp)

// Plots
plot(basis, "SMA 20", color=color.gray)
plot(upper, "Upper Band", color=color.blue)
plot(lower, "Lower Band", color=color.blue)
bgcolor(isSqueeze ? color.new(color.yellow, 90) : na, title="Squeeze Zone")

Overview

The strategy enters long positions when a confirmed candle closes above the upper band following a recent squeeze, and enters short positions on a close below the lower band. Exits are defined by a stop loss at the Bollinger middle band and a take profit target set at twice the distance to that middle band. It plots the bands and highlights squeeze conditions directly on the price chart.

Details

Published by

LePotoRico2105 on GitHub

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Volatility & bands, Breakout

Markets

Forex

Timeframes

M15-H1

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.

Topics

algorithmic-tradingbacktestingforexictpinescriptsmcstrategytradingtradingview

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Bollinger Band Lengthint20bbLen
Bollinger Band Multiplierfloat2.0bbMult
Squeeze Threshold (% of avg width)float0.75squeezeFactor
Squeeze Lookback Periodint20lookback
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

257 stars

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

Strategy v6 Volatility & bandsSupport & resistance
Updated 6 weeks ago · None stated GitHub repository of V1.6 ES - Futures 1M No Repaint
218 stars

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

Strategy v6 BreakoutVolatility & bands
Updated 3 months ago · None stated GitHub repository of Breakout Strategy Template

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

2 Candles Inside ATR

by hasnocool
106 stars

2 Candles Inside ATR is an overlay script that measures volatility compression across two consecutive candles. It calculates a smoothed Average True Range (ATR) and compares it against the combined high and low range of the current and previous bars. When the

Strategy v4 Volatility & bandsBreakout
Updated 2 years ago · GPL-3.0 GitHub repository of 2 Candles Inside ATR

均线突破交易系统

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