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
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
"""
Trading Strategy using Fundamental Data

1. Look at stocks in the Q1500US.
2. Go long in the top 50 stocks by pe_ratio.

"""

from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data import morningstar
from quantopian.pipeline.filters.morningstar import Q1500US
from quantopian.pipeline.factors import BusinessDaysSincePreviousEvent


def initialize(context):
    fetch_csv('https://www.quandl.com/api/v3/datasets/FRED/CFNAI.csv?api_key=qsEnkv4wxdipBoEVzsYw&end_date=2017-05-31',
              date_column='Date',
              symbol='CFNAI',
              date_format='%y-%m-d%')
    
    context.shy=[sid(23911)] #SHY
    
    # Rebalance monthly on the first day of the month at market open
    schedule_function(rebalance, date_rules.month_start(), time_rules.market_open())
    schedule_function(count_positions, date_rules.month_start(), time_rules.market_close())

    attach_pipeline(make_pipeline(), 'fundamentals_pipeline')

def make_pipeline():

    universe = Q1500US()
    roe_ratio = morningstar.operation_ratios.roe.latest
    top_roe_stocks = roe_ratio.top(20, mask=universe)
    securities_to_trade = (top_roe_stocks)
    pipe = Pipeline(
              columns={
                'pe_ratio': roe_ratio,
                'longs': top_roe_stocks,
              },
              screen = securities_to_trade
          )
    return pipe
    
"""
Runs our fundamentals pipeline before the marke opens every day.
"""
def before_trading_start(context, data): 
    context.pipe_output = pipeline_output('fundamentals_pipeline')
    context.longs = context.pipe_output[context.pipe_output['longs']].index


def rebalance(context, data):
    current_CFNAI_price = data.current('CFNAI', 'Value')
    
    if current_CFNAI_price>(-0.7):
        for stock in context.longs:
            if data.can_trade(stock):
                order_target_percent(stock, 0.05) 
        
        for stock in context.portfolio.positions:
            if not stock in context.longs:
                order_target_percent(stock, 0.0)
        
    
    if current_CFNAI_price<=(-0.7):
        for stock in context.longs:
            order_target_percent(stock, 0.0)
        for shy in context.shy:
            order_target_percent(shy, 1.0)

def count_positions(context,data):
        
    longs = 0
    for position in context.portfolio.positions.itervalues():
        if position.amount > 0:
            longs += 1
    leverage=context.account.leverage

    record(longs=longs)
    record(leverage=leverage)
            
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
6
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
"""
Trading Strategy using Fundamental Data

1. Look at stocks in the Q1500US.
2. Go long in the top 50 stocks by pe_ratio.

"""

from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data import Fundamentals
from quantopian.pipeline.filters import Q1500US
from quantopian.pipeline.factors import BusinessDaysSincePreviousEvent


def initialize(context):
    fetch_csv('https://www.quandl.com/api/v3/datasets/FRED/CFNAI.csv?api_key=qsEnkv4wxdipBoEVzsYw&end_date=2017-05-31',
              date_column='Date',
              symbol='CFNAI',
              date_format='%y-%m-d%')
    
    context.shy=[sid(23911)] #SHY
    
    # Rebalance monthly on the first day of the month at market open
    schedule_function(rebalance, date_rules.month_start(), time_rules.market_open())
    schedule_function(count_positions, date_rules.month_start(), time_rules.market_close())

    attach_pipeline(make_pipeline(), 'fundamentals_pipeline')

def make_pipeline():
    # Base universe.
    universe = Q1500US()
    
    # Filters
    roe_filter = Fundamentals.roe.latest > 0.10  
    pcf_top = Fundamentals.pcf_ratio.latest < 15  
    pcf_bottom = Fundamentals.pcf_ratio.latest > 2  
    
    combined_filter = (roe_filter &
                       pcf_top &
                       pcf_bottom
    )
    
    roe_ratio = Fundamentals.roe.latest
    securities_to_trade = roe_ratio.top(20, mask=(universe & combined_filter))

    pipe = Pipeline(
              columns={'longs': securities_to_trade},
              screen = securities_to_trade
          )
    return pipe
    
"""
Runs our fundamentals pipeline before the marke opens every day.
"""
def before_trading_start(context, data): 
    context.pipe_output = pipeline_output('fundamentals_pipeline')
    context.longs = context.pipe_output[context.pipe_output['longs']].index


def rebalance(context, data):
    current_CFNAI_price = data.current('CFNAI', 'Value')
    
    if current_CFNAI_price>(-0.7):
        for stock in context.longs:
            if data.can_trade(stock):
                order_target_percent(stock, 0.05) 
        
        for stock in context.portfolio.positions:
            if not stock in context.longs:
                order_target_percent(stock, 0.0)
        
    
    if current_CFNAI_price<=(-0.7):
        for stock in context.longs:
            order_target_percent(stock, 0.0)
        for shy in context.shy:
            order_target_percent(shy, 1.0)

def count_positions(context,data):
        
    longs = 0
    for position in context.portfolio.positions.itervalues():
        if position.amount > 0:
            longs += 1
    leverage=context.account.leverage

    record(longs=longs)
    record(leverage=leverage)
            
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.