如何在MS Excel中testing从pandaspython获得的EMA Crossover

指数移动平均值在此链接中解释: http : //www.investopedia.com/terms/e/ema.asp

我已经使用了下面的代码:

import pandas_datareader.data as web from datetime import datetime # aapl_df = web.get_data_yahoo('AAPL', datetime(2016, 1, 1), datetime(2016, 03, 31)) aapl_df['SMA5'] = aapl_df['Adj Close'].rolling(window=5,center=False).mean() aapl_df['SMA20'] = aapl_df['Adj Close'].rolling(window=20,center=False).mean() aapl_df['EMA5'] = aapl_df['Adj Close'].ewm(span=5).mean() aapl_df['EMA20'] = aapl_df['Adj Close'].ewm(span=20).mean() #aapl_df['EMA20'] = aapl_df['Adj Close'].ewm(span=20,min_periods=20).mean() # commented to explain the min_periods # Plot price vs various mean aapl_df['2016'][['Adj Close', 'SMA20','EMA5', 'EMA20']].plot(figsize=(12,8)); # Reset the index aapl_df = aapl_df.reset_index() #Replace nan values with 0 aapl_df = aapl_df.fillna(0) # if aapl_df['Adj Close'].iloc[len(aapl_df)-1] > aapl_df['SMA20'].iloc[len(aapl_df)-1]: print "20 day trendUP" #============================================================================== # Since the EMAs represent continuous functions, there is a crossing when, # for a given row, (EMA_5 is less than EMA_20) and (the previous EMA_5 is # greater than the previous EMA_20) -- or vice versa. #============================================================================== ema_previous5 = aapl_df['EMA5'].shift(1) ema_previous20 = aapl_df['EMA20'].shift(1) ema_crossing1 = (aapl_df['EMA5'] < aapl_df['EMA20']) & (ema_previous5 > ema_previous20) ema_crossing2 = (aapl_df['EMA5'] > aapl_df['EMA20']) & (ema_previous5 < ema_previous20) ema_crossing = ema_crossing1 | ema_crossing2 ema_crossing_dates = aapl_df.loc[ema_crossing, 'Date'] print ema_crossing_dates 

。 我的怀疑:

1)如果我查看aapl_df,则列SMA20的前19个值是空的。 但是,对于EMA20来说,情况并非如此(除非我为了计算aapl_df而设置了选项min_periods = 20)。 我们不需要计算EMA20的前19个值吗? 这个疑问已经出现了,因为在我使用的另一个软件中,计算EMA20时,前19天EMA20的价值显示为空白。

2)pandas的EMA计算是否也使用ewm方法。 也就是说,2016年3月31日的EMA20仅基于最后的20个值(2016-03-03至2016-03-31即aapl_df中的索引41-60)。 同样,2016-03-30的EMA20仅基于最后的20个值(2016-03-02至2016-03-30,即aapl_df中的索引40-59)? 这个疑问已经出现了,因为在用窗口选项计算SMA时明确提到了“滚动”的方法,但是EMA的计算不是这样的。

3)我想在MS Excel中testing相同数据的EMA20的正确性。 我怎样才能做到这一点 ?

4)目前我正在使用上面提到的逻辑来计算EMA交叉,有没有使用pandas的方法呢?

谢谢。