def cal_kdj_system(data,N,M1,lower,upper): # 假如是计算kdj(9,3,3),那么,N是9,M1是3,3 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)&(data['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,并不能保证结果正确