Jun-05-2024, 10:22 PM
ctrl+c isn't practical - so this is a script that sends orders to a brokerage for stock execution - it connects and streams multiple different API services to receive real time market data, position data, submit orders and get order status.
I really need a graceful exit because upon exit I need to check if there are any unfilled, or partially filled orders, cancel them, and close out of all open stock positions. Then exit the script. That's what my function handle_input does.
I also really want a way to manually terminate the script in case I want to stop it early. I could use ctrl+c, but then I need to use the broker's desktop software to go into the trading account and manually clean up all open orders and close out open positions.
I need an auto shutdown in two cases - at 4 PM eastern, when the market closes, and when an order comes back with a "rejected" status... which theoretically should never happen but if it does and the script keeps running the subsequent behavior can become super weird, which could cost me real $$$ so I need to close out positions and stop trading until I manually figure out what happened and restart it.
This I am fascinated by... how can I connect to my script? If I can send an external signal to it to start termination activities that could work instead of using an input()... or another idea I had is to just have an empty text file, have the script check the file every second and if I save 'x' to the text file that initiates termination activities instead of input()... thoughts?
I really need a graceful exit because upon exit I need to check if there are any unfilled, or partially filled orders, cancel them, and close out of all open stock positions. Then exit the script. That's what my function handle_input does.
I also really want a way to manually terminate the script in case I want to stop it early. I could use ctrl+c, but then I need to use the broker's desktop software to go into the trading account and manually clean up all open orders and close out open positions.
#This function is used to hard kill a thread def hard_kill(tid): if not isinstance(tid, int): tid = tid.ident res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(SystemExit)) if res == 0: raise ValueError("Invalid thread id") elif res != 1: # If it returns a number greater than one, it's a problem. ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, 0) raise SystemError("PyThreadState_SetAsyncExc failed")I tried using the above to hard kill the thread, and then perform cleanup actions again, but it doesn't seem to terminate the script fully... I guess as long as it performs clean up actions and partially terminates the script (to the point where it won't send new orders) I don't care if it just keeps running.... The point is I can't have it send new orders or not close out existing positions because that could cost me a lot of real $$$... I can't test the above function robustly until tomorrow as even on a sim trading account I can't really simulate things accurately outside of market hours and the market is closed right now.
I need an auto shutdown in two cases - at 4 PM eastern, when the market closes, and when an order comes back with a "rejected" status... which theoretically should never happen but if it does and the script keeps running the subsequent behavior can become super weird, which could cost me real $$$ so I need to close out positions and stop trading until I manually figure out what happened and restart it.
Quote:If your script was a server, you could interrupt it by connecting to it. This is multiplatform.
This I am fascinated by... how can I connect to my script? If I can send an external signal to it to start termination activities that could work instead of using an input()... or another idea I had is to just have an empty text file, have the script check the file every second and if I save 'x' to the text file that initiates termination activities instead of input()... thoughts?