From be5d3552f5f2f816167c95a1f33a2f53ab92eb62 Mon Sep 17 00:00:00 2001 From: Stefano Ariestasia Date: Sat, 17 Aug 2024 11:27:16 +0900 Subject: [PATCH 1/3] update MADR --- technical/indicators/indicators.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/technical/indicators/indicators.py b/technical/indicators/indicators.py index dee1c24b..e5222cc4 100644 --- a/technical/indicators/indicators.py +++ b/technical/indicators/indicators.py @@ -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=2, matype="sma"): """ Moving Averae Deviation RAte just like bollinger bands ... Source: https://tradingview.com/script/25KCgL9H/ @@ -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 = ta.SMA(df, timeperiod=length) + elif matype.lower() == "ema": + ma = ta.EMA(df, timeperiod=length) + else: + ma = ta.SMA(df, timeperiod=length) + + df["rate"] = ((df["close"] / ma) * 100) - 100 + + if matype.lower() == "sma": + df["stdcenter"] = ta.SMA(df.rate, timeperiod=(length * stds)) + elif matype.lower() == "ema": + df["stdcenter"] = ta.EMA(df.rate, timeperiod=(length * stds)) + else: + df["stdcenter"] = ta.SMA(df.rate, timeperiod=(length * stds)) + + std = ta.STDDEV(df.rate, timeperiod=(length * stds)) + df["plusdev"] = df["stdcenter"] + (std * stds) + df["minusdev"] = df["stdcenter"] - (std * stds) # return stdcenter , plusdev , minusdev, rate return df From 39972b71d7bdccb01602d0dbaefdf1960e927e14 Mon Sep 17 00:00:00 2001 From: Stefano Ariestasia Date: Sat, 17 Aug 2024 11:29:07 +0900 Subject: [PATCH 2/3] ruff fix --- technical/indicators/indicators.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/technical/indicators/indicators.py b/technical/indicators/indicators.py index e5222cc4..671210cc 100644 --- a/technical/indicators/indicators.py +++ b/technical/indicators/indicators.py @@ -1061,7 +1061,7 @@ def MADR(dataframe, length=21, stds=2, matype="sma"): ma = ta.EMA(df, timeperiod=length) else: ma = ta.SMA(df, timeperiod=length) - + df["rate"] = ((df["close"] / ma) * 100) - 100 if matype.lower() == "sma": @@ -1070,7 +1070,7 @@ def MADR(dataframe, length=21, stds=2, matype="sma"): df["stdcenter"] = ta.EMA(df.rate, timeperiod=(length * stds)) else: df["stdcenter"] = ta.SMA(df.rate, timeperiod=(length * stds)) - + std = ta.STDDEV(df.rate, timeperiod=(length * stds)) df["plusdev"] = df["stdcenter"] + (std * stds) df["minusdev"] = df["stdcenter"] - (std * stds) From 0f0318bd431b4a3503d3ddb0c5658b4747daec77 Mon Sep 17 00:00:00 2001 From: Stefano Ariestasia Date: Sat, 17 Aug 2024 22:14:11 +0900 Subject: [PATCH 3/3] rename ma to ma_close, stds to stds_dist --- technical/indicators/indicators.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/technical/indicators/indicators.py b/technical/indicators/indicators.py index 671210cc..bccb94e4 100644 --- a/technical/indicators/indicators.py +++ b/technical/indicators/indicators.py @@ -1019,7 +1019,7 @@ def VIDYA(dataframe, length=9, select=True): return df["VIDYA"] -def MADR(dataframe, length=21, stds=2, matype="sma"): +def MADR(dataframe, length=21, stds_dist=2, matype="sma"): """ Moving Averae Deviation RAte just like bollinger bands ... Source: https://tradingview.com/script/25KCgL9H/ @@ -1056,24 +1056,24 @@ def MADR(dataframe, length=21, stds=2, matype="sma"): """ if matype.lower() == "sma": - ma = ta.SMA(df, timeperiod=length) + ma_close = ta.SMA(df, timeperiod=length) elif matype.lower() == "ema": - ma = ta.EMA(df, timeperiod=length) + ma_close = ta.EMA(df, timeperiod=length) else: - ma = ta.SMA(df, timeperiod=length) + ma_close = ta.SMA(df, timeperiod=length) - df["rate"] = ((df["close"] / ma) * 100) - 100 + df["rate"] = ((df["close"] / ma_close) * 100) - 100 if matype.lower() == "sma": - df["stdcenter"] = ta.SMA(df.rate, timeperiod=(length * stds)) + df["stdcenter"] = ta.SMA(df.rate, timeperiod=(length * stds_dist)) elif matype.lower() == "ema": - df["stdcenter"] = ta.EMA(df.rate, timeperiod=(length * stds)) + df["stdcenter"] = ta.EMA(df.rate, timeperiod=(length * stds_dist)) else: - df["stdcenter"] = ta.SMA(df.rate, timeperiod=(length * stds)) + df["stdcenter"] = ta.SMA(df.rate, timeperiod=(length * stds_dist)) - std = ta.STDDEV(df.rate, timeperiod=(length * stds)) - df["plusdev"] = df["stdcenter"] + (std * stds) - df["minusdev"] = df["stdcenter"] - (std * stds) + 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