Supported Indicators

Regression Channel

Introduction

The Regression Channel indicator extends the with the inclusion of two (upper and lower) channel lines that are distanced from the linear regression line by a user defined number of standard deviations. Reference: http://www.onlinetradingconcepts.com/TechnicalAnalysis/LinRegChannel.html

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

Using RC Indicator

To create an automatic indicator for RegressionChannel, call the RCrc helper method from the QCAlgorithm class. The RCrc method creates a RegressionChannel 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 RegressionChannelAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private RegressionChannel _rc;

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

    public override void OnData(Slice data)
    {

        if (_rc.IsReady)
        {
            // The current value of _rc is represented by itself (_rc)
            // or _rc.Current.Value
            Plot("RegressionChannel", "rc", _rc);
            // Plot all properties of abands
            Plot("RegressionChannel", "linearregression", _rc.LinearRegression);
            Plot("RegressionChannel", "upperchannel", _rc.UpperChannel);
            Plot("RegressionChannel", "lowerchannel", _rc.LowerChannel);
            Plot("RegressionChannel", "intercept", _rc.Intercept);
            Plot("RegressionChannel", "slope", _rc.Slope);
        }
    }
}
class RegressionChannelAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        self._rc = self.rc(self._symbol, 20, 2)

    def on_data(self, slice: Slice) -> None:

        if self._rc.is_ready:
            # The current value of self._rc is represented by self._rc.current.value
            self.plot("RegressionChannel", "rc", self._rc.current.value)
            # Plot all attributes of self._rc
            self.plot("RegressionChannel", "linear_regression", self._rc.linear_regression.current.value)
            self.plot("RegressionChannel", "upper_channel", self._rc.upper_channel.current.value)
            self.plot("RegressionChannel", "lower_channel", self._rc.lower_channel.current.value)
            self.plot("RegressionChannel", "intercept", self._rc.intercept.current.value)
            self.plot("RegressionChannel", "slope", self._rc.slope.current.value)

For more information about this method, see the QCAlgorithm classQCAlgorithm class.

You can manually create a RegressionChannel 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 RegressionChannelAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private RegressionChannel _regressionchannel;

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

    public override void OnData(Slice data)
    {
        if (data.Bars.TryGetValue(_symbol, out var bar))
            _regressionchannel.Update(bar.EndTime, bar.Close);

        if (_regressionchannel.IsReady)
        {
            // The current value of _regressionchannel is represented by itself (_regressionchannel)
            // or _regressionchannel.Current.Value
            Plot("RegressionChannel", "regressionchannel", _regressionchannel);
            // Plot all properties of abands
            Plot("RegressionChannel", "linearregression", _regressionchannel.LinearRegression);
            Plot("RegressionChannel", "upperchannel", _regressionchannel.UpperChannel);
            Plot("RegressionChannel", "lowerchannel", _regressionchannel.LowerChannel);
            Plot("RegressionChannel", "intercept", _regressionchannel.Intercept);
            Plot("RegressionChannel", "slope", _regressionchannel.Slope);
        }
    }
}
class RegressionChannelAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        self._regressionchannel = RegressionChannel(20, 2)

    def on_data(self, slice: Slice) -> None:
        bar = slice.bars.get(self._symbol)
        if bar:
            self._regressionchannel.update(bar.end_time, bar.close)

        if self._regressionchannel.is_ready:
            # The current value of self._regressionchannel is represented by self._regressionchannel.current.value
            self.plot("RegressionChannel", "regressionchannel", self._regressionchannel.current.value)
            # Plot all attributes of self._regressionchannel
            self.plot("RegressionChannel", "linear_regression", self._regressionchannel.linear_regression.current.value)
            self.plot("RegressionChannel", "upper_channel", self._regressionchannel.upper_channel.current.value)
            self.plot("RegressionChannel", "lower_channel", self._regressionchannel.lower_channel.current.value)
            self.plot("RegressionChannel", "intercept", self._regressionchannel.intercept.current.value)
            self.plot("RegressionChannel", "slope", self._regressionchannel.slope.current.value)

For more information about this indicator, see its referencereference.

Visualization

The following plot shows values for some of the RegressionChannel indicator properties:

RegressionChannel line plot.

Indicator History

To get the historical data of the RegressionChannel 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 RegressionChannelAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private RegressionChannel _rc;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _rc = RC(_symbol, 20, 2);

        var indicatorHistory = IndicatorHistory(_rc, _symbol, 100, Resolution.Minute);
        var timeSpanIndicatorHistory = IndicatorHistory(_rc, _symbol, TimeSpan.FromDays(10), Resolution.Minute);
        var timePeriodIndicatorHistory = IndicatorHistory(_rc, _symbol, new DateTime(2024, 7, 1), new DateTime(2024, 7, 5), Resolution.Minute);

        // Access all attributes of indicatorHistory
        var linearRegression = indicatorHistory.Select(x => ((dynamic)x).LinearRegression).ToList();
        var upperChannel = indicatorHistory.Select(x => ((dynamic)x).UpperChannel).ToList();
        var lowerChannel = indicatorHistory.Select(x => ((dynamic)x).LowerChannel).ToList();
        var intercept = indicatorHistory.Select(x => ((dynamic)x).Intercept).ToList();
        var slope = indicatorHistory.Select(x => ((dynamic)x).Slope).ToList();
    }
}
class RegressionChannelAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        self._rc = self.rc(self._symbol, 20, 2)

        indicator_history = self.indicator_history(self._rc, self._symbol, 100, Resolution.MINUTE)
        timedelta_indicator_history = self.indicator_history(self._rc, self._symbol, timedelta(days=10), Resolution.MINUTE)
        time_period_indicator_history = self.indicator_history(self._rc, self._symbol, datetime(2024, 7, 1), datetime(2024, 7, 5), Resolution.MINUTE)
    
        # Access all attributes of indicator_history
        indicator_history_df = indicator_history.data_frame
        linear_regression = indicator_history_df["linearregression"]
        upper_channel = indicator_history_df["upperchannel"]
        lower_channel = indicator_history_df["lowerchannel"]
        intercept = indicator_history_df["intercept"]
        slope = indicator_history_df["slope"]

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: