Loading…

Maximum Entropy Spectrum

An FFT needs several multiples of a cycle's length in the window before it can separate that cycle from its neighbours - the frequency resolution of an FFT is fixed by the window length, so a 17-bar cycle buried in 40 bars of data produces a smeared, unreliable peak. Traders who want to know "is there a dominant cycle in the last N bars" rarely have the luxury of a window ten times longer than the cycle itself. Burg's method fits a compact autoregressive (AR) model directly instead of transforming the raw series. It works by minimising the forward and backward prediction error jointly at every model order, reading the reflection coefficient straight off those error powers, then updating every existing AR coefficient with that new reflection coefficient (the Levinson-style joint update that is specifically what "Burg's method" means, as opposed to a plain Yule-Walker autocorrelation fit, which needs noticeably more data to reach the same resolution and is a common disguised substitute). The result is the maximum-entropy spectral estimate: the smoothest spectrum consistent with the model's own autocorrelation, without inventing structure the data does not support. BurgDominantPeriod fits the model and scans it on a frequency grid to report the single strongest cycle length in samples. BurgFullSpectrum returns the whole power curve across all resolvable frequencies, for plotting the shape of the peak rather than trusting one number. Because the model is defined for a fixed order p supplied by the caller, this library does not attempt to auto-select p (a much harder, application- specific problem, usually decided by AIC/FPE criteria this "Lite" header does not carry) - the demo uses an order of 8 as a reasonable default for data windows of a few dozen bars. Notes and limitations - The caller chooses the AR order p; this library does not auto-select it. Too low an order under-resolves close cycles, too high starts fitting noise as if it were structure - there is no single correct value for every series, only a sensible range for a given window length. - Requires strictly more samples than the model order (n > p); BurgFit returns false rather than producing a meaningless result otherwise. - The reported "dominant period" is whichever peak has the most power in the fitted AR spectrum - on a mixed-cycle series this tracks the strongest amplitude component, not necessarily the most persistent or most tradeable one. Use BurgFullSpectrum and inspect the whole curve when more than one cycle matters. - This is a spectral READING tool, not a forecast. A dominant cycle found in the recent past is not a guarantee the market will keep cycling at that period going forward. - MQL4 has no complex-number type; BurgSpectrumAt accumulates the real and imaginary parts of the AR transfer function by hand via MathCos/MathSin, which is standard practice for this formula but worth knowing if you extend it.

Script · MT4 · v1.0 · Free

About Maximum Entropy Spectrum

Maximum Entropy Spectrum is a script for MetaTrader 4. It has 1 setting, such as Ar order. MQL source code included.

Files in the download: MaximumEntropySpectrum.mqh, MaximumEntropySpectrum.txt, MaximumEntropySpectrumDemo.mq4.

Settings

SettingDefaultWhat it does
Ar order8AR model order fed to Burg's method

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: Moving Average · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria