Interactive Brokers API数据流仅返回NaN值

时间:2019-06-01 09:02:39

标签: python interactive-brokers tws

我有一个建立几个市场数据流并将它们存储在一个数组中的类。然后,同一类中的另一个函数访问该数组并从流中请求数据,但是所有值都以NaN形式出现。

这里是一个例子:

 from ib_insync import *

 class Trading:
     def __init__(...):
         self.dataStreams = []
         self.conn = IB()  #establish connecting to the api
         self.tickers = [('BMW', 'IBIS'), ('BAYN', 'IBIS'),  .. ]

     def _stream_data(self): # this function established the data stream and appends it to an array
         for contract in self.tickers:
              stock = Stock(contract[0], contract[1], 'EUR')
              stream = self.conn.reqMktData(stock, '', False, False)
              self.dataStreams.append(stream)
              self.conn.sleep(1)

    def start(self):
        self._stream_data()  # fill the datastream array
        print(self.dataStreams[0].last + self.dataStreams[1].last)  # so this should return the sum of two prices, but it does not

因此,当我在解释器中初始化以上类时:

 strategy = Trading()
 strategy.start()

strategy.start()将打印NaN,因为来自流的数据以NaN的形式出现。但是,如果我像这样从策略对象访问datastreams数组:

strategy.dataStreams[0].last 

它将正确打印最近的价格。

我认为此问题与IB api异步工作的性质有关,但是我没有足够的经验来解决。 我需要使类具有其他功能,以便能够从流中检索数据并执行计算。

是否可以使用上述逻辑?还是我必须将数据流移到一个单独的类中并分别对其进行初始化才能访问数据?

0 个答案:

没有答案