Indicator · Pine Script v5 · Own pane

Bollinger Band Width Percentile and BB Breakout

by hayashi19· code credited to HayashiHitsuji

This indicator ranks the current Bollinger Band width against its own recent history to give a percentile from zero to one hundred, drawn in a separate pane with a moving average of that value and extreme high and low levels at 98 and 2 by default.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code96
Settings12inputs
LicenceNone statedfile: MPL-2.0
Last update4 years ago10 Jan 2022

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

96 lines of Pine Script v5 from indicator/INDICATOR Bollinger Band Width Percentile and BB Breakout (林19).pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

INDICATOR Bollinger Band Width Percentile and BB Breakout (林19).pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HayashiHitsuji

// |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| //
// Credit to The_Caretaker
// Message from The_Caretaker
// "Much respect to John A Bollinger the creator of Bollinger Bands® and Bollinger Band Width indicators.
// Feel free to reuse or develop this script further, please drop me a note below if you find it useful."

// I just modify the bbw to more simple in code and add a bb%b to check the breakout.
// |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| //

//@version=5
indicator('INDICATOR: Bollinger Band Width Percentile and BB Breakout (林19)', 'BBWP And BB%B Breakout (林19)', format = format.percent, max_bars_back = 1000)

////////////////////////////////////////////////////////////////////////////////
// INDICATOR INPUT /////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

// Input: bbw input value ======================================================

bbw             = input.bool    (true, "BBW ==================================")
bbpb            = input.bool    (true, "BB%B =================================")

i_priceSrc      = input.source  ( close, 'Price Source')
i_basisType     = input.string  ( 'SMA', 'Basis Type', options = ['SMA', 'EMA', 'VWMA'])
i_bbwpLen       = input.int     ( 13, 'Length')
i_bbwpLkbk      = input.int     ( 252, 'Lookback')

