required=True) parser.add_argument('--batch_norm', '-b', help='whether to use batch normalization', required=True) args = vars(parser.parse_args()) pprint.pprint(args) if args['debug'] == 'True': DEBUG = True else: DEBUG = False history, abbreviation = read_stock_history( filepath='utils/datasets/stocks_history_target.h5') history = history[:, :, :4] target_stocks = abbreviation num_training_time = 1095 window_length = int(args['window_length']) nb_classes = len(target_stocks) + 1 # 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(target_history,
args = vars(parser.parse_args()) pprint.pprint(args) if args['debug'] == 'True': DEBUG = True else: DEBUG = False rootpath = os.path.dirname(__file__) print("rootpath: " + rootpath) lib_path = LIB_DIR history, abbreviation = read_stock_history( filepath=lib_path + '/datasets/stocks_history_target.h5') history = history[:, :, :4] #remove volumn col? target_stocks = abbreviation num_training_time = 1095 window_length = int(args['window_length']) nb_classes = len(target_stocks) + 1 # 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
from utils.data import create_dataset_from_dir, read_stock_history from datetime import datetime import pandas as pd import matplotlib.pyplot as plt def test_plot(): data = pd.read_csv("c:/data/equity/price/FXI.csv") data.plot(title="FXI", fig=plt.gcf(), rot=30) plt.show() if __name__ == '__main__': # test_plot() # exit(0) # end = datetime.strptime('2019-11-08', '%Y-%m-%d') # start = datetime.strptime('2004-10-08', '%Y-%m-%d') # print((end - start).days) # exit(0) # row = [1, 2, 3, 4, 5, 6] # print(list(map(float, row[1:5] +row[5:5]))) # exit(0) results = create_dataset_from_dir('c:/data/equity/price', 'c:/data/equity/price/target_prices.h5', tickers=['FXI', 'SPY', 'EWZ', 'XLK', 'XLF'], start='2004-10-08', end='2019-12-30') data, tickers = read_stock_history('c:/data/equity/price/target_prices.h5') print(data.shape) # print(data[0]) # print(data[1]) # print(data[2]) print(tickers)
def visualise_Data(): with open('utils/datasets/all_eqw', 'rb') as fr: history = pickle.load(fr, encoding='latin1') with open('utils/datasets/stock_names', 'rb') as fr: abbreviation = pickle.load(fr, encoding='latin1') history = history[:, :, :4] num_training_time = history.shape[1] num_testing_time = history.shape[1] window_length = 3 # get target history target_stocks = ['BLK UN EQUITY', 'GS UN EQUITY', 'USB UN EQUITY'] 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, :] print(target_history[i]) # collect testing data testing_stocks = [ 'AMG UN EQUITY', 'BRK/B UN EQUITY', 'MTB UN EQUITY', ] testing_history = np.empty(shape=(len(testing_stocks), num_testing_time, history.shape[2])) for i, stock in enumerate(target_stocks): testing_history[i] = history[ abbreviation.index(stock), :num_testing_time, :] # dataset for 16 stocks by splitting timestamp history, abbreviation = read_stock_history( filepath='utils/datasets/stocks_history_target.h5') with open('utils/datasets/all_eqw', 'rb') as fr: history = pickle.load(fr, encoding='latin1') with open('utils/datasets/stock_names', 'rb') as fr: abbreviation = pickle.load(fr, encoding='latin1') history = history[:, :, :4] # 16 stocks are all involved. We choose first 3 years as training data num_training_time = 1095 target_stocks = abbreviation 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, :] print((target_history.shape)) # and last 2 years as testing data. testing_stocks = abbreviation testing_history = np.empty(shape=(len(testing_stocks), history.shape[1] - num_training_time, history.shape[2])) for i, stock in enumerate(testing_stocks): testing_history[i] = history[abbreviation.index(stock), num_training_time:, :] print((testing_history.shape)) nb_classes = len(target_stocks) + 1 print(target_history.shape) print(testing_history.shape) if True: date_list = [index_to_date(i) for i in range(target_history.shape[1])] x = range(target_history.shape[1]) for i in range(len(target_stocks)): plt.figure(i) plt.plot( x, target_history[i, :, 1]) # open, high, low, close = [0, 1, 2, 3] plt.xticks(x[::200], date_list[::200], rotation=30) plt.title(target_stocks[i]) plt.show() # common settings batch_size = 64 action_bound = 1. tau = 1e-3 models = [] model_names = [] window_length_lst = [3, 7, 14, 21] predictor_type_lst = ['cnn', 'lstm'] use_batch_norm = True for window_length in window_length_lst: name = 'imit_LSTM%3A window = {}'.format(window_length) model_name = 'imitation_lstm_window_{}'.format(window_length) model_names.append(model_name) # instantiate LSTM model lstm_model = StockLSTM(nb_classes, window_length, weights_file='weights/' + name + '.h5') lstm_model.build_model(load_weights=True) models.append(lstm_model) name = 'imit_CNN%3A window = {}'.format(window_length) model_name = 'imitation_cnn_window_{}'.format(window_length) model_names.append(model_name) # instantiate CNN model cnn_model = StockCNN(nb_classes, window_length, weights_file='weights/' + name + '.h5') cnn_model.build_model(load_weights=True) models.append(cnn_model) # instantiate environment, 3 stocks, with trading cost, window_length 3, start_date sample each time for window_length in window_length_lst: for predictor_type in predictor_type_lst: name = 'DDPG_window_{}_predictor_{}'.format( window_length, predictor_type) model_names.append(name) tf.reset_default_graph() sess = tf.Session() tflearn.config.init_training_mode() action_dim = [nb_classes] state_dim = [nb_classes, window_length] variable_scope = get_variable_scope(window_length, predictor_type, use_batch_norm) with tf.variable_scope(variable_scope): actor = StockActor(sess, state_dim, action_dim, action_bound, 1e-4, tau, batch_size, predictor_type, use_batch_norm) critic = StockCritic( sess=sess, state_dim=state_dim, action_dim=action_dim, tau=1e-3, learning_rate=1e-3, num_actor_vars=actor.get_num_trainable_vars(), predictor_type=predictor_type, use_batch_norm=use_batch_norm) 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) ddpg_model = DDPG(None, sess, actor, critic, actor_noise, obs_normalizer=obs_normalizer, config_file='config/stock.json', model_save_path=model_save_path, summary_path=summary_path) ddpg_model.initialize(load_weights=True, verbose=False) models.append(ddpg_model) env = MultiActionPortfolioEnv(target_history, target_stocks, model_names[8:], steps=500, sample_start_date='2012-10-30') test_model_multiple(env, models[8:])
def run(env_id, seed, noise_type, layer_norm, evaluation, **kwargs): # Configure things. rank = MPI.COMM_WORLD.Get_rank() if rank != 0: logger.set_level(logger.DISABLED) ######################################### DEFAULT DATA ####################################### history, abbreviation = read_stock_history(filepath='utils/datasets/stocks_history_target.h5') history = history[:, :, :4] history[:, 1:, 0] = history[:, 0:-1, 3] # correct opens target_stocks = abbreviation num_training_time = 1095 # 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, :] print("target:", target_history.shape) testing_stocks = abbreviation test_history = np.empty(shape=(len(testing_stocks), history.shape[1] - num_training_time, history.shape[2])) for i, stock in enumerate(testing_stocks): test_history[i] = history[abbreviation.index(stock), num_training_time:, :] print("test:", test_history.shape) window_length = kwargs['window_length'] max_rollout_steps = kwargs['nb_rollout_steps'] ############################################################################################### train_env = PortfolioEnv(target_history, target_stocks, steps=min(max_rollout_steps, target_history.shape[1]-window_length-2), window_length=window_length) infer_train_env = PortfolioEnv(target_history, target_stocks, steps=target_history.shape[1]-window_length-2, window_length=window_length) infer_test_env = PortfolioEnv(test_history, testing_stocks, steps=test_history.shape[1]-window_length-2, window_length=window_length) kwargs['nb_eval_steps'] = infer_train_env.steps kwargs['nb_eval_test_steps'] = infer_test_env.steps print("SPACE:", train_env.observation_space.shape) # Parse noise_type action_noise = None param_noise = None nb_actions = train_env.action_space.shape[-1] for current_noise_type in noise_type.split(','): current_noise_type = current_noise_type.strip() if current_noise_type == 'none': pass elif 'adaptive-param' in current_noise_type: _, stddev = current_noise_type.split('_') param_noise = AdaptiveParamNoiseSpec(initial_stddev=float(stddev), desired_action_stddev=float(stddev)) elif 'normal' in current_noise_type: _, stddev = current_noise_type.split('_') action_noise = NormalActionNoise(mu=np.zeros(nb_actions), sigma=float(stddev) * np.ones(nb_actions)) elif 'ou' in current_noise_type: _, stddev = current_noise_type.split('_') action_noise = OrnsteinUhlenbeckActionNoise(mu=np.zeros(nb_actions), sigma=float(stddev) * np.ones(nb_actions)) else: raise RuntimeError('unknown noise type "{}"'.format(current_noise_type)) # Configure components. memory = Memory(limit=int(1e6), action_shape=train_env.action_space.shape, observation_shape=train_env.observation_space.shape) critic = Critic(nb_actions, layer_norm=layer_norm, asset_features_shape=train_env.asset_features_shape) actor = Actor(nb_actions, layer_norm=layer_norm, asset_features_shape=train_env.asset_features_shape) # Seed everything to make things reproducible. seed = seed + 1000000 * rank logger.info('rank {}: seed={}, logdir={}'.format(rank, seed, logger.get_dir())) tf.reset_default_graph() set_global_seeds(seed) train_env.seed(seed) infer_train_env.seed(seed) infer_test_env.seed(seed) # Disable logging for rank != 0 to avoid noise. if rank == 0: start_time = time.time() training.train(env=train_env, train_eval_env=infer_train_env, test_eval_env=infer_test_env, param_noise=param_noise, action_noise=action_noise, actor=actor, critic=critic, memory=memory, **kwargs) train_env.close() infer_train_env.close() infer_test_env.close() if rank == 0: logger.info('total runtime: {}s'.format(time.time() - start_time))
else: DEBUG = False if args['train'] == 'True': TRAIN = True else: TRAIN = False if args['test'] == 'True': TEST = True else: TEST = False #data dimensions: ticker:price date:open high low close volume # history, tickers = read_stock_history(filepath='utils/datasets/stocks_history_target.h5') history, tickers = read_stock_history(filepath='c:/data/equity/price/target_prices.h5') if DEBUG: print("all data before slicing=", history) print("all data's shape={}".format(history.shape)) history = history[:, :, :4] if DEBUG: print("data=", history[0], "\nshape=", history.shape, "\n stocks=", tickers) print("all data=", history) target_stocks = tickers print("stocks in scope={}".format(tickers)) #the set of prices at the begining for training, the rest for validation, 1825 in total for this data set # num_training_time = history.shape[1] #2095 num_training_time = 3000 if TEST: num_training_time = history.shape[1]