Skip to content

Instantly share code, notes, and snippets.

@theAfricanQuant
Forked from valkheim/alma.py
Created March 17, 2021 17:27
Show Gist options
  • Save theAfricanQuant/5a47b56f9c7fd4a852a75adac9174f17 to your computer and use it in GitHub Desktop.
Save theAfricanQuant/5a47b56f9c7fd4a852a75adac9174f17 to your computer and use it in GitHub Desktop.
Arnaud Legoux Moving Average
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