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 VWMACD indicator to trend.py #214

Open
wants to merge 4 commits into
base: master
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
66 changes: 66 additions & 0 deletions ta/trend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down