Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
beginner question on before_trading_start

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))

1 response

Initialize() is run when the algorithm starts up. Handle_data() is called for every bar. Before_trading_starts() is called before you do any trading. It's not entirely clear to me when / how often it's called, but it will always be called prior to calling handle_data() the first time and probably is called multiple times. My understanding of the Pipeline process is that they chunk the data into batches. The first batch covers a small set of initial days so you can get feedback from your algorithm quickly after executing it. Additional batches are larger but typically don't contain the whole time frame worth of data for long time frames. Before_trading_starts() would have to be called at least once for each batch because it provides you with the Pipeline output. However, there should be no need to call it on every bar.

The open variable is the open price for the day. The low and high variables are the low / high for the day seen so far. They do not provide future information. There is not supposed to be any way you can access future knowledge in the backtests.

In your code, you want the code from before_trading_starts() to be in handle_data().