I am building a trading algo in Python. I read data from a broker API, which lets me subscribe to receive market data for various securities.
I am trying to find patterns in the price of options. Ideally, I would subscribe to thousands of options' live data, and try to detect my patterns there.
As it seems to be an I/O bound situation, since I'm waiting on trade events that happen sporadically, I went with threads: I create one thread per security and make it wait for the pattern to arise. I also create other auxiliary threads, like one other thread per market monitoring thread, that waits for a stopping event to stop the market monitoring thread...
Now that I have a few threads per monitored security, I realize with just a few option chains I hit RuntimeError: can't start new thread
. I used the answer to this question to compute that the limit of my setup was at 884 threads, which is way too low for the application I had in mind for my threads.
I'm quite surprised, I thought threads in Python were just an abstract concept to split CPU time. In that case, why can't I create an arbitrary number of threads at risk of having very little CPU time for each? If threads are not a clever choice for this application, is there a better one?