Strategy Creation

# What is the Williams %R indicator and how to use it on Tuned?

We will look at what it does, how it’s used, and how it can be utilized on Tuned for algorithmic trading.

## What is the Williams %R Indicator?

The Williams %R (Percent Range), created by Larry Williams, is a momentum oscillator. It represents the price level in relation to the highest point in the previous period. The default period is generally set to 14. By doing this, you can monitor overbought and oversold conditions. Since the Williams %R fluctuates between 0 and -100, this would mean that readings between 0 and -20 are overbought, while readings between -80 and -100 are oversold. This means that the Williams %R is a bound indicator.

## How is Williams %R calculated?

The calculation for the Williams %R indicator is straight forward.

Williams %R = (highest(high, n) – close) / (highest(high, n) – lowest(low, n))

Where n = the lookback period

## Common uses

When it comes to interpreting the Williams %R, we can say that a reading above -20 would indicate overbought levels, on the contrary, a reading below -80 would indicate oversold. When the %R value climbs over -20, it’s time to sell, and when it drops below -80, it’s time to buy. Williams %R has a tendency to peak ahead of price, making it a great indicator to identify a trend reversal. During stronger trends, however, the Williams %R can stay among the oversold or overbought zones for an extended period of time.

The indicator’s default settings tell a trader where the current price is in relation to the 14 period high. As mentioned above overbought/oversold levels are indicated using the traditional parameters of -20 for overbought and -80 for oversold. That is why it’s preferable to wait for a shift in price action, before buying or selling. For example, following along with the MACD.

## An example strategy using Williams %R on Tuned

The code is as follows:

### Tuned Script

```// Inputs
length = intInput("Length", 14)
src = srcInput("Source", "close")
lowerThresh = seriesOf(numInput("Lower Threshold", -80.0))
upperThresh = seriesOf(numInput("Upper Threshold", -20.0))

// Function formula and Calculation
def _pr(length) {
max = highest(high, length)
min = lowest(low, length)
100.0 * (src - max) / (max - min)
}

// Setting the Variables ready to be Plot
percentR = _pr(length)

// Plot the output of the Indicator
obPlot = plot(upperThresh, "Upper Band", "#787B86", 1, PlotStyle.LINE, 0, false)
plot(seriesOf(-50.0), "Middle Level", "#787B86", 1, PlotStyle.LINE, 0, false)
osPlot = plot(lowerThresh, "Lower Band", "#787B86", 1, PlotStyle.LINE, 0, false)
fill(obPlot, osPlot, "#B18BF3", 90, "Fill", false)
plot(percentR, "%R", "#7E57C2", 1, PlotStyle.LINE, 0, false)

// Setting conditions for Entry/Exit
sellExit = crossunder(percentR, upperThresh)

// Output Signals

### Pine Alpha

```//@version=4
//Tuned supports Pine Version 4 Only
strategy("Williams Percent Range", shorttitle="Williams %R")

// Inputs
length = input(title="Length", defval=14)
src = input(close, "Source")
lowerThresh = input(title="Lower Threshold", defval = -80.0)
upperThresh = input(title="Upper Threshold", defval = -20.0)

// Function formula and Calculation
_pr(length) =>
max = highest(high, length)
min = lowest(low, length)
100 * (src - max) / (max - min)

// Setting the Variables ready to be Plot
percentR = _pr(length)

// Plot the output of the Indicator
obPlot = plot(upperThresh, title="Upper Band", color=#787B86)
plot(-50, title="Middle Level", linestyle=hline.style_dotted, color=#787B86)
osPlot = plot(lowerThresh, title="Lower Band", color=#787B86)
fill(obPlot, osPlot, title="Background", color=#B18BF3, transp = 90)
plot(percentR, title="%R", color=#7E57C2)

// Setting conditions for Entry/Exit
sellExit = crossunder(percentR, upperThresh)

// Output Signals

strategy.close("Close", when = sellExit and strategy.position_size > 0.0)```

The strategy above will buy when the Williams %R indicator crosses above the -80 threshold. A sell signal is produced when the indicator crosses below the -20 threshold.

Adding risk management rules, like stop loss and take profit, can also help improve profitability.

### How does it behave?

• The market sentiment is overbought when the Williams %R reads higher than -20
• The market sentiment is oversold when the Williams %R reads less than -80
• The Williams %R is range bound between 0 and -100
• Overbought or oversold does not imply that the price will reverse. Overbought just implies the price is towards the high end of its recent range, while oversold simply means the price is near the low end.

## Conclusion

Williams %R (Percent Range) used on its own, can already print a clear story by showing if the market has been exhausted. The Williams %R, like other indicators, isn’t perfect. Traders shouldn’t rely on it as their sole source of information when making decisions. However, when used correctly alongside other indicators, there is a larger ratio of a positive trading experience. The Williams %R is more popular among traders because of this.

With that being said, I hope this has helped your understanding of its function, the conditions of the strategy and how to make it even better!

Have fun scripting!

Disclaimer: The opinions expressed here are for general informational purposes only and are not intended to provide specific advice or recommendations for any specific security or investment product. You should never invest money that you cannot afford to lose. Before trading using complex financial products, please ensure to understand the risks involved. Past performance is no guarantee of future results.

All investing involves risk, including the possible loss of all the money you invest, and past performance does not guarantee future performance. Historical returns expected returns, and probability projections are provided for informational and illustrative purposes, and may not reflect actual future performance.

Tuned is not a broker-dealer, exchange, custodian, wallet provider, or counterparty. Investing is highly speculative and volatile. Tuned is only suitable for investors who fully understand the risk of loss and may experience large drawdowns. Investors should never invest more than they can afford to lose.