Pandas 的 EMA 与股票的 EMA 不匹配?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48775841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas' EMA not matching the stock's EMA?
提问by Rob M
I am trying to use Python (with Pandas) to calculate the 20-day Exponential Moving Averages (EMA) of daily stock data for Intel (INTC). Pandas has a number of ways of doing this, and I've also tried stockstats, which runs on Pandas, but they never return the same EMA as I get from stock/finance websites.
我正在尝试使用 Python(与 Pandas)来计算英特尔 (INTC) 每日股票数据的 20 天指数移动平均线 (EMA)。Pandas 有很多方法可以做到这一点,我也尝试过在 Pandas 上运行的 stockstats,但它们永远不会返回与我从股票/金融网站获得的相同的 EMA。
I've double checked the close prices, and they match, but the EMA always comes out "wrong".
我仔细检查了收盘价,它们匹配,但 EMA 总是“错误”。
This is the CSV I'm using: INTC Stock Data
这是我正在使用的 CSV:INTC 股票数据
It contains the daily Date, Month Name, Open, High, Low, Close, Day Avg, and Volume for Intel's stock (Ticker: INTC) from 4/20/2016 to 2/1/2018.
它包含英特尔股票(股票代码:INTC)从 2016 年 4 月 20 日到 2018 年 2 月 1 日的每日日期、月份名称、开盘价、最高价、最低价、收盘价、日均价和成交量。
When I look to the bigger stock websites like MarketWatchor Fidelity, their numbers don't match mine. They match each other, but not me.
当我查看MarketWatch或Fidelity等较大的股票网站时,他们的数字与我的不符。他们彼此匹配,但不是我。
For example...
例如...
df2['Close'].ewm(span=20,min_periods=0,adjust=False,ignore_na=False).mean()
or...
或者...
df2['Close'].ewm(span=20, min_periods=20, adjust=True).mean()
or...
或者...
df2["Close"].shift().fillna(df["Close"]).ewm(com=1, adjust=False).mean()
Give me EMA's for 2/1/2018 like $44.71, $47.65, $46.15, etc. when the real 20-Day EMAon anyfinance site is $45.65. And I get the wrong numbers no matter what date I try to compute the EMA for. It's even wrong when I just try for 5-Day EMAs.
给我 2/1/2018 的 EMA,如 44.71 美元、47.65 美元、46.15 美元等,当任何金融网站上的实际 20 天 EMA为 45.65 美元时。无论我尝试计算 EMA 的日期,我都会得到错误的数字。当我只尝试 5 天 EMA 时,它甚至是错误的。
I've read, watched and followed tutorials on the subject, but their results also don't match the accepted/published EMA's you'd find on any finance site. The people creating the tutorials and videos simply never check them against each other after Panda's crunches the numbers. And I need my numbers to match.
我已经阅读、观看并遵循了有关该主题的教程,但它们的结果也与您在任何金融网站上找到的已接受/已发布的 EMA 不符。在 Panda 计算数字之后,创建教程和视频的人根本不会相互比较。我需要我的号码匹配。
How do I arrive at the same figures every other finance site on the internet is getting for EMAs? I don't think this has anything to do with adjusted close prices because I'm using old/settled data and my close prices and dates are the same as theirs.
我如何得出互联网上每个其他金融网站为 EMA 获得的相同数字?我认为这与调整后的收盘价没有任何关系,因为我使用的是旧的/已结算的数据,而且我的收盘价和日期与他们的相同。
回答by unutbu
Sort the DataFrame so that the dates are in increasing order.
Since your data is in decreasing order by date, if you don't sort the dates first, your ewm
calculation exponentially weights the earliestdates the most, rather than the latest date (as it should be).
对 DataFrame 进行排序,使日期按递增顺序排列。由于您的数据按日期降序排列,如果您不先对日期进行排序,则您的ewm
计算会以指数方式对最早的日期加权,而不是最晚的日期(应该如此)。
import pandas as pd
df = pd.read_csv('intc_data.txt', parse_dates=['Date'], index_col=['Date'])
df['backward_ewm'] = df['Close'].ewm(span=20,min_periods=0,adjust=False,ignore_na=False).mean()
df = df.sort_index()
df['ewm'] = df['Close'].ewm(span=20,min_periods=0,adjust=False,ignore_na=False).mean()
print(df[['ewm', 'backward_ewm']].tail())
yields
产量
ewm backward_ewm
Date
2018-01-26 45.370936 48.205638
2018-01-29 45.809895 48.008337
2018-01-30 46.093714 47.800794
2018-01-31 46.288599 47.696667
2018-02-01 46.418256 47.650000
This agrees with Marketwatchwhich says the EWMA(20) on 2018-02-01 was 46.42.
这与Marketwatch一致,后者表示 2018 年 2 月 1 日的 EWMA(20) 为 46.42。