Phase Locked Cycle Tracker
A naive "cycle phase" indicator recomputes an angle bar by bar from whatever the current window of price looks like - it jumps around every time the window's shape changes slightly, and it has no notion of cycle COUNT (has the market completed 3 cycles or 4 since the indicator was attached?). A phase-locked loop instead keeps a running oscillator (a numerically controlled oscillator, NCO) whose frequency and phase are nudged, a little at a time, toward agreement with the incoming signal by a feedback loop - the same technique carrier-recovery circuits use to lock onto a noisy radio signal. The result is a phase estimate that moves smoothly (it only wraps at 2*pi, it never jumps) and an unwrapped accumulator that keeps counting past every wrap, so elapsed cycles can be read directly. The discriminator (the block that measures how wrong the current phase guess is) mixes the detrended price with the NCO's own quadrature output and low-pass filters the result to discard the double-frequency term - the classical multiplier/Costas-style phase detector. This is the simpler, well-established alternative to a full Hilbert-transform quadrature discriminator (the technique behind Ehlers' MESA-family indicators), which needs a period-dependent FIR redesign as the tracked period moves; the multiplier discriminator used here needs no such redesign and is well suited to tracking a single dominant cycle. A second-order loop filter (proportional + integral gains) drives both the tracked frequency and the instantaneous phase, the standard PLL design used in control and communications engineering, applied here to a price cycle instead of a radio carrier. Notes and limitations - This is a classical multiplier-discriminator PLL, not a full Hilbert- transform/MESA-style spectral estimator. It tracks ONE dominant cycle well; it has no mechanism to separate two simultaneous cycles of similar strength, and will settle on whichever one currently dominates the discriminator's error signal. - PllUpdate's minPeriodBars/maxPeriodBars clamp keeps the loop from wandering onto pure trend (period -> infinity) or tick noise (period -> near zero); pick a range that brackets the cycle length you actually expect for your instrument/timeframe. - The loop needs an acquisition transient (tens of bars, depending on the gains and how far the initial period guess is from the truth) before PllLockQuality reads consistently high - treat early readings as unreliable, as the demo's own printed samples show. - Detrending is a simple one-pole EMA highpass, not a dedicated bandpass filter - a very strong, fast trend component can still leak into what the discriminator sees and disturb the lock.
Script · MT4 · v1.0 · Free
About Phase Locked Cycle Tracker
Phase Locked Cycle Tracker is a script for MetaTrader 4. It has 2 settings, such as Total bars and True period. MQL source code included.
Files in the download: PhaseLockedCycleTracker.mqh, PhaseLockedCycleTracker.txt, PhaseLockedCycleTrackerDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Total bars | 600 | length of the synthetic test series |
| True period | 20.0 | true dominant cycle period baked into the test signal |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 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 · MT4 Script · MQL4 Source Code