Supported Indicators

Stochastic Relative Strength Index

Introduction

Stochastic RSI, or simply StochRSI, is a technical analysis indicator used to determine whether an asset is overbought or oversold, as well as to identify current market trends. As the name suggests, the StochRSI is a derivative of the standard Relative Strength Index (RSI) and, as such, is considered an indicator of an indicator. It is a type of oscillator, meaning that it fluctuates above and below a center line.

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

Using SRSI Indicator

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

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _srsi = SRSI(_symbol, 14, 14, 3, 3);
    }

    public override void OnData(Slice data)
    {

        if (_srsi.IsReady)
        {
            // The current value of _srsi is represented by itself (_srsi)
            // or _srsi.Current.Value
            Plot("StochasticRelativeStrengthIndex", "srsi", _srsi);
            // Plot all properties of abands
            Plot("StochasticRelativeStrengthIndex", "k", _srsi.K);
            Plot("StochasticRelativeStrengthIndex", "d", _srsi.D);
        }
    }
}
class StochasticRelativeStrengthIndexAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        self._srsi = self.srsi(self._symbol, 14, 14, 3, 3)

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

        if self._srsi.is_ready:
            # The current value of self._srsi is represented by self._srsi.current.value
            self.plot("StochasticRelativeStrengthIndex", "srsi", self._srsi.current.value)
            # Plot all attributes of self._srsi
            self.plot("StochasticRelativeStrengthIndex", "k", self._srsi.k.current.value)
            self.plot("StochasticRelativeStrengthIndex", "d", self._srsi.d.current.value)

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

You can manually create a StochasticRelativeStrengthIndex 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 StochasticRelativeStrengthIndexAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private StochasticRelativeStrengthIndex _stochasticrelativestrengthindex;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _stochasticrelativestrengthindex = new StochasticRelativeStrengthIndex(14, 14, 3, 3);
    }

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

        if (_stochasticrelativestrengthindex.IsReady)
        {
            // The current value of _stochasticrelativestrengthindex is represented by itself (_stochasticrelativestrengthindex)
            // or _stochasticrelativestrengthindex.Current.Value
            Plot("StochasticRelativeStrengthIndex", "stochasticrelativestrengthindex", _stochasticrelativestrengthindex);
            // Plot all properties of abands
            Plot("StochasticRelativeStrengthIndex", "k", _stochasticrelativestrengthindex.K);
            Plot("StochasticRelativeStrengthIndex", "d", _stochasticrelativestrengthindex.D);
        }
    }
}
class StochasticRelativeStrengthIndexAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        self._stochasticrelativestrengthindex = StochasticRelativeStrengthIndex(14, 14, 3, 3)

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

        if self._stochasticrelativestrengthindex.is_ready:
            # The current value of self._stochasticrelativestrengthindex is represented by self._stochasticrelativestrengthindex.current.value
            self.plot("StochasticRelativeStrengthIndex", "stochasticrelativestrengthindex", self._stochasticrelativestrengthindex.current.value)
            # Plot all attributes of self._stochasticrelativestrengthindex
            self.plot("StochasticRelativeStrengthIndex", "k", self._stochasticrelativestrengthindex.k.current.value)
            self.plot("StochasticRelativeStrengthIndex", "d", self._stochasticrelativestrengthindex.d.current.value)

For more information about this indicator, see its referencereference.

Visualization

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

StochasticRelativeStrengthIndex line plot.

Indicator History

To get the historical data of the StochasticRelativeStrengthIndex 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 StochasticRelativeStrengthIndexAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private StochasticRelativeStrengthIndex _srsi;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _srsi = SRSI(_symbol, 14, 14, 3, 3);

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

        // Access all attributes of indicatorHistory
        var k = indicatorHistory.Select(x => ((dynamic)x).K).ToList();
        var d = indicatorHistory.Select(x => ((dynamic)x).D).ToList();
    }
}
class StochasticRelativeStrengthIndexAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        self._srsi = self.srsi(self._symbol, 14, 14, 3, 3)

        indicator_history = self.indicator_history(self._srsi, self._symbol, 100, Resolution.MINUTE)
        timedelta_indicator_history = self.indicator_history(self._srsi, self._symbol, timedelta(days=10), Resolution.MINUTE)
        time_period_indicator_history = self.indicator_history(self._srsi, 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
        k = indicator_history_df["k"]
        d = indicator_history_df["d"]

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: