Вот пример:
Код: Выделить всё
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()
Код: Выделить всё
strategy.dataStreams[0].last
Я думаю, что эта проблема связана с природой IB API работает асинхронно, но у меня недостаточно опыта, чтобы найти обходной путь.
Мне нужно создать в классе другие функции, чтобы иметь возможность извлекать данные из потоков и выполнять вычисления.
Можно ли сделать по приведенной выше логике? или мне нужно переместить потоки данных в отдельный класс и инициализировать его отдельно, чтобы иметь возможность доступа к данным?
Подробнее здесь: https://stackoverflow.com/questions/564 ... nan-values