EMA(Exponential Moving Average)是指数平均数指标
def EMA(df_close, N):
weight = []
for i in range(N):
weight_i = 2 / (N + 1) * (N - i) / N
weight.append(weight_i)
# print(weight_i)
weight_array = np.array(weight)
ema_n = np.empty_like(df_close)
ema_n[:, ] = np.nan
for j in range(N - 1, len(df_close)):
# from 3 to 100 these for
df_close_i = df_close.loc[j - N + 1:j, ]
ema_n[j] = sum(df_close_i * weight_array)
# print(j)
return ema_n
df_close:价格序号从0开始一列(n*1)
N:N期指数平均(int)
以下是我随便扔进去的一个期货的close价格的图片,橙色是移动平均的,蓝色是真实值,数据描述:
symbol='SHFE.RB' frequency='600s' start_time='2014-01-01' end_time='2015-01-03'