i_ma1On         = input.bool    ( true, '', inline = '1')
i_ma1Type       = input.string  ( 'SMA', 'MA 1 Type', options = ['SMA', 'EMA', 'VWMA'], inline = '1')
i_c_ma1         = input.color   ( #FFFFFF, '', inline = '1')
i_ma1Len        = input.int     ( 5, 'Length')

i_upperLevel    = input.int     ( 98, 'Extreme High')
i_lowerLevel    = input.int     ( 2, 'Extreme Low')

// ========================================================================== //

////////////////////////////////////////////////////////////////////////////////
// INDICATOR CALCULATION & ALGORITHM ///////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

// Calc: create the bbw calculation function ===================================

f_maType ( _price, _len) =>
     ta.vwma ( _price, _len )

f_bbwp ( _price, _bbwLen, _bbwpLen) =>
    float _basis = f_maType ( _price, _bbwLen)
    float _dev = ta.stdev ( _price, _bbwLen )
    _bbw = ( _basis + _dev - ( _basis - _dev )) / _basis
    _bbwSum = 0.0
    _len = bar_index < _bbwpLen ? bar_index : _bbwpLen
    for _i = 1 to _len by 1
        _bbwSum += ( _bbw[_i] > _bbw ? 0 : 1 )
        _bbwSum
    _return = bar_index >= _bbwLen ? ( _bbwSum / _len) * 100 : na
    _return
    
// ========================================================================== //

// Algo: set the bbw value =====================================================
bbwp        = f_bbwp ( i_priceSrc, i_bbwpLen, i_bbwpLkbk)
bbwpMA1     = i_ma1On ? f_maType ( bbwp, i_ma1Len) : na
hiAlrtBar   = bbwp >= i_upperLevel ? bbwp : na
loAlrtBar   = bbwp <= i_lowerLevel ? bbwp : na
// ========================================================================== //

// Algo: set the bb%b value ====================================================
[mid, upper, lower] = ta.bb(i_priceSrc, i_bbwpLkbk, 1)
bbr     = (i_priceSrc - lower)/(upper - lower) * 100
// ========================================================================== //

////////////////////////////////////////////////////////////////////////////////
// PLOT INDICATOR //////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

// Plot: indicator bbw =========================================================

p_scaleHi   = hline ( 100,  'Scale High', #ff0000, hline.style_solid )
p_midLine   = hline ( 50,   'Mid-Line', color.silver, hline.style_dashed )
p_scaleLo   = hline ( 0,    'Scale Low', #0000ff, hline.style_solid )

p_bbwp      = plot ( bbw ? bbwp : na,      'BBWP',         color = color.from_gradient(bbwp, 15, 85, color.blue, color.red), style = plot.style_line, linewidth = 2, editable = false)
p_hiAlrt    = plot ( hiAlrtBar, 'Extreme Hi',   color = color.red, style = plot.style_columns, histbase = 0, editable = false )
p_loAlrt    = plot ( loAlrtBar, 'Extreme Lo',   color = color.blue, style = plot.style_columns, histbase = 100, editable = false )
p_ma1       = plot ( bbw ? bbwpMA1 : na,   'MA 1',         color = color.silver, style = plot.style_circles)

// ========================================================================== //

// Plot: indicator BB%B ========================================================

plot(bbpb ? bbr : na, "Bollinger Bands %B", color = bbr >= 0 and bbr <= 100 ? bbr < 50 ? color.fuchsia : color.aqua : color.yellow) 

// ========================================================================== //

Overview

Bollinger Band Width Percentile and BB Breakout also calculates Bollinger percent B to help judge breakouts. Length, lookback, price source, moving average type and thresholds are adjustable. It is adapted from another author's band width percentile script, which the code credits.

Details

Published by

hayashi19 on GitHub

Code credit

HayashiHitsuji, as written in the script's header

Draws

Separate pane below the chart

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
BBW ==================================booltruebbw
BB%B =================================booltruebbpb
Price Sourcesourceclosei_priceSrc
Basis TypestringSMAi_basisType
Lengthint13i_bbwpLen
Lookbackint252i_bbwpLkbk
i_ma1Onbooltruei_ma1On
MA 1 TypestringSMAi_ma1Type
i_c_ma1color#FFFFFFi_c_ma1
Lengthint5i_ma1Len
Extreme Highint98i_upperLevel
Extreme Lowint2i_lowerLevel
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

Chop and Explode

by capissimo
114 stars

This indicator measures market consolidation and breakout phases by computing a scaled Relative Strength Index (RSI) on normalized price data. It identifies chop zones when the RSI oscillates between 40 and 60, highlighting tight consolidation between 45 and 5

Indicator v5 Oscillators & momentumVolatility & bands
Updated 1 year ago · None stated GitHub repository of Chop and Explode

Squeeze Momentum

by mihakralj
99 stars

This indicator plots Squeeze Momentum in a separate pane. It compares Bollinger Bands with a Keltner Channel to mark a squeeze when the bands sit inside the channel. Momentum is a linear regression of price relative to a Donchian midline and an SMA, shown as a

Indicator v6 Volatility & bandsOscillators & momentum
Updated 4 weeks ago · Apache-2.0 GitHub repository of Squeeze Momentum

ATR Breakout

by TamaraKGreen
46 stars

This indicator plots the 100-bar percent rank of ATR in a separate pane and draws a midline at 50. It colors that midline green, red, or gray by comparing the close with the open a set number of bars earlier against a multiple of ATR across a lookback window.

Indicator v5 Volatility & bandsBreakout
Updated 7 weeks ago · None stated GitHub repository of ATR Breakout

Renko Acceleration

by f-cksociety
11 stars

Calculates the time elapsed between consecutive bars to measure market speed and volatility, designed primarily for Renko chart blocks where bar duration varies. In Momentum mode, it contrasts the current duration against a 100-period moving average baseline,

Indicator v4 Volatility & bandsOscillators & momentum
Updated 4 years ago · MPL-2.0 GitHub repository of Renko Acceleration

CRT + Turtle Soup Scanner

by alboogycOdR
6 stars

Calculates CRT session range boundaries and detects Turtle Soup setups to identify potential breakout signals. The script tracks session high, low, and midpoint values to determine intraday bias while measuring price break distances against ATR thresholds. It

Indicator v5 SMC / ICT / price actionSessions & time
Updated 7 months ago · None stated GitHub repository of CRT + Turtle Soup Scanner

EMA Breakout

by jerryinyang
5 stars

This indicator calculates and plots the exponential moving average on the chart, along with Keltner Channel bands. It applies optional filters based on Keltner Channel and Money Flow to identify breakout signals. Breakouts occur when price crosses the EMA with

Indicator v4 TrendMoving averages
Updated 2 years ago · None stated GitHub repository of EMA Breakout

More Volatility & bands scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

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

Compare brokers