TradingView indicator guides

Williams %R indicator on TradingView: formula and limits

Williams %R places the current close within a recent high-low range on a scale from -100 to 0. Readings near zero mean the close is near the window high. That can persist during a rising trend; it does not establish an imminent reversal.

Default length: 14Reference levels: -20 / -80Pine Script v6

Calculate the negative scale

Use the highest high H and lowest low L from the same N bars, including the current bar, with close C. The formula is:

%R = -100 × (H - C) / (H - L)

The following is an illustrative five-bar dataset, not market data or a backtest. At bar 5, H = 110, L = 90 and C = 106, giving -100 × 4 / 20 = -20.

Hypothetical input, length = 5
BarHighLowClose
1110100104
210897100
31059094
410793102
510999106

Holding H = 110 and L = 90 fixed, closes of 110, 106, 100, 94 and 90 produce %R values of 0, -20, -50, -80 and -100. A move from -80 to -20 is an increase in the oscillator. The minus sign does not reverse that ordering.

A crossing without a price move

Now append bar 6 with high 108, low 101 and close 106. Bar 1 leaves the five-bar window. H becomes 109 while L stays 90, so %R becomes -100 × 3 / 19 = -15.789. The close stayed at 106 but the oscillator moved above -20. Check expired extremes before attributing every crossing to new buying or selling.

Copy a Williams %R window check

Paste this complete indicator into TradingView Pine Editor, save it, and add it to the chart. It plots the formula explicitly and exposes the difference from ta.wpr(length) in the Data Window. Alerts describe crossings; the script places no orders.

Pine Script v6
//@version=6
indicator("Williams %R window check", shorttitle="WPR check", overlay=false)

length = input.int(14, "Length", minval=1)
confirmedOnly = input.bool(true, "Confirmed bars only")

highestHigh = ta.highest(high, length)
lowestLow = ta.lowest(low, length)
priceRange = highestHigh - lowestLow
fullWindow = bar_index >= length - 1
wpr = fullWindow and priceRange > 0 ? -100.0 * (highestHigh - close) / priceRange : na
builtIn = ta.wpr(length)
showBar = not confirmedOnly or barstate.isconfirmed

plot(showBar ? wpr : na, "Williams %R", color=color.blue, linewidth=2, style=plot.style_linebr)
upper = hline(-20, "Upper reference", color=color.gray)
hline(-50, "Range midpoint", color=color.new(color.gray, 50))
lower = hline(-80, "Lower reference", color=color.gray)
fill(upper, lower, color=color.new(color.blue, 92))
plot(showBar and not na(wpr) ? wpr - builtIn : na, "Manual minus ta.wpr", display=display.data_window)

crossAbove = ta.crossover(wpr, -80)
crossBelow = ta.crossunder(wpr, -20)
alertcondition(showBar and crossAbove, "WPR crossed above -80", "Williams %R crossed above -80")
alertcondition(showBar and crossBelow, "WPR crossed below -20", "Williams %R crossed below -20")

The blue line is %R; the gray lines mark -20, -50 and -80. On eligible bars, "Manual minus ta.wpr" should be zero apart from floating-point rounding when both calculations use the same OHLC data. A discrepancy is a reason to inspect the inputs, not a trading signal.

InputDefaultEffect
Length14; minimum 1Sets the high-low window and the required startup bar count.
Confirmed bars onlyOnHides the open bar and gates alert conditions until its closing update.

With confirmation off, high, low and close can change during the live bar, and a crossing can disappear. With confirmation on, choose a bar-close alert frequency for the intended workflow. This script reads only the chart timeframe.

Compare Williams %R, Stochastic and RSI

CalculationInputs and transformationComparison boundary
Williams %R-100 × (H - C) / (H - L)Range position on a negative scale.
Raw Stochastic %K100 × (C - L) / (H - L)%R = raw %K - 100 with identical inputs.
Smoothed StochasticSMA of raw %K; %D smooths %K againSmoothing changes the series and can delay crossings.
RSI100 - 100 / (1 + average gain / average loss)Uses smoothed close-to-close gains and losses, not H and L.

For the five-bar example, raw %K is 100 × 16 / 20 = 80, so %R is 80 - 100 = -20. This is an exact algebraic offset, not an approximate inverse or evidence that %R reacts faster. To compare TradingView Stochastic, use the same length and set its extra %K smoothing to 1; %D remains a separate series.

RSI cannot be recovered from those three final numbers. For example, two close sequences ending at 106 can share H = 110 and L = 90 while having different intermediate gains and losses. Their %R matches, but their RSI need not match.

Check these limits before using a signal

  1. A flat window has H = L. This example outputs na, rather than inventing a midpoint or an extreme.
  2. The script intentionally suppresses the first N - 1 bars of the loaded dataset. With N = 14, the first eligible index is 13. This is an explicit startup policy; do not assume every script shares it.
  3. An expired high or low changes the denominator. Use the same window boundary when reconciling two implementations.
  4. A close near the top or bottom of its window can continue there. Treat -20 and -80 as reference levels, not automatic sell and buy orders.
  5. Timeframe, session and feed differences change the bars. Compare the same completed bar before changing the formula to force a match.

To test a trading rule, define entry timing, exit, loss limit, position size and execution costs separately. This calculation example provides no performance estimate. Educational information, not investment advice.

Turn the checked calculation into a script specification

Use Pineify's Pine Script AI Coding Agent to describe the inputs and behavior you need. A reviewable request starts with the calculation and boundary rules:

Create Williams %R with length 14 and chart OHLC. Require a full window, return na when its range is zero, and confirm crossings at bar close. Keep the manual calculation and ta.wpr comparison visible for inspection. Do not add strategy orders.
  1. Open the coding agent and provide the specification.
  2. Inspect the generated length and price inputs.
  3. Copy the code into TradingView and compare completed bars.
  4. Check startup, flat windows and a live bar before enabling alerts.
  5. Define exits and costs separately if you later request a strategy.
Open Pine Script AI Coding Agent

Williams %R FAQ

Williams %R describes where the close sits within a rolling high-low range. For a nonzero range, 0 means the close is at the window high and -100 means it is at the window low. It is a position within a price range, not a percentage return or reversal probability.

Add the built-in Williams %R indicator or paste the example into Pine Editor and add it to the chart. Match length, symbol, timeframe, session and price source before comparing readings. This example uses close, waits for a full window and hides the open bar by default.

Yes for the raw, unsmoothed %K with identical high, low, close and length, where the range is positive. Smoothing %K or comparing %D breaks that direct equality. Set Stochastic smoothing to 1 when checking the relationship.

Williams %R uses the highest and lowest prices in a finite window. RSI uses smoothed average gains and losses between successive closes. The same Williams %R value can therefore accompany different RSI values; changing the close sequence can change RSI without changing the final range position.

The formula has a zero denominator. This example returns na and leaves a gap instead of substituting 0 or -50. Those substitutes would falsely report a close at the high or the midpoint. The next positive-range window can produce a value again.

It deliberately waits for length bars from the start of the loaded dataset. With length 14, bar indices 0 through 12 have no output; index 13 is the first eligible bar. A zero-range window or an unconfirmed live bar can still keep that value hidden.

No. The alert records a threshold event. Its usefulness as an entry rule needs separate testing with exits, position size, commissions and slippage. A level can be crossed because an old extreme leaves the window even when the close does not move.

There is no universal best length established here. Start a comparison with 14, change only the length, and keep the feed, session, timeframe and confirmation rule fixed. A shorter window changes which extremes are eligible; it does not establish better trade outcomes.