Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
minute data time window issue

Hey guys,
I just starts testing some strategies with minutes data but got sth hard to understand.
My sample code like this:

def handle_data(context, data):  
 """  
    Called every minute.  
 """  
    t=get_datetime('US/Eastern')  
    h=t.hour  
    m=t.minute  
    if m%30==0:  
        p=data.history(context.contract,'close',200,'1m')  
        print(p.head(50))  
        print(p.tail(50))  
        print(p.index[0])  
        print(p.index[-1])  
        print(p.index[-1].minute)  

Ideally, I want to get a 200min rolling window for every half hour. For example, if the algo time is 10:00 AM of Aug 15th, I hope to get the close prices at each minute from 13:10 of Aug 14th to 10:00 AM of Aug 15th (maybe 9:59). However, the frames printed like that:

2018-08-15 10:00 PRINT 2018-08-14 17:11:00+00:00 90.845
2018-08-14 17:12:00+00:00 90.890
2018-08-14 17:13:00+00:00 90.871
2018-08-14 17:14:00+00:00 90.905
2018-08-14 17:15:00+00:00 90.910
2018-08-14 17:16:00+00:00 90.920
2018-08-14 17:17:00+00:00 90.935
2018-08-14 17:18:00+00:00 90.940
2018-08-14 17:19:00+00:00 90.936
2018-08-14 17:20:00+00:00 90.950
2018-08-14 17:21:00+00:00 90.910
2018-08-14 17:22:00+00:00 90.910
2018-08-14 17:23:00+00:00 90.924
2018-08-14 17:24:00+00:00 90.925
2018-08-14 17:25:00+00:00 90.983
2018-08-14 17:26:00+00:00 90.965
2018-08-14 17:27:00+00:00 90.960
2018-08-14 17:28:00+00:00 90.965
2018-08-14 17:29:00+00:00 90.990
2018-08-14 17:30:00+00:00 90.990
2018-08-14 17:31:00+00:00 90.989
2018-08-14 17:32:00+00:00 90.985
2018-08-14 17:33:00+00:00 91.050
2018-08-14 17:34:00+00:00 91.050
2018-08-14 17:35:00+00:00 91.070
2018-08-14 17:36:00+00:00 91.060
2018-08-14 17:37:00+00:00 91.050
2018-08-14 17:38:00+00:00 91.095
2018-08-14 17:39...
2018-08-15 10:00 PRINT 2018-08-14 19:41:00+00:00 90.990
2018-08-14 19:42:00+00:00 90.950
2018-08-14 19:43:00+00:00 90.935
2018-08-14 19:44:00+00:00 90.890
2018-08-14 19:45:00+00:00 90.895
2018-08-14 19:46:00+00:00 90.890
2018-08-14 19:47:00+00:00 90.900
2018-08-14 19:48:00+00:00 90.900
2018-08-14 19:49:00+00:00 90.895
2018-08-14 19:50:00+00:00 90.840
2018-08-14 19:51:00+00:00 90.875
2018-08-14 19:52:00+00:00 90.865
2018-08-14 19:53:00+00:00 90.880
2018-08-14 19:54:00+00:00 90.875
2018-08-14 19:55:00+00:00 90.870
2018-08-14 19:56:00+00:00 90.880
2018-08-14 19:57:00+00:00 90.865
2018-08-14 19:58:00+00:00 90.875
2018-08-14 19:59:00+00:00 90.880
2018-08-14 20:00:00+00:00 90.847
2018-08-15 13:31:00+00:00 90.255
2018-08-15 13:32:00+00:00 90.230
2018-08-15 13:33:00+00:00 90.250
2018-08-15 13:34:00+00:00 90.310
2018-08-15 13:35:00+00:00 90.230
2018-08-15 13:36:00+00:00 90.290
2018-08-15 13:37:00+00:00 90.354
2018-08-15 13:38:00+00:00 90.320
2018-08-15 13:39...
2018-08-15 10:00 PRINT 2018-08-14 17:11:00+00:00
2018-08-15 10:00 PRINT 2018-08-15 14:00:00+00:00
2018-08-15 10:00 PRINT 0
2018-08-15 10:30 PRINT 2018-08-14 17:41:00+00:00 90.984
2018-08-14 17:42:00+00:00 91.005
2018-08-14 17:43:00+00:00 90.980
2018-08-14 17:44:00+00:00 90.960
2018-08-14 17:45:00+00:00 90.945
2018-08-14 17:46:00+00:00 90.905
2018-08-14 17:47:00+00:00 90.940
2018-08-14 17:48:00+00:00 90.936
2018-08-14 17:49:00+00:00 90.960
2018-08-14 17:50:00+00:00 90.965
2018-08-14 17:51:00+00:00 90.965
2018-08-14 17:52:00+00:00 90.970
2018-08-14 17:53:00+00:00 90.985
2018-08-14 17:54:00+00:00 90.960
2018-08-14 17:55:00+00:00 90.955
2018-08-14 17:56:00+00:00 90.960
2018-08-14 17:57:00+00:00 90.945
2018-08-14 17:58:00+00:00 90.950
2018-08-14 17:59:00+00:00 90.966
2018-08-14 18:00:00+00:00 90.963
2018-08-14 18:01:00+00:00 90.935
2018-08-14 18:02:00+00:00 90.947
2018-08-14 18:03:00+00:00 90.915
2018-08-14 18:04:00+00:00 90.924
2018-08-14 18:05:00+00:00 90.915
2018-08-14 18:06:00+00:00 90.875
2018-08-14 18:07:00+00:00 90.880
2018-08-14 18:08:00+00:00 90.890
2018-08-14 18:09...

I really can't understand why the datetime of the output seems so weird and ended at 20:00. Is it caused by timezone issue? Otherwise how
can I get the time window I really want?

Thanks a lot!
Victor

2 responses

The results are all in UTC. If you change them to US/Eastern I believe it will look as you you expected. Something like this.

        # get data and change p to US/Eastern so it's easier to read  
        p=data.history(context.contract,'close',200,'1m').tz_convert('US/Eastern')  

See attached algo.

BTW, if you want to do this every 30 minutes, it may be easier to use the 'schedule' method rather than checking the time in 'handle_data'. Take a look at this post https://www.quantopian.com/posts/rebalance-versus-schedule-function-sudtleties-dot-dot-dot .

Hi Dan,
Thank you so much for prompt response and an exact solution! it does work with the conversion of timezone. I know I can use scheduled function recursively to
do similar things, and actually I am using a stupid but clear approach to check what I missed.

Happy Labor Day!
Victor