Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I correctly calculate EMA for a stock using Ta-lib or Pandas?

Edit!

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.


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.

Here is the data for my example: https://pastebin.com/raw/2MsgCeQx

Here are the solutions that I have tried to calculate the 20 day EMA.

#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()

I here is an image of the data and the results. https://i.imgur.com/pFtc7x8.png

As you can see the Real20EMA does not match the TA-lib or the pandas 20EMA. What am I doing wrong?

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).

I noticed that the there was a similar problem on here earlier with the same problem: Pandas' EMA not matching the stock's EMA?. 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.

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.

like image 549
Noksuu Avatar asked Apr 29 '19 21:04

Noksuu


People also ask

What is the formula for calculating EMA?

Finally, the following formula is used to calculate the current EMA: EMA = Closing price x multiplier + EMA (previous day) x (1-multiplier)

How do I get EMA in Python?

The first thing we will do is find the SMA of the first 5 numbers. Let's add 11.05 to our EMA list. Now we will use the EMA formula to calculate the EMA for the 6th number. Continue the process of using the EMA formula for all the numbers in the set and that is how you calculate the EMA of a stock.

What is the method of DataFrame to calculate the 60 days moving average?

In Python, we can calculate the moving average using . rolling() method.


1 Answers

I suggest using Pandas TA to calculate technical indicators in python. I find it more accurate and is easier to install than TA-Lib.

Using Pandas TA, the 20 period exponential moving average is calculated like:

import pandas_ta as ta

data["EMA20"] = ta.ema(data["uClose"], length=20)
like image 187
carloslockward Avatar answered Oct 20 '22 12:10

carloslockward