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