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.

Clone Algorithm
3
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 59600fbde2e6094de1ed6540
There was a runtime error.
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.

Clone Algorithm
3
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5963c307d8b4e951e5f726d7
There was a runtime error.

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.