title: Python: How to code an exponential moving average?
body: <p>I want to do calculations on three columns of a dataframe <code>df</code>. In order to do that I want run a price of assets (cryptocurrencies) list in a three column table in order to calculate the exponential moving average of them after having enough data.</p>
  
  <pre><code>def calculateAllEMA(self,values_array):
      df = pd.DataFrame(values_array, columns=['BTC', 'ETH', 'DASH'])
      column_by_search = ["BTC", "ETH", "DASH"]
      print(df)
      for i,column in enumerate(column_by_search):
          ema=[]
          # over and over for each day that follows day 23 to get the full range of EMA
          for j in range(0, len(column)-24):
              # Add the closing prices for the first 22 days together and divide them by 22.
              EMA_yesterday = column.iloc[1+j:22+j].mean()
              k = float(2)/(22+1)
              # getting the first EMA day by taking the following day’s (day 23) closing price multiplied by k, then multiply the previous day’s moving average by (1-k) and add the two.
              ema.append(column.iloc[23 + j]*k+EMA_yesterday*(1-k))
          print("ema")
          print(ema)
          mean_exp[i] = ema[-1]
      return mean_exp
  </code></pre>
  
  <p>Yet, when I print what's in <code>len(column)-24</code> I get -21 (-24 + 3 ?). I can't therefore go through the loop. How can I cope with this error to get exponential moving average of the assets ?</p>
  
  <p>I tried to apply <a href="http://www.iexplain.org/ema-how-to-calculate/" rel="noreferrer">this link from iexplain.com</a> for the pseudo code of the exponential moving average. </p>
  
  <p>If you have any easier idea, I'm open to hear it.</p>
  
  <p>Here is the data that I use to calculate it when it bugs :</p>
  
  <pre><code>        BTC     ETH    DASH
  0   4044.59  294.40  196.97
  1   4045.25  294.31  196.97
  2   4044.59  294.40  196.97
  3   4045.25  294.31  196.97
  4   4044.59  294.40  196.97
  5   4045.25  294.31  196.97
  6   4044.59  294.40  196.97
  7   4045.25  294.31  196.97
  8   4045.25  294.31  196.97
  9   4044.59  294.40  196.97
  10  4045.25  294.31  196.97
  11  4044.59  294.40  196.97
  12  4045.25  294.31  196.97
  13  4045.25  294.32  197.07
  14  4045.25  294.31  196.97
  15  4045.41  294.46  197.07
  16  4045.25  294.41  197.07
  17  4045.41  294.41  197.07
  18  4045.41  294.47  197.07
  19  4045.25  294.41  197.07
  20  4045.25  294.32  197.07
  21  4045.43  294.35  197.07
  22  4045.41  294.46  197.07
  23  4045.25  294.41  197.07
  </code></pre>