Supported Indicators

Williams Percent R

Introduction

Williams %R, or just %R, is the current closing price in relation to the high and low of the past N days (for a given N). The value of this indicator fluctuates between -100 and 0. The symbol is said to be oversold when the oscillator is below -80%, and overbought when the oscillator is above -20%.

Create Manual Indicators

You can manually create a WilliamsPercentR indicator, so it doesn’t automatically update. Manual indicators let you update their values with any data you choose. The following reference table describes the WilliamsPercentR constructor.

WilliamsPercentR()1/2

            WilliamsPercentR QuantConnect.Indicators.WilliamsPercentR (
     int  period
   )
        

Creates a new Williams %R.

WilliamsPercentR()2/2

            WilliamsPercentR QuantConnect.Indicators.WilliamsPercentR (
     string  name,
     int     period
   )
        

Creates a new Williams %R.

Update Manual Indicators

You can update the indicator automatically or manually.

Automatic Update

To register a manual indicator for automatic updates with the security data, call the RegisterIndicator method.


private WilliamsPercentR _wilr;

// In Initialize()
_wilr = new WilliamsPercentR(name, period);
_wilr.Updated += IndicatorUpdateMethod;

RegisterIndicator(symbol, _wilr, Resolution.Daily);

// In IndicatorUpdateMethod()
if (_wilr.IsReady)
{
    var indicatorValue = _wilr.Current.Value;
}
    
# In Initialize()
self.wilr = WilliamsPercentR(name, period)
self.wilr.Updated += self.IndicatorUpdateMethod

self.RegisterIndicator(symbol, self.wilr, Resolution.Daily)

# In IndicatorUpdateMethod()
if self.wilr.IsReady:
    indicator_value = self.wilr.Current.Value

To customize the data that automatically updates the indicator, see Custom Indicator Periods and Custom Indicator Values.

Manual Update

Updating your indicator manually enables you to control when the indicator is updated and what data you use to update it. To manually update the indicator, call the Update method with a TradeBar, QuoteBar, or an IndicatorDataPoint. The indicator will only be ready after you prime it with enough data.

    
private WilliamsPercentR _wilr;
private Symbol symbol;

// In Initialize()
_wilr = new WilliamsPercentR(period);
symbol = AddEquity("SPY").Symbol;

// In OnData()
if (data.QuoteBars.ContainsKey(_symbol))
{
    _wilr.Update(data.QuoteBars[symbol]);
}
if (_wilr.IsReady)
{
    var indicatorValue = _wilr.Current.Value;
}
    
# In Initialize()
self.wilr = WilliamsPercentR(period)
self.symbol = self.AddEquity("SPY").Symbol

# In OnData()
if data.QuoteBars.ContainsKey(self.symbol):
    self.wilr.Update(data.QuoteBars[self.symbol])
if self.wilr.IsReady:
    indicator_value = self.wilr.Current.Value

Create Automatic Indicators

The WILR method creates an WilliamsPercentR indicator, sets up a consolidator to update the indicator, and then returns the indicator so you can use it in your algorithm.

The following reference table describes the WILR method:

WILR()1/1

            WilliamsPercentR QuantConnect.Algorithm.QCAlgorithm.WILR (
    Symbol                                symbol,
    Int32                                 period,
    *Nullable<Resolution>           resolution,
    *Func<IBaseData, IBaseDataBar>  selector
   )
        

Creates a new Williams %R indicator. This will compute the percentage change of the current closing price in relation to the high and low of the past N periods. The indicator will be automatically updated on the given resolution.


If you don't provide a resolution, it defaults to the security resolution. If you provide a resolution, it must be greater than or equal to the resolution of the security. For instance, if you subscribe to hourly data for a security, you should update its indicator with data that spans 1 hour or longer.

For more information about the selector argument, see Alternative Price Fields.

Get Indicator Values

To get the value of the indicator, use its Current.Value attribute.

private WilliamsPercentR _wilr;

// In Initialize()
var symbol = AddEquity("SPY").Symbol;
_wilr = WILR(symbol, period);

// In OnData()
if (_wilr.IsReady)
{
    var maximum = _wilr.Maximum.Current.Value;
    var minimum = _wilr.Minimum.Current.Value;
    var current = _wilr.Current.Value;
}
# In Initialize()
symbol = self.AddEquity("SPY").Symbol
self.wilr = self.WILR(symbol, period)

# In OnData()
if self.wilr.IsReady:
    maximum = self.wilr.Maximum.Current.Value
    minimum = self.wilr.Minimum.Current.Value
    current = self.wilr.Current.Value

Visualization

To plot indicator values, in the OnData event handler, call the Plot method.

private WilliamsPercentR _wilr;

// In Initialize()
var symbol = AddEquity("SPY").Symbol;
_wilr = WILR(symbol, period);

// In OnData()
if (_wilr.IsReady)
{
    Plot("My Indicators", "maximum", _wilr.Maximum);
    Plot("My Indicators", "minimum", _wilr.Minimum);
    Plot("My Indicators", "williamspercentr", _wilr.Current);
}
# In Initialize()
symbol = self.AddEquity("SPY").Symbol
self.wilr = self.WILR(symbol, period)

# In OnData()
if self.wilr.IsReady:
    self.Plot("My Indicators", "maximum", self.wilr.Maximum)
    self.Plot("My Indicators", "minimum", self.wilr.Minimum)
    self.Plot("My Indicators", "williamspercentr", self.wilr.Current)

The following image shows the plot values in a matplotlib plot. To load algorithm plot data into the Research Environment, see Charts.

For more information about plotting indicators, see Plotting Indicators.

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: