Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How can I implement specific sectors in my pipeline as a filter?

Hello Q,

I have this algorithm, which is based on Fred Piards 'The Lazy Fundamental Analyst' on page 37.

The problem is, that I don't know how I can implement a filter that specifies the sectors of Q500US which are supposed to be used.
Take a look at it in my attached algorithm.

Also I somehow can't set the POR to be 0 as in POR == 0. I found a workaround, but if anybody finds the time to help me with that as well, I would appreciate it.

Thanks for your help.

3 responses

The cleanest way to specify a sector filter is to use the '.element_of' method. An equality filter can be made by using the '.eq' method. In your case, something like this might be what you want.

pipe.set_screen(base_universe  &  POR.eq(0.0) & sector.element_of([239, 77]))

Note, it' typically not a good idea to do equality comparisons with real numbers (eg factor results) since there can be rounding differences. Maybe use two inequalities similar to what you had?

pipe.set_screen(base_universe  &  (POR < 0.01 & POR > -.01) & sector.element_of([239, 77]))

Just a thought. Good luck.

Thanks Dan for helping me, it does sort of work, but I don't get any stocks through my pipeline now. Not sure if something is wrong or maybe there just aren't any stock in those sectors matching my criteria.

How are you defining your sector codes? The Morningstar sector values are

101 Basic Materials
102 Consumer Cyclical
103 Financial Services
104 Real Estate
205 Consumer Defensive
206 Healthcare
207 Utilities
308 Communication Services
309 Energy
310 Industrials
311 Technology

(see http://corporate.morningstar.com/us/documents/methodologydocuments/methodologypapers/equityclassmethodology.pdf)

The values of 239 and 77 aren't valid.