def __log_result_csv(self, index, time): from pgportfolio.trade import backtest dataframe = None csv_dir = './train_package/train_summary.csv' json_dir = "./train_package/" + str( int(index)) + "/weight_history" + str(int(index)) + ".json" tflearn.is_training(False, self._agent.session) #evaluate on the test set v_pv, v_log_mean, benefit_array, v_log_mean_free =\ self._evaluate("test", self._agent.portfolio_value, self._agent.log_mean, self._agent.pv_vector, self._agent.log_mean_free) #initialize backtest backtest = backtest.BackTest(self.config.copy(), net_dir=None, agent=self._agent) #method of trader, initialized by BackTest backtest.start_trading() #collects the result in a dic result = Result( test_pv=[v_pv], test_log_mean=[v_log_mean], test_log_mean_free=[v_log_mean_free], test_history=[''.join(str(e) + ', ' for e in benefit_array)], config=[json.dumps(self.config)], net_dir=[index], backtest_test_pv=[backtest.test_pv], backtest_test_history=[ ''.join(str(e) + ', ' for e in backtest.test_pc_vector) ], backtest_test_log_mean=[np.mean(np.log(backtest.test_pc_vector))], training_time=int(time)) new_data_frame = pd.DataFrame(result._asdict()).set_index("net_dir") historyweights = [] for weight in backtest.test_updated_omega: historyweights.append(weight.tolist()) backtesthistory = {"portfolioweights": historyweights} if os.path.isfile(csv_dir): dataframe = pd.read_csv(csv_dir).set_index("net_dir") dataframe = dataframe.append(new_data_frame) else: dataframe = new_data_frame if int(index) > 0: #writes everything in train_package/train_summary.csv and creates it dataframe.to_csv(csv_dir) #writes everything in train_package/int(index)/weight_history int(index).json with open(json_dir, 'w') as outfile: json.dump(backtesthistory, outfile) return result
def __log_result_csv(self, index, time, path): from pgportfolio.trade import backtest dataframe = None csv_dir = './train_package/train_summary.csv' tflearn.is_training(False, self._agent.session) v_pv, v_log_mean, benefit_array, v_log_mean_free = self._evaluate("test", self._agent.portfolio_value,\ self._agent.log_mean, self._agent.pv_vector,\ self._agent.log_mean_free) backtest = backtest.BackTest(self.config.copy(), self.stockList, self.featureList, self.start_date, self.end_date, self.fake_data, net_dir=None, result_path=path, agent=self._agent) # 回测 backtest.start_trading() result = Result( test_pv=[v_pv], test_log_mean=[v_log_mean], test_log_mean_free=[v_log_mean_free], test_history=[''.join(str(e) + ', ' for e in benefit_array)], config=[json.dumps(self.config)], net_dir=[index], backtest_test_pv=[backtest.test_pv], backtest_test_history=[ ''.join(str(e) + ', ' for e in backtest.test_pc_vector) ], backtest_test_log_mean=[np.mean(np.log(backtest.test_pc_vector))], training_time=int(time)) new_data_frame = pd.DataFrame(result._asdict()).set_index("net_dir") if os.path.isfile(csv_dir): dataframe = pd.read_csv(csv_dir).set_index("net_dir") dataframe = dataframe.append(new_data_frame) else: dataframe = new_data_frame if int(index) > 0: dataframe.to_csv(csv_dir) return result
def __log_result_csv(self, index, time): from pgportfolio.trade import backtest dataframe = None csv_dir = './train_package/train_summary.csv' tflearn.is_training(False, self._agent.session) v_pv, v_log_mean, benefit_array, v_log_mean_free =\ self._evaluate("test", self._agent.portfolio_value, self._agent.log_mean, self._agent.pv_vector, self._agent.log_mean_free) backtest = backtest.BackTest(self.config.copy(), net_dir=None, agent=self._agent) backtest.start_trading() result = Result(test_pv=[v_pv], test_log_mean=[v_log_mean], test_log_mean_free=[v_log_mean_free], test_history=[''.join(str(e)+', ' for e in benefit_array)], config=[json.dumps(self.config)], net_dir=[index], backtest_test_pv=[backtest.test_pv], backtest_test_history=[''.join(str(e)+', ' for e in backtest.test_pc_vector)], backtest_test_log_mean=[np.mean(np.log(backtest.test_pc_vector))], training_time=int(time)) new_data_frame = pd.DataFrame(result._asdict()).set_index("net_dir") if os.path.isfile(csv_dir): dataframe = pd.read_csv(csv_dir).set_index("net_dir") dataframe = dataframe.append(new_data_frame) else: dataframe = new_data_frame if int(index) > 0: dataframe.to_csv(csv_dir) return result
def __log_result_csv(self, index, time): # here the index is folder name from pgportfolio.trade import backtest dataframe = None csv_dir = './train_package/train_summary.csv' csv_weights = './train_package/%s/portfolio_weights.csv' % index tflearn.is_training( False, self._agent.session ) # specifying layers defined by testing instead of training weights = self._evaluate( "test", self._agent.portfolio_weights) # here (66,339) v_pv, v_log_mean, benefit_array, v_log_mean_free =\ self._evaluate("test", self._agent.portfolio_value, self._agent.log_mean, self._agent.pv_vector, self._agent.log_mean_free) backtest = backtest.BackTest(self.config.copy(), net_dir=None, agent=self._agent) backtest.start_trading() # start showing steps and total assets # here weights is still (66,339) result = Result( test_pv=[v_pv], test_log_mean=[v_log_mean], test_log_mean_free=[v_log_mean_free], test_history=[''.join(str(e) + ', ' for e in benefit_array)], config=[json.dumps(self.config)], net_dir=[index], backtest_test_pv=[backtest.test_pv], backtest_test_history=[ ''.join(str(e) + ', ' for e in backtest.test_pc_vector) ], backtest_test_log_mean=[np.mean(np.log(backtest.test_pc_vector))], training_time=int(time)) new_data_frame = pd.DataFrame(result._asdict()).set_index("net_dir") if os.path.isfile(csv_dir): dataframe = pd.read_csv(csv_dir).set_index("net_dir") dataframe = dataframe.append(new_data_frame) else: dataframe = new_data_frame # portfolio weight: weights is a list with one element of np.array(66,339) in coin data [time, assets] self._asset_list.insert(0, 'RMB') self._time_index_test = self._time_index[-int( len(self._time_index) * self._test_portion - self.__window_size):] weight_df = pd.DataFrame(weights[0], index=self._time_index_test, columns=self._asset_list) if int(index) > 0: dataframe.to_csv(csv_dir) weight_df.to_csv(csv_weights) return result