Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
opening price?

I'm trying to access the opening price for the current day using a custom factor (in the example below to include symbols that open over $500). It seems to work however with a one day offset (i.e. the result for a given day is actually the previous day's opening price). Any suggestions? Thanks!

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

class openPrice(CustomFactor):  
    inputs = [USEquityPricing.open]  
    window_length=1  
    def compute(self, today, asset_ids, out, opens):  
        out[:] = opens[0]  


def initialize(context):  
    schedule_function(  
        func=after_trading_end,  
        date_rule = date_rules.every_day(),  
        time_rule = time_rules.market_close(),  
        calendar=calendars.US_EQUITIES,  
        half_days = True  
        )

    # Create and attach an empty Pipeline.  
    pipe = Pipeline()  
    pipe = attach_pipeline(pipe, name='my_pipeline')  
    # Construct Factors.  
    opening = openPrice()

    # Construct a Filter.  
    open_over_500 = (opening > 500)

    # Register outputs.  
    pipe.add(opening,"opening")

    # Remove rows for which the Filter returns False.  
    pipe.set_screen(open_over_500)  


def after_trading_end(context, data):  
    # Access results using the name passed to `attach_pipeline`.  
    results = pipeline_output('my_pipeline')  
    if results.empty == False:  
        print results.head(5)

    # Store pipeline results for use by the rest of the algorithm.  
    context.pipeline_results = results  
3 responses

Pipeline is run before market open, so you cannot fetch current day open price by design.

Ah ok, thank you.

To get the opening price for the current day, would the best way be to use handle_data to find the 9:30 bar?

Yes, you can fetch the opening price inside handle_data (or a scheduled function if you prefer). You can use data.current or data.history to actually retrieve the price, have a look at the help .