Loading…

Fast Fourier Transform

Cycle analysis - finding a dominant period in price or a derived series - comes up constantly in trading system research, and neither MQL4 nor MQL5 offers anything to do it with. This library is a complete, self-contained radix-2 Fast Fourier Transform: FftForward and FftInverse, both in-place, both O(n log n), requiring only that the series length be an exact power of two, the standard constraint every radix-2 FFT shares. The inverse transform is not a second copy of the butterfly math. It is built on the conjugate identity IFFT(x) = (1/N) * conj(FFT(conj(x))) - conjugate the input, run the same FftForward used for the forward direction, conjugate and scale the result. That means there is exactly one place the actual transform arithmetic lives; a bug fix or an optimisation to the butterfly loop automatically applies to both directions instead of risking the two implementations drifting apart over time. Three windowing functions are included because a raw FFT of a finite slice of data has sharp edges the transform reads as a discontinuity, smearing each true frequency's energy into its neighbours - "spectral leakage". FftApplyHannWindow and FftApplyHammingWindow taper the signal to (near) zero at both ends before transforming; FftApplyBlackmanWindow trades a wider main lobe for a much faster roll-off, useful when a strong nearby frequency must not leak into a weaker one you are trying to measure. The demo shows the difference directly: the same off-bin tone transformed with and without a Hann window, side by side. FftMagnitudeSpectrum and FftBinFrequency turn the raw real/imaginary output into what most callers actually want - a magnitude per frequency bin, and the Hz value each bin index corresponds to for a given sample rate. Only the first n/2+1 bins are returned for a real-valued input, because that spectrum is symmetric and the mirrored half carries no new information. Notes and limitations - Series length must be an exact power of two for both FftForward and FftInverse; pad shorter data with zeros to the next power of two rather than trimming it, since trimming discards real samples and zero-padding only adds frequency resolution, not new information. - Windowing functions operate on a real-valued series and are meant to be applied before FftForward, not after - windowing a spectrum after the transform does not have the same effect. - FftMagnitudeSpectrum discards phase; if phase information is needed, compute MathArctan-based phase directly from the returned real[]/imag[] bins yourself. - This is a general-purpose numeric transform, not a trading signal - a detected cycle is a statistical property of the sampled window, not a guarantee it persists into the next one. - Self-contained by design: this header cannot use ComplexNumbers.mqh from elsewhere in this catalogue, so it works on parallel real[]/imag[] arrays instead of a Complex struct throughout.

Script · MT4 · v1.0 · Free

About Fast Fourier Transform

Fast Fourier Transform is a script for MetaTrader 4. It has 1 setting, such as Sample count. MQL source code included.

Files in the download: FastFourierTransform.mqh, FastFourierTransform.txt, FastFourierTransformDemo.mq4.

Settings

SettingDefaultWhat it does
Sample count64FFT length - must be a power of two

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