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)
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)
示例#3
0
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))
示例#4
0
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)