如何在MS Excel中测试从python获得的EMA交叉

如何在MS Excel中测试从python获得的EMA交叉,python,excel,pandas,moving-average,Python,Excel,Pandas,Moving Average,指数移动平均数在本链接中解释: 我使用了以下代码: 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).me

指数移动平均数在本链接中解释:

我使用了以下代码:

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
将pandas\u datareader.data作为web导入
从日期时间导入日期时间
#
aapl_df=web.get_data_yahoo('aapl',datetime(2016,1,1),datetime(2016,03,31))
aapl_-df['SMA5']=aapl_-df['Adj-Close'].滚动(窗口=5,中心=False).平均值()
aapl_df['SMA20']=aapl_df['Adj Close'].滚动(窗口=20,中心=False).平均值()
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(跨度=20,最小周期=20)。平均值()#注释以解释最小周期
#地价与各种平均值
aapl_df['2016'][['Adj Close'、'SMA20'、'EMA5'、'EMA20']]图(figsize=(12,8));
#重置索引
aapl_df=aapl_df.reset_index()
#将nan值替换为0
aapl_df=aapl_df.fillna(0)
#
如果aapl_df['Adj Close'].iloc[len(aapl_df)-1]>aapl_df['SMA20'].iloc[len(aapl_df)-1]:
打印“20天趋势图”
#==============================================================================
#由于EMA代表连续函数,因此当,
#对于给定行,(EMA_5小于EMA_20)和(先前的EMA_5为
#大于之前的EMA_20)——或者反之亦然。
#==============================================================================
ema_先前5=aapl_df['EMA5'].班次(1)
ema_previous20=aapl_df['EMA20'].班次(1)
均线交叉1=(均线方向['EMA5']<均线方向['EMA20'])和(均线方向先前5>均线方向先前20)
ema_交叉2=(aapl_df['EMA5']>aapl_df['EMA20'])和(ema_先前5
。 我的疑问是:

1) 如果我查看aapl_df,SMA20列的前19个值为空。但EMA20的情况并非如此(除非我为计算aapl_df设置了选项min_periods=20)。我们不需要前19个值来计算EMA20吗?在我使用的另一个软件becos中,对于EMA20的计算,前19天的EMA20值显示为空白

2) 使用ewm方法计算大熊猫的EMA是否也基于滚动。也就是说,2016-03-31的EMA20仅基于最后20个值(2016-03-03至2016-03-31,即aapl_df中的指数41-60)。同样,2016-03-30的EMA20是否仅基于最后20个值(2016-03-02至2016-03-30 ie;aapl_df中的指数40-59)?产生这种疑问的原因是,在计算带窗口选项的SMA时,明确提到了“滚动”方法,但在计算EMA时却没有提到

3) 我想在MS Excel中测试EMA20对于相同数据的正确性。我该怎么做

4) 我目前正在使用上面提到的逻辑来计算EMA交叉,是否有其他方法可以使用pandas进行计算

多谢各位