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
- Twsapi
- Messages
Search
Re: Avoiding time.sleep while waiting for openOrder / orderStatus callback after placeOrder
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: |
Re: Avoiding time.sleep while waiting for openOrder / orderStatus callback after placeOrder
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.