Back to Community
Eliminate financials (or some sector)?

Hello,

I am not really sure what am I doing wrong. In my backtest (run only for couple of recent days to check the companies it selects) i have:

sector_filter = (sector != Sector.FINANCIAL_SERVICES)|(sector != Sector.REAL_ESTATE)  

And then I include sector_filter into pipe.set_screen() and nevertheles when I look at the companies that got into the portfolio, I see a lot of financial companies. I checked Morningstar to verify if they don't have it classified in some weird way, but this is not the case.

I am probably doing something wrong in the code, but I cannot figure out what.

Any help would be appreciated.

Thanks,

Sanning

Clone Algorithm
5
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: 57d5752e5d8f101029e1c27c
There was a runtime error.
2 responses

Try the following.
Also read the Fundamentals Reference.

I don't do this filtering much, but I think that abbreviations help make the code legible.

define some abbreviations to make filters more legible

fn    = fundamentals  
fnac  = fn.asset_classification  
fnbs  = fn.balance_sheet  
fncfs = fn.cash_flow_statement  
fncr  = fn.company_reference  
fner  = fn.earnings_ratios  
fnerp = fn.earnings_report  
fnfsf = fn.financial_statement_filing  
fngp  = fn.general_profile  
fnis  = fn.income_statement  
fnor  = fn.operation_ratios  
fnscr = fn.share_class_reference  
fnv   = fn.valuation  
fnvr  = fn.valuation_ratios  

sector_code = fnac.morningstar_sector_code  
growth_grade = fnac.growth_grade  
health_grade = fnac.financial_health_grade  
profit_grade = fnac.profitability_grade

example of a filter to include specific sectors (Consumer Defensive and Utilities)

    .filter(sector_code.in_(['205', '207']))

example of a filter to exclude specific sectors (Financial and Real Estate)

    .filter(~sector_code.in_(['103', '104']))

When filtering with Pipeline the process is somewhat different and uses a classifer feature.
See Scott Sanderson's classifier post and notebook

to include specific sectors (Consumer Defensive and Utilities)
sector_code = morningstar.asset_classification.morningstar_sector_code.latest
sector_screen = (sector_code.eq(205) or sector_code.eq(207) )
my_screen = (sector_screen & IsPrimaryShare())
pipe.set_screen(my_screen)

to exclude specific sectors (Financial and Real Estate)
sector_code = morningstar.asset_classification.morningstar_sector_code.latest
sector_screen = (~sector_code.eq(103) and ~sector_code.eq(104) )
my_screen = (sector_screen & IsPrimaryShare())
pipe.set_screen(my_screen)

Note: IsPrimaryShare() is added to my_screen as an example of how multiple screening factors are combined.