mix_predict_everyday.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. # -*- encoding:utf-8 -*-
  2. import numpy as np
  3. from keras.models import load_model
  4. import joblib
  5. holder_stock_list = [
  6. # 医疗
  7. '000150.SZ', '300300.SZ', '603990.SH', '300759.SZ', '300347.SZ', '300003.SZ', '300253.SZ',
  8. # 5G
  9. '300698.SZ', '600498.SH', '300310.SZ', '600353.SH', '603912.SH', '603220.SH', '300602.SZ', '600260.SH',
  10. # 车联网
  11. '002369.SZ', '002920.SZ', '300020.SZ', '002373.SZ', '002869.SZ',
  12. # 工业互联网
  13. '002184.SZ', '002364.SZ','300310.SZ', '300670.SZ', '300166.SZ', '002169.SZ', '002380.SZ',
  14. # 特高压
  15. '300341.SZ', '300670.SZ', '300018.SZ', '600268.SH', '002879.SZ',
  16. # 基础建设
  17. '300041.SZ', '603568.SH', '000967.SZ', '603018.SH',
  18. # B
  19. '002555.SZ', '002174.SZ',
  20. # ROE
  21. '002976.SZ', '002847.SZ', '002597.SZ', '300686.SZ', '000708.SZ', '603948.SH', '600507.SH', '300401.SZ', '002714.SZ', '600732.SH', '300033.SZ', '300822.SZ', '300821.SZ',
  22. '002458.SZ', '000708.SZ', '600732.SH', '603719.SH', '300821.SZ', '300800.SZ', '300816.SZ', '300812.SZ', '603195.SH', '300815.SZ', '603053.SH', '603551.SH', '002975.SZ',
  23. '603949.SH', '002970.SZ', '300809.SZ', '002968.SZ', '300559.SZ', '002512.SZ', '300783.SZ', '300003.SZ', '603489.SH', '300564.SZ', '600802.SH', '002600.SZ',
  24. '000933.SZ', '601918.SH', '000651.SZ', '002916.SZ', '000568.SZ', '000717.SZ', '600452.SH', '603589.SH', '600690.SH', '603886.SH', '300117.SZ', '000858.SZ', '002102.SZ',
  25. '300136.SZ', '600801.SH', '600436.SH', '300401.SZ', '002190.SZ', '300122.SZ', '002299.SZ', '603610.SH', '002963.SZ', '600486.SH', '300601.SZ', '300682.SZ', '300771.SZ',
  26. '000868.SZ', '002607.SZ', '603068.SH', '603508.SH', '603658.SH', '300571.SZ', '603868.SH', '600768.SH', '300760.SZ', '002901.SZ', '603638.SH', '601100.SH', '002032.SZ',
  27. '600083.SH', '600507.SH', '603288.SH', '002304.SZ', '000963.SZ', '300572.SZ', '000885.SZ', '600995.SH', '300080.SZ', '601888.SH', '000048.SZ', '000333.SZ', '300529.SZ',
  28. '000537.SZ', '002869.SZ', '600217.SH', '000526.SZ', '600887.SH', '002161.SZ', '600267.SH', '600668.SH', '600052.SH', '002379.SZ', '603369.SH', '601360.SH', '002833.SZ',
  29. '002035.SZ', '600031.SH', '600678.SH', '600398.SH', '600587.SH', '600763.SH', '002016.SZ', '603816.SH', '000031.SZ', '002555.SZ', '603983.SH', '002746.SZ', '603899.SH',
  30. '300595.SZ', '300632.SZ', '600809.SH', '002507.SZ', '300198.SZ', '600779.SH', '603568.SH', '300638.SZ', '002011.SZ', '603517.SH', '000661.SZ', '300630.SZ', '000895.SZ',
  31. '002841.SZ', '300602.SZ', '300418.SZ', '603737.SH', '002755.SZ', '002803.SZ', '002182.SZ', '600132.SH', '300725.SZ', '600346.SH', '300015.SZ', '300014.SZ', '300628.SZ',
  32. '000789.SZ', '600368.SH', '300776.SZ', '600570.SH', '000509.SZ', '600338.SH', '300770.SZ', '600309.SH', '000596.SZ', '300702.SZ', '002271.SZ', '300782.SZ', '300577.SZ',
  33. '603505.SH', '603160.SH', '300761.SZ', '603327.SH', '002458.SZ', '300146.SZ', '002463.SZ', '300417.SZ', '600566.SH', '002372.SZ', '600585.SH', '000848.SZ', '600519.SH',
  34. '000672.SZ', '300357.SZ', '002234.SZ', '603444.SH', '300236.SZ', '603360.SH', '002677.SZ', '300487.SZ', '600319.SH', '002415.SZ', '000403.SZ', '600340.SH', '601318.SH',
  35. ]
  36. def read_data(path):
  37. lines = []
  38. with open(path) as f:
  39. for line in f.readlines()[:]:
  40. line = eval(line.strip())
  41. if line[-2][0].startswith('0') or line[-2][0].startswith('3'):
  42. lines.append(line)
  43. size = len(lines[0])
  44. train_x=[s[:size - 2] for s in lines]
  45. train_y=[s[size-1] for s in lines]
  46. return np.array(train_x),np.array(train_y),lines
  47. import pymongo
  48. from util.mongodb import get_mongo_table_instance
  49. code_table = get_mongo_table_instance('tushare_code')
  50. k_table = get_mongo_table_instance('stock_day_k')
  51. stock_concept_table = get_mongo_table_instance('tushare_concept_detail')
  52. all_concept_code_list = list(get_mongo_table_instance('tushare_concept').find({}))
  53. industry = ['家用电器', '元器件', 'IT设备', '汽车服务',
  54. '汽车配件', '软件服务',
  55. '互联网', '纺织',
  56. '塑料', '半导体',]
  57. A_concept_code_list = [ 'TS2', # 5G
  58. 'TS24', # OLED
  59. 'TS26', #健康中国
  60. 'TS43', #新能源整车
  61. 'TS59', # 特斯拉
  62. 'TS65', #汽车整车
  63. 'TS142', # 物联网
  64. 'TS153', # 无人驾驶
  65. 'TS163', # 雄安板块-智慧城市
  66. 'TS175', # 工业自动化
  67. 'TS232', # 新能源汽车
  68. 'TS254', # 人工智能
  69. 'TS258', # 互联网医疗
  70. 'TS264', # 工业互联网
  71. 'TS266', # 半导体
  72. 'TS269', # 智慧城市
  73. 'TS271', # 3D玻璃
  74. 'TS295', # 国产芯片
  75. 'TS303', # 医疗信息化
  76. 'TS323', # 充电桩
  77. 'TS328', # 虹膜识别
  78. 'TS361', # 病毒
  79. ]
  80. gainian_map = {}
  81. hangye_map = {}
  82. def predict_today(file, day, model='10_18d', log=True):
  83. lines = []
  84. with open(file) as f:
  85. for line in f.readlines()[:]:
  86. line = eval(line.strip())
  87. # if line[-1][0].startswith('0') or line[-1][0].startswith('3'):
  88. lines.append(line)
  89. size = len(lines[0])
  90. model=load_model(model)
  91. for line in lines:
  92. train_x = np.array([line[:size - 1]])
  93. train_x_tmp = train_x[:,:18*18]
  94. train_x_a = train_x_tmp.reshape(train_x.shape[0], 18, 18, 1)
  95. # train_x_b = train_x_tmp.reshape(train_x.shape[0], 18, 24)
  96. train_x_c = train_x[:,18*18:]
  97. result = model.predict([train_x_c, train_x_a, ])
  98. # print(result, line[-1])
  99. stock = code_table.find_one({'ts_code':line[-1][0]})
  100. if result[0][0] > 0.5:
  101. if line[-1][0].startswith('688'):
  102. continue
  103. # 去掉ST
  104. if stock['name'].startswith('ST') or stock['name'].startswith('N') or stock['name'].startswith('*'):
  105. continue
  106. if stock['ts_code'] in holder_stock_list:
  107. print(stock['ts_code'], stock['name'], '维持买入评级')
  108. # 跌的
  109. k_table_list = list(k_table.find({'code':line[-1][0], 'tradeDate':{'$lte':day}}).sort("tradeDate", pymongo.DESCENDING).limit(5))
  110. # if k_table_list[0]['close'] > k_table_list[-1]['close']*1.20:
  111. # continue
  112. # if k_table_list[0]['close'] < k_table_list[-1]['close']*0.90:
  113. # continue
  114. # if k_table_list[-1]['close'] > 80:
  115. # continue
  116. # 指定某几个行业
  117. # if stock['industry'] in industry:
  118. concept_code_list = list(stock_concept_table.find({'ts_code':stock['ts_code']}))
  119. concept_detail_list = []
  120. # 处理行业
  121. if stock['sw_industry'] in hangye_map:
  122. i_c = hangye_map[stock['sw_industry']]
  123. hangye_map[stock['sw_industry']] = i_c + 1
  124. else:
  125. hangye_map[stock['sw_industry']] = 1
  126. if len(concept_code_list) > 0:
  127. for concept in concept_code_list:
  128. for c in all_concept_code_list:
  129. if c['code'] == concept['concept_code']:
  130. concept_detail_list.append(c['name'])
  131. if c['name'] in gainian_map:
  132. g_c = gainian_map[c['name']]
  133. gainian_map[c['name']] = g_c + 1
  134. else:
  135. gainian_map[c['name']] = 1
  136. print(line[-1], stock['name'], stock['sw_industry'], str(concept_detail_list), 'buy', k_table_list[0]['pct_chg'])
  137. if log is True:
  138. with open('D:\\data\\quantization\\predict\\' + str(day) + '_mix.txt', mode='a', encoding="utf-8") as f:
  139. f.write(str(line[-1]) + ' ' + stock['name'] + ' ' + stock['sw_industry'] + ' ' + str(concept_detail_list) + ' buy' + '\n')
  140. elif result[0][1] > 0.5:
  141. if stock['ts_code'] in holder_stock_list:
  142. print(stock['ts_code'], stock['name'], '震荡评级')
  143. elif result[0][2] > 0.5:
  144. if stock['ts_code'] in holder_stock_list:
  145. print(stock['ts_code'], stock['name'], '赶紧卖出')
  146. else:
  147. if stock['ts_code'] in holder_stock_list:
  148. print(stock['ts_code'], stock['name'], result[0],)
  149. # print(gainian_map)
  150. # print(hangye_map)
  151. gainian_list = [(key, gainian_map[key])for key in gainian_map]
  152. gainian_list = sorted(gainian_list, key=lambda x:x[1], reverse=True)
  153. hangye_list = [(key, hangye_map[key])for key in hangye_map]
  154. hangye_list = sorted(hangye_list, key=lambda x:x[1], reverse=True)
  155. print(gainian_list)
  156. print(hangye_list)
  157. def _read_pfile_map(path):
  158. s_list = []
  159. with open(path, encoding='utf-8') as f:
  160. for line in f.readlines()[:]:
  161. s_list.append(line)
  162. return s_list
  163. def join_two_day(a, b):
  164. a_list = _read_pfile_map('D:\\data\\quantization\\predict\\' + str(a) + '.txt')
  165. b_list = _read_pfile_map('D:\\data\\quantization\\predict\\dmi_' + str(b) + '.txt')
  166. for a in a_list:
  167. for b in b_list:
  168. if a[2:11] == b[2:11]:
  169. print(a)
  170. def check_everyday(day, today):
  171. a_list = _read_pfile_map('D:\\data\\quantization\\predict\\' + str(day) + '.txt')
  172. x = 0
  173. for a in a_list:
  174. print(a[:-1])
  175. k_day_list = list(k_table.find({'code':a[2:11], 'tradeDate':{'$lte':int(today)}}).sort('tradeDate', pymongo.DESCENDING).limit(5))
  176. if k_day_list is not None and len(k_day_list) > 0:
  177. k_day = k_day_list[0]
  178. k_day_0 = k_day_list[-1]
  179. k_day_last = k_day_list[1]
  180. if ((k_day_last['close'] - k_day_0['pre_close'])/k_day_0['pre_close']) < 0.2:
  181. print(k_day['open'], k_day['close'], 100*(k_day['close'] - k_day_last['close'])/k_day_last['close'])
  182. x = x + 100*(k_day['close'] - k_day_last['close'])/k_day_last['close']
  183. print(x/len(a_list))
  184. if __name__ == '__main__':
  185. # predict(file_path='D:\\data\\quantization\\stock6_5_test.log', model_path='5d_dnn_seq.h5')
  186. # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
  187. # multi_predict()
  188. predict_today("D:\\data\\quantization\\stock186_18d_20200325.log", 20200325, model='186_18d_mix_6D_ma5_s_seq.h5', log=True)
  189. # join_two_day(20200305, 20200305)
  190. # check_everyday(20200311, 20200312)