title: How to Test EMA Crossover obtained from pandas python in MS Excel
body: <p>Exponential Moving Average is explained here in this link:  <a href="http://www.investopedia.com/terms/e/ema.asp" rel="nofollow">http://www.investopedia.com/terms/e/ema.asp</a></p>
  
  <p>I have used the following code: </p>
  
  <pre><code>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] &gt; 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'] &lt; aapl_df['EMA20']) &amp; (ema_previous5 &gt; ema_previous20)
  ema_crossing2 = (aapl_df['EMA5'] &gt; aapl_df['EMA20']) &amp; (ema_previous5 &lt; ema_previous20)
  ema_crossing = ema_crossing1 | ema_crossing2
  ema_crossing_dates = aapl_df.loc[ema_crossing, 'Date']
  print ema_crossing_dates
  </code></pre>
  
  <p>.
  My Doubts:</p>
  
  <p>1) If I look into aapl_df, the first 19 values for the column SMA20 are blank. But the same is no so for EMA20 (unless I set the option min_periods=20 for calculation of aapl_df ). Don't we need the first 19 values for calculation of EMA20 ? This doubt has arisen becos, in another software that I am using, for the calculation of EMA20, the value of EMA20 for the first 19 days show up as blank.</p>
  
  <p>2) Is the EMA calculation in pandas using the ewm method also on rolling basis. Which means, is the EMA20 for 2016-03-31 based only on the last 20 values (for 2016-03-03 to 2016-03-31 ie; index 41-60 in aapl_df). Likewise is the EMA20 for 2016-03-30 based only on the last 20 values (for 2016-03-02 to 2016-03-30 ie; index 40-59 in aapl_df)? This doubt has arisen becos the method "rolling" is explicitly mentioned in calculation of SMA with the window option, but not so for calculation of EMA.</p>
  
  <p>3) I want to test the correctness of EMA20 in MS Excel for the same data. How can I do this ?</p>
  
  <p>4) I am presently using the logic mentioned above to calculate the EMA crossover, Is there any alternative method of doing it using pandas ? </p>
  
  <p>Thank You.</p>