# -*- encoding:utf-8 -*- import numpy as np from keras.models import load_model import joblib holder_stock_list = [ '000063.SZ', '002373.SZ', '300253.SZ', '300059.SZ', # b账户 '002373.SZ', '300422.SZ', '300468.SZ', ] def read_data(path): lines = [] with open(path) as f: for line in f.readlines()[:]: line = eval(line.strip()) if line[-2][0].startswith('0') or line[-2][0].startswith('3'): lines.append(line) size = len(lines[0]) train_x=[s[:size - 2] for s in lines] train_y=[s[size-1] for s in lines] return np.array(train_x),np.array(train_y),lines import pymongo from util.mongodb import get_mongo_table_instance code_table = get_mongo_table_instance('tushare_code') k_table = get_mongo_table_instance('stock_day_k') stock_concept_table = get_mongo_table_instance('tushare_concept_detail') all_concept_code_list = list(get_mongo_table_instance('tushare_concept').find({})) industry = ['家用电器', '元器件', 'IT设备', '汽车服务', '汽车配件', '软件服务', '互联网', '纺织', '塑料', '半导体',] A_concept_code_list = [ 'TS2', # 5G 'TS24', # OLED 'TS26', #健康中国 'TS43', #新能源整车 'TS59', # 特斯拉 'TS65', #汽车整车 'TS142', # 物联网 'TS153', # 无人驾驶 'TS163', # 雄安板块-智慧城市 'TS175', # 工业自动化 'TS232', # 新能源汽车 'TS254', # 人工智能 'TS258', # 互联网医疗 'TS264', # 工业互联网 'TS266', # 半导体 'TS269', # 智慧城市 'TS271', # 3D玻璃 'TS295', # 国产芯片 'TS303', # 医疗信息化 'TS323', # 充电桩 'TS328', # 虹膜识别 'TS361', # 病毒 ] gainian_map = {} hangye_map = {} def predict_today(day, model='10_18d', log=True): lines = [] with open('D:\\data\\quantization\\stock' + model + '_' + str(day) +'.log') as f: for line in f.readlines()[:]: line = eval(line.strip()) # if line[-1][0].startswith('0') or line[-1][0].startswith('3'): lines.append(line) size = len(lines[0]) train_x=[s[:size - 1] for s in lines] np.array(train_x) estimator = joblib.load('km_dmi_18.pkl') models = [] for x in range(0, 12): models.append(load_model(model + '_dnn_seq_' + str(x) + '.h5')) x = 24 # 每条数据项数 k = 18 # 周期 shift = 1 for line in lines: # print(line) v = line[1:x*k + 1] v = np.array(v) v = v.reshape(k, x) v = v[:,4:8] v = v.reshape(1, 4*k) # print(v) r = estimator.predict(v) train_x = np.array([line[:size - 1]]) result = models[r[0]].predict(train_x) # print(result, line[-1]) stock = code_table.find_one({'ts_code':line[-1][0]}) if result[0][0] > 0.5 or result[0][1] > 0.5: if line[-1][0].startswith('688'): continue # 去掉ST if stock['name'].startswith('ST') or stock['name'].startswith('N') or stock['name'].startswith('*'): continue if stock['ts_code'] in holder_stock_list: print(stock['ts_code'], stock['name'], '维持买入评级') # 跌的 k_table_list = list(k_table.find({'code':line[-1][0], 'tradeDate':{'$lte':day}}).sort("tradeDate", pymongo.DESCENDING).limit(5)) # if k_table_list[0]['close'] > k_table_list[-1]['close']*1.20: # continue # if k_table_list[0]['close'] < k_table_list[-1]['close']*0.90: # continue # if k_table_list[-1]['close'] > 80: # continue # 指定某几个行业 # if stock['industry'] in industry: concept_code_list = list(stock_concept_table.find({'ts_code':stock['ts_code']})) concept_detail_list = [] # 处理行业 if stock['sw_industry'] in hangye_map: i_c = hangye_map[stock['sw_industry']] hangye_map[stock['sw_industry']] = i_c + 1 else: hangye_map[stock['sw_industry']] = 1 # if len(concept_code_list) > 0: # for concept in concept_code_list: # for c in all_concept_code_list: # if c['code'] == concept['concept_code']: # concept_detail_list.append(c['name']) # # if c['name'] in gainian_map: # g_c = gainian_map[c['name']] # gainian_map[c['name']] = g_c + 1 # else: # gainian_map[c['name']] = 1 print(line[-1], stock['name'], stock['sw_industry'], str(concept_detail_list), 'buy', k_table_list[0]['pct_chg']) if log is True: with open('D:\\data\\quantization\\predict\\' + str(day) + '.txt', mode='a', encoding="utf-8") as f: f.write(str(line[-1]) + ' ' + stock['name'] + ' ' + stock['sw_industry'] + ' ' + str(concept_detail_list) + ' buy' + '\n') # concept_list = list(stock_concept_table.find({'ts_code':stock['ts_code']})) # concept_list = [c['concept_code'] for c in concept_list] elif result[0][2] > 0.5: if stock['ts_code'] in holder_stock_list: print(stock['ts_code'], stock['name'], '震荡评级') elif result[0][3] > 0.5 or result[0][4] > 0.5: if stock['ts_code'] in holder_stock_list: print(stock['ts_code'], stock['name'], '赶紧卖出') else: if stock['ts_code'] in holder_stock_list: print(stock['ts_code'], stock['name'], result[0], r[0]) print(gainian_map) print(hangye_map) def _read_pfile_map(path): s_list = [] with open(path, encoding='utf-8') as f: for line in f.readlines()[:]: s_list.append(line) return s_list def join_two_day(a, b): a_list = _read_pfile_map('D:\\data\\quantization\\predict\\' + str(a) + '.txt') b_list = _read_pfile_map('D:\\data\\quantization\\predict\\dmi_' + str(b) + '.txt') for a in a_list: for b in b_list: if a[2:11] == b[2:11]: print(a) if __name__ == '__main__': # predict(file_path='D:\\data\\quantization\\stock6_5_test.log', model_path='5d_dnn_seq.h5') # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5') # multi_predict() # predict_today(20200305, model='11_18d', log=True) join_two_day(20200311, 20200311)