Nok*_*suu 8 python pandas ta-lib
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()
Run Code Online (Sandbox Code Playgroud)
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).
我注意到早些时候这里有一个类似的问题,同样的问题:熊猫的 EMA 与股票的 EMA 不匹配?. 当你对索引进行排序时,问题就在那里解决了,我已经确保我已经正确排序了,但是我仍然遇到同样的问题。
我想使用某种工具获得与其他金融网站相同的数字。奇怪的是,即使我尝试过的这两种方法也没有返回相同的结果。
小智 9
我建议在Python中使用Pandas TA来计算技术指标。我发现它比 TA-Lib 更准确并且更容易安装。
使用 Pandas TA,20 周期指数移动平均线的计算如下:
import pandas_ta as ta
data["EMA20"] = ta.ema(data["uClose"], length=20)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6466 次 |
最近记录: |