123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- # -*- encoding:utf-8 -*-
- import numpy as np
- from keras.models import load_model
- import joblib
- holder_stock_list = [
- '000063.SZ', '002093.SZ'
- '300253.SZ', '300807.SZ',
- # b账户
- ]
- 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(file, day, model='10_18d', log=True):
- lines = []
- with open(file) 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])
- model=load_model(model)
- for line in lines:
- train_x = np.array([line[:size - 1]])
- train_x_a = train_x[:,:18*24]
- train_x_a = train_x_a.reshape(train_x.shape[0], 18, 24)
- # train_x_b = train_x[:, 18*18:18*18+2*18]
- # train_x_b = train_x_b.reshape(train_x.shape[0], 18, 2, 1)
- train_x_c = train_x[:,18*24:]
- result = model.predict([train_x_c, train_x_a])
- # print(result, line[-1])
- stock = code_table.find_one({'ts_code':line[-1][0]})
- if result[0][0] + 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) + '_lstm.txt', mode='a', encoding="utf-8") as f:
- f.write(str(line[-1]) + ' ' + stock['name'] + ' ' + stock['sw_industry'] + ' ' + str(concept_detail_list) + ' buy' + '\n')
- elif result[0][2] > 0.5:
- if stock['ts_code'] in holder_stock_list:
- print(stock['ts_code'], stock['name'], '震荡评级')
- elif result[0][3] + 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],)
- # print(gainian_map)
- # print(hangye_map)
- gainian_list = [(key, gainian_map[key])for key in gainian_map]
- gainian_list = sorted(gainian_list, key=lambda x:x[1], reverse=True)
- hangye_list = [(key, hangye_map[key])for key in hangye_map]
- hangye_list = sorted(hangye_list, key=lambda x:x[1], reverse=True)
- print(gainian_list)
- print(hangye_list)
- 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)
- def check_everyday(day, today):
- a_list = _read_pfile_map('D:\\data\\quantization\\predict\\' + str(day) + '.txt')
- x = 0
- for a in a_list:
- print(a[:-1])
- k_day_list = list(k_table.find({'code':a[2:11], 'tradeDate':{'$lte':int(today)}}).sort('tradeDate', pymongo.DESCENDING).limit(5))
- if k_day_list is not None and len(k_day_list) > 0:
- k_day = k_day_list[0]
- k_day_0 = k_day_list[-1]
- k_day_last = k_day_list[1]
- if ((k_day_last['close'] - k_day_0['pre_close'])/k_day_0['pre_close']) < 0.2:
- print(k_day['open'], k_day['close'], 100*(k_day['close'] - k_day_last['close'])/k_day_last['close'])
- x = x + 100*(k_day['close'] - k_day_last['close'])/k_day_last['close']
- print(x/len(a_list))
- 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("D:\\data\\quantization\\stock160_18d_20200316.log", 20200316, model='160_18d_lstm_5D_ma5_s_seq.h5', log=True)
- # join_two_day(20200305, 20200305)
- # check_everyday(20200311, 20200312)
|