¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io

Avoiding time.sleep while waiting for openOrder / orderStatus callback after placeOrder


 

All code?examples I can see are like this:

placeOrder(orderId, contract, order)
time.sleep(10)
Is there a better way of doing this? Maybe using?threading.Lock() with release() in callback functions? Looking for an example. TIA.
?


fran
 

¿ªÔÆÌåÓý


Check this out.

El 8 oct. 2023, a la(s) 11:02, Lipp F. <flipp31a@...> escribi¨®:

?All code?examples I can see are like this:

placeOrder(orderId, contract, order)
time.sleep(10)
Is there a better way of doing this? Maybe using?threading.Lock() with release() in callback functions? Looking for an example. TIA.
?


 

The issue you are having is an old one in programming.? You have two basic choices: polling and interrupting.? Polling means you check a status, usually in a loop with a sleep or yield call.? Interrupting means you wait on an event from the server, usually received as a callback in IB's API.? The interrupting is better but harder to write.? You usually have to add extra logic to implement a timeout in case the server doesn't respond at all.

For what you are doing, polling is likely far better unless you are a pretty good professional programmer with years of experience.? Just wrap a loop around your logic and you are there.? Probably best to make the loop run at most a set number of times.? The max total wait time will be the number of times the loop can run times the length of the sleep.? You probably want to make the max total wait time to be about 15 seconds (5sec to a minute is reasonable).? I hope this helps.

Hunter

On Sunday, October 8, 2023 at 07:21:29 AM PDT, fran <fchiesadoc@...> wrote:



Check this out.

El 8 oct. 2023, a la(s) 11:02, Lipp F. <flipp31a@...> escribi¨®:

?All code?examples I can see are like this:

placeOrder(orderId, contract, order)
time.sleep(10)
Is there a better way of doing this? Maybe using?threading.Lock() with release() in callback functions? Looking for an example. TIA.
?


 

Python if it's python your using - threading.event() module. Can assign different threads to events?

as eg:?

import threading

# event threads
event = threading.Event()
hist_event = threading.Event()
open_order_event = threading.Event()

in the call back functions:?

you may hist_event.set()

? ? def historicalDataEnd(self, reqId, start, end):
? ? ? ? super().historicalDataEnd(reqId, start, end)
? ? ? ? print("HistoricalDataEnd. ReqId:", reqId, "from", start, "to", end)
? ? ? ? hist_event.set()

hist_event.clear()
histData(9, FUTcontract(symbol), '3 D', '1 min')
hist_event.wait()

so you could do something in the ordercall back function i believe.....

This is what I do at least seems to work pretty well.?
I run main() in its own thread which is all my strategy logic which is a while loop?
and the TradeApp() in its own thread?

like this:

def websocket_con(symbol):
? ? app.run()

app = TradeApp()
app.connect(host='127.0.0.1', port=7497,
? ? ? ? ? ? clientId=3)? # port 4002 for ib gateway paper trading/7497 for TWS paper trading
con_thread = threading.Thread(target=websocket_con, args=(symbol,))? # , daemon=True
con_thread.start()
time.sleep(5)? # some lag added to ensure that streaming has started

MainThread = threading.Thread(target=main, args=(app,))
MainThread.start()

On Sun, Oct 8, 2023, 9:02 AM Lipp F. <flipp31a@...> wrote:
All code?examples I can see are like this:

placeOrder(orderId, contract, order)
time.sleep(10)
Is there a better way of doing this? Maybe using?threading.Lock() with release() in callback functions? Looking for an example. TIA.
?


 

