简单的 Python Pandas EMA (ewma)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48613151/
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
Simple Python Pandas EMA (ewma)?
提问by hahahahey
I wrote some code to build my own EMA/MACD, but have decided to give Pandas a try instead.
我编写了一些代码来构建我自己的 EMA/MACD,但我决定尝试一下 Pandas。
I am using this website below as a basic understanding of EMA and trying to get pandas to give me the same answers to be sure I am using pandas correctly:
我正在使用下面的这个网站作为对 EMA 的基本了解,并试图让大Pandas给我相同的答案,以确保我正确使用大Pandas:
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:moving_averages
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:moving_averages
And here is the chart with the data that Im working with.
这是我使用的数据图表。
Here is the code I'm trying to get to work, but it gives me different output than the 10-day EMA column
这是我试图开始工作的代码,但它给我的输出与 10 天 EMA 列不同
import pandas as pd
data=[22.27,22.19,22.08,22.17,22.18,22.13,22.23,22.43,22.24,22.29,22.15,22.39,22.38,22.61,23.36,24.05,23.75,23.83]
df=pd.Series(data)
pd.ewma(df, span=10)
I've also tried this with no luck.
我也试过这个,但没有运气。
pd.ewma(df, span=10, min_periods=10)
Any help is appreciated.
任何帮助表示赞赏。
回答by Peter Leimbigler
Coincidentally, this question was asked and answered here: Does Pandas calculate ewm wrong?
巧的是,这个问题在这里被问到并得到了回答:Pandas 计算 ewm 错误吗?
Check out @chrisb's answer there. To compute the EWM as described in the article you're studying:
在那里查看@chrisb 的回答。要按照您正在学习的文章中所述计算 EWM:
- manually compute the first valid simple MA to serve as a starting point for EWA
- run pandas' EWM with
adjust=False
- 手动计算第一个有效的简单 MA 作为 EWA 的起点
- 运行 pandas 的 EWM
adjust=False
回答by A STEFANI
According to calculate-exponential-moving-average-with-pandasself-answer, and assuming that close
serie is corresponding to the close price
, you may use this to get the EMA 10
: (change the span to what you want if you want another span)
根据calculate-exponential-moving-average-with-pandasself-answer,并假设close
serie对应于close price
,您可以使用它来获得EMA 10
:(如果您想要另一个跨度,请将跨度更改为您想要的)
df['ema10'] = pd.Series.ewm(df['close'], span=10).mean()