Loading…

Cross Spectral Coherence

A cross-spectrum computed from a single Fourier transform of two whole series is a mathematical identity, not a measurement: coherence derived from exactly one segment is always exactly 1.0 at every frequency, regardless of how unrelated the two series actually are, because there is no independent second estimate for the first to disagree with. This is a common trap in hand-rolled coherence code - it looks like it works (the numbers come out "reasonable") while being unable to ever report anything but perfect coherence. This header implements the real fix: Welch-style averaging across multiple non-overlapping segments, which is what makes a coherence value in [0, 1] mean something. The demo verifies this identity directly - a single-segment call is shown to always return exactly 1.0, by construction, and the header comment explains why. There is no FFT in MQL4. None is needed here: CrossSpectralCoherence takes a small caller-chosen LIST of frequencies (in cycles per segment) rather than computing every possible frequency bin, so each requested frequency is evaluated directly via a single-bin DFT summation - exact, and simple for the handful of frequencies a typical caller actually cares about (e.g. "is there a roughly 20-bar cycle shared between these two instruments right now, and if so which one leads the other"). For each frequency, the cross-spectrum Sxy and both auto-spectra Sxx/Syy are averaged across every segment BEFORE coherence = |Sxy|^2/(Sxx*Syy) is computed - averaging the spectra, not the coherence values themselves, which is the detail that separates a real Welch estimator from a biased shortcut. The cross-spectrum's phase angle is converted into a lag in bars using a hand-written two-argument arctangent, since MQL4 has no built-in MathArctan2 (only the single-argument MathArctan) - documented in this catalogue's house style notes. Notes and limitations - Needs at least 2 segments to produce a non-trivial coherence value - a single segment always returns exactly 1.0, by mathematical construction, not as a bug (see the full description above). - Frequencies are expressed in cycles PER SEGMENT, not cycles per bar - a frequency of 4.0 on a 32-bar segment means one cycle every 8 bars, and the same numeric frequency means something different if InpSegmentLen changes. - Phase-derived lag wraps every full cycle (segmentLen/frequency bars) - a true lag larger than half a cycle is reported as the equivalent negative lag on the other side, the usual ambiguity in any phase-based lag estimate. - Uses the most recent (segmentLen*segmentCount) bars only; older history beyond that window is ignored, not weighted in.

Script · MT4 · v1.0 · Free

About Cross Spectral Coherence

Cross Spectral Coherence is a script for MetaTrader 4. It has 6 settings, such as Segment len, Segment count and Shared cycles. MQL source code included.

Files in the download: CrossSpectralCoherence.mqh, CrossSpectralCoherence.txt, CrossSpectralCoherenceDemo.mq4.

Settings

SettingDefaultWhat it does
Segment len32Bars per segment (DFT window length)
Segment count20Number of non-overlapping segments averaged together
Shared cycles4.0Shared sine's frequency, in cycles per segment
Lag bars3Known lag (in bars) of series Y behind series X
Noise amp1.5Independent noise amplitude added to each series
Random seed555Seed so the synthetic series are repeatable

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria