def let_train_invests(self, corps, start_no=1): """입력한 회사들에 대해서 학습시키고 모의투자를 실행한다.""" if self.params.is_all_corps_model == True and self.params.remove_session_file == True: learning = Learning(self.params) learning.delete_learning_image() comp_rmses = [] no = 1 for index, corp_data in corps.iterrows(): if no < start_no: no += 1 continue corp_code = corp_data['종목코드'] corp_name = corp_data['회사명'] try: result = self.let_train_invest(corp_code, corp_name, no) except Exception as inst: print(inst) no += 1 continue comp_rmses.append(result) if no % 10 == 0: df_comp_rmses = pd.DataFrame(comp_rmses, columns=self.result_columns) DataUtils.save_excel(df_comp_rmses, self.get_result_file_path()) no += 1
def get_predicts(self, comp_code, tp): learning = Learning(self.params) investX = tp.data_params.investX #print(investX) graph_params = learning.get_train_model() X = graph_params.X Y_pred = graph_params.Y_pred training = graph_params.training output_keep_prob = graph_params.output_keep_prob saver = tf.train.Saver() session_path = learning.get_session_path(comp_code) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver.restore(sess, session_path) invest_predicts = None if len(investX) > 0: invest_predicts = sess.run(Y_pred, feed_dict={ X: investX, output_keep_prob: 1.0, training: False }) last_predict = sess.run(Y_pred, feed_dict={ X: tp.dataX_last, output_keep_prob: 1.0, training: False }) return invest_predicts, last_predict[0]
def restore(self) -> bool: learning = Learning(self._params) if learning.exist_learning_image(self._corp_code, True, self._name): saver = tf.train.Saver() saved_session_path = learning.get_session_path( self._corp_code, True, self._name) saver.restore(self.sess, saved_session_path) return True return False
def let_invest(self, comp_code, dataX_last, data_params): """학습 후 모의 주식 거래를 한다.""" stacked_rnn = StackedRnn(self.params) learning = Learning(self.params) invest_count = self.params.invest_count invest_money = self.params.invest_money investCloses = data_params['investCloses'] investRealCloses = data_params['investRealCloses'] investX = data_params['investX'] graph_params = stacked_rnn.get_stacted_rnn_model() X = graph_params['X'] Y_pred = graph_params['Y_pred'] output_keep_prob = graph_params['output_keep_prob'] session_file_path = learning.get_session_path(comp_code) now_stock_cnt = 0 all_invest_money = invest_money all_stock_count = now_stock_cnt predicts = [] now_close = 0 saver = tf.train.Saver() with tf.Session() as sess: init = tf.global_variables_initializer() sess.run(init) saver.restore(sess, session_file_path) for i in range(invest_count): invest_predicts = sess.run(Y_pred, feed_dict={ X: investX[i:i + 1], output_keep_prob: 1.0 }) predicts.append(invest_predicts[0]) invest_predict = invest_predicts[0][0] now_scaled_close = investCloses[i][0] now_close = investRealCloses[i] #print(invest_predict, now_scaled_close, now_close) invest_money, now_stock_cnt = self.let_invest_money( invest_predict, now_scaled_close, now_close, invest_money, now_stock_cnt) if i == 0: all_invest_money, all_stock_count = self.let_invest_money( 10.0, now_scaled_close, now_close, all_invest_money, all_stock_count) invest_money += self.to_money(now_stock_cnt, now_close) all_invest_money += self.to_money(all_stock_count, now_close) last_predict = sess.run(Y_pred, feed_dict={ X: dataX_last, output_keep_prob: 1.0 }) # print(now_money) return invest_money, last_predict, predicts, all_invest_money
def let_train_only(self, corp_code, stock_data, invest_count=None): """입력한 회사에 대해서 학습시킨다""" trains_data = TrainsData(self.params) learning = Learning(self.params) data_params, scaler_close, dataX_last = trains_data.get_train_test( stock_data, invest_count) rmse_val, train_cnt, rmse_vals, test_predict = learning.let_learning( corp_code, data_params) return rmse_val, train_cnt, data_params, dataX_last, scaler_close
def let_train_invest(self, corp_code, corp_name, no): """입력한 회사에 대해서 학습시키고 모의투자를 실행한다.""" stocks = Stocks(self.params) stock_data = stocks.get_stock_data(corp_code) invest_count = self.params.invest_count if invest_count == 0: rmse_val, train_cnt, data_params, dataX_last, scaler_close = self.let_train_only( corp_code, stock_data) last_money = self.params.invest_money all_invest_money = last_money else: if self.params.is_all_corps_model == False and self.params.remove_session_file == True: learning = Learning(self.params) learning.delete_learning_image(corp_code) invest = MockInvestment(self.params) rmse_val, train_cnt, data_params, dataX_last, scaler_close = self.let_train_only( corp_code, stock_data) last_money, last_predict, invest_predicts, all_invest_money = invest.let_invest( corp_code, dataX_last, data_params) if self.params.result_type == 'forcast': invest = MockInvestment(self.params) last_money, last_predict, invest_predicts, all_invest_money = \ invest.let_invest(corp_code, dataX_last, data_params) last_date = stock_data.tail(1)['date'].to_string(index=False) last_close_money, last_pred_money = invest.get_real_money( data_params, scaler_close, last_predict) last_pred_ratio = (last_pred_money - last_close_money) / last_close_money * 100 last_pred_ratio = "{:.2f}".format(last_pred_ratio) + "%" print(no, last_date, corp_code, corp_name, rmse_val, train_cnt, last_close_money, last_pred_money, last_pred_ratio) return [ no, last_date, corp_code, corp_name, rmse_val, train_cnt, last_close_money, last_pred_money, last_pred_ratio ] else: print(no, corp_code, corp_name, rmse_val, last_money, all_invest_money, train_cnt) return [ no, corp_code, corp_name, rmse_val, last_money, all_invest_money, train_cnt ]
def _get_predict(self, params, comp_code, investX): stacked_rnn = StackedRnn(params) graph_params = stacked_rnn.get_stacted_rnn_model() Y_pred = graph_params['Y_pred'] output_keep_prob = graph_params['output_keep_prob'] X = graph_params['X'] learning = Learning(params) file_path = learning.get_session_path(comp_code) saver = tf.train.Saver() with tf.Session() as sess: init = tf.global_variables_initializer() sess.run(init) saver.restore(sess, file_path) last_predict = sess.run(Y_pred, feed_dict={ X: investX, output_keep_prob: 1.0 }) return last_predict
def train(self, corp_code: str, corp_name: str, stock_data: pd.DataFrame, scaler_close: preprocessing.MinMaxScaler = None, invest_only: bool = False) -> TrainParams: """입력한 회사에 대해서 학습시킨다""" trains_data = TrainsData(self.params) learning = Learning(self.params) data_params, scaler_close, dataX_last = trains_data.get_train_test( stock_data, scaler_close) if invest_only: tp = learning.get_test_rmse(corp_code, data_params) else: tp = learning.learn(corp_code, corp_name, data_params) tp.scaler_close = scaler_close tp.dataX_last = dataX_last return tp
def let_train_invest_twins(self, corp_code, corp_name, no): """겨별 세션과 통합세션에서 예측한 값의 평균을 예측값으로 한다.""" stocks = Stocks(self.params) trains_data = TrainsData(self.params) learning = Learning(self.params) params_all = TrainParams('ALL_CORPS') learning_all = Learning(params_all) invest = MockInvestment(self.params) stock_data = stocks.get_stock_data(corp_code) data_params, scaler_close, dataX_last = trains_data.get_train_test( stock_data) rmse_val, train_cnt, rmse_vals, test_predict = learning.let_learning( corp_code, data_params) rmse_val_all, train_cnt_all, rmse_vals_all, test_predict_all = learning_all.let_learning( corp_code, data_params) last_money, last_predict, invest_predicts, all_invest_money = \ invest.let_invest_and_all(corp_code, dataX_last, data_params, params_all) rmse_val = np.mean([rmse_val, rmse_val_all]) train_cnt = train_cnt + train_cnt_all print(no, corp_code, corp_name, rmse_val, last_money, all_invest_money, train_cnt) return [ no, corp_code, corp_name, rmse_val, last_money, all_invest_money, train_cnt ]
def train_n_invest_twins(self, corp_code, corp_name, no) -> list: """겨별 세션과 통합세션에서 예측한 값의 평균을 예측값으로 한다.""" stocks = Stocks(self.params) trains_data = TrainsData(self.params) learning = Learning(self.params) params_all = GlobalParams('ALL_CORPS') learning_all = Learning(params_all) invest = MockInvestment(self.params) stock_data = stocks.get_stock_data(corp_code) data_params, scaler_close, dataX_last = trains_data.get_train_test( stock_data) tp = learning.learn(corp_code, corp_name, data_params) tp_all = learning_all.learn(corp_code, corp_name, data_params) ip = invest.invest_n_all(corp_code, dataX_last, data_params, params_all, scaler_close) tp.test_rmse = np.mean([tp.test_rmse, tp_all.test_rmse]) tp.train_count = tp.train_count + tp_all.train_count return self.get_train_invest_result(no, corp_code, corp_name, stock_data, tp, ip)
def save(self) -> None: saver = tf.train.Saver() learning = Learning(self._params) learning.save_learning_image(self.sess, saver, self._corp_code, True, self._name)
def train_n_invests_for_all(self, corps): """입력한 회사들에 대해서 학습시키고 모의투자를 실행한다.""" stocks = Stocks(self.params) trains_data = TrainsData(self.params) learning = Learning(self.params) invest = MockInvestment(self.params) list = [] for index, corp_data in corps.iterrows(): corp_code = corp_data['종목코드'] corp_name = corp_data['회사명'] try: stock_data = stocks.get_stock_data(corp_code) data_params, scaler_close, dataX_last = trains_data.get_train_test( stock_data) except Exception: exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_traceback, file=sys.stdout) continue list.append({ 'corp_code': corp_code, 'corp_name': corp_name, 'scaler_close': scaler_close, 'dataX_last': dataX_last, 'data_params': data_params, 'stock_data': stock_data }) all_data_params = self.gather_data_params(list) tp = learning.learn("ALL_CORPS", "ALL_CORPS", all_data_params) invest_daily_data = [] comp_rmses = [] index = 1 for item in list: corp_code = item['corp_code'] corp_name = item['corp_data'] dataX_last = item['dataX_last'] data_params = item['data_params'] scaler_close = item['scaler_close'] stock_data = item['stock_data'] ip = invest.invest(corp_code, corp_name, tp) visualizer = LearningVisualizer(self.params) visualizer.draw_predictions(corp_name, scaler_close, data_params, tp.test_predict, ip.predict_list) result, invest_daily = self.get_train_invest_result( index, corp_code, corp_name, tp.test_rmse, ip.last_money, ip.index_money, tp.train_count, stock_data, data_params, scaler_close, ip.last_predict) if invest_daily != None: invest_daily_data.append(invest_daily) comp_rmses.append(result) index += 1 df_comp_rmses = pd.DataFrame(comp_rmses, columns=self.result_columns) DataUtils.save_csv(df_comp_rmses, self.get_result_file_path()) if len(invest_daily_data) > 1: try: visualizer = InvestVisualizer(self.params) return visualizer.draw_invest_daily(invest_daily_data, corps) except Exception: exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_traceback, file=sys.stdout)