Пример #1
0
    def test_historical_data(self, con):
        import time
        from twspy import Contract

        def callback(msg):
            if msg.date.startswith('finished'):
                seen.append(msg)

        def error(msg):
            if msg.errorCode == 2105:  # hist data farm is not connected
                seen.append(msg)

        seen = []
        con.register('historicalData', callback)
        con.register('error', error)
        con.connect()

        c = Contract()
        c.m_symbol = 'AAPL'
        c.m_secType = 'STK'
        c.m_exchange = 'SMART'
        c.m_primaryExch = 'NYSE'
        e = time.strftime('%Y%m%d %H:%M:%S')
        con.reqHistoricalData(1, c, e, "5 D", "1 hour", "TRADES", 1, 1, None)

        assert sleep_until(lambda: seen, 5.0)
        if type(seen[0]).__name__ == 'error':
            pytest.xfail(seen[0].errorMsg)
Пример #2
0
    def test_place_cancel_order(self, con):
        from twspy import Contract, Order, TagValue

        @con.listener('nextValidId')
        def nextValidId(msg):
            global next_order_id
            next_order_id = msg.orderId

        global next_order_id
        next_order_id = None
        con.connect()
        assert sleep_until(lambda: next_order_id is not None, 1.0)

        c = Contract()
        c.m_symbol = 'BRK B'
        c.m_secType = 'STK'
        c.m_exchange = 'SMART'
        c.m_primaryExch = 'NYSE'

        o = Order()
        o.m_action = 'BUY'
        o.m_totalQuantity = 1
        o.m_orderType = 'LMT'
        o.m_lmtPrice = 0.01
        o.m_algoStrategy = 'Vwap'
        o.m_algoParams = [TagValue('maxPctVol', '0.10')]

        @con.listener('openOrder')
        def openOrder(msg):
            if msg.orderId == next_order_id:
                seen.append(msg)

        seen = []
        con.placeOrder(next_order_id, c, o)
        assert sleep_until(lambda: seen, 1.0)
        assert seen[0].orderState.m_status == "PreSubmitted"

        seen = []
        con.cancelOrder(next_order_id)
        assert sleep_until(lambda: seen, 1.0)
        assert seen[0].orderState.m_status == "PendingCancel"
Пример #3
0
    def test_historical_data(self, con):
        import time
        from twspy import Contract

        def callback(msg):
            if msg.date.startswith('finished'):
                seen.append(msg)

        def error(msg):
            if msg.errorCode == 2105:  # hist data farm is not connected
                seen.append(msg)

        seen = []
        con.register('historicalData', callback)
        con.register('error', error)
        con.connect()

        c = Contract()
        c.m_symbol = 'AAPL'
        c.m_secType = 'STK'
        c.m_exchange = 'SMART'
        c.m_primaryExch = 'NYSE'
        e = time.strftime('%Y%m%d %H:%M:%S')
        con.reqHistoricalData(1, c, e, "5 D", "1 hour", "TRADES", 1, 1, None)

        assert sleep_until(lambda: seen, 5.0)
        if type(seen[0]).__name__ == 'error':
            pytest.xfail(seen[0].errorMsg)
Пример #4
0
    def test_place_cancel_order(self, con):
        from twspy import Contract, Order, TagValue

        @con.listener('nextValidId')
        def nextValidId(msg):
            global next_order_id
            next_order_id = msg.orderId

        global next_order_id
        next_order_id = None
        con.connect()
        assert sleep_until(lambda: next_order_id is not None, 1.0)

        c = Contract()
        c.m_symbol = 'BRK B'
        c.m_secType = 'STK'
        c.m_exchange = 'SMART'
        c.m_primaryExch = 'NYSE'

        o = Order()
        o.m_action = 'BUY'
        o.m_totalQuantity = 1
        o.m_orderType = 'LMT'
        o.m_lmtPrice = 0.01
        o.m_algoStrategy = 'Vwap'
        o.m_algoParams = [TagValue('maxPctVol', '0.10')]

        @con.listener('openOrder')
        def openOrder(msg):
            if msg.orderId == next_order_id:
                seen.append(msg)

        seen = []
        con.placeOrder(next_order_id, c, o)
        assert sleep_until(lambda: seen, 1.0)
        assert seen[0].orderState.m_status in ["PreSubmitted", "Submitted"]

        con.unregister('openOrder', openOrder)

        @con.listener('orderStatus')
        def orderStatus(msg):
            if msg.orderId == next_order_id:
                seen.append(msg)

        seen = []
        con.cancelOrder(next_order_id)
        assert sleep_until(lambda: seen, 1.0)
        assert seen[0].status == "Cancelled"