All indicators
Momentum / Own pane

Williams %R

How far the close sits below the recent high, on a 0 to -100 scale, with -20 and -80 bands.

  • %R
  • Built into Spectral
  • Updates every tick
  • williams %r
  • williams r
  • oscillator
  • overbought
  • oversold
  • momentum

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.

Williams %R on a Spectral chart
Spectral chart with sample market data, default settings.

Overview

Williams %R measures where the close sits inside the high-low range of the last Length bars, counted down from the high: 0 is a close at the highest high, -100 a close at the lowest low. Bands mark -20 and -80.

How to read it

Between 0 and -20 the market is closing near the top of its recent range; between -80 and -100 near the bottom. Strong trends keep the line in those zones for long stretches, so a reading there describes strength or weakness rather than a turn.

A move back out of a zone (crossing below -20 or above -80) shows the closes leaving the edge of the range, and failures to reach the zone again while price pushes on are divergences worth comparing with price structure.

Settings and limitations

Length defaults to 14 bars, as on TradingView. Shorter lengths react faster and spend more time at the extremes. The first value appears on bar Length (bar 14 with the default). A range with no width (every bar at the same price) has no value.

Notes

Matches Williams %R on TradingView and most platforms: -100 x (highest high - close) / (highest high - lowest low). It is the fast Stochastic %K shifted down by 100.

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 number141 – 500Bars in the high-low range the close is compared with.
SourcePrice sourcecloseopen, high, low, close, hlc3Price compared with the range; the close is standard.
Upper bandNumber-20-100 – 0, step 1Level of the overbought reference band.
Middle bandNumber-50-100 – 0, step 1Level of the middle reference band.
Lower bandNumber-80-100 – 0, step 1Level of the oversold reference band.
Fill between bandsOn / offOn—Shade the area between the upper and lower band.

What it draws

Lines

  • %Rsolid · 1.5 px · primary#B7A1C8

Levels

  • Upper banddashed · 1 px#788493
  • Middle banddotted · 1 px#788493
  • Lower banddashed · 1 px#788493

Zones

  • Band fillfilled at 10% opacity#B7A1C8

Source

Save it as williams-r.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: 'williams-r',
  name: 'Williams %R',
  shortName: '%R',
  version: '1.0.0',
  author: 'Spectral',
  description: 'How far the close sits below the recent high, on a 0 to -100 scale, with -20 and -80 bands.',
  pane: 'oscillator',
  live: { lookback: 'length' },
  inputs: {
    length: { type: 'integer', label: 'Length', default: 14, min: 1, max: 500, description: 'Bars in the high-low range the close is compared with.' },
    source: { type: 'source', label: 'Source', default: 'close', description: 'Price compared with the range; the close is standard.' },
    upperBand: { type: 'number', label: 'Upper band', default: -20, min: -100, max: 0, step: 1, description: 'Level of the overbought reference band.' },
    middleBand: { type: 'number', label: 'Middle band', default: -50, min: -100, max: 0, step: 1, description: 'Level of the middle reference band.' },
    lowerBand: { type: 'number', label: 'Lower band', default: -80, min: -100, max: 0, step: 1, description: 'Level of the oversold reference band.' },
    showFill: { type: 'boolean', label: 'Fill between bands', default: true, description: 'Shade the area between the upper and lower band.' },
  },
  plots: {
    percentR: { label: '%R', color: '#B7A1C8', lineWidth: 1.5, primary: true },
  },
  levels: {
    upper: { label: 'Upper band', color: '#788493', lineStyle: 'dashed' },
    middle: { label: 'Middle band', color: '#788493', lineStyle: 'dotted' },
    lower: { label: 'Lower band', color: '#788493', lineStyle: 'dashed' },
  },
  zones: {
    fill: { label: 'Band fill', color: '#B7A1C8', opacity: 0.1 },
  },
  docs: {
    overview: 'Williams %R measures where the close sits inside the high-low range of the last Length bars, counted down from the high: 0 is a close at the highest high, -100 a close at the lowest low. Bands mark -20 and -80.',
    howToRead: 'Between 0 and -20 the market is closing near the top of its recent range; between -80 and -100 near the bottom. Strong trends keep the line in those zones for long stretches, so a reading there describes strength or weakness rather than a turn.\n\nA move back out of a zone (crossing below -20 or above -80) shows the closes leaving the edge of the range, and failures to reach the zone again while price pushes on are divergences worth comparing with price structure.',
    settings: 'Length defaults to 14 bars, as on TradingView. Shorter lengths react faster and spend more time at the extremes. The first value appears on bar Length (bar 14 with the default). A range with no width (every bar at the same price) has no value.',
    notes: 'Matches Williams %R on TradingView and most platforms: -100 x (highest high - close) / (highest high - lowest low). It is the fast Stochastic %K shifted down by 100.',
  },
  calculate(ctx) {
    const { bars, inputs, ta } = ctx;
    const source = ta.source(bars, inputs.source);
    const highest = ta.highest(bars.map((bar) => bar.high), inputs.length);
    const lowest = ta.lowest(bars.map((bar) => bar.low), inputs.length);
    const percentR = source.map((value, index) => {
      const high = highest[index];
      const low = lowest[index];
      return high === null || low === null || high === low ? null : (-100 * (high - value)) / (high - low);
    });
    const band = (value) => bars.map(() => (inputs.showFill ? value : null));
    return {
      plots: { percentR },
      levels: { upper: inputs.upperBand, middle: inputs.middleBand, lower: inputs.lowerBand },
      zones: { fill: { upper: band(inputs.upperBand), lower: band(inputs.lowerBand) } },
    };
  },
};

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/williams-r.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.