def insert_hist_trade(self): self.set_data() db = Db() engine = db._get_engine() sql_stocklist = "select code,name from stock_code" codes = pd.read_sql_query(sql_stocklist, engine) codes = codes.to_dict('records') i = 1 for row in codes: gta = td.get_hist_data(code=row['code'], start=self.nowdate, end=self.nowdate, ktype='D', retry_count=3, pause=0.001) gta['datain_date'] = self.nowtime gta['code'] = row['code'] gta['name'] = row['name'] gta['c_yearmonthday'] = gta.index gta = gta.to_dict('records') try: db.insertmany( """INSERT INTO trade_hist(c_yearmonthday,code,name,open,high,close,low,volume,price_change,p_change,ma5,ma10,ma20,v_ma5,v_ma10,v_ma20,turnover,datain_date) VALUES (%(c_yearmonthday)s,%(code)s,%(name)s,%(open)s,%(high)s,%(close)s,%(low)s,%(volume)s,%(price_change)s,%(p_change)s,%(ma5)s,%(ma10)s,%(ma20)s,%(v_ma5)s,%(v_ma10)s,%(v_ma20)s,%(turnover)s,%(datain_date)s)""", gta) except Exception, e: log.error('insert error:%s ', e) log.info('%s stock insert finished,%s,%s', i, row['code'], row['name'].decode('utf-8')) i += 1
def predict_today(datatype, timesteps, data_dim=15): # log = logger.log nowdate = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) x_predict, id_predict, name_predict = trade.get_today(seg_len=timesteps, datatype=datatype, split=0.1, debug=False) network = policy.LSTMPolicy.create_network(timesteps=timesteps, data_dim=data_dim) USER_HOME = os.environ['HOME'] out_directory_path = USER_HOME + '/dw/' meta_file = os.path.join(out_directory_path, 'metadata.json') weights_path = policy_trainer.get_best_weights(meta_file) network.load_weights(weights_path) predicts = network.predict(x_predict, batch_size=16) v_predicts = pd.DataFrame() v_predicts['code'] = id_predict v_predicts['name'] = name_predict v_predicts['predict'] = predicts v_predicts['datain_date'] = nowdate db = Db() v_predicts = v_predicts.to_dict('records') db.insertmany( """INSERT INTO predicts(code,name,predict,datain_date) VALUES (%(code)s,%(name)s,%(predict)s,%(datain_date)s)""", v_predicts) log.info('predicts finished')
def predict_today(datatype, timesteps, data_dim=15): # log = logger.log nowdate = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) x_predict, id_predict, name_predict = trade.get_today(seg_len=timesteps, datatype=datatype, split=0.1, debug=False) network = policy.LSTMPolicy.create_network(timesteps=timesteps, data_dim=data_dim) USER_HOME = os.environ['HOME'] out_directory_path = USER_HOME + '/dw/' meta_file = os.path.join(out_directory_path, 'metadata.json') weights_path = policy_trainer.get_best_weights(meta_file) network.load_weights(weights_path) predicts = network.predict(x_predict, batch_size=16) v_predicts = pd.DataFrame() v_predicts['code'] = id_predict v_predicts['name'] = name_predict v_predicts['predict'] = predicts v_predicts['datain_date'] = nowdate db = Db() v_predicts = v_predicts.to_dict('records') db.insertmany("""INSERT INTO predicts(code,name,predict,datain_date) VALUES (%(code)s,%(name)s,%(predict)s,%(datain_date)s)""", v_predicts) log.info('predicts finished')
def insert_predict_statics(): db = Db() nowdate = time.strftime("%Y-%m-%d", time.localtime(time.time())) psummery,headpredict = trade.get_predict() psummery['c_yearmonthday'] = nowdate headpredict['c_yearmonthday'] = nowdate psummery=psummery.to_dict('records') headpredict=headpredict.to_dict('records') db.insertmany("""INSERT INTO predict_head(c_yearmonthday,code,name,predict) VALUES (%(c_yearmonthday)s,%(code)s,%(name)s,%(predict)s)""", headpredict) db.insertmany("""INSERT INTO predict_statics(c_yearmonthday,p_cnt,p_mean,p_std,p_min,p25,p50,p75,p_max) VALUES (%(c_yearmonthday)s,%(p_cnt)s,%(p_mean)s,%(p_std)s,%(p_min)s,%(p25)s,%(p50)s,%(p75)s,%(p_max)s)""", psummery)
def insert_predict_acc(): db = Db() nowdate = time.strftime("%Y-%m-%d", time.localtime(time.time())) acc1 = trade.get_predict_acc1() acc1['c_yearmonthday'] = nowdate acc1=acc1.to_dict('records') db.insertmany("""INSERT INTO acc1(c_yearmonthday,code,name,predict,p_change,acc) VALUES (%(c_yearmonthday)s,%(code)s,%(name)s,%(predict)s,%(p_change)s,%(acc)s)""", acc1) acc2 = trade.get_predict_acc2() acc2['c_yearmonthday'] = nowdate acc2=acc2.to_dict('records') db.insertmany("""INSERT INTO acc2(c_yearmonthday,p_acc,p_change,h_p_acc,h_p_change) VALUES (%(c_yearmonthday)s,%(p_acc)s,%(p_change)s,%(h_p_acc)s,%(h_p_change)s)""", acc2)
def insert_today_trade(self): self.set_data() db = Db() gta = td.get_today_all() gta['datain_date'] = self.nowtime gta['c_yearmonthday'] = self.nowdate gta = gta.to_dict('records') db.insertmany( """INSERT INTO trade_record(c_yearmonthday,code,name,changepercent,trade,open,high,low,settlement ,volume,turnoverratio,amount,per,pb,mktcap,nmc,datain_date) VALUES (%(c_yearmonthday)s,%(code)s,%(name)s,%(changepercent)s,%(trade)s,%(open)s,%(high)s,%(low)s,%(settlement)s,%(volume)s,%(turnoverratio)s,%(amount)s,%(per)s,%(pb)s,%(mktcap)s,%(nmc)s,%(datain_date)s)""", gta)
def insert_predict_statics(): db = Db() nowdate = time.strftime("%Y-%m-%d", time.localtime(time.time())) psummery, headpredict = trade.get_predict() psummery['c_yearmonthday'] = nowdate headpredict['c_yearmonthday'] = nowdate psummery = psummery.to_dict('records') headpredict = headpredict.to_dict('records') db.insertmany( """INSERT INTO predict_head(c_yearmonthday,code,name,predict) VALUES (%(c_yearmonthday)s,%(code)s,%(name)s,%(predict)s)""", headpredict) db.insertmany( """INSERT INTO predict_statics(c_yearmonthday,p_cnt,p_mean,p_std,p_min,p25,p50,p75,p_max) VALUES (%(c_yearmonthday)s,%(p_cnt)s,%(p_mean)s,%(p_std)s,%(p_min)s,%(p25)s,%(p50)s,%(p75)s,%(p_max)s)""", psummery)
def insert_predict_acc(): db = Db() nowdate = time.strftime("%Y-%m-%d", time.localtime(time.time())) acc1 = trade.get_predict_acc1() acc1['c_yearmonthday'] = nowdate acc1 = acc1.to_dict('records') db.insertmany( """INSERT INTO acc1(c_yearmonthday,code,name,predict,p_change,acc) VALUES (%(c_yearmonthday)s,%(code)s,%(name)s,%(predict)s,%(p_change)s,%(acc)s)""", acc1) acc2 = trade.get_predict_acc2() acc2['c_yearmonthday'] = nowdate acc2 = acc2.to_dict('records') db.insertmany( """INSERT INTO acc2(c_yearmonthday,p_acc,p_change,h_p_acc,h_p_change) VALUES (%(c_yearmonthday)s,%(p_acc)s,%(p_change)s,%(h_p_acc)s,%(h_p_change)s)""", acc2)