Goertzel Frequency Detector
Most cycle analysis code reaches for a full spectrum transform even when the actual question is narrow: "is there currently a 20-bar cycle, yes or no, and how strong is it?" An FFT answers that by computing every frequency bin and then searching the result for the one you wanted. The Goertzel algorithm answers it directly, in a single O(N) pass, with no bit-reversal, no complex input buffer, and no requirement that the window length be a power of two. The core is the standard second-order IIR recurrence: one real state variable updated once per sample, with the real and imaginary parts of the result recovered algebraically from just the last two states at the end. There is no per-sample complex multiply, which is what makes it cheap enough to run on every tick rather than once per bar close. Windowing matters more than it looks like it should. A pure sine that does not complete a whole number of cycles inside the sample window has energy that leaks into neighbouring frequencies, and an unwindowed evaluation reports a smeared, understated magnitude for exactly the cycle you are testing for. A Hann window function is included and applied by the price-scan convenience wrapper by default. GoertzelStrongestPeriod turns single-period evaluation into a small scan: give it a handful of candidate periods and it returns which one currently dominates, which is the more honest way to use this than committing to one guessed period and trusting whatever number comes back. Magnitude on raw, unwindowed price has no fixed scale - it depends on price level, volatility and window length all at once. GoertzelNormalizedStrength gives a rough 0-1 figure for cases where a single number is wanted, but comparing several candidate periods against each other is the more reliable use of this library than reading one magnitude in isolation. Notes and limitations - Goertzel tests periods you specify; it does not discover a period you have not thought to test. Scan a reasonable range of candidates rather than evaluating one guess and trusting the result. - Magnitude on raw price has no absolute scale. Compare candidates against each other within one scan rather than against a magnitude threshold picked in isolation, or use GoertzelNormalizedStrength as a rough guide. - Like any cycle measurement on a finite window, results lag - the last cycle or so of data materially affects the reading, so treat detection as a regime signal, not an exact turning-point predictor. - The synthetic test in the demo uses MathRand() for noise, so its exact numbers vary between platforms; the qualitative result (correct period wins) should not.
Script · MT5 · v1.0 · Free
About Goertzel Frequency Detector
Goertzel Frequency Detector is a script for MetaTrader 5. It has 4 settings, such as Synthetic length, Synthetic period and Price bars. MQL source code included.
Files in the download: GoertzelDetector.mq5.mqh, GoertzelDetector.txt, GoertzelDetectorDemo.ex5, GoertzelDetectorDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Synthetic length | 200 | Bars in the synthetic test series |
| Synthetic period | 20.0 | True cycle length injected into the test series |
| Price bars | 200 | Bars of chart Close pulled for the live scan |
| Price shift | 0 | Shift (bars back) where the live scan window starts |
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