cnn_predict_by_stock.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # -*- encoding:utf-8 -*-
  2. import numpy as np
  3. from keras.models import load_model
  4. import joblib
  5. def read_data(path):
  6. stock_lines = {}
  7. with open(path) as f:
  8. for line in f.readlines()[:]:
  9. line = eval(line.strip())
  10. stock = str(line[-2][0])
  11. if stock in stock_lines:
  12. stock_lines[stock].append(line)
  13. else:
  14. stock_lines[stock] = [line]
  15. # print(len(day_lines['20191230']))
  16. return stock_lines
  17. import pymongo
  18. from util.mongodb import get_mongo_table_instance
  19. code_table = get_mongo_table_instance('tushare_code')
  20. k_table = get_mongo_table_instance('stock_day_k')
  21. def predict(file_path='', model_path='15min_dnn_seq'):
  22. stock_lines = read_data(file_path)
  23. print('数据读取完毕')
  24. models = []
  25. # for x in range(0, 12):
  26. models.append(load_model(model_path + '.h5'))
  27. estimator = joblib.load('km_dmi_18.pkl')
  28. print('模型加载完毕')
  29. total_money = 0
  30. total_num = 0
  31. items = sorted(stock_lines.keys())
  32. for key in items:
  33. # print(day)
  34. lines = stock_lines[key]
  35. init_money = 10000
  36. last_price = 1
  37. if lines[0][-2][0].startswith('6'):
  38. continue
  39. buy = 0 # 0空 1买入 2卖出
  40. chiyou_0 = 0
  41. high_price = 0
  42. x = 24 # 每条数据项数
  43. k = 18 # 周期
  44. for line in lines:
  45. # v = line[1:x*k + 1]
  46. # v = np.array(v)
  47. # v = v.reshape(k, x)
  48. # v = v[:,6:10]
  49. # v = v.reshape(1, 4*k)
  50. # print(v)
  51. train_x = np.array([line[:-2]])
  52. train_x = train_x.reshape(train_x.shape[0], 1,6,77)
  53. result = models[0].predict(train_x)
  54. stock_name = line[-2]
  55. today_price = list(k_table.find({'code':line[-2][0], 'tradeDate':{'$gt':int(line[-2][1])}}).sort('tradeDate',pymongo.ASCENDING).limit(1))
  56. today_price = today_price[0]
  57. if result[0][0] > 0.6 or result[0][1] > 0.6: #and (r[0] not in [2,6,8,10]):
  58. chiyou_0 = 0
  59. if buy == 0:
  60. last_price = today_price['open']
  61. high_price = last_price
  62. print('首次买入', stock_name, today_price['open'])
  63. buy = 1
  64. else:
  65. init_money = init_money * (today_price['close'] - last_price)/last_price + init_money
  66. last_price = today_price['close']
  67. print('买入+买入', stock_name, today_price['close'])
  68. buy = 1
  69. if last_price > high_price:
  70. high_price = last_price
  71. elif buy == 1:
  72. chiyou_0 = chiyou_0 + 1
  73. last_price = today_price['close']
  74. if chiyou_0 > 2 and today_price['close'] < last_price:
  75. print('卖出', stock_name, today_price['close'])
  76. init_money = init_money * (today_price['close'] - last_price)/last_price + init_money
  77. buy = 0
  78. chiyou_0 = 0
  79. elif init_money < 9000:
  80. print('止损卖出', stock_name, today_price['close'])
  81. init_money = init_money * (today_price['close'] - last_price)/last_price + init_money
  82. buy = 0
  83. chiyou_0 = 0
  84. print(key, init_money)
  85. with open('D:\\data\\quantization\\stock_16_18d' + '_' + 'profit.log', 'a') as f:
  86. if init_money > 10000:
  87. f.write(str(key) + ' ' + str(init_money) + '\n')
  88. elif init_money < 10000:
  89. f.write(str(key) + ' ' + str(init_money) + '\n')
  90. if init_money != 10000:
  91. total_money = total_money + init_money
  92. total_num = total_num + 1
  93. print(total_money, total_num, total_money/total_num/10000)
  94. if __name__ == '__main__':
  95. # predict(file_path='D:\\data\\quantization\\stock6_5_test.log', model_path='5d_dnn_seq.h5')
  96. # predict(file_path='D:\\data\\quantization\\stock12_18d_test.log', model_path='12_18d_dnn_seq')
  97. predict(file_path='D:\\data\\quantization\\stock16_18d_test.log', model_path='16_18d_cnn_seq')
  98. # predict(file_path='D:\\data\\quantization\\stock12_18d_20190103_20190604.log', model_path='13_18d_dnn_seq')