All indicators
Volatility / Own pane

Bollinger %B

Where price sits within its Bollinger Bands: 1 at the upper band, 0.5 at the basis, 0 at the lower band.

  • %B
  • Built into Spectral
  • Updates every tick
  • bollinger bands
  • percent b
  • %b
  • oscillator
  • overbought
  • oversold
  • volatility

By Spectral · v1.0.0

MIT-0: free to use, change and share, no attribution needed. What you build from it is yours.

Already in Spectral: add it from the Indicators toolbox. The file here is the same indicator written as a custom indicator, to change or learn from. Installed, it appears next to the built-in one, marked Custom.

Bollinger %B on a Spectral chart
Spectral chart with sample market data, default settings.

Overview

Bollinger %B shows where the price is relative to its Bollinger Bands as a single number: (price minus lower band) divided by (upper band minus lower band). A value of 1 means price is exactly on the upper band, 0.5 on the basis and 0 on the lower band.

Values above 1 or below 0 mean price has closed outside the bands.

How to read it

High readings mean price is strong relative to its recent volatility, low readings weak. Above 1, price has closed above the upper band; below 0, below the lower band. In a strong uptrend %B can stay above 0.8 for a long time, so a high value alone is not a sell signal.

Because it is normalised, %B is useful for comparing with other oscillators and for divergences: a new price high with a lower %B peak shows the move is weaker relative to volatility than the previous one.

Settings and limitations

Length, Basis average, Source and Standard deviations define the bands exactly as in Bollinger Bands; keep them the same as the bands on your chart. 20 bars and 2 standard deviations are the standard.

Values appear after Length bars. When the bands have zero width (a perfectly flat market) %B is undefined and is not drawn.

Notes

Matches TradingView's Bollinger Bands %B with its guides at 1, 0.5 and 0. Uses the population standard deviation, like the Bollinger Bands built-in.

Settings

Each setting appears in the indicator's settings panel in Spectral, next to a color setting for every output.

SettingTypeDefaultRange or optionsWhat it does
LengthWhole number202 – 500Bars in the moving average and in the standard deviation of the bands.
Basis averageChoiceSMASMA, EMA, SMMA (RMA), WMAMoving average used for the middle of the bands. Bollinger's definition uses the simple moving average (SMA).
SourcePrice sourcecloseopen, high, low, close, hlc3Price the bands are computed from and that %B places within them.
Standard deviationsNumber20.1 – 10, step 0.1Distance of each band from the basis, in standard deviations of the source over Length bars.

What it draws

Lines

  • %Bsolid · 1.5 px · primary#B7A1C8

Levels

  • Upper banddashed · 1 px#8AA9C3
  • Basisdotted · 1 px#6E7681
  • Lower banddashed · 1 px#8AA9C3

Zones

  • Inside the bandsfilled at 6% opacity#8AA9C3

Source

Save it as bollinger-percent-b.js and add it with Indicators → Custom indicators → Add file…. The format is explained in the custom indicators guide.

