Indicator Reference

Linear Weighted Moving Average

Introduction

This indicator represents the traditional Weighted Moving Average indicator. The weight are linearly distributed according to the number of periods in the indicator. For example, a 4 period indicator will have a numerator of (4 * window[0]) + (3 * window[1]) + (2 * window[2]) + window[3] and a denominator of 4 + 3 + 2 + 1 = 10 During the warm up period, IsReady will return False, but the LWMA will still be computed correctly because the denominator will be the minimum of Samples factorial or Size factorial and the computation iterates over that minimum value. The RollingWindow of inputs is created when the indicator is created. A RollingWindow of LWMAs is not saved. That is up to the caller.

Create Manual Indicators

You can manually create a LinearWeightedMovingAverage 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 LinearWeightedMovingAverage constructor.

LinearWeightedMovingAverage()1/2

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

Initializes a new instance of the LinearWeightedMovingAverage class with the specified name and period.

LinearWeightedMovingAverage()2/2

            LinearWeightedMovingAverage QuantConnect.Indicators.LinearWeightedMovingAverage (
     int  period
   )
        

Initializes a new instance of the LinearWeightedMovingAverage class with the default name and period.

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 LinearWeightedMovingAverage _lwma;

// In Initialize()
_lwma = new LinearWeightedMovingAverage(name, period);
_lwma.Updated += IndicatorUpdateMethod;

RegisterIndicator(symbol, _lwma, Resolution.Daily);

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

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

# In IndicatorUpdateMethod()
if self.lwma.IsReady:
    indicator_value = self.lwma.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 time/decimal pair. The indicator will only be ready after you prime it with enough data.

    
private LinearWeightedMovingAverage _lwma;
private Symbol symbol;

// In Initialize()
_lwma = new LinearWeightedMovingAverage(period);
symbol = AddEquity("SPY").Symbol;

// In OnData()
if (data.ContainsKey(_symbol))
{
    _lwma.Update(data[symbol].EndTime, data[symbol].High);
}
if (_lwma.IsReady)
{
    var indicatorValue = _lwma.Current.Value;
}
    
# In Initialize()
self.lwma = LinearWeightedMovingAverage(period)
self.symbol = self.AddEquity("SPY").Symbol

# In OnData()
if data.ContainsKey(self.symbol):
    self.lwma.Update(data[self.symbol].EndTime, data[self.symbol].High)
if self.lwma.IsReady:
    indicator_value = self.lwma.Current.Value

Create Automatic Indicators

The LWMA method creates an LinearWeightedMovingAverage 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 LWMA method:

LWMA()1/1

            LinearWeightedMovingAverage QuantConnect.Algorithm.QCAlgorithm.LWMA (
    Symbol                           symbol,
    Int32                            period,
    *Nullable<Resolution>      resolution,
    *Func<IBaseData, Decimal>  selector
   )
        

Creates a new LinearWeightedMovingAverage indicator. This indicator will linearly distribute the weights across the periods.


If you don't provide a resolution, it defauls 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 LinearWeightedMovingAverage _lwma;

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

// In OnData()
if (_lwma.IsReady)
{
    var current = _lwma.Current.Value;
}
# In Initialize()
symbol = self.AddEquity("SPY").Symbol
self.lwma = self.LWMA(symbol, period)

# In OnData()
if self.lwma.IsReady:
    current = self.lwma.Current.Value

Visualization

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

private LinearWeightedMovingAverage _lwma;

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

// In OnData()
if (_lwma.IsReady)
{
    Plot("My Indicators", "linearweightedmovingaverage", _lwma.Current);
}
# In Initialize()
symbol = self.AddEquity("SPY").Symbol
self.lwma = self.LWMA(symbol, period)

# In OnData()
if self.lwma.IsReady:
    self.Plot("My Indicators", "linearweightedmovingaverage", self.lwma.Current)

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: