From a530e27a2d55d0b5f1cbb11f83388b9e22ff2069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berk=20Bilgi=C3=A7?= <48670757+bbilgic@users.noreply.github.com> Date: Wed, 9 Dec 2020 03:50:09 +0300 Subject: [PATCH 1/4] Volume Weighted MACD added. --- ta/trend.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/ta/trend.py b/ta/trend.py index 4c596939..ee57006b 100644 --- a/ta/trend.py +++ b/ta/trend.py @@ -146,6 +146,72 @@ def macd_diff(self) -> pd.Series: macd_diff_series, name=f"MACD_diff_{self._window_fast}_{self._window_slow}" ) + +class VWMACD(IndicatorMixin): + """Volume Weighted Moving Average Convergence Divergence (MACD) + + Is a trend-following momentum indicator that shows the relationship between + two moving averages of prices with volume. + + https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/V-Z/VolumeWeightedMACD + + Args: + close(pandas.Series): dataset 'Close' column. + volume(pandas.Series): dataset 'Volume' column. + n_fast(int): n period short-term. + n_slow(int): n period long-term. + n_sign(int): n period to signal. + fillna(bool): if True, fill nan values. + """ + def __init__(self, + close: pd.Series, + volume: pd.Series, + n_slow: int = 26, + n_fast: int = 12, + n_sign: int = 9, + fillna: bool = False): + self._close = close + self._volume = volume + self._n_slow = n_slow + self._n_fast = n_fast + self._n_sign = n_sign + self._fillna = fillna + self._run() + + def _run(self): + self._emafast = ema(self._close*self._volume, self._n_fast, self._fillna)/ema(self._volume, self._n_fast, self._fillna) + self._emaslow = ema(self._close*self._volume, self._n_slow, self._fillna)/ema(self._volume, self._n_slow, self._fillna) + self._macd = self._emafast - self._emaslow + self._macd_signal = ema(self._macd, self._n_sign, self._fillna) + self._macd_diff = self._macd - self._macd_signal + + def macd(self) -> pd.Series: + """MACD Line + + Returns: + pandas.Series: New feature generated. + """ + macd = self._check_fillna(self._macd, value=0) + return pd.Series(macd, name=f'MACD_{self._n_fast}_{self._n_slow}') + + def macd_signal(self) -> pd.Series: + """Signal Line + + Returns: + pandas.Series: New feature generated. + """ + + macd_signal = self._check_fillna(self._macd_signal, value=0) + return pd.Series(macd_signal, name=f'MACD_sign_{self._n_fast}_{self._n_slow}') + + def macd_diff(self) -> pd.Series: + """MACD Histogram + + Returns: + pandas.Series: New feature generated. + """ + macd_diff = self._check_fillna(self._macd_diff, value=0) + return pd.Series(macd_diff, name=f'MACD_diff_{self._n_fast}_{self._n_slow}') class EMAIndicator(IndicatorMixin): """EMA - Exponential Moving Average From eb936dc837a47be07bcebb577075a1e09c80727a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berk=20Bilgi=C3=A7?= <48670757+bbilgic@users.noreply.github.com> Date: Sun, 13 Dec 2020 01:19:48 +0300 Subject: [PATCH 2/4] ema not found fixed --- ta/trend.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/ta/trend.py b/ta/trend.py index ee57006b..9ed8d780 100644 --- a/ta/trend.py +++ b/ta/trend.py @@ -146,8 +146,7 @@ def macd_diff(self) -> pd.Series: macd_diff_series, name=f"MACD_diff_{self._window_fast}_{self._window_slow}" ) - -class VWMACD(IndicatorMixin): + class VWMACD(IndicatorMixin): """Volume Weighted Moving Average Convergence Divergence (MACD) Is a trend-following momentum indicator that shows the relationship between @@ -179,10 +178,10 @@ def __init__(self, self._run() def _run(self): - self._emafast = ema(self._close*self._volume, self._n_fast, self._fillna)/ema(self._volume, self._n_fast, self._fillna) - self._emaslow = ema(self._close*self._volume, self._n_slow, self._fillna)/ema(self._volume, self._n_slow, self._fillna) + self._emafast = _ema(self._close*self._volume, self._n_fast, self._fillna)/_ema(self._volume, self._n_fast, self._fillna) + self._emaslow = _ema(self._close*self._volume, self._n_slow, self._fillna)/_ema(self._volume, self._n_slow, self._fillna) self._macd = self._emafast - self._emaslow - self._macd_signal = ema(self._macd, self._n_sign, self._fillna) + self._macd_signal = _ema(self._macd, self._n_sign, self._fillna) self._macd_diff = self._macd - self._macd_signal def macd(self) -> pd.Series: From 5cd3b2a464fbb07430d02ff7a365ab855029e74d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berk=20Bilgi=C3=A7?= <48670757+bbilgic@users.noreply.github.com> Date: Sun, 13 Dec 2020 01:22:31 +0300 Subject: [PATCH 3/4] indent fixed --- ta/trend.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ta/trend.py b/ta/trend.py index 9ed8d780..5521dcbc 100644 --- a/ta/trend.py +++ b/ta/trend.py @@ -145,8 +145,9 @@ def macd_diff(self) -> pd.Series: return pd.Series( macd_diff_series, name=f"MACD_diff_{self._window_fast}_{self._window_slow}" ) - - class VWMACD(IndicatorMixin): + + +class VWMACD(IndicatorMixin): """Volume Weighted Moving Average Convergence Divergence (MACD) Is a trend-following momentum indicator that shows the relationship between From bb553853aac438618ca5c98aa3f2da13d3953445 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berk=20Bilgi=C3=A7?= <48670757+bbilgic@users.noreply.github.com> Date: Sun, 13 Dec 2020 01:26:38 +0300 Subject: [PATCH 4/4] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 80dac080..4890875d 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ The library has implemented 42 indicators: * Exponential Moving Average (EMA) * Weighted Moving Average (WMA) * Moving Average Convergence Divergence (MACD) +* Volume Weighted Moving Average Convergence Divergence (VWMACD) * Average Directional Movement Index (ADX) * Vortex Indicator (VI) * Trix (TRIX)