pandas 用熊猫计算指数移动平均线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48812198/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:11:19  来源:igfitidea点击:

calculate Exponential Moving Average with pandas

pythonpandas

提问by john

I try to calculate ema with pandas but the result is not good. I try 2 techniques to calculate :

我尝试用Pandas计算 ema 但结果不好。我尝试两种技术来计算:

The first technique is the panda's function ewn:

第一种技术是Pandas的功能ewn

window = 100
c = 2 / float(window + 1)
df['100ema'] = df['close'].ewm(com=c).mean()

But the last result of this function gives. 2695.4but the real result is 2656.2

但是这个函数的最后一个结果给出了。2695.4但真正的结果是2656.2

The second technique is

第二种技术是

window = 100
c = 2 / float(window + 1)
df['100sma'] = df['close'].rolling(window).mean()
df['100ema'] = (c * df['close']) + ((1 - c) * df['100sma'])

The result is 2649.1it's closer than first technique but is always not good

结果是2649.1它比第一种技术更接近,但总是不好

The sma function give the good result

sma 函数给出了很好的结果

** EDIT **

** 编辑 **

The response is

回应是

df['100ema'] = pd.Series.ewm(df['close'], span=window).mean()

采纳答案by john

If you want to calculate EWMA or any technical indicator in Python, I recommend using ta-lib.

如果你想用 Python 计算 EWMA 或任何技术指标,我建议使用ta-lib

回答by Brahmaiahchowdary

expwighted_avg = ts_log.ewm(halflife=12).mean()

where 'ts_log' is dataframe or series of Time Series

其中“ts_log”是数据帧或时间序列系列