Supported Indicators

Relative Vigor Index

Introduction

The Relative Vigor Index (RVI) compares the ratio of the closing price of a security to its trading range. For illustration, let: a = Close−Openb = Close−Open of One Bar Prior to ac = Close−Open of One Bar Prior to bd = Close−Open of One Bar Prior to ce = High−Low of Bar af = High−Low of Bar bg = High−Low of Bar ch = High−Low of Bar d Then let (a+2*(b+c)+d)/6 be NUM and (e+2*(f+g)+h)/6 be DENOM. RVI = SMA(NUM)/SMA(DENOM) for a specified period. https://www.investopedia.com/terms/r/relative_vigor_index.asp

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

Using RVI Indicator

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

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _rvi = RVI(_symbol, 20, MovingAverageType.Simple);
    }

    public override void OnData(Slice data)
    {

        if (_rvi.IsReady)
        {
            // The current value of _rvi is represented by itself (_rvi)
            // or _rvi.Current.Value
            Plot("RelativeVigorIndex", "rvi", _rvi);
            // Plot all properties of abands
            Plot("RelativeVigorIndex", "signal", _rvi.Signal);
        }
    }
}
class RelativeVigorIndexAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        self._rvi = self.rvi(self._symbol, 20, MovingAverageType.SIMPLE)

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

        if self._rvi.is_ready:
            # The current value of self._rvi is represented by self._rvi.current.value
            self.plot("RelativeVigorIndex", "rvi", self._rvi.current.value)
            # Plot all attributes of self._rvi
            self.plot("RelativeVigorIndex", "signal", self._rvi.signal.current.value)

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

You can manually create a RelativeVigorIndex 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 RelativeVigorIndexAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private RelativeVigorIndex _relativevigorindex;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _relativevigorindex = new RelativeVigorIndex(20, MovingAverageType.Simple);
    }

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

        if (_relativevigorindex.IsReady)
        {
            // The current value of _relativevigorindex is represented by itself (_relativevigorindex)
            // or _relativevigorindex.Current.Value
            Plot("RelativeVigorIndex", "relativevigorindex", _relativevigorindex);
            // Plot all properties of abands
            Plot("RelativeVigorIndex", "signal", _relativevigorindex.Signal);
        }
    }
}
class RelativeVigorIndexAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        self._relativevigorindex = RelativeVigorIndex(20, MovingAverageType.SIMPLE)

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

        if self._relativevigorindex.is_ready:
            # The current value of self._relativevigorindex is represented by self._relativevigorindex.current.value
            self.plot("RelativeVigorIndex", "relativevigorindex", self._relativevigorindex.current.value)
            # Plot all attributes of self._relativevigorindex
            self.plot("RelativeVigorIndex", "signal", self._relativevigorindex.signal.current.value)

For more information about this indicator, see its referencereference.

Visualization

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

RelativeVigorIndex line plot.

Indicator History

To get the historical data of the RelativeVigorIndex 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 RelativeVigorIndexAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private RelativeVigorIndex _rvi;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _rvi = RVI(_symbol, 20, MovingAverageType.Simple);

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

        // Access all attributes of indicatorHistory
        var signal = indicatorHistory.Select(x => ((dynamic)x).Signal).ToList();
    }
}
class RelativeVigorIndexAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        self._rvi = self.rvi(self._symbol, 20, MovingAverageType.SIMPLE)

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

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: