Example #1
0
def test_predict():
    print("STARTING PREDICT")
    start_date = date(2017, 10, 1)
    end_date = date(2018, 10, 1)

    # setup environment
    num_training_time = 200
    env = PortfolioEnv(
        start_date,
        end_date,
        window_length,
        tickers_list,
        features_list,
        trading_cost=0.0025,
        # trading_cost=0.0025,
        batch_size=num_training_time,
        buffer_bias_ratio=0.0)

    #variable_scope ="ddpg"
    action_dim = env.action_space.shape[0]
    actor_noise = OrnsteinUhlenbeckActionNoise(mu=np.zeros(action_dim))

    #with tf.variable_scope(variable_scope):
    tf.reset_default_graph()
    sess = tf.Session()

    with sess.as_default():
        # Missing this was the source of one of the most challenging an insidious bugs that I've ever encountered.
        # it was impossible to save the model.
        K.set_session(sess)

        ddpg_model = DDPG(env,
                          sess,
                          actor_noise,
                          obs_normalizer='history',
                          predictor_type=predictor_type,
                          use_batch_norm=use_batch_norm,
                          load_root_model=True,
                          config=CONFIG)
        ddpg_model.initialize(load_weights=True)

        observation = env.reset()
        done = False
        print(
            'date, portfolio_change/y_return, portfolio_value, market_value, weight_cash, weight_AAPL'
        )
        while not done:
            action = ddpg_model.predict_single(observation)
            observation, reward, done, infos = env.step(action)
            print(
                datetime.fromtimestamp(infos['date']).strftime('%Y-%m-%d'),
                infos['portfolio_change'] / infos['y_return'],
                infos['portfolio_value'], infos['market_value'],
                infos['weight_cash'], infos['weight_AAPL'])
        df = env.df_info()
        print(df.iloc[-1, :])
        print(df.describe())
        env.render(mode='notebook')
    sess.close()
Example #2
0
def main2():
    print("STRATING")
    start_date = date(2008, 1, 1)
    end_date = date(2017, 1, 1)
    features_list = ['open', 'high', 'low', 'close']
    tickers_list = [
        'AAPL', 'ATVI', 'CMCSA', 'COST', 'CSX', 'DISH', 'EA', 'EBAY', 'FB',
        'GOOGL', 'HAS', 'ILMN', 'INTC', 'MAR', 'REGN', 'SBUX'
    ]

    predictor_list = ['lstm', 'cnn', 'dense']
    batch_norm_list = [False, True]
    window_length_list = [5, 10, 15, 20]

    for predictor_type in predictor_list:
        for use_batch_norm in batch_norm_list:
            for window_length in window_length_list:

                # setup environment
                num_training_time = 2000
                env = PortfolioEnv(start_date,
                                   end_date,
                                   window_length,
                                   tickers_list,
                                   features_list,
                                   batch_size=num_training_time)

                # variable_scope ="ddpg"
                action_dim = env.action_space.shape[0]
                actor_noise = OrnsteinUhlenbeckActionNoise(
                    mu=np.zeros(action_dim))

                model_save_path = get_model_path(window_length, predictor_type,
                                                 use_batch_norm)
                summary_path = get_result_path(window_length, predictor_type,
                                               use_batch_norm)

                tf.reset_default_graph()
                sess = tf.Session()

                with sess.as_default():
                    # Missing this was the source of one of the most challenging an insidious bugs that I've ever encountered.
                    # it was impossible to save the model.
                    K.set_session(sess)

                    ddpg_model = DDPG2(env,
                                       sess,
                                       actor_noise,
                                       action_dim=action_dim,
                                       obs_normalizer="history",
                                       predictor_type=predictor_type,
                                       use_batch_norm=use_batch_norm,
                                       model_save_path=model_save_path,
                                       summary_path=summary_path)
                    ddpg_model.initialize(load_weights=False)
                    ddpg_model.train()
def test_main():
    print("STARTING MAIN")
    start_date = date(2008, 1, 1)
    end_date = date(2017, 1, 1)

    # setup environment
    num_training_time = 2000
    env = PortfolioEnv(start_date,
                       end_date,
                       window_length,
                       tickers_list,
                       features_list,
                       batch_size=num_training_time)

    nb_assets = len(tickers_list) + 1
    nb_features = len(features_list)
    action_dim = env.action_space.shape[0]

    root_model_dir_path = get_root_model_dir_path(window_length,
                                                  predictor_type,
                                                  use_batch_norm)
    root_model_path = get_root_model_path(window_length, predictor_type,
                                          use_batch_norm)

    # create the network
    state_input = Input(shape=(nb_assets, window_length, nb_features),
                        name="state_input")
    root_model, imitation_model = create_network_given_future(
        state_input,
        predictor_type="cnn",
        use_batch_norm=False,
        weight_path=root_model_path,
        load_weights=True)

    history = env.src.full_history()
    # create the dataset
    (X_train, y_train), (X_validation,
                         y_validation) = create_optimal_imitation_dataset(
                             history, training_data_ratio=0.8)

    # train
    history = imitation_model.fit(X_train,
                                  y_train,
                                  batch_size=128,
                                  epochs=100,
                                  validation_data=(X_validation, y_validation),
                                  shuffle=True)
    os.makedirs(root_model_dir_path, exist_ok=True)
    # remove last layer before saving
    root_model.save_weights(root_model_path)
