pandas 的EMA与股票的EMA不匹配? [英] Pandas' EMA not matching the stock's EMA?

查看:146
本文介绍了 pandas 的EMA与股票的EMA不匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python(与Pandas一起)来计算英特尔(INTC)每日股票数据的20天指数移动平均线(EMA).熊猫有多种方法可以做到这一点,我还尝试了在熊猫上运行的stockstats,但是它们从未返回与我从股票/金融网站获得的相同的EMA.

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.

我仔细检查了收盘价,它们匹配,但是EMA总是错误的显示.

I've double checked the close prices, and they match, but the EMA always comes out "wrong".

这是我正在使用的CSV: INTC股票数据

This is the CSV I'm using: INTC Stock Data

它包含2016年4月20日至2018年1月1日的英特尔股票的每日日期,月份名称,开盘价,最高价,最低价,收盘价,日平均价和交易量(股票代码: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.

当我查看较大的股票网站时,例如 MarketWatch 保真,其编号与我的不匹配.他们匹配彼此 ,但不匹配我.

When I look to the bigger stock websites like MarketWatch or Fidelity, their numbers don't match mine. They match each other, but not me.

例如...

df2['Close'].ewm(span=20,min_periods=0,adjust=False,ignore_na=False).mean()

或...

df2['Close'].ewm(span=20, min_periods=20, adjust=True).mean()

或...

df2["Close"].shift().fillna(df["Close"]).ewm(com=1, adjust=False).mean()

当任何金融网站上的真实20天EMA为45.65美元时,请给我提供2018年1月2日的EMA,如44.71美元,47.65美元,46.15美元等.无论我尝试计算EMA的日期如何,我都会得到错误的数字.当我只尝试5天EMA时,这甚至是错误的.

Give me EMA's for 2/1/2018 like $44.71, $47.65, $46.15, etc. when the real 20-Day EMA on any finance 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.

我已经阅读,观看并遵循了有关该主题的教程,但是它们的结果也与您在任何金融网站上都能找到的已接受/已发布的EMA不匹配.制作教程和视频的人根本不会在Panda处理数字后再相互核对.我需要我的号码才能匹配.

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的相同数字?我认为这与调整后的收盘价没有任何关系,因为我使用的是旧的/已结算的数据,并且我的收盘价和日期与它们的相同.

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.

推荐答案

对DataFrame进行排序,以便日期按升序排列. 由于您的数据按日期降序排列,因此,如果您不首先对日期进行排序,则ewm计算将对最早日期进行加权,而不是对最新日期进行加权(因为应该是).

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 earliest dates the most, rather than the latest date (as it should be).

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

收益

                  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

这与 Marketwatch 一致,该声明在2018年的EWMA(20)- 02-01是46.42.

This agrees with Marketwatch which says the EWMA(20) on 2018-02-01 was 46.42.

这篇关于 pandas 的EMA与股票的EMA不匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