From c481cc6d38ec6b742b039de4e3066a5e5e6cdb16 Mon Sep 17 00:00:00 2001 From: XotEmBotZ Date: Mon, 6 Jan 2025 11:10:08 +0530 Subject: [PATCH] Added mamode in hma --- pandas_ta/overlap/hma.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/pandas_ta/overlap/hma.py b/pandas_ta/overlap/hma.py index b7bea309..4b4a1228 100644 --- a/pandas_ta/overlap/hma.py +++ b/pandas_ta/overlap/hma.py @@ -1,14 +1,15 @@ # -*- coding: utf-8 -*- +from sys import modules as sys_modules from numpy import sqrt from pandas import Series from pandas_ta._typing import DictLike, Int -from pandas_ta.utils import v_offset, v_pos_default, v_series +from pandas_ta.utils import v_offset, v_pos_default, v_series,v_mamode from .wma import wma - +from .ema import ema def hma( - close: Series, length: Int = None, + close: Series, length: Int = None,mamode: str = None, offset: Int = None, **kwargs: DictLike ) -> Series: """Hull Moving Average (HMA) @@ -22,6 +23,7 @@ def hma( Args: close (pd.Series): Series of 'close's length (int): It's period. Default: 10 + mamode (str): Options: 'ema', 'wma'. Default: 'wma' offset (int): How many periods to offset the result. Default: 0 Kwargs: @@ -31,6 +33,7 @@ def hma( pd.Series: New feature generated. """ # Validate + mamode = v_mamode(mamode, "wma") length = v_pos_default(length, 10) close = v_series(close, length + 2) @@ -39,13 +42,22 @@ def hma( offset = v_offset(offset) + supported_mas = [ + "ema","wma" + ] + + if mamode not in supported_mas: + return + # Calculate half_length = int(length / 2) sqrt_length = int(sqrt(length)) - wmaf = wma(close=close, length=half_length) - wmas = wma(close=close, length=length) - hma = wma(close=2 * wmaf - wmas, length=sqrt_length) + fn=getattr(sys_modules[__name__],mamode) + + maf = fn(close=close, length=half_length) + mas = fn(close=close, length=length) + hma = fn(close=2 * maf - mas, length=sqrt_length) # Offset if offset != 0: