What this builds
- Upper and lower Bollinger Bands (SMA +/- 2 standard deviations)
- Basis (middle) line
- Band shading using
fill()on the plot handles - Bandwidth oscillator in a sub-pane
Full script
With %B and bandwidth in sub-pane
Key notes
ta.bbreturnsNaNduring the warmup period (firstlength - 1bars). Guard withif (na(bb.upper)) return.fill(upperP, lowerP, { color })shades between the two plot handles - pass the return values fromplot()directly.- The
%Bvalue is 1.0 at the upper band, 0.0 at the lower band, and 0.5 at the middle. Values outside [0, 1] indicate price is beyond the bands.
Related pages
Volatility
ta.bb and ta.keltner reference.
hline, fill, bgcolor
fill() syntax and budget.
NaN handling
Why na(bb.upper) check is needed.