[英]Compile error in Kaufman’s Adaptive Moving Average python code
I want to use Kaufman's Adaptive Moving Average (KAMA) for my Master's thesis.我想在我的硕士论文中使用 Kaufman 的自适应移动平均线 (KAMA)。 I got the code from Python Pandas Kaufman Adaptive Moving Average (KAMA) --- Recursive Calculation in Pandas or Cython However, I cannot run this code because it is showing these errors:
我从Python Pandas Kaufman Adaptive Moving Average (KAMA) --- Recursive Calculation in Pandas 或 Cython获得了代码但是,我无法运行此代码,因为它显示了以下错误:
AttributeError: 'numpy.ndarray' object has no attribute 'shift'
AttributeError: 'numpy.ndarray' 对象没有属性 'shift'
AttributeError: module 'pandas' has no attribute 'stats'
AttributeError: 模块 'pandas' 没有属性 'stats'
Please help me to solve the problem.请帮我解决问题。
#%%
import numpy as np
import pandas
def KAMA(price, n=10, pow1=2, pow2=30):
''' kama indicator '''
''' accepts pandas dataframe of prices '''
absDiffx = abs(price - price.shift(1) )
ER_num = abs( price - price.shift(n) )
ER_den = pandas.stats.moments.rolling_sum(absDiffx,n)
ER = ER_num / ER_den
sc = ( ER*(2.0/(pow1+1)-2.0/(pow2+1.0))+2/(pow2+1.0) ) ** 2.0
answer = np.zeros(sc.size)
N = len(answer)
first_value = True
for i in range(N):
if sc[i] != sc[i]:
answer[i] = np.nan
else:
if first_value:
answer[i] = price[i]
first_value = False
else:
answer[i] = answer[i-1] + sc[i] * (price[i] - answer[i-1])
return answer
#%%
marray = np.arange(20)
KAMA(marray)
stats.moments.rolling_sum is no longer available in the latest version of pandas. stats.moments.rolling_sum 在最新版本的熊猫中不再可用。 You can do the same thing using the
rolling
method.您可以使用
rolling
方法做同样的事情。
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.rolling.html https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.rolling.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.