Autocorrelation And Ljung-Box Tools
Any mean-reversion or momentum idea is, underneath, a bet that returns are not independent - that today's move says something statistically useful about tomorrow's. Most traders never check that assumption directly; they back-test a rule and treat a positive result as proof. This library runs the actual statistical test. AutocorrelationCompute returns the standard biased-estimator ACF at lags 0..maxLag (index 0 is always 1.0 by definition, so index k always means "lag k" with no off-by-one bookkeeping at the call site). PartialAutocorrelationCompute runs the Durbin-Levinson recursion on top of that ACF to get the PACF - the correlation at lag k after linearly removing what lags 1..k-1 already explain, which is the number that actually tells you the order of an AR process, since the raw ACF mixes in indirect correlation carried through the intermediate lags. LjungBoxTest pools the squared ACF across every lag up to maxLag into a single Q statistic and reports it against a chi-square(maxLag) null hypothesis of "no serial correlation up to this lag" - the standard portmanteau test used precisely for the question "is this whole block of autocorrelations distinguishable from zero", rather than eyeballing lags one at a time and risking a false positive from multiple testing. The p-value is a Wilson-Hilferty cube-root normal approximation to the chi-square distribution, not a full incomplete-gamma computation - accurate to roughly two significant figures for maxLag >= 2, which is enough to answer "significant at 5%?" but not precise enough to quote as an exact figure. The raw Q statistic and degrees of freedom are also returned so a result near a decision boundary can be checked against a proper chi-square table. The PACF's phi[k][j] triangular coefficient table is exactly the ragged 2D shape neither platform can express as a true dynamic 2D array (only the leftmost dimension may be dynamic). The recursion here keeps only the previous order's coefficient row alive at once, in a pair of flat 1D arrays swapped each iteration - the standard Durbin-Levinson space optimisation, and a convenient side effect of the array restriction rather than a workaround bolted on to dodge it. Notes and limitations - The p-value is an approximation, not an exact chi-square CDF - treat values well clear of 0.05 or 0.01 as reliable, and check a proper table for anything sitting close to a decision boundary. - ACF and PACF assume a stationary series. Feeding it raw non-stationary price levels instead of returns (or another already-differenced series) produces a slowly decaying ACF that reflects the trend, not genuine serial dependence - difference the series first. - All three functions read the whole series into memory at once; there is no streaming or incremental-update variant. For a rolling window recomputed every bar, call these only as often as actually needed - each call is O(maxLag * n). - LjungBoxTest requires n > maxLag; it returns false rather than dividing by a non-positive number when that is violated.
Script · MT4 · v1.0 · Free
About Autocorrelation And Ljung-Box Tools
Autocorrelation And Ljung-Box Tools is a script for MetaTrader 4. It has 1 setting, such as Max lag. MQL source code included.
Files in the download: AutocorrelationTools.mqh, AutocorrelationTools.txt, AutocorrelationToolsDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Max lag | 10 | Number of lags to compute and test |
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: Momentum · MT4 Script · MQL4 Source Code