// 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.
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
BB Length
int
20
bbLength
BB Multiplier
float
2.0
bbMult
KC Length
int
20
kcLength
KC Multiplier
float
1.5
kcMult
Use ATR Stop / Target
bool
true
useStop
ATR Length
int
14
atrLen
Stop ATR Mult
float
1.5
slMult
Target ATR Mult
float
2.5
tpMult
Bullish Color
color
#089981
bullColorInput
Bearish Color
color
#f23645
bearColorInput
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
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
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