title: How do I correctly calculate EMA for a stock using Ta-lib or Pandas?
body: <p><strong>Edit!</strong></p>
  
  <p>For anyone wondering this same thing, I figured it out. There is nothing wrong with the implementations below. Its just the fact that EMA requires more than 21 data points to count a 20 data point exponential moving average. The reason for this is that the earlier data points effect the datapoints you are trying to calculate. In simple terms you i tested and you need about 40-50 datapoints to get the same 20 day EMA as with 100+ datapoints.</p>
  
  <hr>
  
  <p>I'm trying to calculate the EMA (Exponential moving average) of a stock, but there is something wrong with my calculations. I have exported the last 22+ days of stock data for AAPL, and when I try to calculate the EMA for this there is something wrong every time.   </p>
  
  <p>Here is the data for my example: <a href="https://pastebin.com/raw/2MsgCeQx" rel="noreferrer">https://pastebin.com/raw/2MsgCeQx</a></p>
  
  <p>Here are the solutions that I have tried to calculate the 20 day EMA.</p>
  
  <pre><code>#Imported the data as "data".
  #With Ta-lib
  data["EMA20Talib"] = talib.EMA(data.uClose, timeperiod = 20)
  
  #And with pandas
  data["EMA20Pandas"] = data["uClose"].ewm(span=20, adjust = False).mean()
  </code></pre>
  
  <p>I here is an image of the data and the results. 
  <a href="https://i.imgur.com/pFtc7x8.png" rel="noreferrer">https://i.imgur.com/pFtc7x8.png</a></p>
  
  <p>As you can see the Real20EMA does not match the TA-lib or the pandas 20EMA. What am I doing wrong?</p>
  
  <p>The uClose is the column Im calulating the EMA on, the "Real20EMA" is taken from tradingview (cross referenced with marketwatch to make sure its the correct one). </p>
  
  <p>I noticed that the there was a similar problem on here earlier with the same problem: <a href="https://stackoverflow.com/questions/48775841/pandas-ema-not-matching-the-stocks-ema">Pandas&#39; EMA not matching the stock&#39;s EMA?</a>. The problem was solved there when you sorted the index, and I have made sure that I have it correctly sorted, but alas I still get the same problem. </p>
  
  <p>I want to get the same numbers as the other finance sites using some tool. Weirdly enough even those two method I tried does not even return the same result.</p>