Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Columns in pipeline: today's open, s&p open and close yesterday

I'm trying to figure out how to add certain fields. I can get yesterdays open and close using this pipeline:


    yesterday_close = USEquityPricing.close.latest  
    yesterday_open = USEquityPricing.open.latest  
    pipe = Pipeline(  
        screen = ...  
        columns = {  
            'close': yesterday_close,  
            'open': yesterday_close  
        }  
    )  

I'm trying to add these
1. today's open
2. yesterdays s&p500 open and close.

I didn't find anything from a search on the forums, tutorial or api. Any ideas anyone?

2 responses

The best way to add columns to the pipeline output (which is a pandas dataframe) is to use the 'assign' method. See http://pandas.pydata.org/pandas-docs/version/0.18/generated/pandas.DataFrame.assign.html . So to add the open prices to the pipeline output, one could do something like this.

    # Get open prices for all the securities in the pipeline output  
    open_prices = data.current(context.output.index.tolist(), 'open')  


    # Add the open prices as a new column using the assign method  
    context.output = context.output.assign(todays_open = open_prices)

There isn't a good way to get the actual S&P500 index data, however it's very common to use the ETF SPY as a proxy. So one can get yesterdays open and close values for SPY just like getting the values for any security in the pipeline output. Just make sure SPY is not filtered out in the pipeline.

    context.sp500 = symbol('SPY')


    sp500_yesterday_open = context.output.at[context.sp500, 'yesterday_open']  
    sp500_yesterday_close = context.output.at[context.sp500, 'yesterday_close']  

See attached algo. Good luck.

Thanks Dan! Thats very helpful