def test_create_tt_trader(self): expert_a = PerfectExpert(Company.A) expert_b = PerfectExpert(Company.B) trader = TrustingTrader(expert_a, expert_b, '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 test_trade_vote_down_stock_a(self): expert_a = PerfectExpert(Company.A) expert_b = PerfectExpert(Company.B) trader = TrustingTrader(expert_a, expert_b, 'test_color', 'test_name') portfolio = Portfolio(1000.0, {Company.A: 10, Company.B: 10}) stock_market_data = StockMarketData([Company.A, Company.B], [Period.TESTING]).deepcopy_first_n_items(4) order_list = trader.trade(portfolio, stock_market_data) self.assertIsNotNone(order_list) self.assertEqual(len(order_list), 2) self.assertEqual(order_list[0].type, OrderType.SELL) self.assertEqual(order_list[0].company, Company.A) self.assertEqual(order_list[0].amount, 10.0) self.assertEqual(order_list[1].type, OrderType.SELL) self.assertEqual(order_list[1].company, Company.B) self.assertEqual(order_list[1].amount, 10.0)
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()
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()