Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

KAMA classmethod - Need to handle Efficiency Ratio calculation when volatility is zero #116

Open
edgetrader opened this issue May 6, 2021 · 1 comment

Comments

@edgetrader
Copy link

Possible to have nan values in the pd.Series output from def ER() classmethod when volatility is zero. Once there are nan values, smoothing_constant will have nan values. Maybe should consider dropping records with nan smoothing_constant or force the smoothing_constant to zero?

    @classmethod
    def ER(cls, ohlc: DataFrame, period: int = 10, column: str = "close") -> Series:
        """The Kaufman Efficiency indicator is an oscillator indicator that oscillates between +100 and -100, where zero is the center point.
         +100 is upward forex trending market and -100 is downwards trending markets."""

        change = ohlc[column].diff(period).abs()
        volatility = ohlc[column].diff().abs().rolling(window=period).sum()

        return pd.Series(change / volatility, name="{0} period ER".format(period))

    @classmethod
    def KAMA(
        cls,
        ohlc: DataFrame,
        er: int = 10,
        ema_fast: int = 2,
        ema_slow: int = 30,
        period: int = 20,
        column: str = "close",
    ) -> Series:
        """Developed by Perry Kaufman, Kaufman's Adaptive Moving Average (KAMA) is a moving average designed to account for market noise or volatility.
        Its main advantage is that it takes into consideration not just the direction, but the market volatility as well."""

        er = cls.ER(ohlc, er)
        fast_alpha = 2 / (ema_fast + 1)
        slow_alpha = 2 / (ema_slow + 1)
        sc = pd.Series(
            (er * (fast_alpha - slow_alpha) + slow_alpha) ** 2,
            name="smoothing_constant",
        )  ## smoothing constant

        ... ...

@0xDub
Copy link

0xDub commented Aug 31, 2021

+1

I copied finta locally and made the adjustment:
sc = pd.Series( (er * (fast_alpha - slow_alpha) + slow_alpha) ** 2, name="smoothing_constant", ).fillna(0) ## smoothing constant

".fillna(0)" added to the end of pd.Series(), it's no longer throwing NaN values

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants