简体   繁体   English

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

[英]calculate Exponential Moving Average with pandas

I try to calculate ema with pandas but the result is not good. 我尝试用熊猫计算ema,但结果并不好。 I try 2 techniques to calculate : 我尝试了两种技术来计算:

The first technique is the panda's function ewn : 第一种技术是熊猫的功能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.4 but 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.1 it'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()

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM