Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Incorporate Sector Data into Pipeline

Hi All,

Could someone please help me filter out sectors from my list of tradable securities?

Create list of all tradable sectors

sectors = {  
   102.0: "Consumer Cyclical",  
   205.0: "Consumer Defensive",  
   206.0: "Healthcare",  
   308.0: "Communication Services",  
   310.0: "Industrials",  
   311.0: "Technology"  
   }  

# Create list of all criteria for securities worth investing in  
tradable_securities = {  
    'top_500':top_500,  
    'not_wi':not_wi,  
    'primary_share':primary_share,  
    'common_stock':common_stock,  
    'not_otc':not_otc  
    }  

pipe_screen = (high_dollar_volume & sectors)  

# Create, register and name a pipeline  
pipe = Pipeline(columns=tradable_securities, screen=pipe_screen)  

return pipe

Above is the list of sectors I wish to trade but I'm not sure how to do this via pipeline.

Thanks,

Rohit

3 responses

Use the 'element_of' method. This works with classifiers only, but sector is a classifier (not technically a factor). See https://www.quantopian.com/help#quantopian_pipeline_classifiers_Classifier. Something like this.

from quantopian.pipeline.classifiers.morningstar import Sector

my_sectors = [  
   102, #"Consumer Cyclical"  
   205, #"Consumer Defensive"  
   206, #"Healthcare"  
   308, #"Communication Services"  
   310, #"Industrials"  
   311, #"Technology"  
    ] 

sector_filter = Sector().element_of(my_sectors)

# Create list of all criteria for securities worth investing in  
tradable_securities = {  
    'top_500':top_500,  
    'not_wi':not_wi,  
    'primary_share':primary_share,  
    'common_stock':common_stock,  
    'not_otc':not_otc  
    }  

pipe_screen = (high_dollar_volume & sector_filter)  

# Create, register and name a pipeline  
pipe = Pipeline(columns=tradable_securities, screen=pipe_screen)

return pipe

Hi Dan,

Thanks for your input. However, when I tried to run that piece of code this is the error i received:

TypeError: Found non-int in choices for Sector.element_of.
Supplied choices were frozenset([308.0, 310.0, 102.0, 311.0, 205.0, 206.0]).

Is there a way to fix this?

Thanks,

Rohit

Don't use 308.0 etc. Use 308. Exactly as I have in the code above. The error you are getting is that classifiers expect integers as values and not real numbers.