pythonalgorithmic-tradinginteractive-brokerstwsib-insync

No pendingTickersEvents being emitted after subscribing to IBKR streaming market data through reqMktData,


Fairly new to ib_insync. Requesting streaming market data but the ticker events aren't being emitted even after 2-3 minutes, no error gets thrown or anything, just nothing happens. Below is the code snippet attached.

def _on_connected_event():
    print(f"Connected to IB!")
    
def _on_pending_tickers_event(tickers):
    print(f"In tickers event")
    for t in tickers:
        print(t)

def stream_data(ib: IB, contract):
    try:
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        print(f"In Thread")

        data = ib.reqMktData(contract, '', True, True)

        while True:
            sleep(1)
            print(f"{datetime.now(tz=pytz.timezone('US/Eastern'))}")
        
    except:
        print(f"Error: {traceback.format_exc()}")
        ib.cancelMktData(contract)


if __name__ == '__main__':
    try:
        ib = IB()
        ib.connectedEvent += _on_connected_event
        ib.pendingTickersEvent += _on_pending_tickers_event
        
        ib.connect('127.0.0.1', port=7497, clientId=1)

        stock = Stock(symbol="SPY", exchange="SMART", currency="USD")
        
        st = threading.Thread(target=stream_data, args=(ib, stock, ))
        st.start()
        
        sleep(150)
    except:    
        ib.cancelMktData(stock)
 

Idk if it's because I am not running it asynchronously or what, but any help would be appreciative.

Thanks.

PS: I have the market data subscription.


Solution

  • Pretty silly mistake, guys. Instead of using ib.sleep() I used time.sleep().