def main(train_stock, val_stock, window_size, batch_size, ep_count, model_name, pretrained, debug): """ Trains the stock trading bot using Deep Q-Learning. Please see https://arxiv.org/abs/1312.5602 for more details. Args: optional arguments [python train.py --help] """ switch_k_backend_device() agent = Agent(window_size, pretrained=pretrained, model_name=model_name) train_data = get_stock_data(train_stock) val_data = get_stock_data(val_stock) initial_offset = val_data[1] - val_data[0] for episode in range(1, ep_count + 1): train_result = train_model(agent, episode, train_data, ep_count=ep_count, batch_size=batch_size, window_size=window_size) val_result, _ = evaluate_model(agent, val_data, window_size, debug) show_train_result(train_result, val_result, initial_offset)
def main(eval_stock, window_size, model_name, debug): """ Evaluates the stock trading bot. Please see https://arxiv.org/abs/1312.5602 for more details. Args: optional arguments [python evaluate.py --help] """ switch_k_backend_device() data = get_stock_data(eval_stock) initial_offset = data[1] - data[0] if model_name is not None: '''Single Model Evaluation''' agent = Agent(window_size, pretrained=True, model_name=model_name) profit, _ = evaluate_model(agent, data, window_size, debug) show_eval_result(model_name, profit, initial_offset) del agent else: '''Multiple Model Evaluation''' for model in os.listdir('models'): if not os.path.isdir('models/{}'.format(model)): agent = Agent(window_size, pretrained=True, model_name=model) profit = evaluate_model(agent, data, window_size, debug) show_eval_result(model, profit, initial_offset) del agent
min_sup = int(args[args.index("-s") + 1]) except: min_sup = int(args[args.index("--min-sup") + 1]) if "-c" in args or "--min-conf" in args: try: min_conf = float(args[args.index("-c") + 1]) except: min_conf = float(args[args.index("--min-conf") + 1]) # Check if result directory exists, if it doesn't make one if not os.path.isdir("results"): os.mkdir("results") # Download stock data if not get_stock_data(ticker): print( f"Data for ${ticker} has previously been fetched, skipping fetching..." ) # Parse stock data into event sequence print("[!] Converting csv data into a sequence...") time_series = get_time_series(ticker) word_length = int(word_length * len(time_series)) if word_length else int( word_length_multiplier * len(time_series)) print("[!] Generating event sequence...") event_sequence = convert_to_event_sequence(time_series, word_length, alphabet_size)
def test_get_comment_counts(self): data = get_stock_data() counts = get_comment_counts(data) self.assertTrue(isinstance(counts, dict)) self.assertTrue(isinstance(counts["AAPL"], int)) self.assertTrue(isinstance(counts["GOOG"], int))
'''Faster computation on CPU (only if using tensorflow-gpu)''' if TENSORFLOW_BACKEND: print('Switching to TensorFlow for CPU...') os.environ['CUDA_VISIBLE_DEVICES'] = '-1' if len(sys.argv) == 8: train_stock_name, val_stock_name, window_size, batch_size, episode_count, model_name, pretrained = sys.argv[1], \ sys.argv[2], int(sys.argv[3]), int(sys.argv[4]), int(sys.argv[5]), sys.argv[6], bool(int(sys.argv[7])) else: print( 'Usage: python train.py [train stock] [val stock] [window] [batch size] [episodes] [model] [pretrained (0/1)]' ) exit(0) agent = Agent(window_size, pretrained=pretrained, model_name=model_name) train_data = get_stock_data(train_stock_name) test_data = get_stock_data(val_stock_name) initial_offset = test_data[1] - test_data[0] for episode in range(1, episode_count + 1): train_result = train_model(agent, episode, train_data, episode_count=episode_count, batch_size=batch_size, window_size=window_size) val_result = evaluate_model(agent, test_data, window_size=window_size) show_train_result(train_result, val_result, initial_offset) print('Done Training!')
model_name = None if len(sys.argv) == 3: stock_name, window_size = sys.argv[1], int(sys.argv[2]) elif len(sys.argv) == 4: stock_name, window_size, model_name = sys.argv[1], int( sys.argv[2]), sys.argv[3] else: print('Usage: python evaluate.py [stock] [window] [model (optional)]') print( 'NOTE - All models in "models/" dir will be evaluated if no pretrained model is provided' ) exit(0) data = get_stock_data(stock_name) initial_offset = data[1] - data[0] if model_name is not None: '''Single Model Evaluation''' agent = Agent(window_size, pretrained=True, model_name=model_name) profit = evaluate_model(agent, data, window_size=window_size) show_eval_result(model_name, profit, initial_offset) del agent else: '''Multiple Model Evaluation''' for model in os.listdir('models'): if not os.path.isdir('models/{}'.format(model)): agent = Agent(window_size, pretrained=True, model_name=model) profit = evaluate_model(agent, data, window_size=window_size) show_eval_result(model, profit, initial_offset)