Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Isolate Midcaps in Pipeline

Hi All,

Noobie question. I want to sort by all market caps in pipeline that rank between 500-1000 (i.e. find midcaps). How would I go about changing the below code which is looking at top500 to do this?

Thanks

def make_pipeline(context,sma_window_length, market_cap_limit):  
    pipe = Pipeline()  
    market_cap = MarketCap()  
# Want to change the below from rather than top 500 to rank between 500th-1000th  
    top_N_market_cap = market_cap.top(market_cap_limit)

    initial_screen = (top_N_market_cap)  
1 response

You could use the 'percentile_between' filter. (See documentation under factors https://www.quantopian.com/help#quantopian_pipeline_factors_Factor )

# Create a filter to get tradeable stocks (excludes etfs etc)  
tradeable_stocks = QTradableStocksUS()


# Create a factor to get the latest market cap. Limit to tradeable stocks with a mask.  
mkt_cap = MarketCap(mask = tradeable_stocks)

# Create a filter to select mid cap stocks  
# Note that this is a percentage and not a fixed value (eg $250M)  
# Let's use the Morningstar definition of 'mid cap' which is stocks that fall  
# between 70th and 90th percentile in market capitalization of the investable universe  
# see http://www.morningstar.com/InvGlossary/morningstar-mid-cap-index.aspx  
mid_cap = mkt_cap.percentile_between(70, 90, mask = tradeable_stocks)

# Now simply use that filter as a screen  
initial_screen = mid_cap  

See the attached notebook with this in action. It also shows the range of market caps being filtered to validate it falls within expectations.

Good luck.