Back to Community
first algo - screening for value and quality

I am trying to screen for value and quality. just barely started, trying to figure out how to rank a double sort on value and quality

Loading notebook preview...
8 responses

How's this?

Loading notebook preview...

Here's a back test

Clone Algorithm
34
Loading...
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
"""
This is a template algorithm on Quantopian for you to adapt and fill in.
"""
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.data import morningstar
from quantopian.pipeline.filters.morningstar import IsPrimaryShare
 
def initialize(context):
    """
    Called once at the start of the algorithm.
    """   
    
    # Rebalance every day, 1 hour after market open.
    schedule_function(my_rebalance, date_rules.month_start(), time_rules.market_open(hours=1))
     
    # Record tracking variables at the end of each day.
    # schedule_function(my_record_vars, date_rules.every_day(), time_rules.market_close())
     
    # Create our dynamic stock selector.
    attach_pipeline(make_pipeline(), 'my_pipeline')
         
        
def tradeable():
    
    primary_share = IsPrimaryShare()
    common_stock = morningstar.share_class_reference.security_type.latest.eq('ST00000001')
    not_depositary = ~morningstar.share_class_reference.is_depositary_receipt.latest
    not_otc = ~morningstar.share_class_reference.exchange_id.latest.startswith('OTC')
    not_wi = ~morningstar.share_class_reference.symbol.latest.endswith('.WI')
    not_lp_name = ~morningstar.company_reference.standard_name.latest.matches('.* L[. ]?P.?$')
    not_lp_balance_sheet = morningstar.balance_sheet.limited_partnership.latest.isnull()
    have_market_cap = morningstar.valuation.market_cap.latest.notnull()
    has_pb_ratio = morningstar.valuation_ratios.pb_ratio.latest.notnull()
    #has_roic_ratio = True
    #not_financial = ~~morningstar.asset_classification.morningstar_sector_code.latest.eq(103)
    #not_energy = ~morningstar.asset_classification.morningstar_sector_code.latest.eq(309)
    not_reit = ~morningstar.asset_classification.morningstar_sector_code.latest.eq(104)
    not_gp_is_nan = morningstar.income_statement.gross_profit.latest.notnull()


    tradeable_stocks = (
        primary_share
        & common_stock
        & not_depositary
        & not_otc
        & not_wi
        & not_lp_name
        & not_lp_balance_sheet
        & have_market_cap
        & has_pb_ratio
        & not_reit
        & not_gp_is_nan
    )
    
    return tradeable_stocks
    
def make_pipeline():
    """
    A function to create our dynamic stock selector (pipeline). Documentation on
    pipeline can be found here: https://www.quantopian.com/help#pipeline-title
    """

    #Stock universe
    tradeable_stocks = tradeable()
    market_cap_filter = morningstar.valuation.market_cap.latest.percentile_between(90, 100)
    value = morningstar.valuation_ratios.pb_ratio.latest.percentile_between(80, 100)
    gross_profit = morningstar.income_statement.gross_profit.latest
    total_assets = morningstar.balance_sheet.total_assets.latest
    gross_profitability = (gross_profit / total_assets).percentile_between(80, 100)
    
    securities = (market_cap_filter & tradeable_stocks & value & gross_profitability)

    return Pipeline(
        columns={
          'market_cap': morningstar.valuation.market_cap.latest,
          'p/b ratio in top 20%':  value,
          'gross_profitability': gross_profitability
        },
        screen = securities
    )
 
def before_trading_start(context, data):
    """
    Called every day before market open.
    """
    context.output = pipeline_output('my_pipeline')
    context.weights = context.output.market_cap / sum(context.output.market_cap)
  
    # These are the securities that we are interested in trading each day.
    context.security_list = context.output.index

 
def my_rebalance(context,data):
    """
    Execute orders according to our schedule_function() timing. 
    """
    
    for security in context.security_list:
        order_target_percent(security, context.weights.loc[security])
    for security in context.portfolio.positions:
        if security not in context.security_list:
            order_target_percent(security, 0)
    
    pass
There was a runtime error.

Sa Al, tried running this backtest and got a runtime error on line 89:

"TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''"

Python beginner here so maybe this is a relatively simple fix?

Hey Steve,

Could you tell me exactly at which point in time you are getting that error? I was able to run Sa's algorithm in the backtester with no issues.

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.

Settings:
From 2010-01-01 to 2016-08-31 with $1,000,000 initial capital

Something went wrong. Sorry for the inconvenience. Try using the built-in debugger to analyze your code. If you would like help, send us an email.
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
There was a runtime error on line 89.

