Exemplo n.º 1
0
    def test_transactions_not_created_for_incompl_orders(self, symbol_lookup):
        with patch('sharadar.live.brokers.ib_broker.TWSConnection.start'):
            broker = IBBroker("localhost:9999:1111", account_id='TEST-123')
            broker._tws.nextValidId(0)

        asset = self.asset_finder.retrieve_asset(1)
        symbol_lookup.return_value = asset
        amount = -4
        limit_price = 43.1
        stop_price = 6
        style = StopLimitOrder(limit_price=limit_price, stop_price=stop_price)
        order = broker.order(asset, amount, style)
        assert not broker.transactions
        assert len(broker.orders) == 1
        assert broker.orders[order.id].open

        ib_order_id = order.broker_order_id
        ib_contract = self._create_contract(str(asset.symbol))
        action, qty, order_type, limit_price, stop_price = \
            'SELL', 4, 'STP LMT', 4.3, 2
        ib_order = self._create_order(action, qty, order_type, limit_price,
                                      stop_price)
        ib_state = self._create_order_state('PreSubmitted')
        broker._tws.openOrder(ib_order_id, ib_contract, ib_order, ib_state)

        broker._tws.orderStatus(ib_order_id,
                                status='Cancelled',
                                filled=0,
                                remaining=4,
                                avg_fill_price=0.0,
                                perm_id=4,
                                parent_id=4,
                                last_fill_price=0.0,
                                client_id=32,
                                why_held='')
        assert not broker.transactions
        assert len(broker.orders) == 1
        assert not broker.orders[order.id].open

        broker._tws.orderStatus(ib_order_id,
                                status='Inactive',
                                filled=0,
                                remaining=4,
                                avg_fill_price=0.0,
                                perm_id=4,
                                parent_id=4,
                                last_fill_price=0.0,
                                client_id=1111,
                                why_held='')
        assert not broker.transactions
        assert len(broker.orders) == 1
        assert not broker.orders[order.id].open
Exemplo n.º 2
0
    def test_transactions_created_for_complete_orders(self, symbol_lookup):
        with patch('sharadar.live.brokers.ib_broker.TWSConnection.start'):
            broker = IBBroker("localhost:9999:1111", account_id='TEST-123')
            broker._tws.nextValidId(0)

        asset = self.asset_finder.retrieve_asset(1)
        symbol_lookup.return_value = asset

        order_count = 0
        for amount, order_style in [(-112,
                                     StopLimitOrder(limit_price=9,
                                                    stop_price=1)),
                                    (43, LimitOrder(limit_price=10)),
                                    (-99, StopOrder(stop_price=8)),
                                    (-32, MarketOrder())]:
            order = broker.order(asset, amount, order_style)
            broker._tws.orderStatus(order.broker_order_id,
                                    'Filled',
                                    filled=int(fabs(amount)),
                                    remaining=0,
                                    avg_fill_price=111,
                                    perm_id=0,
                                    parent_id=1,
                                    last_fill_price=112,
                                    client_id=1111,
                                    why_held='')
            contract = self._create_contract(str(asset.symbol))
            (shares, cum_qty, price, avg_price, exec_time, exec_id) = \
                (int(fabs(amount)), int(fabs(amount)), 12.3, 12.31,
                 pd.to_datetime('now', utc=True), order_count)
            exec_detail = self._create_exec_detail(order.broker_order_id,
                                                   shares, cum_qty, price,
                                                   avg_price, exec_time,
                                                   exec_id)
            broker._tws.execDetails(0, contract, exec_detail)
            order_count += 1

            assert len(broker.transactions) == order_count
            transactions = [
                tx for tx in broker.transactions.values()
                if tx.order_id == order.id
            ]
            assert len(transactions) == 1

            assert broker.transactions[exec_id].asset == asset
            assert broker.transactions[exec_id].amount == order.amount
            assert (broker.transactions[exec_id].dt -
                    pd.to_datetime('now', utc=True) < pd.Timedelta('10s'))
            assert broker.transactions[exec_id].price == price
            assert broker.orders[order.id].commission == 0
Exemplo n.º 3
0
    def test_orders_updated_from_order_status(self, symbol_lookup):
        with patch('sharadar.live.brokers.ib_broker.TWSConnection.start'):
            broker = IBBroker("localhost:9999:1111", account_id='TEST-123')
            broker._tws.nextValidId(0)

        # orderStatus calls only work if a respective order has been created
        asset = self.asset_finder.retrieve_asset(1)
        symbol_lookup.return_value = asset
        amount = -4
        limit_price = 43.1
        stop_price = 6
        style = StopLimitOrder(limit_price=limit_price, stop_price=stop_price)
        order = broker.order(asset, amount, style)

        ib_order_id = order.broker_order_id
        status = 'Filled'
        filled = 14
        remaining = 9
        avg_fill_price = 12.4
        perm_id = 99
        parent_id = 88
        last_fill_price = 12.3
        client_id = 1111
        why_held = ''

        broker._tws.orderStatus(ib_order_id, status, filled, remaining,
                                avg_fill_price, perm_id, parent_id,
                                last_fill_price, client_id, why_held)

        assert len(broker.orders) == 1
        zp_order = list(broker.orders.values())[-1]
        assert zp_order.broker_order_id == ib_order_id
        assert zp_order.status == ORDER_STATUS.FILLED
        assert not zp_order.open
        assert zp_order.asset == asset
        assert zp_order.amount == amount
        assert zp_order.limit == limit_price
        assert zp_order.stop == stop_price
        assert (zp_order.dt - pd.to_datetime('now', utc=True) <
                pd.Timedelta('10s'))
Exemplo n.º 4
0
    def test_new_order_appears_in_orders(self, symbol_lookup):
        with patch('sharadar.live.brokers.ib_broker.TWSConnection.start'):
            broker = IBBroker("localhost:9999:1111", account_id='TEST-123')
            broker._tws.nextValidId(0)

        asset = self.asset_finder.retrieve_asset(1)
        symbol_lookup.return_value = asset
        amount = -4
        limit_price = 43.1
        stop_price = 6
        style = StopLimitOrder(limit_price=limit_price, stop_price=stop_price)
        order = broker.order(asset, amount, style)

        assert len(broker.orders) == 1
        assert broker.orders[order.id] == order
        assert order.open
        assert order.asset == asset
        assert order.amount == amount
        assert order.limit == limit_price
        assert order.stop == stop_price
        assert (order.dt - pd.to_datetime('now', utc=True) <
                pd.Timedelta('10s'))
Exemplo n.º 5
0
    def test_multiple_orders(self, symbol_lookup):
        with patch('sharadar.live.brokers.ib_broker.TWSConnection.start'):
            broker = IBBroker("localhost:9999:1111", account_id='TEST-123')
            broker._tws.nextValidId(0)

        asset = self.asset_finder.retrieve_asset(1)
        symbol_lookup.return_value = asset

        order_count = 0
        for amount, order_style in [(-112,
                                     StopLimitOrder(limit_price=9,
                                                    stop_price=1)),
                                    (43, LimitOrder(limit_price=10)),
                                    (-99, StopOrder(stop_price=8)),
                                    (-32, MarketOrder())]:
            order = broker.order(asset, amount, order_style)
            order_count += 1

            assert order_count == len(broker.orders)
            assert broker.orders[order.id] == order
            is_buy = amount > 0
            assert order.stop == order_style.get_stop_price(is_buy)
            assert order.limit == order_style.get_limit_price(is_buy)