¿ªÔÆÌåÓý

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

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


 

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.
?

Join [email protected] to automatically receive all group messages.