title: How to calculate an exponential moving average with python faster?
body: <p>I am busy building backtesting software and I've run into a hiccup with creating the exponential moving average. I was successful in creating it using a for loop but it took about 20 seconds to run per symbol I want to test (too long).</p>
  
  <p>I am trying to find a <strong>faster</strong> solution, if anyone has any suggestions.</p>
  
  <p>My current code looks like this, but it does not produce the <strong>correct</strong> results.</p>
  
  <pre><code>def exponential_moving_average(df, period):
      # Create a copy of original dataframe to work with.
      dataframe = df.copy()
  
      dataframe['EMA'] = dataframe['Close'].ewm( span        = period,
                                                 adjust      = False,
                                                 min_periods = period,
                                                 ignore_na   = True
                                                 ).mean()
  
      return dataframe['EMA']
  </code></pre>
  
  <p>This method is in an Indicators class and the inputs take the following.</p>
  
  <ul>
  <li><code>df</code> is the <code>Open, High, Low</code> and <code>Close</code> price per day as well as any other indicators that will be used for backtesting and</li>
  <li><code>period</code> is the 'window' or number of days the exponential moving average must be calculated for.</li>
  </ul>
  
  <p>Here is a snippet with the <code>df</code> values:</p>
  
  <pre><code>            symbol     Open     High      Low    Close  ATR     slow_ma
  Date        
  2010-01-03  EURUSD  1.43075  1.43369  1.43065  1.43247  NaN       NaN   
  2010-01-04  EURUSD  1.43020  1.44560  1.42570  1.44120  NaN       NaN   
  2010-01-05  EURUSD  1.44130  1.44840  1.43460  1.43650  NaN       NaN   
  2010-01-06  EURUSD  1.43660  1.44350  1.42820  1.44060  NaN       NaN   
  2010-01-07  EURUSD  1.44070  1.44470  1.42990  1.43070  NaN       NaN   
  2010-01-08  EURUSD  1.43080  1.44380  1.42630  1.44160  NaN       NaN   
  2010-01-10  EURUSD  1.44245  1.44252  1.44074  1.44110  NaN       NaN   
  2010-01-11  EURUSD  1.44280  1.45560  1.44080  1.45120  NaN       NaN   
  2010-01-12  EURUSD  1.45120  1.45450  1.44530  1.44840  NaN       NaN   
  2010-01-13  EURUSD  1.44850  1.45790  1.44570  1.45100  NaN  1.442916   
  2010-01-14  EURUSD  1.45090  1.45550  1.44460  1.44990  NaN  1.444186   
  2010-01-15  EURUSD  1.45000  1.45110  1.43360  1.43790  NaN  1.443043   
  2010-01-17  EURUSD  1.43597  1.43655  1.43445  1.43480  NaN  1.441544   
  2010-01-18  EURUSD  1.43550  1.44000  1.43340  1.43830  NaN  1.440954   
  2010-01-19  EURUSD  1.43820  1.44130  1.42520  1.42870  NaN  1.438726
  </code></pre>
  
  <p>Here is the expected outcome for <code>slow_ma</code> (10 day period)</p>
  
  <pre><code>            symbol     Open     High      Low    Close  ATR     slow_ma
  Date        
  2010-01-03  EURUSD  1.43075  1.43369  1.43065  1.43247  NaN       NaN   
  2010-01-04  EURUSD  1.43020  1.44560  1.42570  1.44120  NaN       NaN   
  2010-01-05  EURUSD  1.44130  1.44840  1.43460  1.43650  NaN       NaN   
  2010-01-06  EURUSD  1.43660  1.44350  1.42820  1.44060  NaN       NaN   
  2010-01-07  EURUSD  1.44070  1.44470  1.42990  1.43070  NaN       NaN   
  2010-01-08  EURUSD  1.43080  1.44380  1.42630  1.44160  NaN       NaN   
  2010-01-10  EURUSD  1.44245  1.44252  1.44074  1.44110  NaN       NaN   
  2010-01-11  EURUSD  1.44280  1.45560  1.44080  1.45120  NaN       NaN   
  2010-01-12  EURUSD  1.45120  1.45450  1.44530  1.44840  NaN       NaN   
  2010-01-13  EURUSD  1.44850  1.45790  1.44570  1.45100  NaN   1.44351   
  2010-01-14  EURUSD  1.45090  1.45550  1.44460  1.44990  NaN   1.44467   
  2010-01-15  EURUSD  1.45000  1.45110  1.43360  1.43790  NaN   1.44344   
  2010-01-17  EURUSD  1.43597  1.43655  1.43445  1.43480  NaN   1.44187   
  2010-01-18  EURUSD  1.43550  1.44000  1.43340  1.43830  NaN   1.44122   
  2010-01-19  EURUSD  1.43820  1.44130  1.42520  1.42870  NaN   1.43894
  </code></pre>
  
  <p>I've changed the values of the first dataframe so that it shows the numbers that were used to calculate the values of <code>slow_ma</code>.</p>
  
  <p>This is my first post on Stackoverflow so just ask if something is unclear.</p>