Am I looking for multiprocessing?

Hey guys. I am super stoked, I went from 0 coding experience 2 weeks ago to downloading python and building a program that takes a list of tickers, ensures the market is open, checks historical and current pricing for a ticker, places a buy order based off conditions, gets the fill price of that order, and calculates and submits a sell order or cancels the single buy order if it doesnt fill within 10 seconds. I ran 30 tickers this morning, giving each order it places a max of $5000 worth of shares… it placed 4 buy orders, cancelled 2, and placed sell orders for the other 2. #quarantine.
anyways… i say all of this because it took pricing data and placed buy orders 1 ticker by 1 ticker by 1 ticker and then tried to place sell orders 1 by 1 as well.
I want this program to not be so linear. I want it to run tickers through simultaneously if possible. if its checking for an order to be filled in that 10sec window, i want it to be checking for other orders and placing sell orders. I have read some on multiprocessing and multithreading and async io and im not exactly sure which I need to make my program do as described.
What exactly am I looking for here?
Brian D.

1 Like

bump. can anyone help out?

                                              list of tickers
                                           /         !       !       \
                                          /          !        !        \
                              for loop   for loop for loop  for loop
                                 func           func    func      func
                                 buy             buy      buy       buy

is this best ran with async io, multiprocessing, multithreading, concurrent.futures?

Hi Brian - You might want to first dig into some reading on Parallel and Distributed processing. Here’s an example article.

In simplistic terms, instead of thinking about Parallel processing, you might want to consider how you would break up your program and functions into their own ‘programs’ that could run on their own. For example, maybe you have one program that just looks for BUY signals. A separately running program then looks for SELL signals. You could then run these on 2 different computers (or start them independently on the same computer) thereby ‘doubling’ your throughput. Then breaking it down further, and instantiating even more separate programs, you could theoretically gain performance. (ex: maybe a BUY program that only looks for Tickers that start with “A”, “B”, etc.).

The first limitation from a home computer setup will be your computer(s).
Each separate program you start will take x-amount of RAM and CPU.
You could overcome that by getting some space on AWS (Amazon Web Services) and have infinite number of CPUs (for a $$ of course). And then you’d need some kind of job scheduler to keep track of all of the separate programs… so it could get pretty deep quickly.

The other limitation is the database or wherever you’re storing the data. Even though you may have 10 different programs doing their own little thing, they have to report back to a central DB presumably to keep track of everything. And if you’re doing this within seconds, you might start seeing connection and read/write availability conflicts in the DB. This can all be overcome - but it would take some work.

Good luck!

Thanks for the response. This is definitely diving a little deeper than I first thought. My program is so simple,
if open < prev close
then sell at $1 above fill price.
not really a profitable strategy, just trying to get my feet wet.
And i dont know how to run tickers through simultaneously.
With your knowledge on distributed processing, do you think this simple of an algorithm is worth trying to implement distributed processing?

I’m certainly no expert Brian… But since you asked, I’ll give you my 2 cents.

In my opinion, the strategy is more important than the frequency. In other words, if a strategy is sound, it should work trading on the 1Min bars, or daily on the 1D bars, or weekly, monthly, etc. There’s some caveats in that, and I’m sure others could come up with good arguments (because a lot can happen during off hours, etc., etc., …)

So my suggestion, don’t worry about trying to make the $Million bucks in a day , try to come up with a strategy that can work daily or at some slower pace. If it works daily, then it ‘should’ work on the minute or in more real-time. Once you find something that looks like it might be working, you could certainly check into more rapid daily trading.

Just a thought…

I def understand that. Ive got a few years of trading experience… patterns work on every timeframe. Its gorgeous! I wrote a post on reddit to grab some input as well, I added a little more there than I did here so maybe this will help build a little more to what I am trying to do, or maybe its redundant. I appreciate all your help.

Hey guys. Successful scalper and day trader here. I have very little coding experience. However, I have built and ran a successful and profitable python program using Alpacas paper API. I need a little help though… This is a dumb example but concepts would still be applicable

