Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
before_trading_start price values not as expected.

Hi,
All I am doing is printing close price values from before_trading_start, but for the previous day, it gives me the close price values at the first minute of market open, and I am expecting the close price from the end of day? Why is this behaving like this? or am I misreading it?
So on 2016-01-05, from before_trading_start() the close price for 2016-01-04 is being given as 102.04, whereas on 2016-01-06, from before_trading_start(), the close price for 2016-01-04 is given as 105.33
Could someone please explain this?

Thanks.

5 responses

The last three nonempty lines are only executed the first time handle_data is called each day, ie. at 9:31am EST/EDT. At that time, your call to history returns the latest price from that minute, and the closing prices from the previous 4 trading days. The previous day's close is in history[-2]. Read the help page on history.

Thanks André,
I was under the impression that before_trading_start() fires before the open, so a print command from within it would never have access to current day's values, but apparently I was wrong in assuming that.

@Ashish - before_trading_start does run before the trading start, but it prints in line 16 what you saved in context.close_prices the most recent time line 26 in handle_data was executed, and that's 9:31am EST/EDT of the previous trading day.

For example, if you're running this live, the latest call to before_trading_start (early this morning, late last night, or at midnight) should have printed the day closing prices from last Friday, Monday, Tuesday, and Wednesday, and the latest prices from 9:31am yesterday (Thursday).

I have inserted some print statements to help you see what happens where when. Run it and see the log.

Thanks André,
I think I get it now, I am updating close_prices once a day at the open so for that day I only get the price for the opening minute and that is the price that is given to before_trading_start for the next day. I was incorrectly expecting the close price for the day to be sent to before_trading_start.