Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Newbie Question: pipe.set_screen() TyperError

Hello!

I wanted to thank the developers for the great Pipeline API, and especially all who were involved with making such a superb and helpful webinar. I had a quick question for understanding the filters needed by pipe.set_screen().

I'm currently trying to make an algorithm that lists companies by ROA (descending) that have a market cap of at least X amount.

I am trying to base my code off of one of the examples:

    market_cap = morningstar.valuation.market_cap  
    market_cap_filter = (market_cap > 500000000000)  
    pipe.set_screen(market_cap_filter)  

but it hasn't been effective and brings an error:

TypeError: zipline.pipeline.pipeline.set_screen() expected a value of type zipline.pipeline.filters.filter.Filter for argument 'screen', but got bool instead.

Here is my full code! Any help for the new API would be greatly appreciated!!

Thanks!

from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline  
from quantopian.pipeline import CustomFactor  
from quantopian.pipeline.data.builtin import USEquityPricing  
from quantopian.pipeline.data import morningstar

class ROA(CustomFactor):  
    # Custom Factor for ROA  
    inputs = [morningstar.operation_ratios.roa]  
    window_length = 1  

    def compute(self, today, assets, out, roa):  
        out[:] = roa          

class MarketCap(CustomFactor):  
    # Shares Outstanding  
    inputs = [morningstar.valuation.market_cap]  
    window_length = 1

    def compute(self, today, assets, out, market_cap):  
        out[:] = market_cap  
def initialize(context):  
    pipe = Pipeline()  
    attach_pipeline(pipe, "pipeline")  
    pipe.add(ROA(), "ROA")  
    pipe.add(MarketCap(), "Market_Cap")  
    market_cap = morningstar.valuation.market_cap  
    market_cap_filter = (market_cap > 500000000000)  
    pipe.set_screen(market_cap_filter)

def before_trading_start(context):  
    context.output = pipeline_output("pipeline")  


    context.output = context.output.sort(['ROA'], ascending=False)  
    context.stock_list = context.output  
    update_universe(context.stock_list.index)  
def handle_data(context, data):  
    print "List"  
    log.info("\n" + str(context.stock_list))

    cash = context.portfolio.cash  
    current_positions = context.portfolio.positions  
    for stock in data:  
        current_position = context.portfolio.positions[stock].amount  
        stock_price = data[stock].price  
        plausible_investment = cash / 10.0  
        amount_to_buy = int(plausible_investment / stock_price)  
        if stock_price < plausible_investment:  
            if current_position == 0:  
                order_target(stock, 1)  
8 responses

Hey Andrew,
I just made a few modifications and your algo is working now. Attached is the revised version, with comments to show what changes I made. In short, I did 2 things. First, I used .latest to get the latest market cap value to set your filter. Second, I changed your screen, because nothing was passing it.

I hope this helps!

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.

Thanks!

That's really helpful!

I had the market cap requirement too high on accident. :D Thanks also for doing the webinar for all of us!

I was wondering how I would create multiple filters in Pipeline?

For example, I was trying to rule out two sectors, [103. 207] and limit the Pipeline to U.S. securities :)

In another example I was working on, I made a filter like this: .filter(~sector_code.in_([103,207]))

Is there a Pipeline equivalent? And will I need a get_fundamentals block in order to access sector_code?

Thanks!

  • Andrew

Hi Andrew,
You can set multiple filters with set_screen. Here is how I would modify your algo to remove those two industries.

    pipe = Pipeline()  
    attach_pipeline(pipe, "pipeline")  


    pipe.add(ROA(), "ROA")  
    pipe.add(MarketCap(), "Market_Cap")  


    # used .latest which returns the most recent value for the market cap  
    # note, you could use this for both your marketcap and ROA factors above, for code similicity  
    market_cap = morningstar.valuation.market_cap.latest  


    # get the latest sector code  
    sector_code = morningstar.asset_classification.morningstar_sector_code.latest  
    # create a list of those I want removed  
    sectors = [103,207]  
    market_cap_filter = (market_cap > 50000000000)


    # set my screen including both a market cap filter and a sector code filter  
    pipe.set_screen((market_cap_filter) & (sector_code not in sectors))

Awesome!

Thanks Karen!

  • Andrew

Hi Karen, if I run:

sector_code = morningstar.asset_classification.morningstar_sector_code.latest

results this error:

UnsupportedDataType: Latest instances with dtype int64 are not supported.

Do you have the same problem?

Thanks

Hi Carlos,

The .latest function is currently not working for int and boolean types. Our engineers are investigating the issue. For now, you can replicate the functionality by creating a custom factor that returns the sector code like this:

class Sector(CustomFactor):  
    inputs = [ morningstar.asset_classification.morningstar_sector_code ]  
    window_length = 1  
    def compute(self, today, assets, out, sector):  
        out[:] = sector  

(Copied from this notebook)

I've also attached an example algo that includes this custom factor for your convenience.

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.

Is there any update on this issue? It is quite annoying to have to create customfactors for simple values.

Mikko, the fix has been issued. You can use int and boolean factors as normal in Pipeline.