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

Adding McGinley Dynamic indicator #190

Merged
merged 1 commit into from
Jan 22, 2021
Merged
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
2 changes: 1 addition & 1 deletion pandas_ta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
# Overlap
"overlap": [
"dema", "ema", "fwma", "hilo", "hl2", "hlc3", "hma", "ichimoku",
"kama", "linreg", "midpoint", "midprice", "ohlc4", "pwma", "rma",
"kama", "linreg", "mcg", "midpoint", "midprice", "ohlc4", "pwma", "rma",
"sinwma", "sma", "ssf", "supertrend", "swma", "t3", "tema", "trima",
"vidya", "vwap", "vwma", "wcp", "wma", "zlma"
],
Expand Down
5 changes: 5 additions & 0 deletions pandas_ta/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,11 @@ def linreg(self, length=None, offset=None, adjust=None, **kwargs):
result = linreg(close=close, length=length, offset=offset, adjust=adjust, **kwargs)
return self._post_process(result, **kwargs)

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

def midpoint(self, length=None, offset=None, **kwargs):
close = self._get_column(kwargs.pop("close", "close"))
result = midpoint(close=close, length=length, offset=offset, **kwargs)
Expand Down
1 change: 1 addition & 0 deletions pandas_ta/overlap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .kama import kama
from .ichimoku import ichimoku
from .linreg import linreg
from .mcg import mcg
from .midpoint import midpoint
from .midprice import midprice
from .ohlc4 import ohlc4
Expand Down
77 changes: 77 additions & 0 deletions pandas_ta/overlap/mcg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# -*- coding: utf-8 -*-
from pandas_ta.utils import get_offset, verify_series


def mcg(close, length: int = 10, offset: int = 0, c: float = 1, **kwargs):
"""Indicator: McGinley Dynamic Indicator"""
# Validate arguments
close = verify_series(close)
length = int(length) if length > 0 else 10
c = c if 1 >= c > 0 else 1
offset = get_offset(offset)

# Calculate Result
close = close.copy()

def mcg_(series):
denom = (c * length * (series.iloc[1] / series.iloc[0]) ** 4)
series.iloc[1] = (series.iloc[0] + ((series.iloc[1] - series.iloc[0]) / denom))
return series.iloc[1]

mcg_cell = close[0:].rolling(2, min_periods=2).apply(mcg_, raw=False)
mcg_ds = close[:1].append(mcg_cell[1:])

# Offset
if offset != 0:
mcg_ds = mcg_ds.shift(offset)

# Handle fills
if "fillna" in kwargs:
mcg_ds.fillna(kwargs["fillna"], inplace=True)
if "fill_method" in kwargs:
mcg_ds.fillna(method=kwargs["fill_method"], inplace=True)

# Name & Category
mcg_ds.name = f"McGinley_{length}"
mcg_ds.category = 'overlap'

return mcg_ds


mcg.__doc__ = \
"""McGinley Dynamic Indicator

The McGinley Dynamic looks like a moving average line, yet it is actually a smoothing mechanism
for prices that minimizes price separation, price whipsaws, and hugs prices much more closely.
Because of the calculation, the Dynamic Line speeds up in down markets as it follows prices
yet moves more slowly in up markets.

Sources:
https://www.investopedia.com/articles/forex/09/mcginley-dynamic-indicator.asp

Calculation:
Default Inputs:
length=10
offset=0
c=1

def mcg_(series):
denom = (constant * length * (series.iloc[1] / series.iloc[0]) ** 4)
series.iloc[1] = (series.iloc[0] + ((series.iloc[1] - series.iloc[0]) / denom))
return series.iloc[1]
mcg_cell = close[0:].rolling(2, min_periods=2).apply(mcg_, raw=False)
mcg_ds = close[:1].append(mcg_cell[1:])

Args:
close (pd.Series): Series of 'close's
length (int): Indicator's period. Default: 10
offset (int): Number of periods to offset the result. Default: 0
c (float): Multiplier for the denominator, sometimes set to 0.6. Default: 1

Kwargs:
fillna (value, optional): pd.DataFrame.fillna(value)
fill_method (value, optional): Type of fill method

Returns:
pd.Series: New feature generated.
"""