Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Bug in historical data for backtest?

I am performing a historical price check using the below script and I find errors in the price output. The backtest date ranges from 01/04/2019 to 01/31/2019.

Here's the output I get on the first day of the backtest:
The print #1 line outputs the current date as 01/04/2019 which is correct
The print #2 outputs the closing price on 01/04/2019 which is incorrect. Shouldn't it output the closing price on 01/03/2019?
The print #3 outputs the same as print #2 above. This is again incorrect. Shouldn't this output the price as of 10:30am on 01/04/2019?
The print #4 outputs the price as of 3:30pm on 01/04/2019. This is also incorrect. Shouldn't this output the price as of 10:30am on 01/04/2019 again?

I would appreciate any help in this regard.

SCRIPT

def initialize(context):
context.security = [symbol("SPY")]
schedule_function(my_func, date_rules.every_day(), time_rules.market_open(hours=1))

def my_func(context,data):
print "Current Date is:", str(get_datetime().date()) #Print 1
print "Closing price is:", data.history(context.security, "close", 1, "1d") #Print 2
print "Cur Price based on daily data is:", data.history(context.security, "price", 1, "1d") #Print 3
print "Cur Price based on minute data is:",data.history(context.security, "price",1,"1m") #Print 4

2 responses

Try this:

yest_daily_close = data.history(context.security, "close", 2, "1d").iloc[-2]  
print "Closing price is:", yest_daily_close  

Thanks Vladimir. That's what I am doing now.

I noticed that all the date and time output are based on the UTC timezone and not the trading time. I think that's where the confusion started.