def calc_ma(df): ar = [ 5, 8, 10, 13, 20, 21, 30, 34, 40, 55, 60, 89, 120, 144, 233, 250, 377, 500, 610 ] for i in ar: s = str(i) s = "ma" + s df[s] = MA(df['close'], i) # 计算MA(5) MA(13) ... MA(610) fibs = _fibs(14) sum = abs(df['ma5'] - df['ma8']) # 需要优化!! for i in range(3, 13): for j in range(i + 1, 14): ma_b = "ma" + str(fibs[i]) ma_e = "ma" + str(fibs[j]) sum = sum + abs(df[ma_b] - df[ma_e]) df['diff'] = sum - abs(df['ma5'] - df['ma8']) # 需要优化!! df['x8610'] = df['close'] - df['ma20'] print('yyyyyyyyyyyy', df['diff']) 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_ma(df): df['ma5'] = MA(df['close'], 5) df['ma8'] = MA(df['close'], 8) df['ma10'] = MA(df['close'], 10) df['ma13'] = MA(df['close'], 13) df['ma20'] = MA(df['close'], 20) df['ma21'] = MA(df['close'], 21) df['ma30'] = MA(df['close'], 30) df['ma34'] = MA(df['close'], 34) df['ma40'] = MA(df['close'], 40) df['ma55'] = MA(df['close'], 55) 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) df['diff'] = abs( df['ma5']-df['ma8'] )+abs( df['ma5']-df['ma13'] ) \ +abs( df['ma5']-df['ma21'] )+abs( df['ma5']-df['ma34'] ) \ +abs( df['ma5']-df['ma55'] )+abs( df['ma5']-df['ma89'] ) \ +abs( df['ma5']-df['ma144'] )+abs( df['ma5']-df['ma233'] ) \ +abs( df['ma5']-df['ma377'] )+abs( df['ma5']-df['ma610'] ) \ +abs( df['ma8']-df['ma13'] ) \ +abs( df['ma8']-df['ma21'] )+abs( df['ma8']-df['ma34'] ) \ +abs( df['ma8']-df['ma55'] )+abs( df['ma8']-df['ma89'] ) \ +abs( df['ma8']-df['ma144'] )+abs( df['ma8']-df['ma233'] ) \ +abs( df['ma8']-df['ma377'] )+abs( df['ma8']-df['ma610'] ) \ +abs( df['ma13']-df['ma21'] )+abs( df['ma13']-df['ma34'] ) \ +abs( df['ma13']-df['ma55'] )+abs( df['ma13']-df['ma89'] ) \ +abs( df['ma13']-df['ma144'] )+abs( df['ma13']-df['ma233'] ) \ +abs( df['ma13']-df['ma377'] )+abs( df['ma13']-df['ma610'] ) \ +abs( df['ma21']-df['ma34'] ) \ +abs( df['ma21']-df['ma55'] )+abs( df['ma21']-df['ma89'] ) \ +abs( df['ma21']-df['ma144'] )+abs( df['ma21']-df['ma233'] ) \ +abs( df['ma21']-df['ma377'] )+abs( df['ma21']-df['ma610'] ) \ +abs( df['ma34']-df['ma55'] )+abs( df['ma34']-df['ma89'] ) \ +abs( df['ma34']-df['ma144'] )+abs( df['ma34']-df['ma233'] ) \ +abs( df['ma34']-df['ma377'] )+abs( df['ma34']-df['ma610'] ) \ +abs( df['ma55']-df['ma89'] ) \ +abs( df['ma55']-df['ma144'] )+abs( df['ma55']-df['ma233'] ) \ +abs( df['ma55']-df['ma377'] )+abs( df['ma55']-df['ma610'] ) \ +abs( df['ma89']-df['ma144'] )+abs( df['ma89']-df['ma233'] ) \ +abs( df['ma89']-df['ma377'] )+abs( df['ma89']-df['ma610'] ) \ +abs( df['ma144']-df['ma233'] ) \ +abs( df['ma144']-df['ma377'] )+abs( df['ma144']-df['ma610'] ) \ +abs( df['ma233']-df['ma377'] )+abs( df['ma233']-df['ma610'] ) \ +abs( df['ma377']-df['ma610'] ) df['x8610'] = df['ma5'] - df['ma610'] return df