Back to Community
Fundamental Pipeline with several restrictions

Hi everyone,
I am trying to make an strategy that does the following:
-When the Chicago Fed Nat Index >-0.7 (bull market): buy the 20 best value stocks filtered by pcf, roe, and market cap
-When the CFNAI<-0.7 (bear market): buy the iShares 1-3Y Treasury Bond.

I don't know how to filter the best 20 value stocks with pipeline. I have read the documentation and I understand how to do it with one filter (I've attached my try which goes a bit slow too), but not with more than one. Any help?

attach_pipeline(make_pipeline(), 'fundamentals_pipeline')

def make_pipeline():

pipe = Pipeline(  
          columns={  
            'close': USEquityPricing.close.latest  
          },  
          screen = (morningstar.operation_ratios.roe > 0.10,  
                   morningstar.valuation_ratios.pcf_ratio <15,  
                   morningstar.valuation_ratios.pcf_ratio >2,  
                   morningstar.valuation.market_cap >= 30e6)  

      )  
return pipe  

def before_trading_start(context, data):
context.pipe_output = pipeline_output('fundamentals_pipeline')
context.longs = context.pipe_output[context.pipe_output['close']].index

Clone Algorithm
4
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: 599ff88cca1df954e459b9c6
There was a runtime error.
1 response

Hello Ro,

You can combine multiple pipeline Filters using the & (and) and | (or) operators. I have attached an example using the algorithm and list of filters you shared. For more examples on how to combine filters, I would recommend you reviewing the Pipeline tutorial.

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: 59c2db1d62befe559191e475
There was a runtime error.
Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.