ACF Period Detector
A series with a genuine repeating pattern - a session structure, a seasonal wobble, an oscillator with a dominant swing length - correlates strongly with a lagged copy of itself at that pattern's period (and its integer multiples). This header computes that autocorrelation across a range of lags and reports which lag correlates most strongly, as a cheap estimate of the dominant cycle length. MQL4 has no built-in FFT or spectral analysis function at all, so a lag-scan autocorrelation approach is the practical route to a cycle-length estimate without hand-writing a full discrete Fourier transform. The peak value itself matters as much as its lag. A peak near 1.0 is strong evidence of a genuine, tight cycle; a peak only modestly above its neighbours is weak evidence and the "detected period" should be treated as a rough hint rather than a confirmed fact. The demo prints the full lag-by-lag table alongside the detected peak so this distinction is visible rather than hidden behind a single number. Notes and limitations This is a plain lag-scan autocorrelation, not a full spectral estimator - it will often also show a strong response at integer multiples of the true period (a 2x-length harmonic), and picking the single global maximum can occasionally lock onto a harmonic instead of the fundamental on a noisy series. Inspecting the full table via AcfComputeAcf, not just the single AcfDetectPeriod answer, is the safer approach for anything that matters. The estimator assumes evenly spaced samples (bar index order) - it does not account for real calendar gaps such as weekends. A very short series relative to the requested maxLag gives a noisy, low-confidence estimate; the header clamps maxLag to the series length but does not refuse a short series outright, so check peakAcfValue before trusting a short-series result. The autocorrelation math here is implemented independently within this header, not shared with or included from any other project in this catalogue.
Script · MT5 · v1.0 · Free
About ACF Period Detector
ACF Period Detector is a script for MetaTrader 5. It has 3 settings, such as Sample count, True cycle and Max lag to scan. MQL source code included.
Files in the download: AcfPeriodDetector.mq5.mqh, AcfPeriodDetector.txt, AcfPeriodDetectorDemo.ex5, AcfPeriodDetectorDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Sample count | 120 | length of the synthetic test series |
| True cycle | 9 | the cycle length baked into the synthetic series |
| Max lag to scan | 30 | maximum lag to scan when detecting the period |
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: Oscillator · Trading Sessions · MT5 Script · MQL5 Source Code