Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Top and bottom filter error!!

Hello,
I have tried to select top 10 long companies and top 5 short companies. However, when I write :

n_longs = (base_universe & ROA_long & ROE_long & PE_long & EPS_Growth_long & Current_R_long & debt_to_marketcap_long) | (base_universe & sector & dividentyield_long)
longs = n_longs.top(10)

I recieve the following error: AttributeError: 'NumExprFilter' object has no attribute 'top'
Please help.

3 responses

The error is an indication of what the problem is. 'NumExprFilter' object has no attribute 'top' means that a filter object doesn't have a 'top' method. The problem is in the two lines

longs = n_longs.top(10)  
shorts = n_shorts.bottom(5)

Both 'n_longs' and 'n_shorts' are filters. Basically they are collections of securities which pass all the constraints. They don't have any intrinsic order so the concept of 'top' or 'bottom' doesn't mean anything. One can only apply 'top' and 'bottom' to factors. Factors have values and therefore can be ordered by that value and return the highest (ie top) and lowest (ie bottom) securities. The result is a filter or collection of securities and NOT a factor.

So, the first step is to determine what factor one wishes to rank the results by. Does one want the highest and lowest ROE? ROA? PE_RATIO? Simply use that factor with a mask. Something like this (I randomly chose the latest close price)

    ranking_factor = USEquityPricing.close.latest  
    longs = ranking_factor.top(10, mask=n_longs)  
    shorts = ranking_factor.bottom(5, mask=n_shorts)

Note that the ranking_factor could also be a calculated value based upon several other factors.

    ranking_factor = factor1 + factor2 + (2*factor3)

Good luck.

Thank you for advice Dan,
But in my algo I'm trying to select, lets say top 10 companies that have a ROA > 0.15, not the top 10 companies with the biggest roa. How can I create a factor that does this?

Hi Daniel,
What do you mean by top 10 in this case?
Let's assume you've selected the companies with ROA > 0.15 and there are 25 such companies.
["Stock 1", "Stock 2", "Stock 3", ..., "Stock 25"] Which 10 should the algorithm pick?