Example #4
0
def test_main():
    print("STARTING MAIN")
    start_date = date(2008, 1, 1)
    end_date = date(2017, 1, 1)

    # setup environment
    num_training_time = 2000
    env = PortfolioEnv(start_date,
                       end_date,
                       window_length,
                       tickers_list,
                       features_list,
                       trading_cost=0.01,
                       batch_size=num_training_time)

    #variable_scope ="ddpg"
    nb_assets = len(tickers_list) + 1
    action_dim = env.action_space.shape[0]
    actor_noise = OrnsteinUhlenbeckActionNoise(mu=np.ones(action_dim) /
                                               nb_assets)
    actor_noise = OrnsteinUhlenbeckActionNoise(mu=np.zeros(action_dim))

    #actor_noise = OrnsteinUhlenbeckActionNoise(mu=np.zeros(action_dim)/nb_assets, sigma=0.1)

    #with tf.variable_scope(variable_scope):
    tf.reset_default_graph()
    sess = tf.Session()

    with sess.as_default():

        # Missing this was the source of one of the most challenging an insidious bugs that I've ever encountered.
        # it was impossible to save the model.
        K.set_session(sess)

        ddpg_model = DDPG(env,
                          sess,
                          actor_noise,
                          obs_normalizer="history",
                          predictor_type=predictor_type,
                          use_batch_norm=use_batch_norm,
                          load_root_model=True,
                          config=CONFIG)
        ddpg_model.initialize(load_weights=True)
        ddpg_model.train()
    sess.close()
Example #5
0
    window_length = int(args['window_length'])
    nb_classes = len(target_stocks) + 1
    nb_features = len(features_list)

    # get target history
    target_history = np.empty(shape=(len(target_stocks), num_training_time,
                                     history.shape[2]))
    for i, stock in enumerate(target_stocks):
        target_history[i] = history[
            abbreviation.index(stock), :num_training_time, :]

    # setup environment

    env = PortfolioEnv(start_date,
                       end_date,
                       window_length,
                       tickers_list,
                       features_list,
                       batch_size=num_training_time)

    action_dim = [nb_classes]
    state_dim = [nb_classes, window_length]
    batch_size = 64
    action_bound = 1.
    tau = 1e-3
    assert args['predictor_type'] in ['cnn', 'lstm'
                                      ], 'Predictor must be either cnn or lstm'
    predictor_type = args['predictor_type']
    if args['batch_norm'] == 'True':
        use_batch_norm = True
    elif args['batch_norm'] == 'False':
        use_batch_norm = False
Example #6
0
def main():
    start_date = date(2008, 1, 1)
    end_date = date(2018, 1, 1)
    features_list = ['open', 'high', 'low', 'close']
    tickers_list = [
        'AAPL', 'ATVI', 'CMCSA', 'COST', 'CSX', 'DISH', 'EA', 'EBAY', 'FB',
        'GOOGL', 'HAS', 'ILMN', 'INTC', 'MAR', 'REGN', 'SBUX'
    ]
    tickers_list = [
        'AAPL', 'ATVI', 'CMCSA', 'FB', 'HAS', 'ILMN', 'INTC', 'MAR', 'REGN',
        'SBUX'
    ]

    historyManager = gdm.HistoryManager(tickers=tickers_list, online=True)
    df = historyManager.historical_data(start=start_date,
                                        end=end_date,
                                        tickers=tickers_list,
                                        features=features_list,
                                        adjusted=True)

    history = prepare_dataframe(df)
    history = history[:, :, :4]

    num_training_time = 1095

    # get target history
    target_history = np.empty(shape=(len(tickers_list), num_training_time,
                                     history.shape[2]))
    for i, stock in enumerate(tickers_list):
        target_history[i] = history[
            tickers_list.index(stock), :num_training_time, :]

    predictor_list = ['cnn', 'lstm']
    batch_norm_list = [False, True]
    window_length_list = [5, 7, 10, 15, 20]

    for predictor_type in predictor_list:
        for use_batch_norm in batch_norm_list:
            for window_length in window_length_list:

                # setup environment
                env = PortfolioEnv(start_date,
                                   end_date,
                                   window_length,
                                   tickers_list,
                                   features_list,
                                   batch_size=num_training_time)

                nb_classes = len(tickers_list) + 1
                nb_features = len(features_list)
                action_dim = [nb_classes]
                state_dim = [nb_classes, window_length]
                action_bound = 1.
                tau = 1e-3

                actor_noise = OrnsteinUhlenbeckActionNoise(
                    mu=np.zeros(action_dim))
                model_save_path = get_model_path(window_length, predictor_type,
                                                 use_batch_norm)
                summary_path = get_result_path(window_length, predictor_type,
                                               use_batch_norm)

                variable_scope = get_variable_scope(window_length,
                                                    predictor_type,
                                                    use_batch_norm)

                tf.reset_default_graph()
                sess = tf.Session()

                with sess.as_default():

                    with tf.variable_scope(variable_scope):
                        root_net = None
                        state_inputs = Input(
                            shape=([nb_classes, window_length, nb_features]))
                        root_net = model_predictor(state_inputs,
                                                   predictor_type,
                                                   use_batch_norm)

                        actor = StockActor(sess=sess,
                                           root_net=root_net,
                                           inputs=state_inputs,
                                           state_dim=state_dim,
                                           action_dim=action_dim,
                                           action_bound=action_bound,
                                           learning_rate=1e-4,
                                           tau=tau,
                                           batch_size=window_length,
                                           predictor_type=predictor_type,
                                           use_batch_norm=use_batch_norm)
                        critic = StockCritic(sess=sess,
                                             root_net=root_net,
                                             inputs=state_inputs,
                                             state_dim=state_dim,
                                             action_dim=action_dim,
                                             tau=1e-3,
                                             learning_rate=1e-3,
                                             predictor_type=predictor_type,
                                             use_batch_norm=use_batch_norm)
                        ddpg_model = DDPG(env=env,
                                          sess=sess,
                                          actor=actor,
                                          critic=critic,
                                          actor_noise=actor_noise,
                                          obs_normalizer="history",
                                          model_save_path=model_save_path,
                                          summary_path=summary_path)
                        ddpg_model.initialize(load_weights=True)
                        ddpg_model.train(debug=True)
                sess.close()
