Skip to content

Commit

Permalink
Merge pull request #473 from stash86/madr
Browse files Browse the repository at this point in the history
update MADR
  • Loading branch information
xmatthias committed Aug 17, 2024
2 parents e6186ce + 0f0318b commit 26b0090
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions technical/indicators/indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ def VIDYA(dataframe, length=9, select=True):
return df["VIDYA"]


def MADR(dataframe, length=21, stds=2):
def MADR(dataframe, length=21, stds_dist=2, matype="sma"):
"""
Moving Average Deviation Rate, similar to bollinger bands
Source: https://tradingview.com/script/25KCgL9H/
Expand Down Expand Up @@ -1054,13 +1054,26 @@ def MADR(dataframe, length=21, stds=2):
_plusDev = _stdCenter + _std * 2
_minusDev = _stdCenter - _std * 2
"""
df["sma"] = ta.SMA(df, timeperiod=length)
df["rate"] = (df["close"] / df["sma"]) * 100 - 100
df["stdcenter"] = ta.SMA(df.rate, timeperiod=length * stds)
df["std"] = ta.STDDEV(df.rate, timeperiod=length * stds)
df["plusdev"] = df["stdcenter"] + df["std"] * stds
df["minusdev"] = df["stdcenter"] - df["std"] * stds
df = df.drop(columns=["sma", "std"])

if matype.lower() == "sma":
ma_close = ta.SMA(df, timeperiod=length)
elif matype.lower() == "ema":
ma_close = ta.EMA(df, timeperiod=length)
else:
ma_close = ta.SMA(df, timeperiod=length)

df["rate"] = ((df["close"] / ma_close) * 100) - 100

if matype.lower() == "sma":
df["stdcenter"] = ta.SMA(df.rate, timeperiod=(length * stds_dist))
elif matype.lower() == "ema":
df["stdcenter"] = ta.EMA(df.rate, timeperiod=(length * stds_dist))
else:
df["stdcenter"] = ta.SMA(df.rate, timeperiod=(length * stds_dist))

std = ta.STDDEV(df.rate, timeperiod=(length * stds_dist))
df["plusdev"] = df["stdcenter"] + (std * stds_dist)
df["minusdev"] = df["stdcenter"] - (std * stds_dist)
# return stdcenter , plusdev , minusdev, rate
return df

Expand Down

0 comments on commit 26b0090

Please sign in to comment.