Supported Indicators

Least Squares Moving Average

Introduction

The Least Squares Moving Average (LSMA) first calculates a least squares regression line over the preceding time periods, and then projects it forward to the current period. In essence, it calculates what the value would be if the regression line continued. source

To view the implementation of this indicator, see the LEAN GitHub repository.

Using LSMA Indicator

To create an automatic indicators for LeastSquaresMovingAverage, call the LSMA helper method from the QCAlgorithm class. The LSMA method creates a LeastSquaresMovingAverage 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 LeastSquaresMovingAverageAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private LeastSquaresMovingAverage _lsma;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _lsma = LSMA(_symbol, 20);
    }

    public override void OnData(Slice data)
    {
        if (_lsma.IsReady)
        {
            // The current value of _lsma is represented by itself (_lsma)
            // or _lsma.Current.Value
            Plot("LeastSquaresMovingAverage", "lsma", _lsma);
            // Plot all properties of lsma
            Plot("LeastSquaresMovingAverage", "intercept", _lsma.Intercept);
            Plot("LeastSquaresMovingAverage", "slope", _lsma.Slope);
        }
    }
}
class LeastSquaresMovingAverageAlgorithm(QCAlgorithm):
    def Initialize(self) -> None:
        self._symbol = self.AddEquity("SPY", Resolution.Daily).Symbol
        self.lsma = self.LSMA(self.symbol, 20)

    def on_data(self, slice: Slice) -> None:
        if self.lsma.IsReady:
            # The current value of self.lsma is represented by self.lsma.Current.Value
            self.plot("LeastSquaresMovingAverage", "lsma", self.lsma.Current.Value)
            # Plot all attributes of self.lsma
            self.plot("LeastSquaresMovingAverage", "intercept", self.lsma.Intercept.Current.Value)
            self.plot("LeastSquaresMovingAverage", "slope", self.lsma.Slope.Current.Value)

The following reference table describes the LSMA method:

LSMA()1/1

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

Creates and registers a new Least Squares Moving Average instance.

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.

For more information about plotting indicators, see Plotting Indicators.

You can manually create a LeastSquaresMovingAverage 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 with time/number pair or an IndicatorDataPoint. The indicator will only be ready after you prime it with enough data.

public class LeastSquaresMovingAverageAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private LeastSquaresMovingAverage _lsma;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _lsma = new LeastSquaresMovingAverage(20);
    }

    public override void OnData(Slice data)
    {
        if (data.Bars.TryGetValue(_symbol, out var bar))
        {      
            _lsma.Update(bar.EndTime, bar.Close);
        }
   
        if (_lsma.IsReady)
        {
            // The current value of _lsma is represented by itself (_lsma)
            // or _lsma.Current.Value
            Plot("LeastSquaresMovingAverage", "lsma", _lsma);
            // Plot all properties of lsma
            Plot("LeastSquaresMovingAverage", "intercept", _lsma.Intercept);
            Plot("LeastSquaresMovingAverage", "slope", _lsma.Slope);
        }
    }
}
class LeastSquaresMovingAverageAlgorithm(QCAlgorithm):
    def Initialize(self) -> None:
        self._symbol = self.AddEquity("SPY", Resolution.Daily).Symbol
        self.lsma = LeastSquaresMovingAverage(20)

    def on_data(self, slice: Slice) -> None:
        bar = slice.Bars.get(self.symbol)
        if bar:
            self.lsma.Update(bar.EndTime, bar.Close)
        if self.lsma.IsReady:
            # The current value of self.lsma is represented by self.lsma.Current.Value
            self.plot("LeastSquaresMovingAverage", "lsma", self.lsma.Current.Value)
            # Plot all attributes of self.lsma
            self.plot("LeastSquaresMovingAverage", "intercept", self.lsma.Intercept.Current.Value)
            self.plot("LeastSquaresMovingAverage", "slope", self.lsma.Slope.Current.Value)

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

public class LeastSquaresMovingAverageAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private LeastSquaresMovingAverage _lsma;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _lsma = new LeastSquaresMovingAverage(20);
        RegisterIndicator(_symbol, _lsma, Resolution.Daily);
    }

    public override void OnData(Slice data)
    {
        if (_lsma.IsReady)
        {
            // The current value of _lsma is represented by itself (_lsma)
            // or _lsma.Current.Value
            Plot("LeastSquaresMovingAverage", "lsma", _lsma);
            // Plot all properties of lsma
            Plot("LeastSquaresMovingAverage", "intercept", _lsma.Intercept);
            Plot("LeastSquaresMovingAverage", "slope", _lsma.Slope);
        }
    }
}
class LeastSquaresMovingAverageAlgorithm(QCAlgorithm):
    def Initialize(self) -> None:
        self._symbol = self.AddEquity("SPY", Resolution.Daily).Symbol
        self.lsma = LeastSquaresMovingAverage(20)
        self.RegisterIndicator(self.symbol, self.lsma, Resolution.Daily)

    def on_data(self, slice: Slice) -> None:
        if self.lsma.IsReady:
            # The current value of self.lsma is represented by self.lsma.Current.Value
            self.plot("LeastSquaresMovingAverage", "lsma", self.lsma.Current.Value)
            # Plot all attributes of self.lsma
            self.plot("LeastSquaresMovingAverage", "intercept", self.lsma.Intercept.Current.Value)
            self.plot("LeastSquaresMovingAverage", "slope", self.lsma.Slope.Current.Value)

The following reference table describes the LeastSquaresMovingAverage constructor:

LeastSquaresMovingAverage()1/2

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

Initializes a new instance of the LeastSquaresMovingAverage class.

LeastSquaresMovingAverage()2/2

            LeastSquaresMovingAverage QuantConnect.Indicators.LeastSquaresMovingAverage (
    int  period
   )
        

Initializes a new instance of the LeastSquaresMovingAverage class.

Visualization

The following image shows plot values of selected properties of LeastSquaresMovingAverage using the plotly library.

LeastSquaresMovingAverage line plot.

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: