Skip to content

Class RelativeStrengthIndex

  • Jump right in for a hands-on Open In Colab

Import

from NitroFE import RelativeStrengthIndex

RelativeStrengthIndex

The RSI is classified as a momentum oscillator, measuring the velocity and magnitude of price movements. Momentum is the rate of the rise or fall in price.

\[ Difference\_value[t] (DFV) = dataframe[t] - dataframe[t-1] \]
\[ Upwards\_movement[t] (UM) = DFV[t] \ if \ DFV[t]>0 \ else \ 0 \]
\[ Downward\_movement[t] (DM) = |DFV[t]| \ if \ DFV[t]<0 \ else \ 0 \]
\[ RS = \frac{Smoothed \ moving \ average \ of \ UM \ over \ 'lookback\_period' \ period }{Smoothed \ moving \ average \ of \ DM \ over \ 'lookback\_period' \ period} \]
\[ RSI = 100 -\frac{100}{1+RS} \]

Methods

__init__(self, lookback_period=8) special

Parameters:

Name Type Description Default
lookback_period int

Size of the rolling window for lookback, by default 8

8
Source code in nitrofe\time_based_features\indicator_features\_relativestrengthindex.py
def __init__(self, lookback_period: int = 8):
    """
    Parameters
    ----------
    lookback_period : int, optional
        Size of the rolling window for lookback, by default 8
    """
    self.lookback_period = lookback_period

fit(self, dataframe, first_fit=True)

Provided dataframe must be in ascending order.

For your training/initial fit phase (very first fit) use fit_first=True, and for any production/test implementation pass fit_first=False

Parameters:

Name Type Description Default
dataframe Union[pandas.core.frame.DataFrame, pandas.core.series.Series]

dataframe containing column values to create feature over

required
first_fit bool

Indicator features require past values for calculation. Use True, when calculating for training data (very first fit) Use False, when calculating for subsequent testing/production data { in which case the values, which were saved during the last phase, will be utilized for calculation }, by default True

True
Source code in nitrofe\time_based_features\indicator_features\_relativestrengthindex.py
def fit(self, dataframe: Union[pd.DataFrame, pd.Series], first_fit: bool = True):
    """

    Provided dataframe must be in ascending order.

    For your training/initial fit phase (very first fit) use fit_first=True, and for any production/test implementation pass fit_first=False

    Parameters
    ----------
    dataframe : Union[pd.DataFrame, pd.Series]
        dataframe containing column values to create feature over
    first_fit : bool, optional
        Indicator features require past values for calculation.
        Use True, when calculating for training data (very first fit)
        Use False, when calculating for subsequent testing/production data { in which case the values, which
        were saved during the last phase, will be utilized for calculation }, by default True
    """

    if first_fit:
        self._up_object = weighted_window_features()
        self._down_object = weighted_window_features()

        self._up_smoothed = SmoothedMovingAverage(
            lookback_period=self.lookback_period
        )
        self._down_smoothed = SmoothedMovingAverage(
            lookback_period=self.lookback_period
        )

    if isinstance(dataframe, pd.Series):
        dataframe = dataframe.to_frame()

    up_value = self._up_object._template_feature_calculation(
        function_name="_up_object",
        win_function=_identity_window,
        first_fit=first_fit,
        dataframe=dataframe,
        window=2,
        min_periods=None,
        symmetric=None,
        operation=self._diff_pos,
        operation_args=(),
    )

    down_value = self._down_object._template_feature_calculation(
        function_name="_down_object",
        win_function=_identity_window,
        first_fit=first_fit,
        dataframe=dataframe,
        window=2,
        min_periods=None,
        symmetric=None,
        operation=self._diff_neg,
        operation_args=(),
    )

    smoothed_up_value = self._up_smoothed.fit(
        dataframe=up_value, first_fit=first_fit
    )
    smoothed_down_value = self._down_smoothed.fit(
        dataframe=down_value, first_fit=first_fit
    )

    rsi = 100 - 100 / (1 + (smoothed_up_value / smoothed_down_value))
    return rsi

References