Back to Community
How to Trade Only One Sector?

I am trying to only trade stocks within the same sector but I can't seem to figure out how to do so. I have tried to set the universe to only stocks in a certain sector like below but it does not work. Any suggestions?

universe = Q1500US() & morningstar.asset_classification.morningstar_sector_code == 206  
1 response

Try this:

from quantopian.pipeline.classifiers.morningstar import Sector

universe = Q1500US() & (Sector() == 206)  

If that doesn't work try:

universe = Q1500US() & Sector().eq(206)  

In the documentation you can read: "For internal technical reasons, Factor does not override ==. The eq method can be used to produce a Filter that performs a direct equality comparison against the output of a factor."

As 'Sector' is a Classifier I believe it overrides == operator, but just in case you have the second option.