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.
JavaScript
x
25
25
1
from ibapi.client import EClient
2
from ibapi.wrapper import EWrapper
3
from ibapi.common import TickerId
4
from threading import Timer, Thread
5
6
class ib_class(EWrapper, EClient):
7
def __init__(self, addr, port, client_id):
8
EClient.__init__(self, self)
9
self.connect(addr, port, client_id) # Connect to TWS
10
thread = Thread(target=self.run, daemon=True) # Launch the client thread
11
thread.start()
12
13
def nextValidId(self, orderId: int):
14
print('hi')
15
self.nextOrderID = orderId
16
self.nextOrderID.to_pickle('id')
17
18
def error(self, reqId:TickerId, errorCode:int, errorString:str):
19
if reqId > -1:
20
print("Error. Id: " , reqId, " Code: " , errorCode , " Msg: " , errorString)
21
22
ib_api = ib_class("127.0.0.1", 7490, 1)
23
ib_api.reqIds(-1)
24
ib_api.disconnect()
25
Advertisement
Answer
delete these lines
JavaScript
1
3
1
ib_api.reqIds(-1)
2
ib_api.disconnect()
3
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.