def calc_kdj1(df): kdj1 = KDJ(df, 45, 15, 15) kdj2 = KDJ(df, 45 * 5, 15 * 5, 15 * 5) kdj3 = KDJ(df, 45 * 5 * 5, 15 * 5 * 5, 15 * 5 * 5) kdj4 = KDJ(df, 45 * 5 * 5 * 5, 15 * 5 * 5 * 5, 15 * 5 * 5 * 5) df['k1'] = kdj1['KDJ_K'] df['d1'] = kdj1['KDJ_D'] df['j1'] = kdj1['KDJ_J'] df['e1'] = EMA(kdj1['KDJ_J'], 5) df['k2'] = kdj2['KDJ_K'] df['d2'] = kdj2['KDJ_D'] df['j2'] = kdj2['KDJ_J'] df['e2'] = EMA(kdj2['KDJ_J'], 5 * 5) df['k3'] = kdj3['KDJ_K'] df['d3'] = kdj3['KDJ_D'] df['j3'] = kdj3['KDJ_J'] df['e3'] = EMA(kdj3['KDJ_J'], 5 * 5 * 5) df['k4'] = kdj4['KDJ_K'] df['d4'] = kdj4['KDJ_D'] df['j4'] = kdj4['KDJ_J'] df['e4'] = EMA(kdj4['KDJ_J'], 5 * 5 * 5 * 5) # 计算KDJ值的斜率 ------------ begin ----------------------------------- """ df['x1'] = np.nan df['x2'] = np.nan df['x3'] = np.nan df['x4'] = np.nan n = 6 x = np.arange( n ) yy = df['e1'].values for i in range( n-1, len( df ) ): y1 = df['e1'][i-n+1:i+1].values y2 = df['e2'][i-n+1:i+1].values y3 = df['e3'][i-n+1:i+1].values y4 = df['e4'][i-n+1:i+1].values # print(x) # print(y) z1 = np.polyfit( x, y1, 3 ) z2 = np.polyfit( x, y2, 3 ) z3 = np.polyfit( x, y3, 3 ) z4 = np.polyfit( x, y4, 3 ) # print('zzzzzz',z) r1 = z1[0]*x[n-1]*x[n-1]+z1[1]*x[n-1]+z1[2] r2 = z2[0]*x[n-1]*x[n-1]+z2[1]*x[n-1]+z2[2] r3 = z3[0]*x[n-1]*x[n-1]+z3[1]*x[n-1]+z3[2] r4 = z4[0]*x[n-1]*x[n-1]+z4[1]*x[n-1]+z4[2] df['x1'].iat[i] = r1 df['x2'].iat[i] = r2 df['x3'].iat[i] = r3 df['x4'].iat[i] = r4 """ # 计算KDJ值的斜率 ------------- end ----------------------------------- return df
def makePicture(code, name): #df = ts.get_hist_data(code, start=begin_time, end=end_time) #df = ts.get_hist_data('600848') ret = md.init("13601380996","it@iZ23psatkqsZ") df = tt.get_bars("SHFE.ru1709", 3600*4, "2017-02-01 09:00:00", "2017-06-02 23:05:00") print("kkkkkkkkkkkkkkkkkkkkkkkkkkkk!!!") print("df:",df) #df = df.sort_index(0) # df.plot() C = df['close'] # 切片收盘价 C30 = C.tail(30) # 取最后30个收盘价数据 MA30 = C30.mean() # 均值 print("MA30:",MA30) xx = MA(C,5) print("MA30 ==== ",xx) kdj = KDJ(df,9,3,3) print("KDJ ==== ", kdj) drawPic(df, code, name)
def calc_kdj_ma_ema(df): """ # KDJ1 df['k1'] = np.nan df['d1'] = np.nan df['j1'] = np.nan df['e1'] = np.nan df['x1'] = np.nan # KDJ2 df['k2'] = np.nan df['d2'] = np.nan df['j2'] = np.nan df['e2'] = np.nan df['x2'] = np.nan # KDJ3 df['k3'] = np.nan df['d3'] = np.nan df['j3'] = np.nan df['e3'] = np.nan df['x3'] = np.nan # KDJ4 df['k4'] = np.nan df['d4'] = np.nan df['j4'] = np.nan df['e4'] = np.nan df['x4'] = np.nan # MA df['ma5'] = np.nan df['ma10'] = np.nan df['ma20'] = np.nan df['ma30'] = np.nan df['ma40'] = np.nan df['ma60'] = np.nan df['ma89'] = np.nan df['ma144'] = np.nan df['ma233'] = np.nan df['ma377'] = np.nan df['ma610'] = np.nan """ # ------------------------------------------------------------ df['ma5'] = MA(df['close'], 5) df['ma10'] = MA(df['close'], 10) df['ma20'] = MA(df['close'], 20) df['ma30'] = MA(df['close'], 30) df['ma40'] = MA(df['close'], 40) df['ma60'] = MA(df['close'], 60) df['ma89'] = MA(df['close'], 89) df['ma144'] = MA(df['close'], 144) df['ma233'] = MA(df['close'], 233) df['ma377'] = MA(df['close'], 377) df['ma610'] = MA(df['close'], 610) kdj1 = KDJ(df, 45, 15, 15) kdj2 = KDJ(df, 45 * 5, 15 * 5, 15 * 5) kdj3 = KDJ(df, 45 * 5 * 5, 15 * 5 * 5, 15 * 5 * 5) kdj4 = KDJ(df, 45 * 5 * 5 * 5, 15 * 5 * 5 * 5, 15 * 5 * 5 * 5) """ #ema1 = EMA( kdj1['KDJ_J'], 5 ) #ema2 = EMA( kdj2['KDJ_J'], 5*5 ) #ema3 = EMA( kdj3['KDJ_J'], 5*5*5 ) #ema4 = EMA( kdj4['KDJ_J'], 5*5*5*5 ) """ df['k1'] = kdj1['KDJ_K'] df['d1'] = kdj1['KDJ_D'] df['j1'] = kdj1['KDJ_J'] df['e1'] = EMA(kdj1['KDJ_J'], 5) df['k2'] = kdj2['KDJ_K'] df['d2'] = kdj2['KDJ_D'] df['j2'] = kdj2['KDJ_J'] df['e2'] = EMA(kdj2['KDJ_J'], 5 * 5) df['k3'] = kdj3['KDJ_K'] df['d3'] = kdj3['KDJ_D'] df['j3'] = kdj3['KDJ_J'] df['e3'] = EMA(kdj3['KDJ_J'], 5 * 5 * 5) df['k4'] = kdj4['KDJ_K'] df['d4'] = kdj4['KDJ_D'] df['j4'] = kdj4['KDJ_J'] df['e4'] = EMA(kdj4['KDJ_J'], 5 * 5 * 5 * 5) df['x1'] = np.nan df['x2'] = np.nan df['x3'] = np.nan df['x4'] = np.nan n = 6 x = np.arange(n) yy = df['e1'].values for i in range(n - 1, len(df)): y1 = df['e1'][i - n + 1:i + 1].values y2 = df['e2'][i - n + 1:i + 1].values y3 = df['e3'][i - n + 1:i + 1].values y4 = df['e4'][i - n + 1:i + 1].values # print(x) # print(y) z1 = np.polyfit(x, y1, 3) z2 = np.polyfit(x, y2, 3) z3 = np.polyfit(x, y3, 3) z4 = np.polyfit(x, y4, 3) # print('zzzzzz',z) r1 = z1[0] * x[n - 1] * x[n - 1] + z1[1] * x[n - 1] + z1[2] r2 = z2[0] * x[n - 1] * x[n - 1] + z2[1] * x[n - 1] + z2[2] r3 = z3[0] * x[n - 1] * x[n - 1] + z3[1] * x[n - 1] + z3[2] r4 = z4[0] * x[n - 1] * x[n - 1] + z4[1] * x[n - 1] + z4[2] df['x1'].iat[i] = r1 df['x2'].iat[i] = r2 df['x3'].iat[i] = r3 df['x4'].iat[i] = r4 return df
def calc_kdj(df, x): loc1 = 5 loc2 = 20 loc3 = 50 loc4 = 80 loc5 = 95 N1 = -1 N2 = -1 N3 = -1 var = 4 if (x == 1): N1 = 9 * var N2 = 3 * var N3 = 3 * var N4 = 5 k = 'k1' d = 'd1' j = 'j1' e = 'e1' # E 值: j值 的ema jpos = 'jpos1' # J 值拐点位置 jupd = 'jupd1' # J 值 上升或下降 jepos = 'jepos1' # J 值 穿越 E值 的位置 jeupd = 'jeupd1' # J 值 在 E值 的上还是下 jloc = 'jloc1' # J 值 的位置 1 ( <loc1 ) 2:( loc1<x<loc2 ) 3:< loc2<x<loc3 ) 4: ...... if (x == 2): N1 = 9 * var * var N2 = 3 * var * var N3 = 3 * var * var N4 = 5 * var k = 'k2' d = 'd2' j = 'j2' e = 'e2' # E 值: j值 的ema jpos = 'jpos2' # J 值拐点位置 jupd = 'jupd2' # J 值 上升或下降 jepos = 'jepos2' # J 值 穿越 E值 的位置 jeupd = 'jeupd2' # J 值 在 E值 的上还是下 jloc = 'jloc2' # J 值 的位置 1 ( <loc1 ) 2:( loc1<x<loc2 ) 3:< loc2<x<loc3 ) 4: ...... if (x == 3): N1 = 9 * var * var * var N2 = 3 * var * var * var N3 = 3 * var * var * var N4 = 5 * var * var k = 'k3' d = 'd3' j = 'j3' e = 'e3' # E 值: j值 的ema jpos = 'jpos3' # J 值拐点位置 jupd = 'jupd3' # J 值 上升或下降 jepos = 'jepos3' # J 值 穿越 E值 的位置 jeupd = 'jeupd3' # J 值 在 E值 的上还是下 jloc = 'jloc3' # J 值 的位置 1 ( <loc1 ) 2:( loc1<x<loc2 ) 3:< loc2<x<loc3 ) 4: ...... if (x == 4): N1 = 9 * var * var * var * var N2 = 3 * var * var * var * var N3 = 3 * var * var * var * var N4 = 5 * var * var * var * var k = 'k4' d = 'd4' j = 'j4' e = 'e4' # E 值: j值 的ema jpos = 'jpos4' # J 值拐点位置 jupd = 'jupd4' # J 值 上升或下降 jepos = 'jepos4' # J 值 穿越 E值 的位置 jeupd = 'jeupd4' # J 值 在 E值 的上还是下 jloc = 'jloc4' # J 值 的位置 1 ( <loc1 ) 2:( loc1<x<loc2 ) 3:< loc2<x<loc3 ) 4: ...... rt = KDJ(df, N1, N2, N3) df[k] = rt['KDJ_K'] df[d] = rt['KDJ_D'] df[j] = rt['KDJ_J'] df[e] = EMA(rt['KDJ_J'], N4) jj = df[j].values ee = df[e].values df[jpos] = np.nan df[jupd] = np.nan df[jepos] = np.nan df[jeupd] = np.nan df[jloc] = np.nan df[jpos].iat[0] = 0 df[jupd].iat[0] = 1 df[jepos].iat[0] = 0 df[jeupd].iat[0] = 1 ddxx1 = np.nan ddxx0 = np.nan for i in range(2, len(df)): df[jpos].iat[i] = 0 df[jupd].iat[i] = 1 df[jepos].iat[i] = 0 df[jeupd].iat[i] = 1 # ======================= j value location from 0 to 110? ========== if (jj[i] <= loc1): df[jloc].iat[i] = 1 elif (loc1 < jj[i] and jj[i] <= loc2): df[jloc].iat[i] = 2 elif (loc2 < jj[i] and jj[i] <= loc3): df[jloc].iat[i] = 3 elif (loc3 < jj[i] and jj[i] <= loc4): df[jloc].iat[i] = 4 elif (loc4 < jj[i] and jj[i] <= loc5): df[jloc].iat[i] = 5 elif (loc5 < jj[i]): df[jloc].iat[i] = 6 # ======================= j value > j(-1) value ? ================= cha1 = jj[i] - jj[i - 1] cha0 = jj[i - 1] - jj[i - 2] if (cha1 > 0): df[jupd].iat[i] = 1 if (cha0 > 0): df[jpos].iat[i] = df[jpos].iat[i - 1] elif (cha0 < 0): df[jpos].iat[i] = i elif (cha1 < 0): df[jupd].iat[i] = -1 if (cha0 > 0): df[jpos].iat[i] = i elif (cha0 < 0): df[jpos].iat[i] = df[jpos].iat[i - 1] # ======================= j value > e value ? ================= cha1 = jj[i] - ee[i - 1] cha0 = jj[i - 1] - ee[i - 2] if (cha1 > 0): df[jeupd].iat[i] = 1 if (cha0 > 0): df[jepos].iat[i] = df[jepos].iat[i - 1] elif (cha0 < 0): df[jepos].iat[i] = i elif (cha1 < 0): df[jeupd].iat[i] = -1 if (cha0 > 0): df[jepos].iat[i] = i elif (cha0 < 0): df[jepos].iat[i] = df[jepos].iat[i - 1] return df
def drawPic(df, code, name): mondays = WeekdayLocator(MONDAY) # 主要刻度 alldays = DayLocator() # 次要刻度 #weekFormatter = DateFormatter('%b %d') # 如:Jan 12 mondayFormatter = DateFormatter('%m-%d-%Y') # 如:2-29-2015 dayFormatter = DateFormatter('%d') # 如:12 plt.figure(2) # 创建图表2 ax1 = plt.subplot(211) # 在图表2中创建子图1 ax2 = plt.subplot(212) # 在图表2中创建子图2 plt.sca(ax1) ax1.xaxis.set_major_locator(mondays) ax1.xaxis.set_minor_locator(alldays) ax1.xaxis.set_major_formatter(mondayFormatter) _candlestick(ax1, df, width=0.6, colorup='r', colordown='g') ax1.xaxis_date() plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right') #ax.grid(True) plt.sca(ax2) kdj = KDJ(df,45,15,15) JJ=pd.DataFrame({'KDJ_J':kdj['KDJ_J']}) ema = EMA(JJ,5) kdj2 = KDJ(df,45*5,15*5,15*5) #print("kdj2222222222222222222222222222222222222222222:",kdj2) kdj3 = KDJ(df, 45 * 5*5, 15 * 5*5, 15 * 5*5) kdj4 = KDJ(df, 45 * 5 * 5*5, 15 * 5 * 5*5, 15 * 5 * 5*5) JJ2 = pd.DataFrame({'KDJ_J': kdj2['KDJ_J']}) JJ3 = pd.DataFrame({'KDJ_J': kdj3['KDJ_J']}) JJ4 = pd.DataFrame({'KDJ_J': kdj4['KDJ_J']}) ema2 = EMA(JJ2, 5*5) ema3 = EMA(JJ3, 5 * 5*5) ema4 = EMA(JJ4, 5 * 5*5*5) #print('ema:',ema) #print("KDJ ==== ", kdj) xx=[] t=0 for DAT,DD in kdj.iterrows(): t=t+10 xx.append(t) #print(xx) plt.sca(ax2) #plt.plot(xx, ema, linewidth=0.5) #plt.plot(xx, kdj['KDJ_J'], linewidth=0.5) #plt.plot(xx, ema2, linewidth=0.5) #plt.plot(xx, kdj2['KDJ_J'], linewidth=0.5) #plt.plot(xx, ema3, linewidth=0.5) #plt.plot(xx, kdj3['KDJ_J'], linewidth=0.5) plt.plot(xx, ema4, linewidth=0.5) plt.plot(xx, kdj4['KDJ_J'], linewidth=0.5) print("ema44444444444444444444444444444444444:",ema4) print("len:of df:",len(df)) plt.show()