pandas resample('5min') on M1: which bars land in the 10:05 candle, and the label trap
Posted: Thu Oct 29, 2026 11:10 am
pandas resample('5min') on M1: which bars land in the 10:05 candle, and the label trap
If you build M5 bars from M1 data in pandas and compare them with the platform's chart, a one bar difference is common. This is the cause.
I used a toy frame with the M1 open prices 1.0, 2.0, up to 12.0 starting at 10:00, so you can see which bar went where.
The default is left labelled and left closed. The bar stamped 10:05 contains M1 bars 10:05 to 10:09, and its open is 6.0, the open of the 10:05 bar. That's what MT5 and most platforms show, since a bar is stamped with its opening time.
With label and closed both set to right, the bar stamped 10:05 contains 10:01 to 10:05, opens at 2.0, and the very first 10:00 bar sits alone in a bucket labelled 10:00. At the end of the data there is a partial bucket labelled 10:15 holding only the 10:11 bar. Every M5 candle then differs from the platform's, and a strategy that looks at closes sees a different number.
It goes wrong when someone copies a snippet for daily or weekly data, where right labelling is conventional, and uses it for intraday bars.
Check one bar by hand against the chart before running anything on the full data.
If you build M5 bars from M1 data in pandas and compare them with the platform's chart, a one bar difference is common. This is the cause.
I used a toy frame with the M1 open prices 1.0, 2.0, up to 12.0 starting at 10:00, so you can see which bar went where.
Code: Select all
ohlc = {"open": "first", "high": "max", "low": "min", "close": "last"}
m5 = m1.resample("5min").agg(ohlc)
m5r = m1.resample("5min", label="right", closed="right").agg(ohlc)
With label and closed both set to right, the bar stamped 10:05 contains 10:01 to 10:05, opens at 2.0, and the very first 10:00 bar sits alone in a bucket labelled 10:00. At the end of the data there is a partial bucket labelled 10:15 holding only the 10:11 bar. Every M5 candle then differs from the platform's, and a strategy that looks at closes sees a different number.
It goes wrong when someone copies a snippet for daily or weekly data, where right labelling is conventional, and uses it for intraday bars.
Check one bar by hand against the chart before running anything on the full data.