Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Alphabet and Berkshire Appearing Twice in Top 10 Mkt Cap Filter

I'm trying to make a pipeline that gives me 10 stocks with the highest market cap. However, I noticed that Alphabet and Berkshire Hathaway were listed twice in the initial run because of their class shares. Is there anyway to make only one of them appear for each company?

primary_share = IsPrimaryShare() #Doesn't seem to work  

Sorry if it may seem like a dumb question. This is my first post. Thanks a lot in advance!

-Lee

2 responses

The problem was I forgot to mask the filter. At least this was a good reminder.

top_10_marketcap = morningstar_marketcap.top(10, mask=tradeable_stocks)  
#Masks can be also be applied to methods that return filters like top, bottom, and percentile_between  

Thank you for this, I was totally unaware of tradeable_stocks mask!

edit: ... and it seems it's not in-built quantopian mask after all, here is tradeable stocks from notebook if anyone else wonders what it is:

    # Filter for primary share equities. IsPrimaryShare is a built-in filter.  
    primary_share = IsPrimaryShare()

    # Equities listed as common stock (as opposed to, say, preferred stock).  
    # 'ST00000001' indicates common stock.  
    common_stock = morningstar.share_class_reference.security_type.latest.eq('ST00000001')

    # Non-depositary receipts. Recall that the ~ operator inverts filters,  
    # turning Trues into Falses and vice versa  
    not_depositary = ~morningstar.share_class_reference.is_depositary_receipt.latest

    # Equities not trading over-the-counter.  
    not_otc = ~morningstar.share_class_reference.exchange_id.latest.startswith('OTC')

    # Not when-issued equities.  
    not_wi = ~morningstar.share_class_reference.symbol.latest.endswith('.WI')

    # Equities without LP in their name, .matches does a match using a regular  
    # expression  
    not_lp_name = ~morningstar.company_reference.standard_name.latest.matches('.* L[. ]?P.?$')

    # Equities with a null value in the limited_partnership Morningstar  
    # fundamental field.  
    not_lp_balance_sheet = morningstar.balance_sheet.limited_partnership.latest.isnull()

    # Equities whose most recent Morningstar market cap is not null have  
    # fundamental data and therefore are not ETFs.  
    have_market_cap = morningstar.valuation.market_cap.latest.notnull()

    # Filter for stocks that pass all of our previous filters.  
    tradeable_stocks = (  
        primary_share  
        & common_stock  
        & not_depositary  
        & not_otc  
        & not_wi  
        & not_lp_name  
        & not_lp_balance_sheet  
        & have_market_cap  
    )