pythonapitws

IBKR TWS API reqIds does not send back any messages to nextValidId


Shouldn't the following code at least print "hi", as I connect to the API and then reqIds is supposed to shoot a message back to nextValidId?

Instead, when I run this, nothing happens at all.

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.common import TickerId
from threading import Timer, Thread

class ib_class(EWrapper, EClient):
    def __init__(self, addr, port, client_id):
        EClient.__init__(self, self)
        self.connect(addr, port, client_id) # Connect to TWS
        thread = Thread(target=self.run, daemon=True)  # Launch the client thread
        thread.start()

    def nextValidId(self, orderId: int):
        print('hi')
        self.nextOrderID = orderId
        self.nextOrderID.to_pickle('id')

    def error(self, reqId:TickerId, errorCode:int, errorString:str):
        if reqId > -1:
            print("Error. Id: " , reqId, " Code: " , errorCode , " Msg: " , errorString)

ib_api = ib_class("127.0.0.1", 7490, 1)
ib_api.reqIds(-1)
ib_api.disconnect()

Solution

  • delete these lines

    ib_api.reqIds(-1)
    ib_api.disconnect()
    

    You don't need to request ids on startup, the next one comes automatically. Don't disconnect before you get a response. If you would like to disconnect automatically, call disconnect() in the nextValidId method after getting a response.

    You shouldn't use this filter if reqId > -1: The -1 errors are important information about the servers.