In the interest of helping, Andrew's suggestion is for an interrupt style.? It also has a synchronization bug in it (the blocks of code that reads/writes the flag needs to be in a synch block, them being thread-safe isn't enough because the logic depends on multiple accesses).? And that's my point about doing this in an interrupt style way, its really (really really really) hard to get right.? As in most professional programmers don't attempt this stuff and instead use frameworks that don't require threading or give this type of work to a specialist (read old and expensive) programmer in most situations.? Just do a while loop as I suggested and if you don't want the blocking behavior, launch a thread to send the request and wait on the response (but you probably don't need to do that).? IBApi is really designed to be used in a single-threaded design.

Hunter

On Sunday, October 8, 2023 at 03:33:36 PM PDT, Andrew Bannerman <bannerman1985@...> wrote:


Python if it's python your using - threading.event() module. Can assign different threads to events?

as eg:?

import threading

# event threads
event = threading.Event()
hist_event = threading.Event()
open_order_event = threading.Event()

in the call back functions:?

you may hist_event.set()

? ? def historicalDataEnd(self, reqId, start, end):
? ? ? ? super().historicalDataEnd(reqId, start, end)
? ? ? ? print("HistoricalDataEnd. ReqId:", reqId, "from", start, "to", end)
? ? ? ? hist_event.set()

hist_event.clear()
histData(9, FUTcontract(symbol), '3 D', '1 min')
hist_event.wait()

so you could do something in the ordercall back function i believe.....

This is what I do at least seems to work pretty well.?
I run main() in its own thread which is all my strategy logic which is a while loop?
and the TradeApp() in its own thread?

like this:

def websocket_con(symbol):
? ? app.run()

app = TradeApp()
app.connect(host='127.0.0.1', port=7497,
? ? ? ? ? ? clientId=3)? # port 4002 for ib gateway paper trading/7497 for TWS paper trading
con_thread = threading.Thread(target=websocket_con, args=(symbol,))? # , daemon=True
con_thread.start()
time.sleep(5)? # some lag added to ensure that streaming has started

MainThread = threading.Thread(target=main, args=(app,))
MainThread.start()

On Sun, Oct 8, 2023, 9:02 AM Lipp F. <flipp31a@...> wrote:
All code?examples I can see are like this:

placeOrder(orderId, contract, order)
time.sleep(10)
Is there a better way of doing this? Maybe using?threading.Lock() with release() in callback functions? Looking for an example. TIA.
?


 

threading.Event is a method to communicate between threads. I find it useful when waiting for a server to respond versus time.sleep() - when we get a server response the flag on the response is .set() (true) and the other threads waiting for it are released.?





On Sun, Oct 8, 2023 at 6:50?PM Hunter C Payne via <hunterpayne2001=[email protected]> wrote:
In the interest of helping, Andrew's suggestion is for an interrupt style.? It also has a synchronization bug in it (the blocks of code that reads/writes the flag needs to be in a synch block, them being thread-safe isn't enough because the logic depends on multiple accesses).? And that's my point about doing this in an interrupt style way, its really (really really really) hard to get right.? As in most professional programmers don't attempt this stuff and instead use frameworks that don't require threading or give this type of work to a specialist (read old and expensive) programmer in most situations.? Just do a while loop as I suggested and if you don't want the blocking behavior, launch a thread to send the request and wait on the response (but you probably don't need to do that).? IBApi is really designed to be used in a single-threaded design.

Hunter

On Sunday, October 8, 2023 at 03:33:36 PM PDT, Andrew Bannerman <bannerman1985@...> wrote:


Python if it's python your using - threading.event() module. Can assign different threads to events?

as eg:?

import threading

# event threads
event = threading.Event()
hist_event = threading.Event()
open_order_event = threading.Event()

in the call back functions:?

you may hist_event.set()

? ? def historicalDataEnd(self, reqId, start, end):
? ? ? ? super().historicalDataEnd(reqId, start, end)
? ? ? ? print("HistoricalDataEnd. ReqId:", reqId, "from", start, "to", end)
? ? ? ? hist_event.set()

hist_event.clear()
histData(9, FUTcontract(symbol), '3 D', '1 min')
hist_event.wait()

so you could do something in the ordercall back function i believe.....

This is what I do at least seems to work pretty well.?
I run main() in its own thread which is all my strategy logic which is a while loop?
and the TradeApp() in its own thread?

like this:

def websocket_con(symbol):
? ? app.run()

app = TradeApp()
app.connect(host='127.0.0.1', port=7497,
? ? ? ? ? ? clientId=3)? # port 4002 for ib gateway paper trading/7497 for TWS paper trading
con_thread = threading.Thread(target=websocket_con, args=(symbol,))? # , daemon=True
con_thread.start()
time.sleep(5)? # some lag added to ensure that streaming has started

MainThread = threading.Thread(target=main, args=(app,))
MainThread.start()

On Sun, Oct 8, 2023, 9:02 AM Lipp F. <flipp31a@...> wrote:
All code?examples I can see are like this:

placeOrder(orderId, contract, order)
time.sleep(10)
Is there a better way of doing this? Maybe using?threading.Lock() with release() in callback functions? Looking for an example. TIA.
?


 

None of that invalidates anything I said about the code you posted.

Hunter


On Sunday, October 8, 2023 at 06:42:57 PM PDT, Andrew Bannerman <bannerman1985@...> wrote:


threading.Event is a method to communicate between threads. I find it useful when waiting for a server to respond versus time.sleep() - when we get a server response the flag on the response is .set() (true) and the other threads waiting for it are released.?





On Sun, Oct 8, 2023 at 6:50?PM Hunter C Payne via <hunterpayne2001=[email protected]> wrote:
In the interest of helping, Andrew's suggestion is for an interrupt style.? It also has a synchronization bug in it (the blocks of code that reads/writes the flag needs to be in a synch block, them being thread-safe isn't enough because the logic depends on multiple accesses).? And that's my point about doing this in an interrupt style way, its really (really really really) hard to get right.? As in most professional programmers don't attempt this stuff and instead use frameworks that don't require threading or give this type of work to a specialist (read old and expensive) programmer in most situations.? Just do a while loop as I suggested and if you don't want the blocking behavior, launch a thread to send the request and wait on the response (but you probably don't need to do that).? IBApi is really designed to be used in a single-threaded design.

Hunter

On Sunday, October 8, 2023 at 03:33:36 PM PDT, Andrew Bannerman <bannerman1985@...> wrote:


Python if it's python your using - threading.event() module. Can assign different threads to events?

as eg:?

import threading

# event threads
event = threading.Event()
hist_event = threading.Event()
open_order_event = threading.Event()

in the call back functions:?

you may hist_event.set()

? ? def historicalDataEnd(self, reqId, start, end):
? ? ? ? super().historicalDataEnd(reqId, start, end)
? ? ? ? print("HistoricalDataEnd. ReqId:", reqId, "from", start, "to", end)
? ? ? ? hist_event.set()

hist_event.clear()
histData(9, FUTcontract(symbol), '3 D', '1 min')
hist_event.wait()

so you could do something in the ordercall back function i believe.....

This is what I do at least seems to work pretty well.?
I run main() in its own thread which is all my strategy logic which is a while loop?
and the TradeApp() in its own thread?

like this:

def websocket_con(symbol):
? ? app.run()

app = TradeApp()
app.connect(host='127.0.0.1', port=7497,
? ? ? ? ? ? clientId=3)? # port 4002 for ib gateway paper trading/7497 for TWS paper trading
con_thread = threading.Thread(target=websocket_con, args=(symbol,))? # , daemon=True
con_thread.start()
time.sleep(5)? # some lag added to ensure that streaming has started

MainThread = threading.Thread(target=main, args=(app,))
MainThread.start()

On Sun, Oct 8, 2023, 9:02 AM Lipp F. <flipp31a@...> wrote:
All code?examples I can see are like this:

placeOrder(orderId, contract, order)
time.sleep(10)
Is there a better way of doing this? Maybe using?threading.Lock() with release() in callback functions? Looking for an example. TIA.
?