Advertisement IC Markets

Resampling edges: which bar does a tick belong to?

Share, develop, and backtest custom MQL4/MQL5 Expert Advisors, Python data-scraping scripts, trading bots, and automated market alert systems.
Post Reply
Fairman
Posts: 6757
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Resampling edges: which bar does a tick belong to?

Post by Fairman »

Resampling edges: which bar does a tick belong to?

Turning ticks into one-minute bars looks trivial. A tick at exactly 10:05:00.000 belongs to the bar starting at 10:05, but different tools disagree.

Some libraries put the boundary tick in the earlier bar, labeling bars by their end time. Others put it in the new bar, labeling by start time. The convention changes where highs and lows fall, and shifts the whole series by one bar relative to another source.

The effect shows up when comparing a self-built chart with a platform's chart. Small differences in highs and lows appear, and signals fire on different bars.

For strategies sensitive to bar boundaries, the mismatch matters. An entry rule based on "the close of the 10:05 bar" refers to different prices under different conventions.

The solution is to define the rule explicitly, label the convention in the code, and test it against a known bar. Take one bar from the platform and check the code reproduces its open, high, low, and close exactly.

It sounds fussy. But a one-bar offset can turn a good backtest into a poor live result, and it's among the more common hidden errors.

Does the self-built bar data match the platform's bars exactly, and has that been verified?
It’s Fairman :geek:
Recommended broker for automated trading & scalping IC Markets
Amanhasnoname
Posts: 1506
Joined: Fri Oct 30, 2026 9:45 am

Re: Resampling edges: which bar does a tick belong to?

Post by Amanhasnoname »

This caught me when I built my own M1 bars from Dukascopy ticks to backtest a breakout rule. My bars matched the platform most of the time, but highs and lows near the round minute were sometimes a pip or two off. The backtest showed entries on bars where the platform chart clearly had no breakout at all.

The cause was exactly what you describe. My script used end-of-bar labels while the platform labels by start time, and the pandas default I had used put the boundary tick in the earlier bar. Once I switched to start labels with the left edge included, my bars matched the platform on a sample of a few hundred bars, apart from small differences caused by the two data sources.

I now keep a short test file with ten bars copied by hand from the platform, including a couple around the daily rollover and one at the weekly open. Every time I change the resampling code, the test runs first. It takes a few seconds and has caught two regressions already.
Post Reply