I think I'm misunderstanding something fundamental about how the data universe (why and when do you need update_universe??) and before_trading_starts() works. My understanding is that initialize() is run only once at the beginning of the back trace in which any stocks added to the context are automatically added to the data dict, and then before_trading_starts() and handle_data() are run for every bar.
And also, I have a question about data[stock].open, data[stock].low, and data[stock].high. Does open give the price of the stock at the open of the current day, or the open of the current bar? Does high and low give the actual high and low for the whole day (including future times from now till end of day), or does it give the current high and low for the day from open till now, or does it give the current high and low for the current bar only?
code (sorry, not sure how to get the indentation/formatting to show up correctly):
Put any initialization logic here. The context object will be passed to
the other methods in your algorithm.
def initialize(context):
# set variables (bull and bear are assumed to be inverse of each other)
context.spy = sid(8554)
Will be called on every trade event for the securities you specify.
def handle_data(context, data):
pass
update context variables
def before_trading_start(context, data):
# update context
context.current = data[context.spy].price ### WHY DOES THIS LINE FAIL????
if context.current > context.high:
context.high = context.current
if context.current < context.low:
context.low = context.current
### will context.high and context.low always be equal to data[context.spy].high, data[context.spy].low???
cur_time = str(get_datetime('US/Eastern'))
log.info( "%s, %.2f %.2f %.2f %.2f"% (cur_time, context.open, context.current, context.high, context.low))