Python Forum
Having difficulty with threads and input()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Having difficulty with threads and input()
#10
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.

#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?
Reply


Messages In This Thread
RE: Having difficulty with threads and input() - by sawtooth500 - Jun-05-2024, 10:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Difficulty in adapting duplicates-filter in script ledgreve 5 1,081 Jul-17-2023, 03:46 PM
Last Post: ledgreve
  Difficulty with installation standenman 2 1,170 May-03-2023, 06:39 PM
Last Post: snippsat
  Difficulty with installation standenman 0 793 May-02-2023, 08:33 PM
Last Post: standenman
  Difficulty in understanding transpose with a tuple of axis numbers in 3-D new_to_python 0 1,624 Feb-11-2020, 06:03 AM
Last Post: new_to_python
  Difficulty installing Pycrypto KipCarter 4 13,261 Feb-10-2020, 07:54 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020