kaufman adaptive moving average

来源:互联网 发布:php商品管理系统 编辑:程序博客网 时间:2024/05/17 23:13
#%%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#%%#%%#calculate KAMA#---------------kama = KAMA(d.Close, n=10, pow1=2, pow2=30)kama#%%