def get_EMA(df,a): for i in range(len(df)): if i==0: df.ix[i,'ema']=df.ix[i,'close'] if i>0: df.ix[i,'ema']=(1-a)*df.ix[i-1,'ema']+a*df.ix[i,'close'] return df from WindPy import* import pandas as pd import numpy as np import datetime import time #获取数据 w.start(60) symbol="000001.SZ" start_date="2016-01-01" end_date="2017-05-02" stock=w.wsd(symbol, "trade_code,open,high,low,close,pre_close,\ volume,amt", start_date,end_date) df = pd.DataFrame() df['date']=stock.Times df['stock_code']=stock.Data[0] #index_data['stock_code'] =symbol df['open'] =stock.Data[1] df['high'] =stock.Data[2] df['low'] =stock.Data[3] df['close']=stock.Data[4] df['pre_close']=stock.Data[5] df['volume']=stock.Data[6] df['amt']=stock.Data[7] df get_EMA(df,0.7)
#基于pandas的计算,效率有点低。