1Copy the scriptPress Copy script above, or the copy button on the code below.
2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.
Source Code
65 lines of Pine Script v6 from pinescript/indicators/ttm_squeeze_box.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.
ttm_squeeze_box.pine
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=6
indicator("TTM Squeeze Box", shorttitle="TTM SQZ Box", overlay=true, max_boxes_count=500)
// === Inputs ===
bbLen = input.int(20, "BB Length", minval=1, group="Bollinger Bands")
bbMult = input.float(2.0, "BB Multiplier", minval=0.1, step=0.1, group="Bollinger Bands")
kcLen = input.int(20, "KC Length", minval=1, group="Keltner Channel")
kcMult = input.float(1.5, "KC Multiplier", minval=0.1, step=0.1, group="Keltner Channel")
boxColor = input.color(color.yellow, "Box Border Color", group="Style")
boxWidth = input.int(1, "Box Border Width", minval=1, maxval=4, group="Style")
// === Bollinger Bands ===
basis = ta.sma(close, bbLen)
dev = bbMult * ta.stdev(close, bbLen)
bbUpper = basis + dev
bbLower = basis - dev
// === Keltner Channel ===
kma = ta.sma(close, kcLen)
krange = kcMult * ta.atr(kcLen)
kcUpper = kma + krange
kcLower = kma - krange
// === Squeeze Detection ===
// Squeeze is ON when Bollinger Bands are fully inside the Keltner Channel
squeezeOn = bbUpper < kcUpper and bbLower > kcLower
// Squeeze state transitions
squeezeStart = squeezeOn and not squeezeOn[1] // Rising edge: squeeze just turned on
squeezeEnd = not squeezeOn and squeezeOn[1] // Falling edge: squeeze just turned off
// === Box State Variables ===
var box sqBox = na // The currently active squeeze box
var float sqHi = na // Highest high during current squeeze
var float sqLo = na // Lowest low during current squeeze
// === Box Drawing Logic ===
if squeezeStart
// Open a new box at the start of each squeeze period
sqHi := high
sqLo := low
sqBox := box.new(
left = bar_index,
right = bar_index,
top = high,
bottom = low,
bgcolor = na, // Border only, no background fill
border_color = boxColor,
border_width = boxWidth
)
else if squeezeOn and not na(sqBox)
// Expand the box to track the full high/low range during the squeeze
sqHi := math.max(sqHi, high)
sqLo := math.min(sqLo, low)
box.set_top(sqBox, sqHi)
box.set_bottom(sqBox, sqLo)
box.set_right(sqBox, bar_index)
// When the squeeze ends, the box is frozen in place automatically —
// no further updates occur once squeezeOn is false.
// (We do not reset sqBox to na so the last box remains visible.)
Overview
A squeeze starts when the Bollinger Bands move fully inside the Keltner Channel. While it lasts, a border-only box grows to follow the highest high and lowest low, and it is left on the chart when the squeeze ends. Lengths and multipliers for both bands, plus box colour and width, are adjustable. It marks compression periods only, without direction, entries or exits.
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
bbLen
BB Multiplier
float
2.0
bbMult
KC Length
int
20
kcLen
KC Multiplier
float
1.5
kcMult
Box Border Color
color
color.yellow
boxColor
Box Border Width
int
1
boxWidth
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.
This overlay indicator marks Linda Raschke short-term bar setups on the price chart. It flags Turtle Soup and Turtle Soup plus one false breakouts of a prior extreme, 80-20 bars, inside bars, failed inside-bar breakouts, outside up and down bars, NR4 and NR7 c
Acceleration Bands is an overlay indicator that plots a volatility envelope around a central simple moving average. It calculates upper and lower boundary bands scaled by the high-to-low bar range relative to the midpoint price, smoothed by an SMA over a user-
This indicator calculates and plots Donchian Channels directly on the price chart. It determines the highest high and lowest low over a user-selected lookback period using an optimized monotonic deque algorithm, as well as a middle basis line computed as their
The Donchian Channels indicator plots the middle basis line as the average of the high and low over the user-defined period. It also draws the upper band as the highest high and the lower band as the lowest low across the same period on the price chart. The ma
This indicator plots Donchian channel bands and a midline on the price chart. It finds the highest and lowest values over a user-set length, then draws the upper band, lower band, and their average as the basis. The source setting chooses high and low, or the
This indicator plots a Donchian channel on the price chart. It calculates the highest high and the lowest low over a user-set lookback, then draws those values as upper and lower bands with a midline equal to their average. The area between the bands is filled