def EMA(df_close, N):
# df_close放資料一列,N是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
EMA 指數平均數名額
def EMA(df_close, N)
df_close: 價格序号從0開始一列(n*1)
N:N期指數平均(int)
以下是我随便扔進去的一個期貨的close價格的圖檔,橙色是移動平均的,藍色是真實值,資料描述:
history(symbol='SHFE.RB', frequency='600s', start_time='2014-01-01', end_time='2015-01-03', fields='', df = True)