Keyboard Shortcuts
ctrl + shift + ? :
Show all keyboard shortcuts
ctrl + g :
Navigate to a group
ctrl + shift + f :
Find
ctrl + / :
Quick actions
esc to dismiss
Likes
Search
No or delayed response on placed orders using IB Python API TWS 10.19 on Linux EC2
Hello,
I've been using the TWS Python API for almost two years for automatically trading futures. I run my code on an amazon Linux EC2 machine, Python 3.9.5 , ibapi version 9.81.1.post1, TWS version 10.19. Over the past few weeks I've noticed that some of my orders have been getting "lost". By lost I mean no response in the orderStatus() callback. Normally I get a response almost immediately (my system checks every 2 seconds), sometimes it takes 8-10 seconds, sometimes up to 3-4 minutes, and on one occasion I didn't get a response at all. I checked by the TWS logs, the API logs, and my own system logs, and nothing. Anyone have any idea what might cause this??? Thank you, Yonatan |
fran
¿ªÔÆÌåÓýPossibly facing disconnection/reconnection? Explore a finite state machine to detect lost orders, resend when logic conditions are met. El 6 dic. 2023, a la(s) 13:54, yonatan@... escribi¨®:
|
For me the delay happened on race conditions when unloading the api message queue. Once I found a way to monitor it, I have noticed it could grow to thousands of unprocessed messages, which caused the delay. Make sure it's not get cluttered, I used semaphores etc. to make sure different threads do not race on the same queue. Now it never happens.
|
If your instance of IBapi() is called app like mine, in every thread I check app.msg_queue.qsize().?
Make sure to use semaphores, as the .qsize() reading itself can create race if a few threads try to do it all at once.
Here is how I do it:
?
with semaphore_apiq:
?
len_var = app.msg_queue.qsize()
?
log_msg = symbol + ' ### length of api msg_queue: ' + str(len_var)
?
logger.info(log_msg)
?
info_dict['api_qsize'] = str(len_var)
?
if len_var > max_len_var:
?
max_len_var = len_var
?
info_dict['max_api_qsize'] = str(max_len_var)
?
if len_var > 100: # delay to sort backlog
?
time.sleep(random())
?
In effect, if the queue grows to over 100 messages, the thread is pause for up to 1 sec letting callbacks work its course.
?
I am sure this is not the smartest solution, and I don't like it, but it works apparently. Maybe you or someone can come up with a better idea, please do share. |
In which function are you running this bit of code? Inside the EWrapper callbacks (ie tickSize, tickPrice etc)?
If you do time.sleep() on the api thread, wouldn't messages get lost when they are sent to you but not received? Btw my solution was to not do any handling of the messages received in the callbacks, but instead enqueue them to my own queue, then handle that queue at a later time in a different thread. |
to navigate to use esc to dismiss