Skip to main content

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.bb returns NaN during the warmup period (first length - 1 bars). Guard with if (na(bb.upper)) return.
  • fill(upperP, lowerP, { color }) shades between the two plot handles - pass the return values from plot() directly.
  • The %B value 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.

Volatility

ta.bb and ta.keltner reference.

hline, fill, bgcolor

fill() syntax and budget.

NaN handling

Why na(bb.upper) check is needed.