コード例 #1
0
ファイル: simple_trader_test.py プロジェクト: Asteur/traderAI
    def testSimpleTraderWithTwoStocks(self):
        trader = SimpleTrader(PerfectPredictor(CompanyEnum.COMPANY_A),
                              PerfectPredictor(CompanyEnum.COMPANY_B))
        self.assertIsNotNone(trader)
        stock_market_data = read_stock_market_data(
            [CompanyEnum.COMPANY_A, CompanyEnum.COMPANY_B], [PERIOD_1])
        self.assertIsNotNone(stock_market_data)
        portfolio = Portfolio(10000, [])
        self.assertIsNotNone(portfolio)

        # Buy stocks based on prediction:
        # SimpleTrader buys only stocks A again
        # Why doesn't SimpleTrader buy stocks B?
        # This is because `SimpleTrader` buys sequentially without considering future/earlier trade actions. So in case
        # of two BUY actions, all available cash is spent for buying the first stock - an issue I already raised (jh)
        order_list = trader.doTrade(portfolio, 0.0, stock_market_data)

        order_list = order_list
        self.assertIsNotNone(order_list)

        self.assertEqual(len(order_list), 1)

        order = order_list[0]
        self.assertEqual(order.action, OrderType.BUY)
        self.assertEqual(order.shares.amount, 287)
        self.assertEqual(order.shares.company_enum, CompanyEnum.COMPANY_A)
コード例 #2
0
    def test_update_and_draw(self):
        """
        Tests: Evaluator#inspect_over_time

        Inspects portfolios over time and checks the results
        """
        trader = SimpleTrader(RandomPredictor(), RandomPredictor())

        evaluator = PortfolioEvaluator([trader] * 3, draw_results=False)

        full_stock_market_data = read_stock_market_data(
            [CompanyEnum.COMPANY_A, CompanyEnum.COMPANY_B],
            [PERIOD_1, PERIOD_2])

        # Calculate and save the initial total portfolio value (i.e. the cash reserve)
        portfolio1 = Portfolio(50000.0, [], 'portfolio 1')
        portfolio2 = Portfolio(100000.0, [], 'portfolio 2')
        portfolio3 = Portfolio(150000.0, [], 'portfolio 3')

        portfolios_over_time = evaluator.inspect_over_time(
            full_stock_market_data, [portfolio1, portfolio2, portfolio3],
            evaluation_offset=100)

        last_date = list(portfolios_over_time['portfolio 1'].keys())[-1]

        assert last_date == datetime.strptime('2015-12-30', '%Y-%m-%d').date()

        data_row_lengths = set(
            [len(value_set) for value_set in portfolios_over_time.values()])
        assert len(data_row_lengths) == 1
        assert data_row_lengths.pop() == 100
コード例 #3
0
    def test_different_mappings(self):
        """
        Tests: Evaluator#inspect_over_time and #inspect_over_time_with_mappings

        Compares if both methods return the same values
        """
        stock_market_data = read_stock_market_data(
            [CompanyEnum.COMPANY_A], [PERIOD_1, PERIOD_2, PERIOD_3])

        portfolio_1 = Portfolio(20000,
                                [SharesOfCompany(CompanyEnum.COMPANY_A, 200)])
        portfolio_2 = Portfolio(20000,
                                [SharesOfCompany(CompanyEnum.COMPANY_A, 200)])
        portfolios = [portfolio_1, portfolio_2]

        perfect_predictor = PerfectPredictor(CompanyEnum.COMPANY_A)

        trader_1 = SimpleTrader(perfect_predictor, perfect_predictor)
        trader_2 = SimpleTrader(perfect_predictor, perfect_predictor)
        trader_3 = SimpleTrader(RandomPredictor(), RandomPredictor())
        traders = [trader_1, trader_2]

        portfolio_trader_mappings = list(
            zip(portfolios, traders, [None] * len(portfolios)))

        evaluator_1 = PortfolioEvaluator([trader_1, trader_1])
        evaluator_2 = PortfolioEvaluator([])
        evaluator_3 = PortfolioEvaluator([trader_3, trader_3])

        # 1 Use evaluator that is initialized *with* traders and call `#inspect_over_time` (i.e. without traders)
        result_1 = evaluator_1.inspect_over_time(stock_market_data, portfolios,
                                                 100)

        # 2 Use evaluator that is initialized *without* traders and call `#inspect_over_time_with_mapping`
        # (i.e. with traders)
        result_2 = evaluator_2.inspect_over_time_with_mapping(
            stock_market_data, portfolio_trader_mappings, 100)

        # 3 Use evaluator that is initialized *with* traders and call `#inspect_over_time_with_mapping`
        # (i.e. with traders) - this should be no problem, because the traders given at initialization time are ignored
        result_3 = evaluator_3.inspect_over_time_with_mapping(
            stock_market_data, portfolio_trader_mappings, 100)

        assert result_1 == result_2 == result_3
コード例 #4
0
ファイル: simple_trader_test.py プロジェクト: Asteur/traderAI
    def testSimpleTraderWithOneStock(self):
        trader = SimpleTrader(PerfectPredictor(CompanyEnum.COMPANY_A),
                              PerfectPredictor(CompanyEnum.COMPANY_B))
        self.assertIsNotNone(trader)
        stock_market_data = read_stock_market_data([CompanyEnum.COMPANY_A],
                                                   [PERIOD_1])
        self.assertIsNotNone(stock_market_data)
        portfolio = Portfolio(10000, [])
        self.assertIsNotNone(portfolio)

        # Buy stocks based on prediction: With 10000, we can buy 287 stocks A for 34.80 each
        order_list = trader.doTrade(portfolio, 0.0, stock_market_data)
        self.assertIsNotNone(order_list)

        self.assertEqual(len(order_list), 1)

        order = order_list[0]
        self.assertEqual(order.action, OrderType.BUY)
        self.assertEqual(order.shares.amount, 287)
        self.assertEqual(order.shares.company_enum, CompanyEnum.COMPANY_A)
コード例 #5
0
    def test_inspect__date_offset(self):
        """
        Tests: Evaluator#inspect_over_time

        Flavour: Test with an date offset
        """
        data = StockData([(date(2017, 1, 1), 150.0), (date(2017, 1, 2), 200.0),
                          (date(2017, 1, 3), 250.0)])
        stock_market_data = StockMarketData({CompanyEnum.COMPANY_A: data})

        portfolio = Portfolio(20000,
                              [SharesOfCompany(CompanyEnum.COMPANY_A, 200)])

        evaluator = PortfolioEvaluator(
            [SimpleTrader(RandomPredictor(), RandomPredictor())])

        portfolio_over_time: dict = \
            evaluator.inspect_over_time(stock_market_data, [portfolio], date_offset=date(2017, 1, 2))['nameless']

        assert date(2016, 12, 31) not in portfolio_over_time.keys()
        assert date(2017, 1, 1) in portfolio_over_time.keys()
        assert date(2017, 1, 2) in portfolio_over_time.keys()
        assert date(2017, 1, 3) not in portfolio_over_time.keys()