def session(config,mode): if mode == 'train': files = glob.glob('saved_network/PG/*') for f in files: os.remove(f) from data.environment import Environment 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,mode) env = Environment(start_date, end_date, codes, features, int(window_length),market,mode) global M M=len(codes)+1 if framework == 'DDPG': print("*-----------------Loading DDPG Agent---------------------*") from agents.ddpg import DDPG agent = DDPG(predictor, len(codes) + 1, int(window_length), len(features), '-'.join(agent_config), reload_flag,trainable) elif framework == 'PPO': print("*-----------------Loading PPO Agent---------------------*") from agents.ppo import PPO agent = PPO(predictor, len(codes) + 1, int(window_length), len(features), '-'.join(agent_config), reload_flag,trainable) elif framework == 'PG': print("*-----------------Loading PG Agent---------------------*") from agents.pg import PG agent = PG(len(codes) + 1, int(window_length), len(features), '-'.join(agent_config), reload_flag,trainable) stocktrader=StockTrader() if mode=='train': print("Training with {:d}".format(epochs)) for epoch in range(epochs): print("Now we are at epoch", epoch) traversal(stocktrader,agent,env,epoch,noise_flag,framework,method,trainable) if record_flag=='True': stocktrader.write(epoch) if plot_flag=='True': stocktrader.plot_result() agent.reset_buffer() stocktrader.print_result(epoch,agent) stocktrader.reset() elif mode=='test': backtest(agent, env)
def session(config, args): global PATH_prefix 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() global M M = codes + 1 stocktrader = StockTrader() PATH_prefix = "result/PPO/" + str(args['num']) + '/' if args['mode'] == 'train': if not os.path.exists(PATH_prefix): os.makedirs(PATH_prefix) train_start_date, train_end_date, test_start_date, test_end_date, codes = env.get_repo( start_date, end_date, codes, market) env.get_data(train_start_date, train_end_date, features, window_length, market, codes) print("Codes:", codes) print('Training Time Period:', train_start_date, ' ', train_end_date) print('Testing Time Period:', test_start_date, ' ', test_end_date) with open(PATH_prefix + 'config.json', 'w') as f: json.dump( { "train_start_date": train_start_date.strftime('%Y-%m-%d'), "train_end_date": train_end_date.strftime('%Y-%m-%d'), "test_start_date": test_start_date.strftime('%Y-%m-%d'), "test_end_date": test_end_date.strftime('%Y-%m-%d'), "codes": codes }, f) print("finish writing config") else: with open("result/PPO/" + str(args['num']) + '/config.json', 'r') as f: dict_data = json.load(f) print("successfully load config") train_start_date, train_end_date, codes = datetime.datetime.strptime( dict_data['train_start_date'], '%Y-%m-%d'), datetime.datetime.strptime( dict_data['train_end_date'], '%Y-%m-%d'), dict_data['codes'] env.get_data(train_start_date, train_end_date, features, window_length, market, codes) for noise_flag in [ 'True' ]: #['False','True'] to train agents with noise and without noise in assets prices print("*-----------------Loading PPO Agent---------------------*") agent = PPO(predictor, len(codes) + 1, int(window_length), len(features), '-'.join(agent_config), reload_flag, trainable) print("Training with {:d}".format(epochs)) for epoch in range(epochs): print("Now we are at epoch", epoch) traversal(stocktrader, agent, env, epoch, noise_flag, framework, method, trainable) if record_flag == 'True': stocktrader.write(epoch, framework) if plot_flag == 'True': stocktrader.plot_result() agent.reset_buffer() stocktrader.print_result(epoch, agent, noise_flag) stocktrader.reset() agent.close() del agent elif args['mode'] == 'test': with open("result/PPO/" + str(args['num']) + '/config.json', 'r') as f: dict_data = json.load(f) 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) backtest([ PPO(predictor, len(codes) + 1, int(window_length), len(features), '-'.join(agent_config), reload_flag, trainable) ], env)
from data.environment import Environment from keras.models import load_model import os # os.environ['CUDA_VISIBLE_DEVICES'] = '-1' # turn off gpu training """ Script controlling the evaluation of softmax model ensure the label_scheme, data_dir, and model_path are set correctly below """ label_scheme = 1 data_dir = 'C:/out2' model_path = 'C:\models\softmax-d2-l1-RMSProp-07-256-0.4499-9997.60.l.h5' env = Environment() train_list, dev_list, test_list = env.generate_train_dev_test_lists( data_dir, .95, .025, .025, label_scheme=label_scheme) model = load_model(model_path) batch_size = 256 train_steps = 630 val_steps = 58 test_steps = 58 nb_epoch = 10 score = model.evaluate_generator( env.single_distortion_data_generator(test_list, data_dir, batch_size=batch_size, flatten=True, batch_name="test", steps=test_steps, label_scheme=label_scheme),
def session(config): global PATH_prefix from data.environment import Environment num_assets, start_date, end_date, features, agent_config, market, predictor, framework, window_length, hidden_size, epochs = parse_config( config) env = Environment() PATH_prefix = "result/PG/" if not os.path.exists(PATH_prefix): os.makedirs(PATH_prefix) train_start_date, train_end_date, test_start_date, test_end_date, assets_tickers = env.preprocess_data( start_date, end_date, num_assets, market, window_length, features) print("Assets_tickers:", assets_tickers) print('Training Time Period:', train_start_date, ' ', train_end_date) print('Testing Time Period:', test_start_date, ' ', test_end_date) with open(PATH_prefix + 'config.json', 'w') as f: json.dump( { "train_start_date": train_start_date.strftime('%Y-%m-%d'), "train_end_date": train_end_date.strftime('%Y-%m-%d'), "test_start_date": test_start_date.strftime('%Y-%m-%d'), "test_end_date": test_end_date.strftime('%Y-%m-%d'), "assets_tickers": assets_tickers }, f) print("finish writing config") with open("result/PG/config.json", 'r') as f: dict_data = json.load(f) print("Config loaded successfully!") train_start_date, train_end_date, assets_tickers = datetime.datetime.strptime( dict_data['train_start_date'], '%Y-%m-%d'), datetime.datetime.strptime( dict_data['train_end_date'], '%Y-%m-%d'), dict_data['assets_tickers'] env.get_data(train_start_date, train_end_date, features, window_length, market, assets_tickers, 'Train') print("*-----------------Loading PG Agent---------------------*") if predictor == 'MLP': agent = DPG_MLP( len(assets_tickers) + 1, int(window_length), len(features), hidden_size, predictor) print('Actor: ' + predictor + 'network') print(agent.eval()) elif predictor == 'LSTM': agent = DPG_LSTM( len(assets_tickers) + 1, int(window_length), len(features), hidden_size, predictor) print('Actor: ' + predictor + 'network') print(agent.eval()) print("Training with {:d}".format(epochs)) growth_all_epoch = [] for epoch in range(epochs): print("Now we are at epoch", epoch) total_train_days, growth = train_agent(agent, env, epoch, assets_tickers) growth_all_epoch.append(growth) plt.plot(growth_all_epoch) plt.savefig(r'' + PATH_prefix + 'Training_growth_rc_' + str(rc_factor) + '_' + predictor + '_' + market + '_hs_' + str(hidden_size) + '.png') plt.show() # Back Test print("Running back test now.....") test_start_date, test_end_date, assets_tickers = datetime.datetime.strptime( dict_data['test_start_date'], '%Y-%m-%d'), datetime.datetime.strptime( dict_data['test_end_date'], '%Y-%m-%d'), dict_data['assets_tickers'] env.get_data(train_start_date, train_end_date, features, window_length, market, assets_tickers, 'Test') backtest(agent, env, assets_tickers, 'Train_Set', market, predictor) env.get_data(test_start_date, test_end_date, features, window_length, market, assets_tickers, 'Test') backtest(agent, env, assets_tickers, 'Test_Set', market, predictor) del agent
def setUp(self): self.env = Environment()
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 session(config, args): global PATH_prefix from data.environment import Environment 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() global M if market == 'China': M = codes + 1 else: M = len(codes) + 1 # print("len codes",len(codes)) # M=codes+1 # M = số lượng stock -> ảnh huong đến noise - chi tiết from agents.ornstein_uhlenbeck import OrnsteinUhlenbeckActionNoise # if framework == 'DDPG': # print("*-----------------Loading DDPG Agent---------------------*") # from agents.ddpg import DDPG # agent = DDPG(predictor, len(codes) + 1, int(window_length), len(features), '-'.join(agent_config), reload_flag,trainable) # # elif framework == 'PPO': # print("*-----------------Loading PPO Agent---------------------*") # from agents.ppo import PPO # agent = PPO(predictor, len(codes) + 1, int(window_length), len(features), '-'.join(agent_config), reload_flag,trainable) stocktrader = StockTrader() PATH_prefix = "./result_new/PG/" + str(args['num']) + '/' #<- if args['mode'] == 'train': if not os.path.exists(PATH_prefix): print('Create new path at', PATH_prefix) os.makedirs(PATH_prefix) if market == "China": train_start_date, train_end_date, test_start_date, test_end_date, codes = env.get_repo( start_date, end_date, codes, market) else: train_start_date, train_end_date, test_start_date, test_end_date, codes = env.get_repo( start_date, end_date, len(codes), market) env.get_data(train_start_date, train_end_date, features, window_length, market, codes) print("Codes:", codes) print('Training Time Period:', train_start_date, ' ', train_end_date) print('Testing Time Period:', test_start_date, ' ', test_end_date) with open(PATH_prefix + 'config.json', 'w') as f: json.dump( { "train_start_date": train_start_date.strftime('%Y-%m-%d'), "train_end_date": train_end_date.strftime('%Y-%m-%d'), "test_start_date": test_start_date.strftime('%Y-%m-%d'), "test_end_date": test_end_date.strftime('%Y-%m-%d'), "codes": codes }, f) print("finish writing config") else: with open("./result_new/PG/" + str(args['num']) + '/config.json', 'r') as f: dict_data = json.load(f) print("successfully load config") if market == "China": train_start_date, train_end_date, test_start_date, test_end_date, codes = env.get_repo( start_date, end_date, codes, market) else: train_start_date, train_end_date, test_start_date, test_end_date, codes = env.get_repo( start_date, end_date, len(codes), market) env.get_data(train_start_date, train_end_date, features, window_length, market, codes) # train_start_date, train_end_date, codes = datetime.datetime.strptime(dict_data['train_start_date'], '%Y-%m-%d'), datetime.datetime.strptime(dict_data['train_end_date'], '%Y-%m-%d'), dict_data['codes'] # env.get_data(train_start_date, train_end_date, features, window_length, market, codes) for noise_flag in [ 'True' ]: #['False','True'] to train agents with noise and without noise in assets prices if framework == 'PG': print( "*-----------------Loading PG Agent---------------------*") agent = PG( len(codes) + 1, int(window_length), len(features), '-'.join(agent_config), reload_flag, trainable, noise_flag, args['num']) print("Finish import {}".format(agent.name)) elif framework == 'DDPG': print( "*-----------------Loading DDPG Agent---------------------*" ) from agents.ddpg import DDPG agent = DDPG(predictor, len(codes) + 1, int(window_length), len(features), '-'.join(agent_config), reload_flag, trainable) print("Finish import {}".format(agent.name)) print("Training with {:d}".format(epochs)) for epoch in range(epochs): print("Now we are at epoch", epoch) traversal(stocktrader, agent, env, epoch, noise_flag, framework, method, trainable) if record_flag == 'True': stocktrader.write(epoch, framework) if plot_flag == 'True': stocktrader.plot_result() #print(agent) agent.reset_buffer() stocktrader.print_result(epoch, agent, noise_flag) stocktrader.reset() agent.close() del agent ####### # TESTING elif args['mode'] == 'test': with open("./result_new/PG/" + str(args['num']) + '/config.json', 'r') as f: dict_data = json.load(f) 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) backtest([ PG( len(codes) + 1, int(window_length), len(features), '-'.join(agent_config), 'True', 'False', 'True', args['num']) ], env, market)
def session(config, args): codes, start_date, end_date, features, agent_config, \ market, predictor, framework, window_length, noise_flag, record_flag, \ plot_flag, reload_flag, trainable, method, epochs = parse_config(config, args) env = Environment(args.seed) stocktrader = StockTrader() path = "result/{}/{}/".format(framework, args.num) logger.info('Mode: {}'.format(args.mode)) if args.mode == 'train': if not os.path.exists(path): os.makedirs(path) train_start_date, train_end_date, test_start_date, test_end_date, codes = env.get_repo( start_date, end_date, codes, market) logger.debug("Training with codes: {}".format(codes)) env.get_data(train_start_date, train_end_date, features, window_length, market, codes) with open(path + 'config.json', 'w') as f: print(train_start_date) print(train_end_date) print(test_start_date) print(test_end_date) json.dump( { "train_start_date": train_start_date.strftime('%Y-%m-%d'), "train_end_date": train_end_date.strftime('%Y-%m-%d'), "test_start_date": test_start_date.strftime('%Y-%m-%d'), "test_end_date": test_end_date.strftime('%Y-%m-%d'), "codes": codes }, f) else: with open('result/{}/{}/config.json'.format(framework, args.num), 'r') as f: dict_data = json.load(f) train_start_date, train_end_date, codes = datetime.strptime( dict_data['train_start_date'], '%Y-%m-%d'), datetime.strptime(dict_data['train_end_date'], '%Y-%m-%d'), dict_data['codes'] env.get_data(train_start_date, train_end_date, features, window_length, market, codes) if framework == 'PG': logger.debug("Loading PG Agent") agent = PG( len(codes) + 1, int(window_length), len(features), '-'.join(agent_config), reload_flag, trainable, args.num) elif framework == 'DDPG': logger.debug("Loading DDPG Agent") agent = DDPG( len(codes) + 1, int(window_length), len(features), '-'.join(agent_config), reload_flag, trainable, args.num) logger.info("Training: %d epochs", epochs) for epoch in range(epochs): traversal(stocktrader, agent, env, epoch, True, framework, method, trainable) if record_flag: stocktrader.write(epoch, framework) if plot_flag: stocktrader.plot_result() agent.reset_buffer() stocktrader.print_result(epoch, agent, True) stocktrader.reset() agent.close() elif args.mode == 'test': with open("result/{}/{}/config.json".format(framework, args.num), 'r') as f: dict_data = json.load(f) test_start_date, test_end_date, codes = datetime.strptime( dict_data['test_start_date'], '%Y-%m-%d'), 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) if framework == 'PG': logger.info("Loading PG Agent") agent = PG( len(codes) + 1, int(window_length), len(features), '-'.join(agent_config), True, False, args.num) elif framework == 'DDPG': logger.info("Loading DDPG Agent") agent = DDPG( len(codes) + 1, int(window_length), len(features), '-'.join(agent_config), True, False, args.num) backtest([agent], env, "result/{}/{}/".format(framework, args.num), framework)
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()