Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Run a pipeline in algo between a specific time window

Hi everyone, i am new of quantopian so forgive me for the very dumb question.
I was wondering what is the equivalent in algo of run_pipeline(my_pipe, 'start_date', 'end_date') that i usually use in research enviroment. In my specific case i am interested in gathering data starting 2 months ago and ending one week before today's date

4 responses

In an algo, pipelines will only return data for the backtest date. There isn't an equivalent to run_pipeline with a start and end date. The intention, and really the sole purpose of pipelines, is to present the data which a trader would have seen on that date. If you are looking to gather data starting 2 months ago and ending one week before today's date, then one would probably need to write a custom factor. Use a window_length of 40 (approximately 2 months) to fetch all the data.

Hope that makes sense. If there is a specific factor you need, maybe post it here and the community may be able to help.

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.

Hi Dan, first of all thank you for the reply. Basically this is the code from research enviroment i am trying to replicate in algorithm. For the fcf/p and e/p i want to compute the ratio between their mean value from 2 weeks ago to one week ago and the mean between 8 weeks ago to 2 weeks ago.
In the research i just used the resample method after select date time with run_pipeline. Even an example with just one of the 2 factors will help me to understand better the workflow.
Thanks a lot

def make_pipeline():  
    # Get open, close and market cap  
    close_price = USEquityPricing.close.latest  
    open_price = USEquityPricing.open.latest  
    market_cap = MarketCap()  
    # get S&P 500  
    QTU = QTradableStocksUS()  
    top_500_market_cap = market_cap.top(500)  
    # Create the enviroment  
    QTU_top_500 = QTU & top_500_market_cap  
    # Take only companies with closing price  
    has_pricing_data = close_price.notnull()  
    # Get the sector  
    morningstar_sector = Sector()  
    # Get factors  
    roe = Fundamentals.roe.latest  
    net_profit_margin = Fundamentals.net_income_income_statement.latest / Fundamentals.total_revenue.latest  
    EP_ratio = 1 / morningstar.valuation_ratios.pe_ratio.latest  
    fcf = Fundamentals.free_cash_flow.latest / close_price  

    # Return a dataframe  
    return Pipeline(  
        columns={'close_price': close_price, 'Sector_Id' : morningstar_sector,  
                 'Roe' : roe, 'E/P' : EP_ratio, 'FCF/P' : fcf, 'open_price' : open_price,  
                 'NPM' : net_profit_margin},  
        screen=QTU_top_500 & has_pricing_data  
    )


df = run_pipeline(make_pipeline(), '2020-3-1', '2020-4-20')  

One can get the previous value of a factor using a short custom factor like this

class Factor_N_Days_Ago(CustomFactor):  
    def compute(self, today, assets, out, input_factor):  
        out[:] = input_factor[0]

Then create an instance of this and pass the factor you want n days ago as the input. Pass the window length which is n+1.

previous_8wks = (8*5) + 1  
roe_8wks_ago = Factor_N_Days_Ago(inputs=[roe], window_length=previous_8wks)

The one thing you need to ensure is that the original factor is 'window safe'. There is a bit more on that in this post https://www.quantopian.com/posts/get-lagged-output-of-pipeline-custom-factor

See the attached notebook.

This is really helpful, thanks for your time Dan!