Ejemplo n.º 1
0
    def test_trade_buy_multiple_securities_with_exception(self):

        buy_positions = [('BA', 1.0), ('GE', 1.0), ('XOM', 1.0)]

        # portfolio has 3 positions
        portfolio = deepcopy(self.base_portfolio)

        portfolio = Portfolio.from_dict(portfolio)

        with patch.object(td_ameritrade, 'place_order', side_effect=[
                'order-1', 'order-2', TradeError("Some Error", None, None)]), \
                patch.object(td_ameritrade, 'list_recent_orders', return_value={
                    "order-1": {
                        "status": "FILLED",
                        "symbol": "BA",
                        "quantity": 1,
                        "closeTime": "2020-05-04T03:21:04+0000",
                        "tag": "AA_myuser"
                    },
                    "order-2": {
                        "status": "FILLED",
                        "symbol": "GE",
                        "quantity": 1,
                        "closeTime": "2020-05-04T03:21:04+0000",
                        "tag": "AA_myuser"
                    }
                }):

            broker = Broker()
            self.assertFalse(broker.trade('BUY', buy_positions, portfolio))
Ejemplo n.º 2
0
    def test_trade_buy_single_security(self):

        buy_positions = [('BA', 1.0)]

        portfolio = deepcopy(self.base_portfolio)
        del portfolio['current_portfolio']['securities'][2]
        del portfolio['current_portfolio']['securities'][1]

        portfolio = Portfolio.from_dict(portfolio)

        with patch.object(td_ameritrade, 'place_order', return_value='order-xxx'), \
                patch.object(td_ameritrade, 'list_recent_orders', return_value={
                    "order-xxx": {
                        "status": "FILLED",
                        "symbol": "BA",
                        "quantity": 1,
                        "closeTime": "2020-05-04T03:21:04+0000",
                        "tag": "AA_myuser"
                    },
                }):

            broker = Broker()
            self.assertTrue(broker.trade('BUY', buy_positions, portfolio))

            pos = portfolio.get_position('BA')
            self.assertEqual(pos['trade_state'], 'FILLED')
            self.assertEqual(pos['quantity'], 1)
            self.assertIsNotNone(pos['purchase_date'])
Ejemplo n.º 3
0
    def test_trade_nothing_to_sell(self):
        '''
            Tests that when there is nothing to trade, 'trade' will still
            return true
        '''
        sell_positions = []

        broker = Broker()
        self.assertTrue(broker.trade('SELL', sell_positions, None))
Ejemplo n.º 4
0
    def test_trade_buy_failed_trades(self):

        buy_positions = [('BA', 1.0), ('GE', 1.0), ('XOM', 1.0)]

        # portfolio has 3 positions
        portfolio = deepcopy(self.base_portfolio)

        portfolio = Portfolio.from_dict(portfolio)

        with patch.object(td_ameritrade, 'place_order', side_effect=[
                'order-1', 'order-2', 'order-3']), \
                patch.object(td_ameritrade, 'list_recent_orders', return_value={
                    "order-1": {
                        "status": "FILLED",
                        "symbol": "BA",
                        "quantity": 1,
                        "closeTime": "2020-05-04T03:21:04+0000",
                        "tag": "AA_myuser"
                    },
                    "order-2": {
                        "status": "FILLED",
                        "symbol": "GE",
                        "quantity": 1,
                        "closeTime": "2020-05-04T03:21:04+0000",
                        "tag": "AA_myuser"
                    },
                    "order-3": {
                        "status": "UNKNOWN",
                        "symbol": "XOM",
                        "quantity": 1,
                        "closeTime": "2020-05-04T03:21:04+0000",
                        "tag": "AA_myuser"
                    },
                }):

            broker = Broker()
            self.assertTrue(broker.trade('BUY', buy_positions, portfolio))

            pos = portfolio.get_position('BA')
            self.assertEqual(pos['trade_state'], 'FILLED')
            self.assertEqual(pos['quantity'], 1)
            self.assertIsNotNone(pos['purchase_date'])

            pos = portfolio.get_position('GE')
            self.assertEqual(pos['trade_state'], 'FILLED')
            self.assertEqual(pos['quantity'], 1)
            self.assertIsNotNone(pos['purchase_date'])

            pos = portfolio.get_position('XOM')
            self.assertEqual(pos['trade_state'], 'UNFILLED')
            self.assertEqual(pos['quantity'], 0)
            self.assertIsNone(pos['purchase_date'])
Ejemplo n.º 5
0
    def test_trade_sell_single_security(self):

        sell_positions = [('BA', 1.0)]

        with patch.object(td_ameritrade, 'place_order', return_value='order-xxx'), \
                patch.object(td_ameritrade, 'list_recent_orders', return_value={
                    "order-xxx": {
                        "status": "FILLED",
                        "symbol": "BA",
                        "quantity": 1,
                        "closeTime": "2020-05-04T03:21:04+0000",
                        "tag": "AA_myuser"
                    },
                }):

            broker = Broker()
            self.assertTrue(broker.trade('SELL', sell_positions, None))