Example #7
0
def predict():
    print("STRATING PreDICT")
    start_date = date(2017, 1, 1)
    end_date = date(2018, 1, 1)
    features_list = ['open', 'high', 'low', 'close']
    tickers_list = [
        'AAPL', 'ATVI', 'CMCSA', 'COST', 'CSX', 'DISH', 'EA', 'EBAY', 'FB',
        'GOOGL', 'HAS', 'ILMN', 'INTC', 'MAR', 'REGN', 'SBUX'
    ]

    predictor_list = ['lstm', 'cnn', 'dense']
    batch_norm_list = [False, True]
    window_length_list = [5, 10, 15, 20]

    for predictor_type in predictor_list:
        for use_batch_norm in batch_norm_list:
            for window_length in window_length_list:
                # setup environment
                num_training_time = 200
                env = PortfolioEnv(start_date,
                                   end_date,
                                   window_length,
                                   tickers_list,
                                   features_list,
                                   trading_cost=0.00,
                                   batch_size=num_training_time)

                # variable_scope ="ddpg"
                action_dim = env.action_space.shape[0]
                actor_noise = OrnsteinUhlenbeckActionNoise(
                    mu=np.zeros(action_dim))

                model_save_path = get_model_path(window_length, predictor_type,
                                                 use_batch_norm)
                summary_path = get_result_path(window_length, predictor_type,
                                               use_batch_norm)

                print("MODEL   :", model_save_path)
                tf.reset_default_graph()
                sess = tf.Session()

                with sess.as_default():
                    # Missing this was the source of one of the most challenging an insidious bugs that I've ever encountered.
                    # it was impossible to save the model.
                    K.set_session(sess)

                    ddpg_model = DDPG2(env,
                                       sess,
                                       actor_noise,
                                       action_dim=action_dim,
                                       obs_normalizer="history",
                                       predictor_type=predictor_type,
                                       use_batch_norm=use_batch_norm,
                                       model_save_path=model_save_path,
                                       summary_path=summary_path)
                    ddpg_model.initialize(load_weights=True)
                    observation = env.reset()
                    done = False
                    while not done:
                        action = ddpg_model.predict_single(observation)
                        observation, reward, done, infos = env.step(action)
                        print(
                            datetime.fromtimestamp(
                                infos['date']).strftime('%Y-%m-%d'),
                            infos['portfolio_change'] / infos['y_return'],
                            infos['portfolio_value'], infos['market_value'],
                            infos['weight_cash'], infos['weight_AAPL'])
                    df = env.df_info()
                    print(df.iloc[-1, :])
                    print(df.describe())
                sess.close()
Example #8
0
        """
        :param config: config dictionary
        :param fake_data: if True will use data generated randomly
        :param restore_dir: path to the model trained before
        :param save_path: path to save the model
        :param device: the device used to train the network
        :param agent: the nnagent object. If this is provides, the trainer will not
        create a new agent by itself. Therefore the restore_dir will not affect anything.
        """


env = PortfolioEnv(
    df=df_train,
    steps=40,
    scale=True,
    trading_cost=0.0025,
    window_length=window_length,
    output_mode='EIIE',
    #     random_reset=False,
)
# wrap it in a few wrappers
env = ConcatStates(env)
env = SoftmaxActions(env)
environment = TFOpenAIGymCust('CryptoPortfolioEIIE-v0', env)

# sanity check out environment is working
state = environment.reset()
state, reward, done = environment.execute(env.action_space.sample())
state.shape

# sanity check out environment is working