Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to build the 5 min chart from the 1 min chart?

Hi,

This question seems to be too easy or even stupid. But truly to say I've thought all the time that the 'close' of the 5th bar on the 1-min chart is equal to the 'close' of the 1st bar of the 5-min chart. But these days I watch serveral charts from yahoo/finance or google/finance, they are not the case.

Who has right?

Assume the close of the 1-min-chart look as follow:

1: 1.10
2: 1.20
3: 1.30
4: 1.40
5: 1.50
6: 1.60
7: 1.70
8: 1.80
9: 1.90
10: 2.00
11: 2.1.0
12: 2.20
... ...

And the 'close' of the 5-min-chart should look as follow:
1:1.50
2:2.00
... ...

Cheers

2 responses

It may be more clear to look at the series with the timestamps (similar to the way they are returned by the 'data.history' method). Your example would look like this.

Timestamp('2017-06-01 13:31:00+0000', tz='UTC'): 1:10  
Timestamp('2017-06-01 13:32:00+0000', tz='UTC'): 1:20  
Timestamp('2017-06-01 13:33:00+0000', tz='UTC'): 1:30  
Timestamp('2017-06-01 13:34:00+0000', tz='UTC'): 1:40  
Timestamp('2017-06-01 13:35:00+0000', tz='UTC'): 1:50  
Timestamp('2017-06-01 13:36:00+0000', tz='UTC'): 1:60  
Timestamp('2017-06-01 13:37:00+0000', tz='UTC'): 1:70  
Timestamp('2017-06-01 13:38:00+0000', tz='UTC'): 1:80  
Timestamp('2017-06-01 13:39:00+0000', tz='UTC'): 1:90  
Timestamp('2017-06-01 13:40:00+0000', tz='UTC'): 2:00  
Timestamp('2017-06-01 13:41:00+0000', tz='UTC'): 2:10  
Timestamp('2017-06-01 13:42:00+0000', tz='UTC'): 2:20  

The close of the 5 minute chart would have bars / timestamps of hh:m0 or hh:m5 like this

Timestamp('2017-06-01 13:35:00+0000', tz='UTC')  
Timestamp('2017-06-01 13:40:00+0000', tz='UTC')  
Timestamp('2017-06-01 13:45:00+0000', tz='UTC')  
Timestamp('2017-06-01 13:50:00+0000', tz='UTC')  

The close price associated with each bar would then be as you stated

Timestamp('2017-06-01 13:35:00+0000', tz='UTC'):1.50  
Timestamp('2017-06-01 13:40:00+0000', tz='UTC'):2.00  

To accomplish this behavior using the pandas '.resample' method one could do something like this

    close_1m = data.history(my_stocks, 'close', my_timeframe, '1m')  
    close_5m = close_1m.resample('5T', closed='right', label='right').last()

The "closed='right'" parameter includes the data from the end of the bucket (ie time 0, 5, 10 etc) in the calculation. The "label='right'" parameter labels each bucket with the last point in the bucket (otherwise we would have forward bias looking into the future). The 'last()' method simply returns the last non-NaN data value in the bucket.

However, we often don't want simply the last price but maybe the average price over the resampling period. One could do something like this

    close_1m = data.history(my_stocks, 'close', my_timeframe, '1m')  
    close_5m = close_1m.resample('5T', closed='right', label='right').mean()

By using the resample() I found out something "intersting". This is described in my another post:
https://www.quantopian.com/posts/how-to-use-the-resample-correctly?utm_campaign=how-to-use-the-resample-correctly&utm_medium=email&utm_source=forums