Hi,?
Please help to advise on this.
Since getting ticker for each stock just taking too much time. Calling?
reqTickers() for 500 stocks took 1 hour.
So I am trying to write a func to get the current price for all SP500 stocks in parallel for my ML model. The code as below:
def get_all_pre_closed_and_current_price_from_ib_concurrently(self, stock_names):
self.open_prices = {}
self.close_prices = {}
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
futures = [executor.submit(self.get_pre_closed_and_current_price_from_ib_for_one_stock_concurrently, stock_name) for
stock_name in stock_names]
wait(futures, timeout=5, return_when=ALL_COMPLETED) # ALL_COMPLETED is actually the default
loop.close()
return self.open_prices, self.close_prices
def get_pre_closed_and_current_price_from_ib_for_one_stock_concurrently(self, stock_name):
contract = Stock(stock_name, 'SMART', 'USD')
s_ticker_snap = self.ib.reqTickers(contract, regulatorySnapshot=False)
s_last = s_ticker_snap[0].last
s_bid = s_ticker_snap[0].bid
s_ask = s_ticker_snap[0].ask
s_peg = (s_bid + s_ask) / 2
s_peg = round(s_peg, 2)
print("{} stock price: {}, ".format(contract.symbol, s_peg))
self.close_prices[stock_name], self.open_prices[stock_name] = s_peg, s_ticker_snap[0].close
However, it doesn't work as it fails to download any ticker at all.
Much appreciated.