class Test_Environment(unittest.TestCase): def setUp(self): self.env = Environment() #def tearDown(self): #self.widget.dispose() def test_create_environment(self): self.assertNotEqual(self.env, None) def test_get_data(self): with open('config.json') as f: config = json.load(f) start_date = config['session']['start_date'] end_date = config['session']['end_date'] codes_num = config['session']['codes'] market = config['session']['market_types'] features = config['session']['features'] train_start_date, train_end_date, test_start_date, test_end_date, codes = self.env.get_repo( start_date, end_date, codes_num, market) window_length = 10 self.env.get_data(train_start_date, train_end_date, features, window_length, market, codes) self.assertTrue( len(self.env.states) > 0 ) # states has shape (1,6,10,2) 1,codes_num+1,window_length, features self.assertTrue(len(self.env.price_history) > 0) #price_history has shape (6,1) codes_num + 1 ; #First element in price_history is always 1, means cash #print (self.env.states[0].shape) #print (self.env.price_history[0].shape) #print (self.env.price_history[0]) def test_get_repo(self): with open('config.json') as f: config = json.load(f) start_date = config['session']['start_date'] end_date = config['session']['end_date'] codes_num = config['session']['codes'] market = config['session']['market_types'] self.train_start_date, self.train_end_date, test_start_date, test_end_date, self.codes = self.env.get_repo( start_date, end_date, codes_num, market) self.assertTrue(len(self.env.data) > 0) self.assertTrue(len(self.env.date_set) > 0) # step requires get_data to have been called first to fill the environment. def test_step(self): self.test_get_data() self.env.reset() noise_flag = False info = self.env.step(None, None, noise_flag) # dict_keys(['reward', 'continue', 'next state', 'weight vector', 'price', 'risk']) #print (info.keys()) #print (info['reward']) # Reward is an integer #print (info['continue']) # continue is True/False #print (info['next state'].shape) # Shape for next state is (1,6,10,2) #print (info['weight vector'].shape) # Shape for weight vector is (1,6) #print (info['risk']) #Risk is an integer #print (info['price'].shape) #Shape for price is 6,1) self.assertEqual(len(info.keys(), 6))
def experiment_cost(): parser = build_parser() args = vars(parser.parse_args()) with open('config.json') as f: config = json.load(f) with open("result/PG/" + str(args['num']) + '/config.json', 'r') as f: dict_data = json.load(f) codes, start_date, end_date, features, agent_config, market, predictor, framework, window_length, noise_flag, record_flag, plot_flag, reload_flag, trainable, method = parse_config( config, args) env = Environment() test_start_date, test_end_date, codes = datetime.datetime.strptime( dict_data['test_start_date'], '%Y-%m-%d'), datetime.datetime.strptime(dict_data['test_end_date'], '%Y-%m-%d'), dict_data['codes'] env.get_data(test_start_date, test_end_date, features, window_length, market, codes) costs = [0, 0.001, 0.005, 0.01, 0.02] agent = PG( len(codes) + 1, int(window_length), len(features), '-'.join(agent_config), 'True', 'False', 'True', args['num']) wealths_result = [] rs_result = [] for i in range(len(costs)): stocktrader = StockTrader() env.cost = costs[i] info = env.step(None, None, 'False') r, contin, s, w1, p, risk = parse_info(info) contin = 1 wealth = 10000 wealths = [wealth] rs = [1] while contin: w2 = agent.predict(s, w1) env_info = env.step(w1, w2, 'False') r, contin, s_next, w1, p, risk = parse_info(env_info) wealth = wealth * math.exp(r) rs.append(math.exp(r) - 1) wealths.append(wealth) s = s_next stocktrader.update_summary(0, r, 0, 0, w2, p) print('finish one agent') wealths_result.append(wealths) rs_result.append(rs) print('agent', ' ', 'cumulative return', ' ', 'average daily return', ' ', 'sharpe ratio', ' ', 'maximum drawback') plt.figure(figsize=(8, 6), dpi=100) for i in range(len(costs)): plt.plot(wealths_result[i], label=costs[i]) cumr = float((wealths_result[i][-1] - 10000) / 10000 * 100) mrr = float(np.mean(rs_result[i]) * 100) sharpe = float( np.mean(rs_result[i]) / np.std(rs_result[i]) * np.sqrt(252)) maxdrawdown = float( max(1 - min(wealths_result[i]) / np.maximum.accumulate(wealths_result[i]))) print(costs[i], ' ', round(cumr, 3), '%', ' ', round(mrr, 3), '%', ' ', round(sharpe, 3), ' ', round(maxdrawdown, 3), '%') plt.legend() plt.xlabel('time') plt.ylabel('wealth') plt.savefig(PATH_prefix + 'backtest_differntcost.png') plt.show()