Skip to content

Instantly share code, notes, and snippets.

@dharma6872
Created February 10, 2021 07:55
Show Gist options
  • Save dharma6872/469fa4d9eaace65121dc6dce87f60d02 to your computer and use it in GitHub Desktop.
Save dharma6872/469fa4d9eaace65121dc6dce87f60d02 to your computer and use it in GitHub Desktop.
[지수 이동평균] #퀀트
# 가중 이동평균 함수
# 지수 이동평균 함수
def ema(Data, alpha, lookback, what, where):
# alpha is the smoothing factor
# window is the lookback period
# what is the column that needs to have its average calculated
# where is where to put the exponential moving average
alpha = alpha / (lookback + 1.0)
beta = 1 - alpha
# First value is a simple SMA
Data = ma(Data, lookback, what, where)
# Calculating first EMA
Data[lookback + 1, where] = (Data[lookback + 1, what] * alpha) + (Data[lookback, where] * beta) # Calculating the rest of EMA
for i in range(lookback + 2, len(Data)):
try:
Data[i, where] = (Data[i, what] * alpha) + (Data[i - 1, where] * beta)
except IndexError:
pass
return Data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment