Williams %R in Pine Script
A bounded oscillator that measures how far the close sits below the highest high of the last 14 bars, on a scale from 0 to −100, with −20 and −80 as the extreme lines.
Definition
Williams %R is a bounded momentum oscillator that reads the distance between the latest close and the highest high of the last n bars, as a share of that period's range. A close at the top of the range reads 0; a close at the bottom reads −100. The whole scale is negative, which looks odd at first but is simply the stochastic turned upside down. It belongs to the bounded family with RSI and the stochastic. Of the three it is the rawest: no smoothing, no signal line, just the position of the close inside the range.
The reading works because closes tell you who won the bar. A market that keeps closing near its recent highs reads between 0 and −20, and that is what a rising market does most of the time. A market closing near its recent lows reads between −80 and −100. The oscillator therefore flags when closes have been one-sided against the recent range, and a return from the zone marks the moment closes stop being one-sided. The lack of smoothing is the trade-off: it turns on the same bar the close turns, and it also flickers.
Larry Williams introduced the indicator and used it on commodity futures with short lookbacks. The usual defaults are 14 bars, −20 as the overbought line and −80 as the oversold line, so that the top fifth of the range is overbought and the bottom fifth is oversold. Some traders use 10 bars with −10 and −90 for a faster, rarer signal, and Williams himself favoured shorter windows. Because there is no smoothing, changing the length is the only way to change its speed.
On the chart %R moves like a fast stochastic %K. It reaches the zones several times a month on daily bars, sits inside them for stretches during trends, and crosses the −50 midline often. A single wide bar shifts the reading sharply, because both the close and the range change at once. Readings that hold above −20 for many bars describe a strong uptrend rather than an imminent top. The library's script marks the exits, a cross up through −80 as Long and a cross down through −20 as Short, and colours the line red above −20 and green below −80.
Reading the chart
Read the scale first, because it runs the other way from every other oscillator, then read the zone and its exit.
- Add a 14-bar Williams %R in a pane with lines at −20 and −80. Remember that the scale runs from 0 at the top to −100 at the bottom, so −10 is overbought and −90 is oversold.
- Note where the line sits and how long it has been there. A brief dip below −80 after a pullback in an uptrend and a month spent below −80 in a bear market are different situations with the same reading.
- Wait for the exit from the zone. The long trigger is the cross back above −80, the short trigger the cross back below −20; the entry into the zone is only the setup.
- Check the bar that produced the cross. Because there is no smoothing, one wide bar can lift the line from −95 to −60; a cross on a small bar is more likely to hold than one on a spike.
- Read the midline. In an uptrend %R spends most of its time above −50 and dips to −80 are long setups; below −50 for many bars the trend has turned and rallies to −20 are short setups.
- Confirm with the trend or with a level. A −80 exit at a prior day's low, a pivot or a rising moving average has a reason to hold; the same exit in the middle of a fall usually does not.
The maths
Williams %R measures the close from the top of the recent range and scales it from 0 to −100.
The highest high and lowest low include the current bar, so the reading can never leave the 0 to −100 range.
Raw stochastic %K and %R carry the same information: %R equals %K minus 100. Some platforms drop the sign and plot 0 to 100, which changes the labels but not the shape.
There is no smoothing in the standard form. A 3-bar average of %R is sometimes added and turns it into a slow stochastic under another name.
When the range in the window is zero the division is undefined; platforms return the previous value or 0.
Inputs that matter
| Input | Typical | What changing it does |
|---|---|---|
| Length | 14 | Shorter windows reach −20 and −80 on nearly every swing; longer windows keep the line in the middle and reserve the zones for moves that stand out against a bigger range. |
| Overbought | −20 | Moving it to −10 waits for closes at the very top of the range before a short mark; −30 turns the line into a trend pullback marker. |
| Oversold | −80 | Moving it to −90 waits for closes at the very bottom before a long mark; −70 registers ordinary pullbacks inside an uptrend. |
In practice
- As a range oscillator: the rule goes long when %R crosses back above −80 and short when it crosses back below −20, which is what the library's script marks as Long and Short on confirmed bars.
- As a trend pullback trigger: in an uptrend defined by a moving average or a higher timeframe, the rule treats dips below −80 as the entry setup and takes no shorts on the overbought readings.
- As a strength gauge: readings that hold above −20 for many bars show closes pinned at the highs. That is a sign the trend is strong, and the first exit from the zone is a pause rather than a top.
- As a fast confirmation for level tests: a bounce from the previous day's low or a pivot that also lifts %R out of oversold has two reasons to hold. The cross gives a clean bar for the alert.
- As a substitute for the stochastic on short lengths: with 5 to 10 bars it turns on the same bar as the raw %K and suits scalpers who want the earliest exit from a zone.
- As a midline trend read: a shift from spending most bars above −50 to most bars below it marks a change of character before the moving averages cross.
Where it goes wrong
- Reading the scale backwards. Near 0 is overbought and near −100 is oversold; check the sign before setting alerts.
- Trading the entry into the zone instead of the exit. The line can stay below −80 for weeks in a decline; the signal is the cross back above the line.
- Expecting the smoothness of RSI. %R has no smoothing, so single bars move it a long way; either accept the noise or read it on a higher timeframe.
- Taking every overbought reading as a short. Closes near the highs are what an uptrend looks like; a short belongs only to a chart whose trend has already turned.
Compared with
Stochastic Oscillator. The stochastic is the same measurement taken from the bottom of the range and then smoothed twice, with a %D signal line to cross. Williams %R is the raw, unsmoothed version measured from the top. Use %R for the earliest possible read, the stochastic when you want the cross signal and less noise.
RSI. RSI averages gains against losses with heavy smoothing, so it holds a level for many bars and reads the regime. Williams %R reads only where the last close sits in the range, so it turns immediately and flickers. RSI answers how one-sided the closes have been; %R answers where the latest close is.
CCI. CCI measures the distance of typical price from its average in mean-deviation units and has no ceiling, so it can run past 200 in a strong trend. Williams %R is capped at 0 and −100. CCI tells you a move is large for the instrument; %R tells you the close is at the edge of its recent range.
Donchian Channels. A Donchian channel draws the same highest high and lowest low on the price chart; %R expresses where the close sits between them as a percentage. A close at the upper Donchian line is a %R of 0. The channel is for breakouts; %R is for the position inside the channel.
Build it in Quant
Type one of these in the Quant desk, or click it. A request that fits a checked template is assembled on the spot without a credit; anything beyond the blanks goes to the copilot, which edits on top of the checked script.
Questions
Why is Williams %R negative?
Because it measures the close down from the highest high of the window instead of up from the lowest low. A close at the high is 0 percent below the top, so it reads 0; a close at the low is 100 percent below, so it reads −100. The sign is only a convention. Some platforms plot it from 0 to 100 and the shape is identical.
What are the standard Williams %R levels?
−20 and −80 on a 14-bar length. Above −20 the close is in the top fifth of the recent range, which is overbought; below −80 it is in the bottom fifth, oversold. Faster traders use 10 bars with −10 and −90 so that only closes at the extreme edges register. Whatever the levels, the signal is the cross back out of the zone, not the entry.
Is Williams %R the same as the stochastic?
Nearly. Raw stochastic %K measures the close from the bottom of the range, %R measures it from the top, and %R equals %K minus 100. The stochastic then smooths %K and adds a %D signal line; %R stays raw. So %R is the fast stochastic without the cross signal, and it turns a bar or two earlier.
What does the library's Williams %R script mark?
It plots %R with dashed lines at −20 and −80, and colours the line red above −20 and green below −80. It marks Long when the line crosses back above −80 and Short when it crosses back below −20. Marks wait for the bar to close, and an optional alert fires on each one.
How is Williams %R used in a trend?
A trend rule keeps only the marks that agree with the trend. In an uptrend, dips below −80 followed by a cross back above it are pullback entries; readings above −20 are simply strength. In a downtrend, rallies above −20 that then cross back below it are the entries. The trend comes from a moving average or a higher timeframe first.
Which length works best?
14 is the default and reads the same swing as a 14-period RSI or stochastic. 10 is common on intraday charts and on futures, and Williams used short windows himself. Lengths above 20 make the zones rare and turn the tool into a slow range gauge. Change the levels with the length: shorter windows suit wider zones such as −10 and −90.