Series Resampler
Grouping "every N source bars" into a bigger bar sounds trivial until the target period does not evenly divide the calendar. A 7-minute or 90- minute bucket does not repeat on the same wall-clock boundaries every hour or every day the way M5 or H1 do, so a naive resampler silently produces different-looking buckets depending on exactly when it started running - which is not a bug anyone notices until two resampled series built from the same source data at different times disagree. This library makes bucket alignment an explicit, chosen policy instead of an accident: RESAMPLE_ALIGN_EPOCH floors every bar's time to a fixed multiple of the target period since the Unix epoch, so the same target period always produces the same boundaries regardless of when resampling started - calendar-stable. RESAMPLE_ALIGN_FIRST_BAR instead anchors boundaries to the first source bar actually seen, useful for a session-relative bucket that should start exactly where the feed starts. It also separates a genuinely different concern: whether the newest, still-possibly-forming resampled bucket should be returned at all. RESAMPLE_PARTIAL_INCLUDE returns it (mirroring how shift 0 behaves on a live chart); RESAMPLE_PARTIAL_EXCLUDE drops it until a later source bar proves - by landing in the next bucket - that the previous one is closed. A resampler that conflates these two questions, or answers neither explicitly, is the one that quietly mixes a forming bar into what a caller assumed was a closed one. Notes and limitations - AddBar() must be called in oldest-to-newest order. The resampler groups contiguous input; it does not sort out-of-order bars, and feeding them backwards (as MQL4's own shift-0-is-newest series arrays naturally are) will produce a nonsensical result. - RESAMPLE_PARTIAL_EXCLUDE only ever drops the LAST bucket - every earlier bucket is already proven closed by the presence of the bucket after it, so nothing before the final one is ever excluded. - Tick volume is summed across every source bar that feeds a bucket. If a source series already reports 0 for a gap, that gap contributes 0 to the resampled bucket's volume rather than being treated as missing data. - This resamples OHLC/tick-volume bar data only; it does not resample raw ticks - feed it bars from any timeframe, including another resampled output if you want to chain two resampling passes.
Script · MT5 · v1.0 · Free
About Series Resampler
Series Resampler is a script for MetaTrader 5. MQL source code included.
Files in the download: SeriesResampler.mq5.mqh, SeriesResampler.txt, SeriesResamplerDemo.ex5, SeriesResamplerDemo.mq5.
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT5 Script · MQL5 Source Code