Pine Script v5 · Indicator
Bollinger Band Width and %B
A Pine Script v5 indicator built on BB (close, 20) and BBW (close, 20). It draws 2 lines, 2 level lines and bar colouring in its own pane. It declares 1 alert. 4 settings; defaults include Source close, Length 20 and Standard deviations 2.0.
Generated from the script's code by a template: every clause is a fact read from the source. Not written by a person or a model, and not taken from the author's description.
- Does Bollinger Band Width and %B repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 300 of 300 bars.
- How many settings does Bollinger Band Width and %B have?
- 4
- Defaults include Source close, Length 20, Standard deviations 2.0.
- What alerts does Bollinger Band Width and %B have?
- 1
- Squeeze.
- Does Bollinger Band Width and %B run in Pine Script v6?
- Yes
- Every plotted value identical on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. The v6 script can be downloaded below.
- Does Bollinger Band Width and %B draw on the price chart?
- No
- It draws in its own pane below the price.
BTCUSDT · 1D · default settings
- Bandwidth
- %B
- %B upper
- %B lower
- Bandwidth
- 0.0725
- on the last bar
- %B
- 0.7372
- on the last bar
- %B upper
- 1.0000
- on the last bar
- %B lower
- 0.0000
- on the last bar
The value of each line the script plots, on the last of 1,000 bars, with the default settings.
Review
What a careful reader of this script would point out, found by checking the source and the backtest.
Nothing to point out.
Settings
Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Source | close | source | basis upper lower width percent |
| Length | 20 | int · min 1 | basis upper lower width |
| Standard deviations | 2.0 | float · min 0.1 · step 0.1 | basis upper lower width |
| Squeeze lookback | 125 | int · min 10 | isSqueeze |
What it calculates
- BBSource (close), Length (20), Standard deviations (2.0)
basis · line 11 - BBSource (close), Length (20), Standard deviations (2.0)
upper · line 11 - BBSource (close), Length (20), Standard deviations (2.0)
lower · line 11 - BBWSource (close), Length (20), Standard deviations (2.0)
width · line 12
Signal flow
How price data and settings reach what is drawn. Each box is a name in the script, or several computed the same way; each arrow is a value one of them reads. Hover a box for its members.
What it draws
| Output | Call | Value | By default | Line |
|---|---|---|---|---|
| Bandwidth | plot | width | Shown | 18 |
| %B | plot | percent | Shown | 19 |
| hline | hline | 1 | Shown | 20 |
| hline | hline | 0 | Shown | 21 |
| Squeeze | bgcolor | isSqueeze ? color.new(#42a5f5, 85) : na | Shown | 22 |
Alerts
| Alert | Call | Message | Line |
|---|---|---|---|
| Squeeze | alertcondition | Bollinger Band width hit its lowest for the lookback | 24 |
Does it repaint?
No
No repainting patterns in the source, and the live replay matched history.
- ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- ClearIntrabar recalculationNo varip or realtime-only state. Values move while a bar forms and settle when it closes, as every indicator's do.
The checks read the source; the replay runs it. Together they catch the common causes, not every possible one.
Other markets
The same script with default settings on 8 Binance markets, each the most recent 1,000 bars.
| Market | Bandwidth | %B | %B upper | %B lower |
|---|---|---|---|---|
| BTCUSDT 1D | 0.0725 | 0.7372 | 1.0000 | 0.0000 |
| BTCUSDT 4h | 0.0481 | 0.9941 | 1.0000 | 0.0000 |
| ETHUSDT 1D | 0.1743 | 0.4226 | 1.0000 | 0.0000 |
| ETHUSDT 4h | 0.0670 | 0.8606 | 1.0000 | 0.0000 |
| SOLUSDT 1D | 0.2288 | 0.3855 | 1.0000 | 0.0000 |
| SOLUSDT 4h | 0.0650 | 0.8164 | 1.0000 | 0.0000 |
| BNBUSDT 1D | 0.1240 | 0.5071 | 1.0000 | 0.0000 |
| BNBUSDT 4h | 0.0602 | 0.8707 | 1.0000 | 0.0000 |
Each line's value on the last bar.
Similar scripts
Indicators and strategies built from the same calculations.
| Script | Shares | Type |
|---|---|---|
| Bollinger -B Candles Strategy by Trendoscope | bb lowest | v4 strategy |
| Bollinger Bands Modified (Stormer) by AugustoErni | bb lowest | v5 strategy |
| Directional Movement Index by wojak_bogdanoff | lowest bb | v5 strategy |
Upgrade and export
Written in Pine Script v5. The clean-v5 step types the settings and moves when = into if blocks, which v6 requires; it is run before and after like any upgrade.
- No v6-only features. v6 is not needed to run this script.
Clean v5
VerifiedNothing to change.
Does it run in Pine Script v6?
Yes
Every plotted value identical on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. It differs from the v5 version by 1 edit.
⋯// © Zeal Terminal-//@version=5+//@version=6indicator("Bollinger Band Width and %B", shorttitle = "BBW %B", overlay = false, precision = 3)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 13 ms | Yes 1,000 bars in 21 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
“Matches TradingView” needs this script's own output exported from TradingView and compared bar by bar, which has not been done for this script. The engine is built on OpenPineScript, whose conformance results are in its conformance report.
Source
Written by Zeal Terminal and published under the Mozilla Public License 2.0.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © Zeal Terminal//@version=5indicator("Bollinger Band Width and %B", shorttitle = "BBW %B", overlay = false, precision = 3) src = input.source(close, "Source")length = input.int(20, "Length", minval = 1)mult = input.float(2.0, "Standard deviations", minval = 0.1, step = 0.1)squeeze = input.int(125, "Squeeze lookback", minval = 10) [basis, upper, lower] = ta.bb(src, length, mult)width = ta.bbw(src, length, mult)percent = (src - lower) / (upper - lower) // Width at its lowest for the lookback: the bands are as narrow as they have been.isSqueeze = width <= ta.lowest(width, squeeze) plot(width, "Bandwidth", color = #42a5f5, linewidth = 2)plot(percent, "%B", color = #ffb74d)hline(1, "%B upper", color = color.new(#787b86, 50), linestyle = hline.style_dotted)hline(0, "%B lower", color = color.new(#787b86, 50), linestyle = hline.style_dotted)bgcolor(isSqueeze ? color.new(#42a5f5, 85) : na, title = "Squeeze") alertcondition(isSqueeze and not isSqueeze[1], "Squeeze", "Bollinger Band width hit its lowest for the lookback")