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) diff --git a/ta/trend.py b/ta/trend.py index 4c596939..5521dcbc 100644 --- a/ta/trend.py +++ b/ta/trend.py @@ -145,7 +145,73 @@ 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): + """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