Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Algorithm that runs for multiple stocks

Hello all,
I am new to Q.

My goal - I want to implement an algorithm that reads form a large set of stocks and execute my strategy on all of them.

E.g
Say my strategy is to buy when an any stock SMA 20 cuts above SMA 200 & sell when SME 200 cuts above SMA 20 (something like that)
I want to run an algorithm that watches all the stocks on Russel 2000 (for an example). Or 500 stocks that I have specifically selected.
So, the algo will keep running across all the stocks in my scope and keep looking for the ones that meets my buy criteria. Today it may buy 10 stocks, tomorrow it may be 20 more. Also later today it may sell 2 of them and tomorrow it may sell nothing...

Hope this make sense.

Can someone help me how to go about this?

Much appreciate it

5 responses

First off, Welcome to Quantopian.

What you are asking is exactly what a typical algorithm does. One would first define the 'universe' of securities to trade within. This is typically decided by constraints on liquidity, type of security (ie only stocks and no ETFs), or some knowledge that a strategy performs well only on a specific group of securities (eg only works well on a specific sector). One can also set specific securities (eg AAPL IBM) but this has the inherent drawback of potential lookahead bias (why choose AAPL? Would we have chosen that stock 10 years ago when we start our backtest?) The 'universe' is typically defined in the pipeline and then set as a screen on the pipeline so only securities in the universe are returned.

The pipeline pulls together all the data one needs for their strategy. Rules can then be developed which use this data to determine what to buy and sell. One task which wasn't alluded to in the original post was how much to buy. Determining the weighting of stocks can be quite complex. If today we buy 10 stocks and tomorrow we buy 20 stocks, what should the algo do if there isn't enough money left in the portfolio to buy those stocks. The Quantopian platform will let you 'borrow' as much money as you wish. However, that won't match real life. The logic should rebalance existing positions to free up money for these new stocks or, alternately, cap the new stocks to only buy when there is a positive account balance.

Attached is a simple algo which is a start to what you are looking to do. Do take a look at the tutorials (https://www.quantopian.com/tutorials) especially part 2 on pipeline. That may give some better direction.

Good luck.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

The algo in this thread is doing exactly what you propose. you will have to manually insert all sid.

https://www.quantopian.com/posts/zero-commission-algorithmic-trading-robinhood-and-quantopian

Hi Dan,
Thank you very much for the details. This really helps.

Yes, the 'how much to buy' also was in my mind but I did forgot to put it out there. Thanks for highlighting that. I think I also will have to consider settlement time of money (typical 2 days?) unless there is option to have margin accounts with Q).

Ioannis- thanks for sharing the link as well.

Appreciate the quick response from all.

All the best with what you do!

All accounts on Quantopian are considered margin accounts. Moreover, you aren't given any specific limit on margin. You can borrow as much as you want. Simply keep buying stock. The cash in the account will go negative which implies the amount which was borrowed (shorting stock is a bit trickier in that calculation). Best practice is to have the algo limit the 'leverage' to what one expects for real trading (in most cases 1).

Thank you Dan.