Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to Construct a Filter from morningstar_industry_group_code?

Title says it, I'd like to construct a filter based off of the industry group codes much like you can with the Sector() classifier, and everything I have tried has resulted in various errors, and while my solutions can get increasingly convoluted, I'm sure there is an easy way to do this that I am overlooking.

Any help would be nice.

4 responses

One can use the 'element_of' method. Something like this.


# Make a list of the industry groups we want (or maybe exclude as the case may be)  
my_industries = [31054, 20639]

# Use the 'element_of' method to create a filter from this list  
my_industry_filter = industry_group.element_of(my_industries)

# Now run our pipeline with this filter as a screen  
filtered_pipe = Pipeline(  
    columns={  
        'economy_sphere' : economy_sphere,  
        'sector': sector,  
        'sector_built_in': sector_built_in,  
        'industry_group': industry_group,  
        'industry': industry,  
    },  
    screen = my_industry_filter  
)

See attached notebook. Hope that helps.

This is what I was trying before, the problem is that I am trying to filter individual factors, and I was trying to mask them with both the universe I already had defined and the industry I wanted to apply to it, so something kinda like:

somefactor = somefactor.latest.rank(mask=(universe & industry))

and it was throwing up an error acting as if the universe and the industry were not the same thing and could not be &ed. In retrospect I probably should have been able to figure this out but by taking out the universe part of it it now works alright, although I don't know why it would not have worked before.

So its all working now, Thanks for your time.

In the given example, how would one exclude the two given industry codes in the industry filter. I could not find a proper way to write the exclusion in the filter command

my_industries = [31054, 20639]

# How can we exclude items in the filter?  
my_industry_filter = industry_group.element_of(not in my_industries)  

One can exclude certain industries by using the "~" operator.

# Make a list of the industry groups we want to exclude  
my_industries = [31054, 20639]

# Use the 'element_of' method to create a filter from this list  
# Use the "~" operator to get the inverse of a filter  
all_industries_except_my_industries_filter = ~industry_group.element_of(my_industries)

That will return a filter with all industries EXCEPT 31054 and 20639.

Filters can be combined using the "&" and "|" operators. Say one wanted to only look at sectors 310 and 206 but exclude the specific industries 31054 and 20639. That could be done like this:


# Let's look at just a couple of sectors, namely 310 and 206, and make a filter for those sectors  
my_sectors = [310, 206]  
my_sectors_filter = sector.element_of(my_sectors)

# Now make a list of the industry groups we want to exclude  
my_industries = [31054, 20639]

# Use the 'element_of' method to create a filter from this list  
# Use the "~" operator to get the inverse of a filter  
all_industries_except_my_industries_filter = ~industry_group.element_of(my_industries)

# Now run our pipeline using both the sector and excluded industries filters as a screen  
filtered_pipe = Pipeline(  
    columns={  
        'economy_sphere' : economy_sphere,  
        'sector': sector,  
        'sector_built_in': sector_built_in,  
        'industry_group': industry_group,  
        'industry': industry,  
    },  
    screen = my_sectors_filter & all_industries_except_my_industries_filter  
)

Notice that the two filters are logically evaluated using the "&" operator. Good luck.

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.