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.