python-kdj算法

def cal_kdj_system(data,N,M1,lower,upper):
    data['llv_low']=data['low'].rolling(N).min()
    data['hhv_high']=data['high'].rolling(N).max()
    data['rsv']=(data['close']-data['llv_low'])/(data['hhv_high']-data['llv_low'])
    data['k']=data['rsv'].ewm(adjust=False,alpha=1/M1).mean()
    data['d']=data['k'].ewm(adjust=False,alpha=1/M1).mean()
    data['j']=3*data['k']-2*data['d']
    data['pre_j']=data['j'].shift(1)
    data['long_signal']=np.where((data['pre_j']=lower),1,0)
    data['short_signal']=np.where((data['pre_j']>upper)&(data['j']<=upper),-1,0)
    data['signal']=data['long_signal']+data['short_signal']
    return data

注:在计算ema的时候,使用了pandas的ewm函数,未debug,并不能保证结果正确

你可能感兴趣的:(python)