lstm_predict_everyday.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. # -*- encoding:utf-8 -*-
  2. import numpy as np
  3. from keras.models import load_model
  4. import joblib
  5. holder_stock_list = [
  6. '000063.SZ', '002093.SZ'
  7. '300253.SZ', '300807.SZ',
  8. # b账户
  9. ]
  10. def read_data(path):
  11. lines = []
  12. with open(path) as f:
  13. for line in f.readlines()[:]:
  14. line = eval(line.strip())
  15. if line[-2][0].startswith('0') or line[-2][0].startswith('3'):
  16. lines.append(line)
  17. size = len(lines[0])
  18. train_x=[s[:size - 2] for s in lines]
  19. train_y=[s[size-1] for s in lines]
  20. return np.array(train_x),np.array(train_y),lines
  21. import pymongo
  22. from util.mongodb import get_mongo_table_instance
  23. code_table = get_mongo_table_instance('tushare_code')
  24. k_table = get_mongo_table_instance('stock_day_k')
  25. stock_concept_table = get_mongo_table_instance('tushare_concept_detail')
  26. all_concept_code_list = list(get_mongo_table_instance('tushare_concept').find({}))
  27. industry = ['家用电器', '元器件', 'IT设备', '汽车服务',
  28. '汽车配件', '软件服务',
  29. '互联网', '纺织',
  30. '塑料', '半导体',]
  31. A_concept_code_list = [ 'TS2', # 5G
  32. 'TS24', # OLED
  33. 'TS26', #健康中国
  34. 'TS43', #新能源整车
  35. 'TS59', # 特斯拉
  36. 'TS65', #汽车整车
  37. 'TS142', # 物联网
  38. 'TS153', # 无人驾驶
  39. 'TS163', # 雄安板块-智慧城市
  40. 'TS175', # 工业自动化
  41. 'TS232', # 新能源汽车
  42. 'TS254', # 人工智能
  43. 'TS258', # 互联网医疗
  44. 'TS264', # 工业互联网
  45. 'TS266', # 半导体
  46. 'TS269', # 智慧城市
  47. 'TS271', # 3D玻璃
  48. 'TS295', # 国产芯片
  49. 'TS303', # 医疗信息化
  50. 'TS323', # 充电桩
  51. 'TS328', # 虹膜识别
  52. 'TS361', # 病毒
  53. ]
  54. gainian_map = {}
  55. hangye_map = {}
  56. def predict_today(file, day, model='10_18d', log=True):
  57. lines = []
  58. with open(file) as f:
  59. for line in f.readlines()[:]:
  60. line = eval(line.strip())
  61. # if line[-1][0].startswith('0') or line[-1][0].startswith('3'):
  62. lines.append(line)
  63. size = len(lines[0])
  64. model=load_model(model)
  65. for line in lines:
  66. train_x = np.array([line[:size - 1]])
  67. train_x_a = train_x[:,:18*24]
  68. train_x_a = train_x_a.reshape(train_x.shape[0], 18, 24)
  69. # train_x_b = train_x[:, 18*18:18*18+2*18]
  70. # train_x_b = train_x_b.reshape(train_x.shape[0], 18, 2, 1)
  71. train_x_c = train_x[:,18*24:]
  72. result = model.predict([train_x_c, train_x_a])
  73. # print(result, line[-1])
  74. stock = code_table.find_one({'ts_code':line[-1][0]})
  75. if result[0][0] + result[0][1] > 0.5:
  76. if line[-1][0].startswith('688'):
  77. continue
  78. # 去掉ST
  79. if stock['name'].startswith('ST') or stock['name'].startswith('N') or stock['name'].startswith('*'):
  80. continue
  81. if stock['ts_code'] in holder_stock_list:
  82. print(stock['ts_code'], stock['name'], '维持买入评级')
  83. # 跌的
  84. k_table_list = list(k_table.find({'code':line[-1][0], 'tradeDate':{'$lte':day}}).sort("tradeDate", pymongo.DESCENDING).limit(5))
  85. # if k_table_list[0]['close'] > k_table_list[-1]['close']*1.20:
  86. # continue
  87. # if k_table_list[0]['close'] < k_table_list[-1]['close']*0.90:
  88. # continue
  89. # if k_table_list[-1]['close'] > 80:
  90. # continue
  91. # 指定某几个行业
  92. # if stock['industry'] in industry:
  93. concept_code_list = list(stock_concept_table.find({'ts_code':stock['ts_code']}))
  94. concept_detail_list = []
  95. # 处理行业
  96. if stock['sw_industry'] in hangye_map:
  97. i_c = hangye_map[stock['sw_industry']]
  98. hangye_map[stock['sw_industry']] = i_c + 1
  99. else:
  100. hangye_map[stock['sw_industry']] = 1
  101. if len(concept_code_list) > 0:
  102. for concept in concept_code_list:
  103. for c in all_concept_code_list:
  104. if c['code'] == concept['concept_code']:
  105. concept_detail_list.append(c['name'])
  106. if c['name'] in gainian_map:
  107. g_c = gainian_map[c['name']]
  108. gainian_map[c['name']] = g_c + 1
  109. else:
  110. gainian_map[c['name']] = 1
  111. print(line[-1], stock['name'], stock['sw_industry'], str(concept_detail_list), 'buy', k_table_list[0]['pct_chg'])
  112. if log is True:
  113. with open('D:\\data\\quantization\\predict\\' + str(day) + '_lstm.txt', mode='a', encoding="utf-8") as f:
  114. f.write(str(line[-1]) + ' ' + stock['name'] + ' ' + stock['sw_industry'] + ' ' + str(concept_detail_list) + ' buy' + '\n')
  115. elif result[0][2] > 0.5:
  116. if stock['ts_code'] in holder_stock_list:
  117. print(stock['ts_code'], stock['name'], '震荡评级')
  118. elif result[0][3] + result[0][4] > 0.5:
  119. if stock['ts_code'] in holder_stock_list:
  120. print(stock['ts_code'], stock['name'], '赶紧卖出')
  121. else:
  122. if stock['ts_code'] in holder_stock_list:
  123. print(stock['ts_code'], stock['name'], result[0],)
  124. # print(gainian_map)
  125. # print(hangye_map)
  126. gainian_list = [(key, gainian_map[key])for key in gainian_map]
  127. gainian_list = sorted(gainian_list, key=lambda x:x[1], reverse=True)
  128. hangye_list = [(key, hangye_map[key])for key in hangye_map]
  129. hangye_list = sorted(hangye_list, key=lambda x:x[1], reverse=True)
  130. print(gainian_list)
  131. print(hangye_list)
  132. def _read_pfile_map(path):
  133. s_list = []
  134. with open(path, encoding='utf-8') as f:
  135. for line in f.readlines()[:]:
  136. s_list.append(line)
  137. return s_list
  138. def join_two_day(a, b):
  139. a_list = _read_pfile_map('D:\\data\\quantization\\predict\\' + str(a) + '.txt')
  140. b_list = _read_pfile_map('D:\\data\\quantization\\predict\\dmi_' + str(b) + '.txt')
  141. for a in a_list:
  142. for b in b_list:
  143. if a[2:11] == b[2:11]:
  144. print(a)
  145. def check_everyday(day, today):
  146. a_list = _read_pfile_map('D:\\data\\quantization\\predict\\' + str(day) + '.txt')
  147. x = 0
  148. for a in a_list:
  149. print(a[:-1])
  150. k_day_list = list(k_table.find({'code':a[2:11], 'tradeDate':{'$lte':int(today)}}).sort('tradeDate', pymongo.DESCENDING).limit(5))
  151. if k_day_list is not None and len(k_day_list) > 0:
  152. k_day = k_day_list[0]
  153. k_day_0 = k_day_list[-1]
  154. k_day_last = k_day_list[1]
  155. if ((k_day_last['close'] - k_day_0['pre_close'])/k_day_0['pre_close']) < 0.2:
  156. print(k_day['open'], k_day['close'], 100*(k_day['close'] - k_day_last['close'])/k_day_last['close'])
  157. x = x + 100*(k_day['close'] - k_day_last['close'])/k_day_last['close']
  158. print(x/len(a_list))
  159. if __name__ == '__main__':
  160. # predict(file_path='D:\\data\\quantization\\stock6_5_test.log', model_path='5d_dnn_seq.h5')
  161. # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
  162. # multi_predict()
  163. predict_today("D:\\data\\quantization\\stock160_18d_20200312.log", 20200313, model='160_18d_lstm_5D_ma5_s_seq.h5', log=True)
  164. # join_two_day(20200305, 20200305)
  165. # check_everyday(20200311, 20200312)