Last active
March 17, 2021 17:27
-
-
Save valkheim/0a865b2f6ab207bd9bef7d6b237fc4e9 to your computer and use it in GitHub Desktop.
Arnaud Legoux Moving Average
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add_alma(df): | |
alma = pd.DataFrame(columns=["ALMA"]) | |
close = df["close"] | |
size = len(close) | |
def ALMA(data, sigma=6, offset=0.90, size=40): | |
""" | |
Arnaud Legoux Moving Average | |
:param data: data array | |
:param sigma: filter | |
:param offset: responsiveness/smoothness in range [0.0 - 1.0] | |
:param size: window size | |
""" | |
try: | |
m = offset * (size - 1) | |
s = size / sigma | |
sum_ = 0 | |
norm = 0 | |
for i in range(size): | |
coeff = math.exp(- pow(i - m, 2) / 2 * pow(s, 2)) | |
sum_ += data[size - i - 1] * coeff | |
norm += coeff | |
return sum_ / norm | |
except: | |
return math.nan | |
for i in reversed(close.index): | |
if i == 0: | |
subset = list(close)[::-1] | |
else: | |
subset = list(close)[:-i][::-1] | |
alma.loc[size - i - 1] = ALMA(subset) | |
df["ALMA"] = alma | |
return df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment