Пример #1
0
 def setUp(self):
     self.__testTWS = TestEClientSocket(self)
     self.__conn = Connection(eClientSocket=self.__testTWS)
     self.__feed = LiveFeed(self.__conn)
Пример #2
0
class IBLiveFeedTestCase(unittest.TestCase):
    TestInstrument = "spy"

    def setUp(self):
        self.__testTWS = TestEClientSocket(self)
        self.__conn = Connection(eClientSocket=self.__testTWS)
        self.__feed = LiveFeed(self.__conn)

    def testSubscription(self):
        instrument1 = 'XXX'
        instrument2 = 'YYY'
        instrument3 = 'ZZZ'

        for useRTH in (0, 1):
            # Check if multiple instruments are supported
            self.__feed.subscribeRealtimeBars(instrument1, useRTH)
            self.assertIn(instrument1, self.__feed.getRegisteredInstruments())
            self.__feed.subscribeRealtimeBars(instrument2, useRTH)
            self.assertIn(instrument1, self.__feed.getRegisteredInstruments())
            self.assertIn(instrument2, self.__feed.getRegisteredInstruments())

            # XXX: There is no way to unsubscribe from a BarFeed yet
            # self.__feed.unsubscribeRealtimeBars(instrument1)
            # self.assertNotIn(instrument1, self.__feed.getRegisteredInstruments())
            # self.assertIn(instrument2, self.__feed.getRegisteredInstruments())
            # self.__feed.unsubscribeRealtimeBars(instrument2)
            # self.assertNotIn(instrument1, self.__feed.getRegisteredInstruments())
            # self.assertNotIn(instrument2, self.__feed.getRegisteredInstruments())

            self.__feed.subscribeRealtimeBars(instrument3, useRTH)

            # Test if the realtime bar request reached tws
            self.assertEqual(instrument3, self.__testTWS.realtimeContract.m_symbol)
            self.assertEqual("STK", self.__testTWS.realtimeContract.m_secType)
            self.assertEqual("SMART", self.__testTWS.realtimeContract.m_exchange)
            self.assertEqual("USD", self.__testTWS.realtimeContract.m_currency)
            self.assertEqual(5, self.__testTWS.realtimeBarSize)
            self.assertEqual("TRADES", self.__testTWS.realtimeWhatToShow)
            self.assertEqual(useRTH, self.__testTWS.realtimeUseRTH)

            self.__feed.unsubscribeRealtimeBars(instrument3)

    @pytest.mark.xfail(strict=True)
    def testRealtimeBars(self):
        # Call onRealtimeBar with the instrument & bar tuple.
        # The result should appear in the consecutive fetchNextBars call
        instrument1 = 'XXX'
        instrument2 = 'YYY'
        instrument3 = 'ZZZ'
        instrument4 = 'QQQ'
        bar1 = Bar(datetime.datetime(2012, 8, 9, 12, 20, 00), open_=1.31, high=2.51, low=0.91, close=2.11,
                                                           volume=11, vwap=14.11, tradeCount=51)
        bar2 = Bar(datetime.datetime(2012, 8, 9, 12, 20, 00), open_=1.32, high=2.52, low=0.92, close=2.12,
                                                           volume=12, vwap=14.12, tradeCount=52)
        bar3 = Bar(datetime.datetime(2012, 8, 9, 12, 20, 05), open_=1.43, high=2.53, low=0.93, close=2.13,
                                                           volume=13, vwap=14.13, tradeCount=53)
        bar4 = Bar(datetime.datetime(2012, 8, 9, 12, 20, 10), open_=1.44, high=2.54, low=0.94, close=2.14,
                                                           volume=11, vwap=14.14, tradeCount=54)

        # Add bar1 and test the presence using fetchNextBars
        # Bar1 should not be present until the timestamp is increased
        # on the nex onRealtimeBar call
        self.__feed.onIBBar((instrument1, bar1))

        # Add bar2 with the same timestamp. No bars should be present
        self.__feed.onIBBar((instrument2, bar2))

        # Add bar3 with the next timestamp to trigger transfer between onRealtimeBar and fetchNextBars
        self.__feed.onIBBar((instrument3, bar3))
        bars = self.__feed.fetchNextBars()
        self.assertIn(instrument1, bars.keys())
        self.assertIn(instrument2, bars.keys())
        self.assertNotIn(instrument3, bars.keys())
        self.assertNotIn(instrument4, bars.keys())
        self.assertEqual(bars[instrument1], bar1)
        self.assertEqual(bars[instrument2], bar2)

        # Add bar4, only bar3 should return
        self.__feed.onIBBar((instrument4, bar4))
        bars = self.__feed.fetchNextBars()
        self.assertNotIn(instrument1, bars.keys())
        self.assertNotIn(instrument2, bars.keys())
        self.assertIn(instrument3, bars.keys())
        self.assertNotIn(instrument4, bars.keys())
        self.assertEqual(bars[instrument3], bar3)