mix_predict_everyday.py 7.0 KB

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