// Copyright (c) 2026 Spectral
// SPDX-License-Identifier: MIT-0
// Free to use, copy, modify and share: https://spectral.trade/docs/custom-indicators/LICENSE.txt
/** @type {import('./spectral-indicator').IndicatorV2} */
export default {
  schemaVersion: 2,
  id: 'bollinger-percent-b',
  name: 'Bollinger %B',
  shortName: '%B',
  version: '1.0.0',
  author: 'Spectral',
  description: 'Where price sits within its Bollinger Bands: 1 at the upper band, 0.5 at the basis, 0 at the lower band.',
  pane: 'oscillator',
  live: { lookback: 'length' },
  inputs: {
    length: { type: 'integer', label: 'Length', default: 20, min: 2, max: 500, description: 'Bars in the moving average and in the standard deviation of the bands.' },
    maType: { type: 'select', label: 'Basis average', default: 'SMA', options: ['SMA', 'EMA', 'SMMA (RMA)', 'WMA'], description: 'Moving average used for the middle of the bands. Bollinger\'s definition uses the simple moving average (SMA).' },
    source: { type: 'source', label: 'Source', default: 'close', description: 'Price the bands are computed from and that %B places within them.' },
    mult: { type: 'number', label: 'Standard deviations', default: 2, min: 0.1, max: 10, step: 0.1, description: 'Distance of each band from the basis, in standard deviations of the source over Length bars.' },
  },
  plots: {
    percentB: { label: '%B', color: '#B7A1C8', lineWidth: 1.5, primary: true },
  },
  levels: {
    upperBand: { label: 'Upper band', color: '#8AA9C3', lineStyle: 'dashed' },
    middle: { label: 'Basis', color: '#6E7681', lineStyle: 'dotted' },
    lowerBand: { label: 'Lower band', color: '#8AA9C3', lineStyle: 'dashed' },
  },
  zones: {
    band: { label: 'Inside the bands', color: '#8AA9C3', opacity: 0.06 },
  },
  docs: {
    overview: 'Bollinger %B shows where the price is relative to its Bollinger Bands as a single number: (price minus lower band) divided by (upper band minus lower band). A value of 1 means price is exactly on the upper band, 0.5 on the basis and 0 on the lower band.\n\nValues above 1 or below 0 mean price has closed outside the bands.',
    howToRead: 'High readings mean price is strong relative to its recent volatility, low readings weak. Above 1, price has closed above the upper band; below 0, below the lower band. In a strong uptrend %B can stay above 0.8 for a long time, so a high value alone is not a sell signal.\n\nBecause it is normalised, %B is useful for comparing with other oscillators and for divergences: a new price high with a lower %B peak shows the move is weaker relative to volatility than the previous one.',
    settings: 'Length, Basis average, Source and Standard deviations define the bands exactly as in Bollinger Bands; keep them the same as the bands on your chart. 20 bars and 2 standard deviations are the standard.\n\nValues appear after Length bars. When the bands have zero width (a perfectly flat market) %B is undefined and is not drawn.',
    notes: 'Matches TradingView\'s Bollinger Bands %B with its guides at 1, 0.5 and 0. Uses the population standard deviation, like the Bollinger Bands built-in.',
  },
  calculate(ctx) {
    const { bars, inputs, ta } = ctx;
    const source = ta.source(bars, inputs.source);
    const basis = movingAverage(ta, source, inputs.maType, inputs.length);
    const deviation = ta.stdev(source, inputs.length);
    const percentB = basis.map((value, index) => {
      if (value === null || deviation[index] === null) return null;
      const width = 2 * inputs.mult * deviation[index];
      // A flat window's deviation is rounding noise, not zero, for most prices: treat it as no width.
      return width > 1e-10 * Math.abs(value) ? (source[index] - (value - inputs.mult * deviation[index])) / width : null;
    });
    return {
      plots: { percentB },
      levels: { upperBand: 1, middle: 0.5, lowerBand: 0 },
      zones: { band: { upper: bars.map(() => 1), lower: bars.map(() => 0) } },
    };
  },
};

function movingAverage(ta, values, type, length) {
  if (type === 'EMA') return ta.ema(values, length);
  if (type === 'SMMA (RMA)') return ta.rma(values, length);
  if (type === 'WMA') return ta.wma(values, length);
  return ta.sma(values, length);
}

Adapt it with an LLM

Paste this into your assistant and replace the second-to-last line with the change you want.

Start from this Spectral custom indicator: https://spectral.trade/indicators/bollinger-percent-b.js
Format reference: https://spectral.trade/docs/custom-indicators.llm.md
Keep the file format and every rule of the reference; give the result a new id, name, shortName and version, and update docs and input descriptions to match.
Keep its live updates exact and fast (section "Fast live updates" of the reference): carry values from bar to bar only through ctx.ta, and raise live.lookback if the change reads further back; Spectral rejects a file whose live updates differ from a full recalculation or are too slow.
Change I want: <describe the change>
Answer with the complete file in one code block, ready to save as <id>.js.