Indicator · Pine Script v6 · Own pane

Bollinger %B (BBB)

by mihakralj

This indicator calculates the Bollinger Bands %B oscillator, measuring the position of price relative to the upper and lower Bollinger Bands.

View on GitHub

GitHub stars99on the repository
Pine versionv6current
Lines of code82
Settings3inputs
LicenceApache-2.0
Last update4 weeks ago27 Sep 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 indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

82 lines of Pine Script v6 from lib/oscillators/bbb/bbb.pine, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

bbb.pine
// Licensed under the Apache License, Version 2.0
// © mihakralj
//@version=6
indicator("Bollinger %B (BBB)", "BBB", overlay=false)

//@function Calculates Bollinger Bands %B oscillator
//@param source Series to calculate %B from
//@param period Lookback period for Bollinger Bands calculation
//@param multiplier Standard deviation multiplier for band width
//@returns Bollinger %B value (0 = lower band, 1 = upper band)
//@optimized Uses circular buffer SMA/StdDev with O(1) complexity per bar
bbb(series float source, simple int period, simple float multiplier) =>
    if period <= 0 or multiplier <= 0.0
        runtime.error("Period and multiplier must be greater than 0")

    var int p = 0
    var int head = 0
    var int count = 0
    var array<float> buffer = array.new_float(0)
    var float sum = 0.0
    var float sumSq = 0.0
    var string lastSymbol = ""
    var string lastTimeframe = ""

    string currentSymbol = syminfo.tickerid
    string currentTimeframe = timeframe.period
    bool needsReset = (p != period) or (currentSymbol != lastSymbol) or (currentTimeframe != lastTimeframe)

    if needsReset
        p := period
        head := 0
        count := 0
        buffer := array.new_float(p, na)
        sum := 0.0
        sumSq := 0.0
        lastSymbol := currentSymbol
        lastTimeframe := currentTimeframe

    float result = na

    if not na(source)
        float oldest = array.get(buffer, head)
        if not na(oldest)
            sum -= oldest
            sumSq -= oldest * oldest
        else
            count += 1

        sum += source
        sumSq += source * source
        array.set(buffer, head, source)
        head := (head + 1) % p

        int n = math.max(1, count)
        float basis = sum / n
        float variance = math.max(0.0, sumSq / n - basis * basis)
        float stddev = math.sqrt(variance)
        float dev = multiplier * stddev

        float upper = basis + dev
        float lower = basis - dev
        float bandWidth = upper - lower

        result := bandWidth > 0 ? (source - lower) / bandWidth : 0.5

    result

// Inputs
i_period = input.int(20, "Period", minval=1)
i_source = input.source(close, "Source")
i_multiplier = input.float(2.0, "StdDev Multiplier", minval=0.001, step=0.1)

// Calculation
result = bbb(i_source, i_period, i_multiplier)

// Plot
plot(result, "Bollinger %B", color=color.yellow, linewidth=2)
hline(1.0, "Upper Band Level", color=color.gray, linestyle=hline.style_dashed)
hline(0.8, "Overbought", color=color.red, linestyle=hline.style_dotted)
hline(0.5, "Midline", color=color.gray, linestyle=hline.style_solid)
hline(0.2, "Oversold", color=color.green, linestyle=hline.style_dotted)
hline(0.0, "Lower Band Level", color=color.gray, linestyle=hline.style_dashed)

Overview

A value of 1 represents the upper band, 0 represents the lower band, and 0.5 indicates the middle band. Standard reference levels for overbought and oversold conditions are plotted alongside boundary markers. Inputs allow users to adjust the lookback period, input source, and standard deviation multiplier. It plots as a continuous line in a separate pane.

Details

Published by

mihakralj on GitHub

Type

Indicator, Pine Script v6

Draws

Separate pane below the chart

Category

Oscillators & momentum, Volatility & bands

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.

Topics

algorithmic-tradingcsharpdotnetpinescriptpythonquantitative-financesimdta-libtechnical-analysistechnical-indicators

Settings

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

SettingTypeDefaultVariable
Periodint20i_period
Sourcesourceclosei_source
StdDev Multiplierfloat2.0i_multiplier
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

TTM Squeeze

by Alorse
330 stars

This indicator plots a TTM Squeeze momentum oscillator in a separate pane. It compares a Bollinger Band offset to a Keltner Channel offset and marks the zero line white when the difference is non-negative and black when the bands are inside the channel. A line

Indicator v5 Oscillators & momentumVolatility & bands
Updated 2 months ago · None stated GitHub repository of TTM Squeeze

Function - Logistic Equation

by ricardosantos79
261 stars

Calculates dynamic modeling values by applying a mathematical logistic differential equation to price change over a specified lookback length. It measures price momentum relative to the recent high-low price range. The script plots positive and negative logist

Indicator v5 Oscillators & momentumVolatility & bands
Updated 4 years ago · None stated GitHub repository of Function - Logistic Equation

MACD-V

by harryguiacorn
219 stars

MACD-V is a volatility-normalized momentum indicator based on research by Alex Spiroglou. It calculates the standard MACD histogram and normalizes it by dividing by the Average True Range (ATR). Plotted in a separate pane as a colored histogram, it includes us

Indicator v5 Oscillators & momentumVolatility & bands
Updated 1 year ago · None stated GitHub repository of MACD-V
218 stars

This indicator detects a Bollinger Band squeeze when the bands sit inside Keltner Channels, marking low volatility. It plots red dots during a squeeze and green dots when the bands are outside, in a separate pane. An optional histogram shows momentum as the cl

Indicator v6 Volatility & bandsOscillators & momentum
Updated 3 months ago · None stated GitHub repository of Bollinger Band Squeeze Detector
147 stars

This indicator generates trade entry signals and target levels optimized for 1-hour timeframes, designed for external signal extraction. It calculates 7-period and 16-period EMAs alongside 10-period RSI and ATR filters. Long signals occur when the fast EMA cro

Indicator v5 TrendOscillators & momentum
Updated 6 weeks ago · MIT GitHub repository of Signal Strategy 100 Candles of 1H Timeframe

Bubbles Indicator

by capissimo
114 stars

The Bubbles Indicator measures volatility squeezes and expansion bursts using Bollinger Bands combined with a sinusoid oscillator and RSI divergence. It processes price data through scaled Bollinger Bands to highlight volatility changes while confirming trend

Indicator v4 Volatility & bandsOscillators & momentum Repaints
Updated 1 year ago · None stated GitHub repository of Bubbles Indicator

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers