Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Conditional Sorting

I am new to coding and am trying to write an algorithm that does multiple conditional sorts on a universe of stocks. First, I want to filter out all stocks with liquidity below a threshold, stocks with avg price below $5 and then take the top 3000 by market cap of the remaining stocks as my universe. I have figured out how to do that part already.

Next, I want do several conditional sorts on that universe of the 3000 remaining stocks using various factors. This is the part I cannot figure out. For example, I want to sort the 3000 stocks by PE ratio, and take the top 1000. Then I want to sort that list of 1000 stocks by 12 month momentum, and take the highest 333. Then I want to take that list of 333 stocks and sort by ROE, and take the highest 111. Then I want to go long the top 10 stocks in the list of the 111 stocks. Any help would be greatly appreciated.

3 responses

Scratch this question, I have been told that this cannot be done in Quantopian. Looks like I am stuck with Excel and R for the time being.

@kirk I'd be interested to hear what part of this you were told isn't possible on Quantopian. As far as I can see, all of these steps could be done via the Pipeline API.

I've attached a notebook that shows how you'd do a multi-layered filter in the style you describe. The essential point is that you can use the .top() method of any Factor to get a filter returning the top N stocks ranked by that factor each day, and you can pass another filter as a mask to cause top to compute those ranks ignoring any stocks that were filtered out by the mask.

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.

thanks for the info, exactly what I was looking for. Ironically, the same the person who told me about Quantopian.com told me that he didn't think the conditional sorting part was possible with only one algorithm. Apparently he was wrong. Thanks again for the info.