I have an trading API that allows me to send/cancel/update orders. I have marketdata that I can use through another API that gives me orderbook data.

Now let's say I want to build a simple arbitrage strategy, for products X1 vs X2, Y1 vs Y2, and Z1 vs Z2.

X1 and X2 should have the same price, and if they differ more than 0.5% in price I want to trade on this difference.

My question is how to structure my python code, so that it can handle 100 of those pairs simultaneously and continuously. I find a lot of information on backtesting, but I want to trade this live and need to prevent any unnecessary loops.

  • 4
    $\begingroup$ I’m voting to close this question because this is a vague, too much IT / architecture oriented, question $\endgroup$ – lehalle May 10 '20 at 9:01
  • $\begingroup$ Could you please be more specific? Perhaps show some code you have already written instead of asking where to take the first bite out of a whale. $\endgroup$ – Hamish Gibson May 10 '20 at 9:04
  • $\begingroup$ Sorry don't know how to properly phrase it, but the question is mainly how to get the different instruments being read in simultaneously while also trading it at the same time. So it is purely about the architecture of the code. I already have all the function for reading the data and deciding whether or not to trade. But if I go over the instruments one by one this is probably not the best method. I think it is called multithreading what I need. $\endgroup$ – Cindy88 May 10 '20 at 9:47
  • $\begingroup$ Yes. Multi-threading or event-driven programming would be of interest to you, I would think. (But this probably not a good place to discuss it, sorry). $\endgroup$ – noob2 May 10 '20 at 10:19

This is indeed so broad people can write books about this and not a good fit for this site. An introductory book has been written and you could give it a try.


Use C, and if you need math functions, use OpenBLAS. That will run 100-1000 times faster than Python. Then you can use one simple loop for your calculations. Pipe that output into your trading API code running in a separate process.


Not the answer you're looking for? Browse other questions tagged or ask your own question.