Simple Bollinger Band Width Percentile
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © EltAlt
//@version=5

// I'm a big fan of The_Caretaker's BBWP and wanted to add it as a volatility indicator to some of my scripts, but since it is over
// 100 lines of code (plus spacing and comments) I wanted to find if there was a simpler way to get comparable results. SBBWP uses Pine 5 
// built in functions that I don't believe were available when The_Caretaker wrote BBPW. The main limitations compared to The_Caretaker's
// version is that it can only use SMA as its Basis Type and the colors are also not as pretty. I have not included alerts or scale lines
// since I'm not trying to replace BBWP, just give a simple example that you can easily build in to your scripts.
// Full credit and respect to The_Caretaker!

indicator('Simple Bollinger Band Width Percentile', 'SBBWP', overlay=false, format=format.percent, precision=2, max_bars_back = 1000)

i_priceSrc = input.source ( close,   'Price Source',                                 inline='1', group='BBWP Properties')
i_bbwpLen  = input.int    ( 13,      'Length',       minval=1,                       inline='2', group='BBWP Properties')
i_bbwpLkbk = input.int    ( 252,     '     Lookback',  minval=1,                     inline='2', group='BBWP Properties')
i_ma1On    = input.bool   ( true,    '',                                             inline='3', group='BBWP Properties')
i_ma1Type  = input.string ( 'SMA',   'BBWP MA Type', options=['SMA', 'EMA', 'VWMA'], inline='3', group='BBWP Properties')
i_ma1Len   = input.int    ( 5,       'Length',       minval=1,                       inline='3', group='BBWP Properties')

bbwp       = ta.percentrank(ta.bbw(i_priceSrc, i_bbwpLen, 1), i_bbwpLkbk) 
c_bbwp     = bbwp >= 50 ? color.from_gradient(bbwp, 50, 100, #0AFF00, #FF2900): color.from_gradient(bbwp, 0, 49, #0000FF, #0AFF00)
bbwpMA1    = i_ma1On ? i_ma1Type == 'VWMA' ? ta.vwma ( bbwp, i_ma1Len ) : i_ma1Type == 'EMA' ? ta.ema ( bbwp, i_ma1Len ) : ta.sma ( bbwp, i_ma1Len ) : na

p_bbwp     = plot ( bbwp,    'BBWP',    c_bbwp,  2, plot.style_line, editable=false )
p_ma1      = plot ( bbwpMA1, 'BBWP MA', #FFFFFF, 1, plot.style_line, 0 )