If market is open
For ticker in tickers:
Get prev close price
Get tick open price
If open < close
Buy order
While loop 10 times with 1 sec wait
Check to see if order is filled.
If it is, return fill price
If not, cancel buy order
calc sell price
Sell order

I want to run, say 100 tickers thru once each but I want it concurrent. My bottle neck is the while loop and also the program being synchronous in general. What should I do here… multiprocessing, threading, asyncio, some other type of answer? I’ve read into the 3 listed and I cant figure out which I need to put effort into and learn and apply Thank you for your help.

Did you check out this blog post yet? It explains how python asyncio helps something like what you want to do.

Hey hitoshi. I did take a look at that. It’s amazing. I’ve tried to decipher what each thing means and does and I have a general idea of how it works but dont know how to apply those concepts to mine. That one continuously scalps all day which may be useful for me in the future, but for now, I am just placing one order to buy and one order to sell for each ticker and I want it to do that concurrently for the tickers. I feel like it’s probably 3 or 4 lines of simple code, I just havent figured it out yet haha

Hi Brian,

I am going to contradict everything that has been said in this thread so far and say that they are all wrong. I also saw your post on reddit the other day but this place is a better for a reply.

Here are my comments on the limited information you provided:

  1. if you have a limited set of symbols (lets say under 100) you can simply run it in a for loop that sleeps for a second at the end before running again.
  2. once you made #1 work and you find the need to expand to 1000+ symbols you might want to consider switching to the stream api. This will create the multiprocessing you desire and it will be done some what for free. You will need to do more research on how it works but you will be on the right track, just make sure to have good logic separation into functions.
  3. i don’t know why you are not submitting market orders for your BUY orders but continue to check fills. If your strategy is that sensitive to price movement i think you are going to run into serious limitations with any retail piece of software.
    4 what @procasky said is completely wrong, different strategies work at different time frames for various reasons. What you want is a robust or specific strategy. What i mean by that is that you either have a robust strategy that works at the same timeframe for different set of instruments (like for equities) or a specific strategy that works for a specific set of instruments (for example like FEDEX and UPS since they are probably highly correlated)
  4. his other comment regarding computing power is more forward thinking in case you want to make this really really hands free but crawl, walk, run. Your home computer is perfectly fine to handle this.

Finally, i saw your code and its logic is slightly inefficient here is some better sudo code:

if market is open
   For ticker in tickers:
      Get prev close price
      if not hasBuyOrder:
          Get tick open price
          if open < close :
              submit BUY order for ticker
              hasBuyOrder = true
          if not fillPrice:
              fillPrice = getFillPrice(ticker)
          else if not submittedSellOrder:
              calculate sell price
              submit sell order
              submittedSellOrder = true
              getFillPrice(ticker) <- for sell order
              hasBuyOrder = false
              fillPrice = 0
              submittedSellOrder = false
   sleep 1 second

this allows you to check all your symbols once every second instead of dwelling on a single symbol for 10 seconds.

hope it helps!

1 Like

hey man. thanks for the reply. I wound up using Asyncio and im running about 400 tickers through. so far so good. we will see how it fairs out long term.

Anyone doing a program with multi-threading?

My program needs to take in every single trade + minute bar of stock(s) and begin doing concurrent calculations.

Since independent incoming trades and incoming minute bars (via WebSocket) both may occur at the exact same time, I began to use threads. The fact that polygon sometimes ‘merges’ messages together, which I then have to ‘unmerge’ in real time without affecting the realtime flow of the program also led me to use threads. So I now have a multi-threaded program (I think this is the right definition).

Is there anything wrong with this approach? I took a look at the SDK after creating everything from scratch, and noticed it uses asyncio. I am wondering if using threads for this sort of high-frequency realtime strategy is not the way to go about it.

Just for clarity sake @SharpeTrader, what I said was not completely wrong… I was speaking in a general sense about high level strategies holistically in regards to timeframe. Yes, different instruments with different intraday volatility (and other reasons) may work better/worse with different timeframes. Of course. But that was not the gist of my comment to Brian.

I was expressing my opinion as an option to get started. A lot of experimentation and tuning will be required depending on the specifics of the list of equities.

@procasky, i see where you were coming from and i agree.