Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Apply top and bottom to QTradableUniverse

When I use top and bottom, this is applied to the entire universe (before QTradableUniverse Filter is applied), so this:

shorts = Returns(window_length=2).top(1)  
longs = Returns(window_length=2).bottom(1)

picks the top 1 and bottom 1 of the entire universe. Is it possible to change it such that it is applied to QTradableUniverse only?

The attached notebook illustrates how the top 1 and bottom 1 are not part of the QTradableUniverse and thus I get an empty dataframe (note: I do understand that I could just remove the QTradableUniverse filter and then I would get the top 1 and bottom 1 of the entire universe- but that is not what I want. I want the top 1 and bottom 1 of the QTradableUniverse).

3 responses

Also, I could obviously apply this screen after the creation of the pipeline, but I would like to do this inside of the pipeline so I can take this over to the Algorithm IDE.

The top and bottom methods have a mask parameter which allows one to limit the assets the method looks at. Here's the documentation https://www.quantopian.com/docs/api-reference/pipeline-api-reference#zipline.pipeline.Factor.top

So, the following would get the assets in QTradableStocksUS which have the highest and lowest 2 day returns

shorts = Returns(window_length=2).top(1, mask=QTradableStocksUS())  
longs = Returns(window_length=2).bottom(1, mask=QTradableStocksUS)

Most of the built in factors and methods allow a mask just for this purpose.

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.

Perfect, I did not think of that at all, despite seeing that in the tutorial. highly appreciate the help Dan!!