mix_predict_everyday_200.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. # -*- encoding:utf-8 -*-
  2. import numpy as np
  3. from keras.models import load_model
  4. import random
  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. # 华为
  19. '300687.SZ','002316.SZ','300339.SZ','300378.SZ','300020.SZ','300634.SZ','002570.SZ',
  20. '600801.SH', '300113.SZ','002555.SZ', '002174.SZ',
  21. ]
  22. ROE_stock_list = [ # ROE
  23. '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',
  24. '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',
  25. '603949.SH', '002970.SZ', '300809.SZ', '002968.SZ', '300559.SZ', '002512.SZ', '300783.SZ', '300003.SZ', '603489.SH', '300564.SZ', '600802.SH', '002600.SZ',
  26. '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',
  27. '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',
  28. '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',
  29. '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',
  30. '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',
  31. '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',
  32. '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',
  33. '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',
  34. '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',
  35. '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',
  36. '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',
  37. ]
  38. import pymongo
  39. from util.mongodb import get_mongo_table_instance
  40. code_table = get_mongo_table_instance('tushare_code')
  41. k_table = get_mongo_table_instance('stock_day_k')
  42. stock_concept_table = get_mongo_table_instance('tushare_concept_detail')
  43. all_concept_code_list = list(get_mongo_table_instance('tushare_concept').find({}))
  44. industry = ['家用电器', '元器件', 'IT设备', '汽车服务',
  45. '汽车配件', '软件服务',
  46. '互联网', '纺织',
  47. '塑料', '半导体',]
  48. A_concept_code_list = [ 'TS2', # 5G
  49. 'TS24', # OLED
  50. 'TS26', #健康中国
  51. 'TS43', #新能源整车
  52. 'TS59', # 特斯拉
  53. 'TS65', #汽车整车
  54. 'TS142', # 物联网
  55. 'TS153', # 无人驾驶
  56. 'TS163', # 雄安板块-智慧城市
  57. 'TS175', # 工业自动化
  58. 'TS232', # 新能源汽车
  59. 'TS254', # 人工智能
  60. 'TS258', # 互联网医疗
  61. 'TS264', # 工业互联网
  62. 'TS266', # 半导体
  63. 'TS269', # 智慧城市
  64. 'TS271', # 3D玻璃
  65. 'TS295', # 国产芯片
  66. 'TS303', # 医疗信息化
  67. 'TS323', # 充电桩
  68. 'TS328', # 虹膜识别
  69. 'TS361', # 病毒
  70. ]
  71. gainian_map = {}
  72. hangye_map = {}
  73. Z_list = [] # 自选
  74. R_list = [] # ROE
  75. O_list = [] # 其他
  76. def predict_today(file, day, model='10_18d', log=True):
  77. lines = []
  78. with open(file) as f:
  79. for line in f.readlines()[:]:
  80. line = eval(line.strip())
  81. lines.append(line)
  82. size = len(lines[0])
  83. model=load_model(model)
  84. for line in lines:
  85. train_x = np.array([line[:size - 1]])
  86. train_x_tmp = train_x[:,:18*20]
  87. train_x_a = train_x_tmp.reshape(train_x.shape[0], 18, 20, 1)
  88. # train_x_b = train_x_tmp.reshape(train_x.shape[0], 18, 24)
  89. train_x_c = train_x[:,18*20:]
  90. result = model.predict([train_x_c, train_x_a, ])
  91. # print(result, line[-1])
  92. stock = code_table.find_one({'ts_code':line[-1][0]})
  93. if result[0][0] > 0.6:
  94. if line[-1][0].startswith('688'):
  95. continue
  96. # 去掉ST
  97. if stock['name'].startswith('ST') or stock['name'].startswith('N') or stock['name'].startswith('*'):
  98. continue
  99. k_table_list = list(k_table.find({'code':line[-1][0], 'tradeDate':{'$lte':day}}).sort("tradeDate", pymongo.DESCENDING).limit(5))
  100. # if k_table_list[0]['close'] > k_table_list[-1]['close']*1.20:
  101. # continue
  102. # if k_table_list[0]['close'] < k_table_list[-1]['close']*0.90:
  103. # continue
  104. # if k_table_list[-1]['close'] > 80:
  105. # continue
  106. # 指定某几个行业
  107. # if stock['industry'] in industry:
  108. concept_code_list = list(stock_concept_table.find({'ts_code':stock['ts_code']}))
  109. concept_detail_list = []
  110. # 处理行业
  111. if stock['sw_industry'] in hangye_map:
  112. i_c = hangye_map[stock['sw_industry']]
  113. hangye_map[stock['sw_industry']] = i_c + 1
  114. else:
  115. hangye_map[stock['sw_industry']] = 1
  116. if len(concept_code_list) > 0:
  117. for concept in concept_code_list:
  118. for c in all_concept_code_list:
  119. if c['code'] == concept['concept_code']:
  120. concept_detail_list.append(c['name'])
  121. if c['name'] in gainian_map:
  122. g_c = gainian_map[c['name']]
  123. gainian_map[c['name']] = g_c + 1
  124. else:
  125. gainian_map[c['name']] = 1
  126. if stock['ts_code'] in holder_stock_list:
  127. print(line[-1], stock['name'], stock['sw_industry'], str(concept_detail_list), 'buy', k_table_list[0]['pct_chg'])
  128. print(stock['ts_code'], stock['name'], '买入评级')
  129. Z_list.append([stock['name'], stock['sw_industry'], k_table_list[0]['pct_chg']])
  130. elif stock['ts_code'] in ROE_stock_list:
  131. print(line[-1], stock['name'], stock['sw_industry'], str(concept_detail_list), 'buy', k_table_list[0]['pct_chg'])
  132. print(stock['ts_code'], stock['name'], '买入评级')
  133. R_list.append([stock['name'], stock['sw_industry'], k_table_list[0]['pct_chg']])
  134. else:
  135. O_list.append([stock['name'], stock['sw_industry'], k_table_list[0]['pct_chg']])
  136. if log is True:
  137. with open('D:\\data\\quantization\\predict\\' + str(day) + '_mix.txt', mode='a', encoding="utf-8") as f:
  138. f.write(str(line[-1]) + ' ' + stock['name'] + ' ' + stock['sw_industry'] + ' ' + str(concept_detail_list) + ' buy' + '\n')
  139. elif result[0][1] > 0.5:
  140. if stock['ts_code'] in holder_stock_list or stock['ts_code'] in ROE_stock_list:
  141. print(stock['ts_code'], stock['name'], '震荡评级')
  142. elif result[0][2] > 0.4:
  143. if stock['ts_code'] in holder_stock_list or stock['ts_code'] in ROE_stock_list:
  144. print(stock['ts_code'], stock['name'], '赶紧卖出')
  145. else:
  146. if stock['ts_code'] in holder_stock_list or stock['ts_code'] in ROE_stock_list:
  147. print(stock['ts_code'], stock['name'], result[0],)
  148. # print(gainian_map)
  149. # print(hangye_map)
  150. gainian_list = [(key, gainian_map[key])for key in gainian_map]
  151. gainian_list = sorted(gainian_list, key=lambda x:x[1], reverse=True)
  152. hangye_list = [(key, hangye_map[key])for key in hangye_map]
  153. hangye_list = sorted(hangye_list, key=lambda x:x[1], reverse=True)
  154. print(gainian_list)
  155. print(hangye_list)
  156. print('-----买入列表---------')
  157. print(Z_list)
  158. print(R_list)
  159. print(O_list)
  160. print('------随机结果--------')
  161. random.shuffle(Z_list)
  162. print('自选')
  163. print(Z_list[:3])
  164. random.shuffle(R_list)
  165. print('ROE')
  166. print(R_list[:3])
  167. random.shuffle(O_list)
  168. print('其他')
  169. print(O_list[:3])
  170. def _read_pfile_map(path):
  171. s_list = []
  172. with open(path, encoding='utf-8') as f:
  173. for line in f.readlines()[:]:
  174. s_list.append(line)
  175. return s_list
  176. def join_two_day(a, b):
  177. a_list = _read_pfile_map('D:\\data\\quantization\\predict\\' + str(a) + '.txt')
  178. b_list = _read_pfile_map('D:\\data\\quantization\\predict\\dmi_' + str(b) + '.txt')
  179. for a in a_list:
  180. for b in b_list:
  181. if a[2:11] == b[2:11]:
  182. print(a)
  183. def check_everyday(day, today):
  184. a_list = _read_pfile_map('D:\\data\\quantization\\predict\\' + str(day) + '.txt')
  185. x = 0
  186. for a in a_list:
  187. print(a[:-1])
  188. k_day_list = list(k_table.find({'code':a[2:11], 'tradeDate':{'$lte':int(today)}}).sort('tradeDate', pymongo.DESCENDING).limit(5))
  189. if k_day_list is not None and len(k_day_list) > 0:
  190. k_day = k_day_list[0]
  191. k_day_0 = k_day_list[-1]
  192. k_day_last = k_day_list[1]
  193. if ((k_day_last['close'] - k_day_0['pre_close'])/k_day_0['pre_close']) < 0.2:
  194. print(k_day['open'], k_day['close'], 100*(k_day['close'] - k_day_last['close'])/k_day_last['close'])
  195. x = x + 100*(k_day['close'] - k_day_last['close'])/k_day_last['close']
  196. print(x/len(a_list))
  197. if __name__ == '__main__':
  198. # predict(file_path='D:\\data\\quantization\\stock6_5_test.log', model_path='5d_dnn_seq.h5')
  199. # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
  200. # multi_predict()
  201. predict_today("D:\\data\\quantization\\stock216_18d_20200327.log", 20200327, model='216_18d_mix_6D_ma5_s_seq.h5', log=True)
  202. # join_two_day(20200305, 20200305)
  203. # check_everyday(20200311, 20200312)