Strategy · Pine Script v6 · On the chart

BB Squeeze Breakout

by shubhamtaywade82

A strategy that trades the release of a Bollinger Band and Keltner Channel squeeze.

View on GitHub

Archived by its author

GitHub stars1on the repository
Pine versionv6current
Lines of code52
Settings10inputs
LicenceNone statedfile: MPL-2.0
Last update5 months ago31 May 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

52 lines of Pine Script v6 from strategies/agent-examples/bb-squeeze-breakout-strategy.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

bb-squeeze-breakout-strategy.pine
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Strategy derived from pinescript-agents/examples/simple/bollinger-squeeze.pine for backtesting in pinescripts-collections.

//@version=6
strategy("BB Squeeze Breakout [Agent Example]", shorttitle="BBSqzStr", overlay=true, margin_long=100, margin_short=100, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.1)

bbLength = input.int(20, "BB Length", minval=1, group="Bands")
bbMult = input.float(2.0, "BB Multiplier", minval=0.1, step=0.1, group="Bands")
kcLength = input.int(20, "KC Length", minval=1, group="Keltner")
kcMult = input.float(1.5, "KC Multiplier", minval=0.1, step=0.1, group="Keltner")

useStop = input.bool(true, "Use ATR Stop / Target", group="Risk")
atrLen = input.int(14, "ATR Length", minval=1, group="Risk")
slMult = input.float(1.5, "Stop ATR Mult", minval=0.1, step=0.1, group="Risk")
tpMult = input.float(2.5, "Target ATR Mult", minval=0.1, step=0.1, group="Risk")
bullColorInput = input.color(#089981, "Bullish Color", group="Visuals")
bearColorInput = input.color(#f23645, "Bearish Color", group="Visuals")

basis = ta.sma(close, bbLength)
dev = bbMult * ta.stdev(close, bbLength)
bbUpper = basis + dev
bbLower = basis - dev
kcMA = ta.ema(close, kcLength)
kcRange = ta.atr(kcLength)
kcUpper = kcMA + kcRange * kcMult
kcLower = kcMA - kcRange * kcMult

squeeze = bbUpper < kcUpper and bbLower > kcLower
noSqueeze = bbUpper > kcUpper or bbLower < kcLower

donchianMid = (ta.highest(high, kcLength) + ta.lowest(low, kcLength)) / 2
momentum = close - donchianMid

releaseLong = noSqueeze and not noSqueeze[1] and momentum > 0
releaseShort = noSqueeze and not noSqueeze[1] and momentum < 0

atr = ta.atr(atrLen)

if releaseLong
    strategy.entry("Long", strategy.long)
if releaseShort
    strategy.entry("Short", strategy.short)

if useStop
    if strategy.position_size > 0
        strategy.exit("LX", "Long", stop=strategy.position_avg_price - slMult * atr, limit=strategy.position_avg_price + tpMult * atr)
    if strategy.position_size < 0
        strategy.exit("SX", "Short", stop=strategy.position_avg_price + slMult * atr, limit=strategy.position_avg_price - tpMult * atr)

plotshape(releaseLong, "Long Release", shape.triangleup, location.belowbar, color.new(bullColorInput, 0), size=size.tiny, text="L")
plotshape(releaseShort, "Short Release", shape.triangledown, location.abovebar, color.new(bearColorInput, 0), size=size.tiny, text="S")
bgcolor(squeeze ? color.new(#2a2e39, 93) : na, title="Squeeze Regime")

Overview

When the bands move back outside the channel, it buys if momentum, measured against the Donchian midline, is positive and sells short if it is negative. An optional ATR-based stop loss and take profit are attached to each position, 1.5 and 2.5 ATR by default. Position size is ten percent of equity, with a commission assumption in the settings.

Details

Published by

shubhamtaywade82 on GitHub

Type

Strategy, Pine Script v6

Draws

On the price chart

Category

Volatility & bands, Breakout

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
BB Lengthint20bbLength
BB Multiplierfloat2.0bbMult
KC Lengthint20kcLength
KC Multiplierfloat1.5kcMult
Use ATR Stop / TargetbooltrueuseStop
ATR Lengthint14atrLen
Stop ATR Multfloat1.5slMult
Target ATR Multfloat2.5tpMult
Bullish Colorcolor#089981bullColorInput
Bearish Colorcolor#f23645bearColorInput
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 5 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