Skip to content

Class AverageTrueRange

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

Import

from NitroFE import AverageTrueRange

AverageTrueRange

Average true range is a volatility indicator.The indicator does not provide an indication of price trend, simply the degree of volatility.The average true range is an N-period smoothed moving average of the true range values.

\[ first\_range[t]= \max{(dataframe[t-true\_range\_lookback \to t])} -\min{(dataframe[t-true\_range\_lookback \to t])} \]
\[ second\_range[t]= |{\max{(dataframe[t-true\_range\_lookback \to t])} - dataframe[t] )}| \]
\[ third\_range[t]= |{\min{(dataframe[t-true\_range\_lookback \to t])} - dataframe[t] )}| \]
\[ True\_Range[t] = \max{ (first\_range[t], second\_range[t], third\_range[t]) } \]
\[ Average\_True\_range[t] = \operatorname{Mean}(True\_Range[t-average\_true\_range\_span \to t]) \]

Methods

__init__(self, true_range_lookback=4, average_true_range_span=6, true_range_min_periods=None, average_true_range_periods=1, return_true_range=False) special

Parameters:

Name Type Description Default
true_range_lookback int

Size of the rolling window for true range value calculation, by default 4

4
average_true_range_span int

Size of the rolling window for average true range value calculation, by default 6

6
true_range_min_periods int

Minimum number of observations in window required to have a value for true range calculation, by default None

None
average_true_range_periods int

Minimum number of observations in window required to have a value for average true range calculation , by default 1

1
return_true_range bool

If true, True range is returned instead of Average True range

False
Source code in nitrofe\time_based_features\indicator_features\_averagetruerange.py
def __init__(
    self,
    true_range_lookback: int = 4,
    average_true_range_span: int = 6,
    true_range_min_periods: int = None,
    average_true_range_periods: int = 1,
    return_true_range: bool = False,
):
    """
    Parameters
    ----------
    true_range_lookback : int, optional
        Size of the rolling window for true range value calculation, by default 4
    average_true_range_span : int, optional
        Size of the rolling window for average true range value calculation, by default 6
    true_range_min_periods : int, optional
        Minimum number of observations in window required to have a value for true range calculation, by default None
    average_true_range_periods : int, optional
        Minimum number of observations in window required to have a value for average true range calculation , by default 1
    return_true_range : bool, optional
        If true, True range is returned instead of Average True range
    """

    self.true_range_lookback = true_range_lookback
    self.average_true_range_span = average_true_range_span

    self.true_range_min_periods = true_range_min_periods
    self.average_true_range_periods = average_true_range_periods
    self.return_true_range = return_true_range

fit(self, dataframe, first_fit=True)

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\_averagetruerange.py
def fit(self, dataframe: Union[pd.DataFrame, pd.Series], first_fit: bool = True):
    """
    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._true_range_moving_average_object = weighted_window_features()
        self._average_true_range_moving_average_object = weighted_window_features()

    true_range = (
        self._true_range_moving_average_object._template_feature_calculation(
            function_name="true_range",
            win_function=_equal_window,
            first_fit=first_fit,
            dataframe=dataframe,
            window=self.true_range_lookback,
            min_periods=self.true_range_min_periods,
            symmetric=None,
            operation=self.true_range,
            operation_args=(),
        )
    )
    if self.return_true_range:
        return true_range

    average_true_range = self._average_true_range_moving_average_object._template_feature_calculation(
        function_name="average_true_range",
        win_function=_equal_window,
        first_fit=first_fit,
        dataframe=true_range,
        window=self.true_range_lookback,
        min_periods=self.average_true_range_periods,
        symmetric=None,
        operation=np.mean,
        operation_args=(),
    )

    return average_true_range

References