def test_create_bah_trader(self): expert_a = ObscureExpert(Company.A) expert_b = ObscureExpert(Company.B) trader = DeepQLearningTrader(expert_a, expert_b, False, False, 'test_color', 'test_name') self.assertIsNotNone(trader) self.assertEqual(trader.get_color(), 'test_color') self.assertEqual(trader.get_name(), 'test_name')
def main_orig(): """ Code from the original main routine. Visualizes portfolio value over the testing period for different traders. """ # Load stock market data for testing period stock_market_data = StockMarketData([Company.A, Company.B], [Period.TESTING]) # create new stock exchange with initial portfolio cash for each traders stock_exchange = StockExchange(2000.0) # create the traders bah_trader = BuyAndHoldTrader('black', 'Buy and hold') tt_trader_obscure = TrustingTrader(ObscureExpert(Company.A), ObscureExpert(Company.B), 'green', 'Trust experts') tt_trader2 = TrustingTrader2(ObscureExpert(Company.A), ObscureExpert(Company.B), 'limegreen', 'Trust experts for sell only') dql_trader = deep_q_learning_trader.DeepQLearningTrader( ObscureExpert(Company.A), ObscureExpert(Company.B), True, False, 'red', plot_name='Deep Q-learning trader') # run the stock exchange over the testing period, with 100 skipped trading days stock_exchange.run(stock_market_data, [bah_trader, tt_trader_obscure, tt_trader2, dql_trader]) # visualize the results stock_exchange.visualize_last_run()
def main_print_model_performance(): """ Just prints the final portfolio value and v score (return % per trading day) for training and testing data set for different traders. """ stock_market_data_train = StockMarketData([Company.A, Company.B], [Period.TRAINING]) # stock_market_data_train = stock_market_data_train.deepcopy_first_n_items( # int(stock_market_data_train.get_row_count() / 5)) stock_market_data_test = StockMarketData([Company.A, Company.B], [Period.TESTING]) bah_trader = BuyAndHoldTrader(name='buy and hold') tt_trader1 = TrustingTrader(ObscureExpert(Company.A), ObscureExpert(Company.B), 'green', 'trust experts, prefer A') tt_trader2 = TrustingTrader2(ObscureExpert(Company.A), ObscureExpert(Company.B), 'limegreen', 'trust experts for sell only') dql_trader = deep_q_learning_trader.DeepQLearningTrader( ObscureExpert(Company.A), ObscureExpert(Company.B), True, False, 'red') all_traders = [bah_trader, tt_trader1, tt_trader2, dql_trader] trader_names = [] for trader in all_traders: if isinstance(trader, deep_q_learning_trader.DeepQLearningTrader ) and trader.plot_name is not None: trader_name = trader.plot_name else: trader_name = trader.get_name() trader_names.append(trader_name) max_trader_name_length = max((len(name) for name in trader_names)) for trader, trader_name in zip(all_traders, trader_names): is_first_line = True for dataset, dataset_name, starting_cash in [ (stock_market_data_train, 'train', 10000.0), (stock_market_data_test, 'test', 2000.0) ]: stock_exchange = StockExchange(starting_cash) stock_exchange.run(dataset, [trader]) p = stock_exchange.get_final_portfolio_value(trader) samples = dataset.get_row_count() v = 100.0 * (math.pow(p / starting_cash, 1 / samples) - 1.0) if is_first_line: header = ('{: <' + str(max_trader_name_length) + '}').format(trader_name) else: header = ' ' * max_trader_name_length header += f' ({dataset_name: <5}): ' print(f'{header}{v:.5f}% return per trading day' f' (final portfolio value of {p:.1e})') is_first_line = False if isinstance(trader, BuyAndHoldTrader): trader.reset()
# This method retrains the traders from scratch using training data from TRAINING and test data from TESTING EPISODES = 5 if __name__ == "__main__": # Create the training data and testing data # Hint: You can crop the training data with training_data.deepcopy_first_n_items(n) training_data = StockMarketData([Company.A, Company.B], [Period.TRAINING]) testing_data = StockMarketData([Company.A, Company.B], [Period.TESTING]) #training_data = training_data.deepcopy_first_n_items(6000) # 12588 #print(f'training_data[Company.A].get_row_count(): {training_data[Company.A].get_row_count()}') # Create the stock exchange and one traders to train the net stock_exchange = stock_exchange.StockExchange(10000.0) training_trader = DeepQLearningTrader(ObscureExpert(Company.A), ObscureExpert(Company.B), False, True) # Save the final portfolio values per episode final_values_training, final_values_test = [], [] for i in range(EPISODES): logger.info(f"DQL Trader: Starting training episode {i}") # train the net stock_exchange.run(training_data, [training_trader]) training_trader.save_trained_model() final_values_training.append( stock_exchange.get_final_portfolio_value(training_trader))
pyplot.legend(trader_names) pyplot.show() # This main method evaluates all traders over the testing period and visualize the results. if __name__ == "__main__": mpl.rcParams["savefig.directory"] = os.chdir( os.path.dirname(__file__) + '\\eval') # Load stock market data for testing period stock_market_data = StockMarketData([Company.A, Company.B], [Period.TESTING]) # create new stock exchange with initial portfolio cash for each traders stock_exchange = StockExchange(2000.0) # create the traders bah_trader = BuyAndHoldTrader() tt_trader_obscure = TrustingTrader(ObscureExpert(Company.A), ObscureExpert(Company.B), 'green', 'tt obscure') dql_trader = deep_q_learning_trader.DeepQLearningTrader( ObscureExpert(Company.A), ObscureExpert(Company.B), True, False, 'red') # run the stock exchange over the testing period, with 100 skipped trading days stock_exchange.run(stock_market_data, [bah_trader, tt_trader_obscure, dql_trader]) # visualize the results stock_exchange.visualize_last_run()
pyplot.figure() trader_names = [] for trader in self.__trader_portfolios: portfolios = self.__trader_portfolios[trader] keys = portfolios.keys() values = [pf.get_value(self.__complete_stock_market_data, date) for date, pf in portfolios.items()] pyplot.plot(keys, values, label=trader.get_name(), color=trader.get_color()) trader_names.append(trader.get_name()) pyplot.legend(trader_names) pyplot.show() # This main method evaluates all traders over the testing period and visualize the results. if __name__ == "__main__": # Load stock market data for testing period stock_market_data = StockMarketData([Company.A, Company.B], [Period.TESTING]) # create new stock exchange with initial portfolio cash for each traders stock_exchange = StockExchange(2000.0) # create the traders bah_trader = BuyAndHoldTrader() tt_trader_obscure = TrustingTrader(ObscureExpert(Company.A), ObscureExpert(Company.B), 'green', 'tt obscure') dql_trader = deep_q_learning_trader.DeepQLearningTrader(ObscureExpert(Company.A), ObscureExpert(Company.B), True, False, 'red') # run the stock exchange over the testing period, with 100 skipped trading days stock_exchange.run(stock_market_data, [bah_trader, tt_trader_obscure, dql_trader]) # visualize the results stock_exchange.visualize_last_run()
traders = dict(train=[], test=[]) def get_last_portfolio_value(phase: str, run: int) -> float: index = min(run * EPISODES + EPISODES - 1, len(final_portfolio_values[phase]) - 1) return final_portfolio_values[phase][index] def get_last_v_score(phase: str, run: int) -> float: index = min(run * EPISODES + EPISODES - 1, len(return_pct_per_day[phase]) - 1) return return_pct_per_day[phase][index] for run in range(TRAINING_RUNS): # Create the stock exchange and one traders to train the net starting_cash = dict(train=10000.0, test=2000.0) stock_exchanges = dict(train=stock_exchange.StockExchange(starting_cash["train"]), test=stock_exchange.StockExchange(starting_cash["test"])) traders['train'].append(DeepQLearningTrader(ObscureExpert(Company.A), ObscureExpert(Company.B), False, True)) for episode in range(EPISODES): # logger.info(f"DQL Trader: Starting training episode {episode}") for phase in ['train', 'test']: if phase == 'test': testing_trader = DeepQLearningTrader(ObscureExpert(Company.A), ObscureExpert(Company.B), True, False) if episode == 0: traders[phase].append(testing_trader) else: traders[phase][-1] = testing_trader # replace testing trader from previous episode trader = traders[phase][-1] stock_exchanges[phase].run(data[phase], [trader]) if phase == 'train': trader.save_trained_model() # required to be able to create testing trader in next iteration