Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Looping to compare all stocks to pick highest gainer

So I'm brand new to Quantopian and so far all the algorithms I have seen have defined the stock to be traded in the initialize functions. This seemed really odd to me, because wouldn't you want to identify macro trends, find out what stocks are exemplifying those trends and then trade those stocks instead of just picking a stock to begin with and running your algorithm on just the one stock?

Is there a way to loop through and compare all available stocks, find stocks that meet certain criteria, then initialize those stocks, then run your algorithm?

For example is it possible to write an algorithm that

  1. compares all stocks after 5 minutes of market open
  2. singles out the top gaining stock for those first 5 minutes
  3. initializes that stock
  4. buys x dollars of that stock at market price
  5. sets a 0.25% trailing stop loss
  6. sells stock at 4:15 if stop loss isn't met

Under this model, your algorithm would be trading a different stock each day.

Maybe it's because I just started exploring Quantopian, but I haven't seen this methodology so far. Why is that? If I'm wrong, could you point me to a sample algorithm that uses this logic of comparing all stocks at market open or close to determine which stock should initialize?

5 responses

Hello Matthew,

Seems doable. I don't have time now to work on an example, but you might have a look at the set_universe functionality on the help page. You can access the largest number of securities for analysis with it. Otherwise, you can provide a fixed list of up to 100 securities to analyze.

Grant

Hi there

Actually as far as I can tell, DollarVolumeUniverse is the only function that can be used with set_universe? I can't seem to find anything other function for some other way of creating the universe, e.g. top 10 stocks by market cap, or top 10 stocks by market cap as of date XXX.

Mathew,

Here's an algorithm that goes through all the stocks at the beginning of day, picks a winner within the first five minutes, and sells it all at the end of day if it hasn't passed the stop-loss limit. I've set the algo to only purchase 100 shares at a time so playing around with the stop-loss limit and the number of shares should provide you with different results.

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.

Hello Dominic,

I'm not aware of screening tools on Quantopian other than using set_universe as described on the help page. I haven't tried it, but you can import using fetcher (see the help page), so perhaps you could load in market cap data by security.

Grant

Thanks that's a good idea, I will explore this more!