Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added mamode in hma #872

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pandas_ta/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1202,9 +1202,9 @@ def hlc3(self, offset=None, **kwargs: DictLike):
result = hlc3(high=high, low=low, close=close, offset=offset, **kwargs)
return self._post_process(result, **kwargs)

def hma(self, length=None, offset=None, **kwargs: DictLike):
def hma(self, length=None, mamode=None, offset=None, **kwargs: DictLike):
close = self._get_column(kwargs.pop("close", "close"))
result = hma(close=close, length=length, offset=offset, **kwargs)
result = hma(close=close, length=length, mamode=mamode,offset=offset, **kwargs)
return self._post_process(result, **kwargs)

def hwma(self, na=None, nb=None, nc=None, offset=None, **kwargs: DictLike):
Expand Down
24 changes: 18 additions & 6 deletions pandas_ta/overlap/hma.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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:
Expand All @@ -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)

Expand All @@ -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:
Expand Down
Loading