def min60(time): min60_times = [1030, 1130, 1400, 1500] if time in min60_times: temp = time // 100 * 60 + time % 100 - 60 temp = temp // 60 * 100 + temp % 60 print(temp) sql = {'time': {'$gt': temp, '$lte': time}, 'date': Calendar.today()} curror = BaseModel('real_kline_min5').query(sql=sql) if curror.count(): data = pd.DataFrame(list(curror)) data = data.sort_values(by=['time'], ascending=True) data = data.groupby(by=['stock_code'], as_index=False).agg({ 'volume': 'sum', 'amount': 'sum', 'open': 'first', 'close': 'last', 'high': 'max', 'low': 'min' }) data['time'] = time data['date'] = Calendar.today() BaseModel('real_kline_min60').insert_batch( data.to_dict(orient='records')) print('min60 ok') # min60(time=1030)
def get_all_stock_code_info(cls, date=None, time=None, kline='kline_min5'): sql = analyzer('date = {} and time = {}'.format(date, time)) data = pd.DataFrame(list(BaseModel(kline).query(sql))) if len(data) == 0: return pd.DataFrame() data['pro'] = data.close / data.open - 1 #以上是把数据从数据库中取出 #并且求出各个时刻的收益 #注意,如果要求收益的计算方式close/pre_close-1,则close需要通过shift()调整 data2 = None if time == 1500: #如果time是1500,需要把明天的第一根取出来计算 x = Calendar.calc(analyzer('date = {}'.format(date))['date'], 1)['date'] sql = analyzer('date = {} and time = {}'.format( str(x.year * 10000 + x.month * 100 + x.day), 935)) data2 = pd.DataFrame(list(BaseModel(kline).query(sql))) if len(data2) == 0: return pd.DataFrame() data = pd.merge(data, data2, on='stock_code') data['profit'] = (data.close_y / data.close_x - 1) return data.loc[:, [ 'stock_code', 'date_x', 'time_x', 'profit', 'pro' ]] else: time_index = KlineBase.get_time_index(time) sql = analyzer('date = {} and time = {}'.format(date, time_index)) data2 = pd.DataFrame(list(BaseModel(kline).query(sql))) if len(data2) == 0: return pd.DataFrame() data = pd.merge(data, data2, on='stock_code') data['profit'] = (data.close_y / data.close_x - 1) return data.loc[:, [ 'stock_code', 'date_x', 'time_x', 'profit', 'pro' ]]
def fun(stocks): # print(os.getpid(), stocks) kline = 'hk_kline_day' pixel = 64 # path = "f://zjf/image/USA_boll_kline_day_test" interval = KlineInfo.get_interval(kline) detal = KlineInfo.get_detal(kline) tabel_name = "boll_" + kline # path = "f://zjf/image/ma_kline_min30_test" for sc in stocks: end_time = dt.datetime(2018, 6, 25) start_time = dt.datetime(2016, 1, 1) data = KlineData.read_data(code=sc, start_date=Calendar.calc(start_time, -203)['date'], end_date=end_time, kline=kline, timemerge=True) if len(data) <= 200: continue data = CalBoll.cal_boll(data, interval) data = data.dropna() while end_time > dt.datetime(2016, 1, 1): data0 = data.tail(pixel) data0 = data0.reset_index(drop=True) profit = data0['profit'] date = data0['date'] data0 = CalBoll.data_normalization(data0, pixel) data0['date'] = date data0['profit'] = profit if len(data0) < pixel: break data0 = data0.reset_index(drop=True) profit = data0.profit.iloc[len(data0) - 1] date = data0.date.iloc[len(data0) - 1] if 'min' in kline: if abs(profit) > 0.11: data = data[data.date < dt.datetime( date.year, date.month, date.day)] continue else: if abs(profit) > 0.1 * interval: data = data[data.date < dt.datetime( date.year, date.month, date.day)] continue try: visualization.drow_boll_line(data=data0, profits=profit, date=date, code=sc, pixel=pixel, table_name=tabel_name) except Exception as e: pass data = data[ data.date < dt.datetime(date.year, date.month, date.day)] end_time = date return 1
def fun(stocks): # print(os.getpid(), stocks) kline = 'hk_kline_day' table_name='MA_'+kline pixel=64 interval = KlineInfo.get_interval(kline) for sc in stocks: end_time = dt.datetime(2018, 6, 25) start_time = dt.datetime(2016, 1, 1) data = KlineData.read_data(code=sc, start_date=Calendar.calc(start_time, -203)['date'], end_date=end_time, kline=kline, timemerge=True) if len(data) <= 200: continue data = CalMa.cal_ma(data, interval) data = data.dropna() print(sc) while end_time > dt.datetime(2016, 1, 1): data0 = data.tail(pixel) data0 = data0.reset_index(drop=True) profit = data0['profit'] date = data0['date'] price=data0.close.iloc[0] data0 = CalMa.data_normalization(data0,pixel=pixel) data0['date'] = date data0['profit'] = profit if len(data0) < pixel or price<2: break date = data0.date.iloc[len(data0) - 1] profit = data0.profit.iloc[len(data0) - 1] if 'min' in kline: if abs(profit) > 0.11: data = data[data.date < dt.datetime(date.year, date.month, date.day)] continue else: if abs(profit) > 0.1*interval: data = data[data.date < dt.datetime(date.year, date.month, date.day)] continue data0 = data0.reset_index(drop=True) try: visualization.draw_ma_line(data=data0, profits=profit, date=date, code=sc, table_name=table_name,pixel=pixel) except Exception as e: print(e) pass data = data[data.date<dt.datetime(date.year,date.month,date.day)] end_time = date return 1
def draw_rl_pic_for_day(cls, data, end_date): code3 = KlineBase.get_all_stock_code_info_for_day( start_date=Calendar.calc(end_date, -1)['date'], end_date=end_date) if len(code3) == 0: return pd.DataFrame() data = pd.merge(data, code3, on=['stock_code']) # data['pro'] =data.close_x/data.open_x-1 pro = data.pro.mean() data = data[0:2500] arr = np.zeros((51, 51)) y = 0 for i, r in data.iterrows(): if (i + 1) % 50 != 0: # print((i + 1) % 50) arr[y, i % 50] = r['pro'] else: y = y + 1 # print('y', y) arr[y, i % 50] = r['pro'] proifit = data.profit.mean() if abs(proifit) < 0.005: return ax = sns.heatmap(arr, vmin=-0.03, vmax=0.03, cbar=False) # plt.matshow(arr, cmap='hot') ax.set_yticks([]) ax.set_xticks([]) # plt.show() # plt.showbar(False) if proifit >= 0: up = '/up' else: up = '/down' title = str( data.date_x.iloc[0])[0:10] + '_' + str(pro) + '_' + str(proifit) path = 'F:/zjf/images/hot_day/' + up + '/' File.check_file(path=path) file_name = path + title + '.png' plt.savefig(fname=file_name, format='png', dpi=50) plt.close() gc.collect()
def fun(stocks): # print(os.getpid(), stocks) kline = 'index_min5' pixel=54 n=5 count=2 interval = KlineInfo.get_interval(kline=kline) detal=KlineInfo.get_detal(kline=kline) table_name='A_Volume_'+kline+'_version5' print(table_name) for sc in stocks: end_time = dt.datetime(2018, 8, 15) start_time = dt.datetime(2016, 1, 1) data = KlineData.read_data(code=sc, start_date=Calendar.calc(start_time, -203)['date'], end_date=end_time, kline=kline, timemerge=True) if len(data) <= 200 : continue data=CalMa.cal_profit(data,interval,detal) data=data.dropna() d = data.date.iloc[-1] data=data[data.date<=dt.datetime(d.year,d.month,d.day,9,55)] print(sc) while end_time > start_time: data0 = data.tail(pixel*4) amax=data0.volume.max() data0 = data0.tail(pixel) data0 = data0.reset_index(drop=True) profit = data0.profit.iloc[ - 1] profitself = data0.profit_self.iloc[- 1] date = data0['date'] if len(data0) < pixel: break date2 = date.iloc[ - 6] date1 = date.iloc[ - 1] data0=CalMa.data_normalization_volume2(data0,pixel=pixel+n*count,amax=amax) if 'min' in kline: if 'A' in table_name: if date1.hour != 9 and date1.minute!=55: data = data[data.date <= dt.datetime(date2.year, date2.month, date2.day, 9, 55)] break if abs(profit) > 0.11 or abs(profitself)>0.097: data = data[data.date <= dt.datetime(date2.year, date2.month, date2.day, 9, 55)] continue else: if abs(profit) > 0.1 * interval: data = data[data.date <= dt.datetime(date2.year, date2.month, date2.day, 9, 55)] continue if 'A' in table_name: if abs(profitself) > 0.097: data = data[data.date <= dt.datetime(date2.year, date2.month, date2.day, 9, 55)] print(sc, str(profitself) + '_' + str(date)) continue try: visualization.draw_volume(data=data0, profits=profit, date=date1, code=sc, table_name=table_name, pixel=pixel, n=n, count=count) pass except Exception as e: print(e) pass data = data[data.date <= dt.datetime(date2.year, date2.month, date2.day,9,55)] end_time = date2 return 1
def fun(stocks): # print(os.getpid(), stocks) kline = 'index_min5' pixel = 128 n = 0 count = 0 hour = 9 minute = 55 off = 203 interval = KlineInfo.get_interval(kline=kline) detal = KlineInfo.get_detal(kline=kline) table_name = 'A_MA_index_min5_time955_pixel128' print(table_name) for sc in stocks: end_time = dt.datetime(2018, 8, 10) start_time = dt.datetime(2016, 1, 1) data = KlineData.read_data(code=sc, start_date=Calendar.calc(start_time, -off)['date'], end_date=end_time, kline=kline, timemerge=True) if len(data) <= 200: continue data = CalMa.cal_ma(data=data, interval=interval, detal=detal) data = data.dropna() d = data.date.iloc[-1] data = data[ data.date <= dt.datetime(d.year, d.month, d.day, hour, minute)] print(sc) while end_time > start_time: # print(sc,dt.datetime.now()) data0 = data.tail(pixel) data0 = data0.reset_index(drop=True) profit = data0['profit'] profit_self = data0['profit_self'] date = data0['date'] # amin, amax = data0.low.min(), data0.high.max() if len(data0) < pixel: break date2 = date.iloc[pixel - interval - 1] date1 = date.iloc[pixel - 1] data0 = CalMa.data_normalization_ok(data0, pixel=pixel + n * count) profit = profit.iloc[pixel - 1] profitself = profit_self.iloc[pixel - 1] profit_relative = 0 if 'min' in kline: if 'A' in table_name: if date1.hour != hour and date1.minute != minute: data = data[data.date <= dt.datetime( date2.year, date2.month, date2.day, hour, minute)] break if abs(profit) > 0.11 or abs(profitself) > 0.097: data = data[data.date <= dt.datetime( date2.year, date2.month, date2.day, hour, minute)] continue else: if abs(profit) > 0.1 * interval: data = data[data.date <= dt.datetime( date2.year, date2.month, date2.day, hour, minute)] continue if 'A' in table_name: if abs(profitself) > 0.097: data = data[data.date <= dt.datetime( date2.year, date2.month, date2.day, hour, minute)] print(sc, str(profitself) + '_' + str(date)) continue try: visualization.draw_ma_line3(data=data0, profits=profit, date=date1, code=sc, table_name=table_name, pixel=pixel, n=n, profit_relative=profit_relative, count=count) pass except Exception as e: print(e) pass data = data[data.date <= dt.datetime(date2.year, date2.month, date2.day, hour, minute)] end_time = date2 return 1
def fun(stocks): # print(os.getpid(), stocks) kline = 'usa_kline_day' pixel = 64 length = pixel * 2 interval = KlineInfo.get_interval(kline=kline) off = KlineInfo.get_off(kline=kline) table_name = 'MACD_and_MA_' + kline print(table_name) for sc in stocks: print(os.getpid(), sc) end_time = dt.datetime(2018, 6, 20) start_time = dt.datetime(2015, 1, 1) data = KlineData.read_data(code=sc, start_date=Calendar.calc(start_time, -off)['date'], end_date=end_time, kline=kline, timemerge=True) if len(data) <= (off - 20): continue data = calculate.calculate_data(data, interval) # data = data.sort_values(by=['date'], ascending=True) data = data.dropna() # data = data[0:len(data) - interval] while end_time > dt.datetime(2016, 1, 5): # print(sc,dt.datetime.now()) data0 = data.tail(length) data0 = data0.reset_index(drop=True) profit = data0['profit'] date = data0['date'] data0 = calculate.data_normalization(data=data0, pixel=pixel, length=length) # print(sc, dt.datetime.now()) data0['date'] = date data0['profit'] = profit if len(data0) < length: break data0 = data0.reset_index(drop=True) profit = data0.profit.iloc[length - 1] date = data0.date.iloc[length - 1] if 'min' in kline: if 'A' in table_name: if date.hour != 15: data = data[data.date < dt.datetime( date.year, date.month, date.day)] break if abs(profit) > 0.11: data = data[data.date < dt.datetime( date.year, date.month, date.day)] continue else: if abs(profit) > 0.1 * interval: data = data[data.date < dt.datetime( date.year, date.month, date.day)] continue # print(sc, dt.datetime.now()) try: visualization.draw_macd_and_ma(data=data0, profits=profit, date=date, code=sc, table_name=table_name, pixel=pixel, length=length) pass except Exception as e: print(e) pass data = data[ data.date < dt.datetime(date.year, date.month, date.day)] end_time = date return 1
def fun(stocks): # print(os.getpid(), stocks) kline = 'hk_kline_min30' pixel = 64 table_name = 'MACD_' + kline print(table_name) interval = KlineInfo.get_interval(kline=kline) # detal= KlineInfo.get_detal(kline=kline) # path = "//DESKTOP-4JKCMO0/zjf1/H_macd_kline_day" # path = "f:/zjf/image/macd_kline_day_test" # File.check_file(path=path) for sc in stocks: end_time = dt.datetime(2018, 6, 20) start_time = dt.datetime(2015, 1, 1) data = KlineData.read_data(code=sc, start_date=Calendar.calc(start_time, -203)['date'], end_date=end_time, kline=kline, timemerge=True) if len(data) <= 200: continue data = CalMacd.cal_macd(data, interval) data = data.sort_values(by=['date'], ascending=True) data = data.dropna() # data = data[0:len(data) - interval] print(sc) while end_time > dt.datetime(2016, 1, 5): # print(sc,dt.datetime.now()) data0 = data.tail(pixel) data0 = data0.reset_index(drop=True) profit = data0['profit'] date = data0['date'] data0 = CalMacd.data_normalization(data0, pixel=pixel) # print(sc, dt.datetime.now()) data0['date'] = date data0['profit'] = profit if len(data0) < pixel: break data0 = data0.reset_index(drop=True) profit = data0.profit.iloc[pixel - 1] date = data0.date.iloc[pixel - 1] if 'min' in kline: if abs(profit) > 0.11: data = data[data.date < dt.datetime( date.year, date.month, date.day)] end_time = date continue else: if abs(profit) > 0.1 * interval: data = data[data.date < dt.datetime( date.year, date.month, date.day)] end_time = date continue # print(sc, dt.datetime.now()) try: visualization.draw_macd_line(data=data0, profits=profit, date=date, code=sc, table_name=table_name, pixel=pixel) # pass except Exception as e: print(e) pass # data = data[0:len(data) - detal] data = data[ data.date < dt.datetime(date.year, date.month, date.day)] end_time = date return 1
def is_trade_day(cls, date): curror = BaseModel('calendar').query(dict(date=Calendar.today())) return True if curror.count() else False
def fun(stocks): # print(os.getpid(), stocks) kline = 'index_min5' pixel = 64 length = 12 count = 5 interval = 48 hour = 9 minute = 55 detal = KlineInfo.get_detal(kline=kline) # wave=0.02 # amount_data=50000000 table_name = 'A_MA_Stub_' + kline + '_version5' print(table_name) for sc in stocks: print(sc) end_time = dt.datetime(2018, 8, 15) start_time = dt.datetime(2016, 1, 1) data = KlineData.read_data(code=sc, start_date=Calendar.calc(start_time, -8)['date'], end_date=end_time, kline=kline, timemerge=True) if len(data) <= 200: continue # data = CalMacd.cal_macd(data=data, interval=interval, detal=detal) # data=CalMa.cal_ma_stub(data=data,interval=interval,detal=detal) data = CalMa.cal_profit(data=data, interval=interval, detal=detal) data = data.dropna() d = data.date.iloc[-1] data = data[ data.date <= dt.datetime(d.year, d.month, d.day, hour, minute)] while end_time > start_time: # print(sc,dt.datetime.now()) data0 = data.tail(length) data0 = data0.reset_index(drop=True) profit = data0['profit'] profit_self = data0['profit_self'] date = data0['date'] # amin, amax = data0.low.min(), data0.high.max() if len(data0) < length: break date1 = date.iloc[length - 1] date2 = date.iloc[length - 10] # data0 = CalMacd.data_normalization(data0,pixel=pixel) data0 = CalMa.data_stub_normalization(data0, pixel=pixel) profit = profit.iloc[length - 1] profitself = profit_self.iloc[length - 1] profit_relative = 0 if 'min' in kline: if 'A' in table_name: if date1.hour != hour and date1.minute != minute: # data = data[data.date < dt.datetime(date2.year, date2.month, date2.day)] break if abs(profit) > 0.11 or abs(profitself) > 0.097: data = data[data.date <= dt.datetime( date2.year, date2.month, date2.day, hour, minute)] continue else: if abs(profit) > 0.1 * interval: data = data[data.date <= dt.datetime( date2.year, date2.month, date2.day, hour, minute)] continue if 'A' in table_name: if abs(profitself) > 0.097: data = data[data.date <= dt.datetime( date2.year, date2.month, date2.day, hour, minute)] print(sc, str(profitself) + '_' + str(date1)) continue try: visualization.draw_ma_stub_line2( data=data0, profits=profit, date=date1, code=sc, table_name=table_name, pixel=pixel, length=length, profit_relative=profit_relative, count=count) pass except Exception as e: print(e) pass data = data[data.date <= dt.datetime(date2.year, date2.month, date2.day, hour, minute)] end_time = date2 return 1
def fun(stocks): # print(os.getpid(), stocks) kline = 'index_min5' pixel=56 n=4 count=2 interval = KlineInfo.get_interval(kline=kline) detal=KlineInfo.get_detal(kline=kline) table_name='A_MACD_left_right_'+kline+'_version6' print(table_name) for sc in stocks: end_time = dt.datetime(2018, 8, 10) start_time = dt.datetime(2016, 1, 1) data = KlineData.read_data(code=sc, start_date=Calendar.calc(start_time, -203)['date'], end_date=end_time, kline=kline, timemerge=True) if len(data) <= 200 : continue data = CalMacd.cal_macd(data=data, interval=interval, detal=detal) data = data.dropna() d = data.date.iloc[-1] data = data[data.date <= dt.datetime(d.year, d.month, d.day, 9, 35)] print(sc) while end_time > start_time: # print(sc,dt.datetime.now()) data0 = data.tail(pixel) data0 = data0.reset_index(drop=True) profit = data0['profit'] profit_self = data0['profit_self'] date = data0['date'] if len(data0) < pixel: break date2 = date.iloc[pixel - 6] date1 = date.iloc[pixel - 1] data0 = CalMacd.data_normalization(data0,pixel=pixel+n*count) profit = profit.iloc[pixel-1] profitself = profit_self.iloc[pixel-1] profit_relative = 0 if 'min' in kline: if 'A' in table_name: if date1.hour != 9 and date1.minute!=35: data = data[data.date <= dt.datetime(date2.year, date2.month, date2.day, 9, 35)] break if abs(profit) > 0.11 or abs(profitself)>0.097: data = data[data.date <= dt.datetime(date2.year, date2.month, date2.day, 9, 35)] continue else: if abs(profit) > 0.1 * interval: data = data[data.date <= dt.datetime(date2.year, date2.month, date2.day, 9, 35)] continue if 'A' in table_name: if abs(profitself) > 0.097: data = data[data.date <= dt.datetime(date2.year, date2.month, date2.day, 9, 35)] print(sc, str(profitself) + '_' + str(date)) continue try: visualization.draw_macd_line4(data=data0, profits=profit, date=date1, code=sc, table_name=table_name, pixel=pixel, n=n, profit_relative=profit_relative, count=count) pass except Exception as e: print(e) pass data = data[data.date <= dt.datetime(date2.year, date2.month, date2.day, 9, 35)] end_time = date2 return 1
def fun(stocks): # print(os.getpid(), stocks) kline = 'kline_day' table_name = 'A_new_plus_last8_MA_' + kline print(table_name) interval = KlineInfo.get_interval(kline) detal = KlineInfo.get_detal(kline) pixel = 64 n = 8 for sc in stocks: end_time = dt.datetime(2018, 6, 25) start_time = dt.datetime(2016, 1, 1) data = KlineData.read_data(code=sc, start_date=Calendar.calc(start_time, -203)['date'], end_date=end_time, kline=kline, timemerge=True) if len(data) <= 200: continue data = CalMa.cal_ma(data, interval, detal) data = data.dropna() print(sc) while end_time > dt.datetime(2016, 1, 1): data0 = data.tail(pixel) data0 = data0.reset_index(drop=True) profit = data0['profit'] profitself = data0['profit_self'] date = data0['date'] data0 = CalMa.data_normalization2(data0, pixel) data0['date'] = date data0['profit'] = profit data0['profitself'] = profitself if len(data0) < pixel: break data0 = data0.reset_index(drop=True) profit = data0.profit.iloc[len(data0) - 1] profitself = data0.profitself.iloc[len(data0) - 1] date = data0.date.iloc[len(data0) - 1] if 'min' in kline: if 'A' in table_name: if date.hour != 15: data = data[data.date < dt.datetime( date.year, date.month, date.day)] break if abs(profit) > 0.11 or abs(profitself) > 0.097: data = data[data.date < dt.datetime( date.year, date.month, date.day)] continue else: if abs(profit) > 0.1 * interval: data = data[data.date < dt.datetime( date.year, date.month, date.day)] continue if 'A' in table_name: if abs(profitself) > 0.097: data = data[data.date < dt.datetime( date.year, date.month, date.day)] print(sc, str(profitself) + '_' + str(date)) continue try: visualization.draw_ma_line2(data=data0, profits=profit, date=date, code=sc, table_name=table_name, pixel=pixel, n=n) except Exception as e: print(e) pass data = data[ data.date < dt.datetime(date.year, date.month, date.day)] end_time = date return 1
def fun(data): stocks = data['stocks'] kline = data['kline'] pixel = 64 interval = KlineInfo.get_interval(kline=kline) detal = KlineInfo.get_detal(kline=kline) table_name = 'MACD_A_' + kline day_interval = KlineInfo.get_day_interval(kline) print(table_name) for sc in stocks: # end_time = dt.datetime(2018, 8, 1) # start_time = dt.datetime(2018, 7, 16) end_time = Calendar.today() start_time = Calendar.calc(end_time, -day_interval)['date'] data = KlineData.read_data(code=sc, start_date=Calendar.calc(start_time, -203)['date'], end_date=end_time, kline=kline, timemerge=True) if len(data) <= 200: continue data = CalMacd.cal_macd(data=data, interval=interval, detal=detal) data = data.dropna() # data = data[0:len(data) - interval] print(sc) while end_time > start_time: # print(sc,dt.datetime.now()) data0 = data.tail(pixel) data0 = data0.reset_index(drop=True) profit = data0['profit'] profit_self = data0['profit_self'] date = data0['date'] data0 = CalMacd.data_normalization(data0, pixel=pixel) # print(sc, dt.datetime.now()) data0['date'] = date data0['profit'] = profit data0['profit_self'] = profit_self # print(len(data0)) if len(data0) < pixel: break data0 = data0.reset_index(drop=True) profit = data0.profit.iloc[pixel - 1] profitself = data0.profit_self.iloc[pixel - 1] date = data0.date.iloc[pixel - 1] if 'min' in kline: if 'A' in table_name: if date.hour != 15: data = data[data.date < dt.datetime( date.year, date.month, date.day)] break if abs(profit) > 0.11 or abs(profitself) > 0.097: data = data[data.date < dt.datetime( date.year, date.month, date.day)] continue else: if abs(profit) > 0.1 * interval: data = data[data.date < dt.datetime( date.year, date.month, date.day)] continue if 'A' in table_name: if abs(profitself) > 0.097: data = data[data.date < dt.datetime( date.year, date.month, date.day)] print(sc, str(profitself) + '_' + str(date)) continue try: visualization.draw_macd_line(data=data0, profits=profit, date=date, code=sc, table_name=table_name, pixel=pixel) pass except Exception as e: print(e) pass data = data[ data.date < dt.datetime(date.year, date.month, date.day)] end_time = date return 1