I moved from IB official API to ib_insync, and with the second one I don't see a way to handle errors like IB API. Is there any way in ib_insync to handle also the errors like this?
def error(self, reqId, errorCode, errorString):
if(errorCode == 502) or (errorCode == 504):
print("ERROR: Fallo de conexión - Comprobar sockets TWS")
exit()
elif(errorCode not in self.goodErrors):
print("Error {} {} {}".format(reqId,errorCode,errorString))
#self.disconnect()
# Errores no importantes:
# - 2104: Market Data Farm OK
# - 2106: Historical Data Farm OK
# - 2158: Sec-Def Data Farm OK
goodErrors = (2104, 2106, 2158)`
This works for IBAPI but not in ib_insync
Two steps. First, an error event handler function must be written. Second, it must be registered. Here are some snippets of my own code that may get you started. I wrote a Logger class for obvious reasons.
Create your ib_insync object:
Hope this helps.