Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to grab just the top 10, or top 100 stocks within a percentile?

I have an algo that calculates the top 10th percentile by calculating the fundamental market cap / book value. The I'm using is:

pf['cap_book'] = (now_fund.market_cap / now_fund.tangible_book_value).apply(lambda x: np.log(x))  
cap_book_5 = np.percentile(pf.cap_book, 10)  

Next I use:
pf = pf[pf.cap_book < cap_book_5] To select those stocks that are within the top 10th percentile. What I want to do is filter this more. If there is 100 stocks that fall into the top 10th percentile, I want to just select the top 10. Essentially if the calculated cap_book is < top 10 of the 10th percentile. What is the easiest way to do that?

1 response

Try this. Might need to remove the np & reference the factor object you are using

cap_book_5 = np.bottom(10)  

Some documentation in line with text here - http://www.zipline.io/_modules/zipline/pipeline/factors/factor.html