Yes, it works in default dates but when I switch to 2010-01-01 to 2016-09-23, it throws the above error. The last transaction in the log occurs on 2015-02-02 13:00 WARN Your order for 81 shares of BF_A has been partially filled. 76 shares were successfully purchased. 5 shares were not filled by the end of day and were canceled.
End of logs.

So it looks like the backtest runs up until that point.

So, it seems that from the period of 05-20-2015 to 08-18 there are no stocks which pass the pipeline filter - leading the value sum(context.output.market_cap) to evaluate to 0, which results in your NaN error. You might want to fiddle around with the logic of your algorithm to buy something else when no stocks pass the filter or make the filter more lenient.

In any case - the modifications I made should make the algorithm work - although there will be no stocks purchased when no stocks pass the filter (hence the flat line).

Clone Algorithm
23
Loading...
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
"""
This is a template algorithm on Quantopian for you to adapt and fill in.
"""
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.data import morningstar
from quantopian.pipeline.filters.morningstar import IsPrimaryShare
import math

def initialize(context):
    """
    Called once at the start of the algorithm.
    """   
    
    # Rebalance every day, 1 hour after market open.
    schedule_function(my_rebalance, date_rules.month_start(), time_rules.market_open(hours=1))
     
    # Record tracking variables at the end of each day.
    # schedule_function(my_record_vars, date_rules.every_day(), time_rules.market_close())
     
    # Create our dynamic stock selector.
    attach_pipeline(make_pipeline(), 'my_pipeline')
         
        
def tradeable():
    
    primary_share = IsPrimaryShare()
    common_stock = morningstar.share_class_reference.security_type.latest.eq('ST00000001')
    not_depositary = ~morningstar.share_class_reference.is_depositary_receipt.latest
    not_otc = ~morningstar.share_class_reference.exchange_id.latest.startswith('OTC')
    not_wi = ~morningstar.share_class_reference.symbol.latest.endswith('.WI')
    not_lp_name = ~morningstar.company_reference.standard_name.latest.matches('.* L[. ]?P.?$')
    not_lp_balance_sheet = morningstar.balance_sheet.limited_partnership.latest.isnull()
    have_market_cap = morningstar.valuation.market_cap.latest.notnull()
    has_pb_ratio = morningstar.valuation_ratios.pb_ratio.latest.notnull()
    #has_roic_ratio = True
    #not_financial = ~~morningstar.asset_classification.morningstar_sector_code.latest.eq(103)
    #not_energy = ~morningstar.asset_classification.morningstar_sector_code.latest.eq(309)
    not_reit = ~morningstar.asset_classification.morningstar_sector_code.latest.eq(104)
    not_gp_is_nan = morningstar.income_statement.gross_profit.latest.notnull()


    tradeable_stocks = (
        primary_share
        & common_stock
        & not_depositary
        & not_otc
        & not_wi
        & not_lp_name
        & not_lp_balance_sheet
        & have_market_cap
        & has_pb_ratio
        & not_reit
        & not_gp_is_nan
    )
    
    return tradeable_stocks
    
def make_pipeline():
    """
    A function to create our dynamic stock selector (pipeline). Documentation on
    pipeline can be found here: https://www.quantopian.com/help#pipeline-title
    """

    #Stock universe
    tradeable_stocks = tradeable()
    market_cap_filter = morningstar.valuation.market_cap.latest.percentile_between(90, 100)
    value = morningstar.valuation_ratios.pb_ratio.latest.percentile_between(80, 100)
    gross_profit = morningstar.income_statement.gross_profit.latest
    total_assets = morningstar.balance_sheet.total_assets.latest
    gross_profitability = (gross_profit / total_assets).percentile_between(80, 100)
    
    securities = (market_cap_filter & tradeable_stocks & value & gross_profitability)

    return Pipeline(
        columns={
          'market_cap': morningstar.valuation.market_cap.latest,
          'p/b ratio in top 20%':  value,
          'gross_profitability': gross_profitability
        },
        screen = securities
    )
 
def before_trading_start(context, data):
    """
    Called every day before market open.
    """
    context.output = pipeline_output('my_pipeline')
    
    #add logic for when there are no stocks which pass the filter 
    if sum(context.output.market_cap) != 0:
        context.weights = context.output.market_cap / sum(context.output.market_cap)
    
    
    
    # These are the securities that we are interested in trading each day.
    context.security_list = context.output.index

 
def my_rebalance(context,data):
    """
    Execute orders according to our schedule_function() timing. 
    """
    
    for security in context.security_list:
        order_target_percent(security, context.weights.loc[security])
    for security in context.portfolio.positions:
        if security not in context.security_list:
            order_target_percent(security, 0)
    
    pass
There was a runtime error.

@Matt Great, thanks for the clarification!