Multi Symbol Series Aligner
Any calculation that combines more than one symbol - correlation, a pair spread, a currency exposure matrix, a basket value at risk - silently assumes that index i on symbol A and index i on symbol B refer to the same moment. That assumption breaks the instant the symbols do not share identical trading hours, and it breaks completely on holidays, session gaps, or a feed that simply missed a bar. The usual fix, reaching for iBarShift per symbol per bar, produces a different silent misalignment: it finds the nearest bar without ever telling you how near. This header builds one shared timestamp axis explicitly, and makes the decision about missing bars a parameter instead of an accident. Three policies are offered: INTERSECTION keeps only timestamps present on every symbol. The safest choice for anything statistical, because every remaining row is a genuine simultaneous observation - the axis simply shrinks to whatever all the symbols actually agree on. FORWARD_FILL builds a union of every timestamp seen on any symbol, and where a symbol has no bar at a shared timestamp, repeats its last known close. This keeps every symbol's array the same length as the widest history without inventing new price discovery, which is usually the right behaviour for a symbol that simply trades less often than the others. MARK_EMPTY also builds the union axis, but leaves missing entries as EMPTY_VALUE with an explicit isFilled flag rather than guessing at a value. Use this whenever a wrong fill would be worse than a gap the caller has to handle explicitly. Every result reports its own coverage - the fraction of the axis that came from a genuine quote rather than a policy decision - so a basket that turns out not to share real trading hours is visible immediately instead of quietly degrading a downstream calculation. MQL4 only allows the leftmost dimension of a 2D array to be dynamic, which rules out a single ragged "one row per symbol" table. This header uses the struct-of-arrays pattern instead: one AlignedSeries per symbol, each holding its own values[] and isFilled[] parallel to the single shared sharedTimes[] axis returned alongside them. Notes and limitations - CopyRates requests history from the server if it is not already cached, which can take a moment on the first call for a symbol the terminal has not looked at recently. - INTERSECTION can shrink the axis a great deal when symbols keep very different hours - always check ArraySize(sharedTimes) rather than assuming it equals barsRequested. - Alignment works on close prices via CopyRates. Aligning open, high or low instead requires a small change to AlignerLoadRawSeries and AlignerSampleOntoAxis - both are plain functions, not hidden behind a class. - Symbol count is O(n) per merge; fine for the handful of symbols a portfolio calculation typically uses, not designed for hundreds of symbols at once. - No thread or timer concerns: everything runs synchronously inside AlignSeries and returns once complete.
Script · MT5 · v1.0 · Free
About Multi Symbol Series Aligner
Multi Symbol Series Aligner is a script for MetaTrader 5. It has 4 settings, such as Symbol b, Symbol c and Bars. MQL source code included.
Files in the download: SeriesAligner.mq5.mqh, SeriesAligner.txt, SeriesAlignerDemo.ex5, SeriesAlignerDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Symbol b | Second symbol, blank = first other Market Watch symbol | |
| Symbol c | Third symbol, blank = second other Market Watch symbol | |
| Bars | 30 | Bars requested per symbol |
| Print tail | 10 | How many of the most recent aligned rows to print |
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: Trading Sessions · Time Filter · Multi-Currency · MT5 Script · MQL5 Source Code