Supported Indicators
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.
To view the implementation of this indicator, see the LEAN GitHub repository.
Using LWMA Indicator
To create an automatic indicator for LinearWeightedMovingAverage, call the LWMAlwma helper method from the QCAlgorithm class. The LWMAlwma method creates a LinearWeightedMovingAverage object, hooks it up for automatic updates, and returns it so you can used it in your algorithm. In most cases, you should call the helper method in the Initializeinitialize method.
public class LinearWeightedMovingAverageAlgorithm : QCAlgorithm
{
private Symbol _symbol;
private LinearWeightedMovingAverage _lwma;
public override void Initialize()
{
_symbol = AddEquity("SPY", Resolution.Daily).Symbol;
_lwma = LWMA(_symbol, 20);
}
public override void OnData(Slice data)
{
if (_lwma.IsReady)
{
// The current value of _lwma is represented by itself (_lwma)
// or _lwma.Current.Value
Plot("LinearWeightedMovingAverage", "lwma", _lwma);
}
}
} class LinearWeightedMovingAverageAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
self._lwma = self.lwma(self._symbol, 20)
def on_data(self, slice: Slice) -> None:
if self._lwma.is_ready:
# The current value of self._lwma is represented by self._lwma.current.value
self.plot("LinearWeightedMovingAverage", "lwma", self._lwma.current.value)For more information about this method, see the QCAlgorithm classQCAlgorithm class.
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.
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 Updateupdate method. The indicator will only be ready after you prime it with enough data.
public class LinearWeightedMovingAverageAlgorithm : QCAlgorithm
{
private Symbol _symbol;
private LinearWeightedMovingAverage _linearweightedmovingaverage;
public override void Initialize()
{
_symbol = AddEquity("SPY", Resolution.Daily).Symbol;
_linearweightedmovingaverage = new LinearWeightedMovingAverage(20);
}
public override void OnData(Slice data)
{
if (data.Bars.TryGetValue(_symbol, out var bar))
_linearweightedmovingaverage.Update(bar.EndTime, bar.Close);
if (_linearweightedmovingaverage.IsReady)
{
// The current value of _linearweightedmovingaverage is represented by itself (_linearweightedmovingaverage)
// or _linearweightedmovingaverage.Current.Value
Plot("LinearWeightedMovingAverage", "linearweightedmovingaverage", _linearweightedmovingaverage);
}
}
} class LinearWeightedMovingAverageAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
self._linearweightedmovingaverage = LinearWeightedMovingAverage(20)
def on_data(self, slice: Slice) -> None:
bar = slice.bars.get(self._symbol)
if bar:
self._linearweightedmovingaverage.update(bar.end_time, bar.close)
if self._linearweightedmovingaverage.is_ready:
# The current value of self._linearweightedmovingaverage is represented by self._linearweightedmovingaverage.current.value
self.plot("LinearWeightedMovingAverage", "linearweightedmovingaverage", self._linearweightedmovingaverage.current.value)For more information about this indicator, see its referencereference.
Indicator History
To get the historical data of the LinearWeightedMovingAverage indicator, call the IndicatorHistoryself.indicator_history method. This method resets your indicator, makes a history request, and updates the indicator with the historical data. Just like with regular history requests, the IndicatorHistoryindicator_history method supports time periods based on a trailing number of bars, a trailing period of time, or a defined period of time. If you don't provide a resolution argument, it defaults to match the resolution of the security subscription.
public class LinearWeightedMovingAverageAlgorithm : QCAlgorithm
{
private Symbol _symbol;
private LinearWeightedMovingAverage _lwma;
public override void Initialize()
{
_symbol = AddEquity("SPY", Resolution.Daily).Symbol;
_lwma = LWMA(_symbol, 20);
var indicatorHistory = IndicatorHistory(_lwma, _symbol, 100, Resolution.Minute);
var timeSpanIndicatorHistory = IndicatorHistory(_lwma, _symbol, TimeSpan.FromDays(10), Resolution.Minute);
var timePeriodIndicatorHistory = IndicatorHistory(_lwma, _symbol, new DateTime(2024, 7, 1), new DateTime(2024, 7, 5), Resolution.Minute);
}
} class LinearWeightedMovingAverageAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
self._lwma = self.lwma(self._symbol, 20)
indicator_history = self.indicator_history(self._lwma, self._symbol, 100, Resolution.MINUTE)
timedelta_indicator_history = self.indicator_history(self._lwma, self._symbol, timedelta(days=10), Resolution.MINUTE)
time_period_indicator_history = self.indicator_history(self._lwma, self._symbol, datetime(2024, 7, 1), datetime(2024, 7, 5), Resolution.MINUTE)
