Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
The Returns Factor never works for me, can someone explain what I am doing wrong?
from quantopian.pipeline.factors import Returns, DailyReturns

def make_pipeline():  
    pipe = Pipeline()  
    universe=Q500US()  
    daily_returns = DailyReturns(mask=universe)  
    returns = Returns(window_length=2, mask=universe)  
    Pipeline(  
        columns={  
            'daily_returns' : daily_returns,  
            'returns' : returns  
    },  
        screen = universe)  
    return pipe  

When I run this it returns an empty pipeline with just the multi-index. I've tried several combinations. For example, using QTradableStocksUS as the mask, not using a mask at all, not using a window_length, adding an input even though it has a default input. Can someone help explain what I am doing wrong?

3 responses

Thank you for that. I was doing it in the research environment. Do you know how to make it work there?

The issue is your assignment statement. Nothing to do with the factors, filters, window lengths, or masks. I've added comments to the code below which steps through the problem.

def make_pipeline():  
    # The code below creates a new undefined pipeline and sets the variable 'pipe' to reference this object  
    pipe = Pipeline()  

    # The following code creates a universe filter and a couple of factors  
    # All looks good with these  
    universe=Q500US()  
    daily_returns = DailyReturns(mask=universe)  
    returns = Returns(window_length=2, mask=universe)  

    # The following creates a new pipeline with the defined columns and screen.  
    # It works as expected BUT the pipeline object isn't assigned to anything.  
    # It gets created but immediately deleted by Python because there is no way to reference it  
    Pipeline(  
        columns={  
            'daily_returns' : daily_returns,  
            'returns' : returns  
    },  
        screen = universe)  

    # The function returns the first undefined pipeline 'pipe' which isn't what's intentioned  
    return pipe  

The fix is simple. Eliminate the initial 'pipe' assignment and, instead, assign 'pipe' to the second pipeline.

def make_pipeline():  

    # The following code creates a universe filter and a couple of factors  
    # All looks good with these  
    universe=Q500US()  
    daily_returns = DailyReturns(mask=universe)  
    returns = Returns(window_length=2, mask=universe)  

    # The following creates a new pipeline with the defined columns and screen.  
    # Ensure it gets assigned to the variable 'pipe'  
    pipe = Pipeline(  
            columns={  
                'daily_returns' : daily_returns,  
                'returns' : returns  
            },  
            screen = universe)  

    # Now return 'pipe' which is correctly defined  
    return pipe  

That should do it.

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.

Thank you so much Dan!