Singular Spectrum Analysis
An ordinary low-pass or high-pass filter needs a cutoff frequency chosen in advance. Singular Spectrum Analysis instead lets the data's own covariance structure decide what counts as trend versus oscillation versus noise. It works by embedding the 1D series into a 2D trajectory (Hankel) matrix of overlapping lagged windows, decomposing that matrix's covariance structure by eigenvalue/eigenvector (largest eigenvalue explains the most variance, in strict descending order), and reconstructing each component back into a 1D series by averaging over the matrix's anti-diagonals - the standard "diagonal averaging" step of SSA. This library implements the genuine core of the method, not a shortcut approximation of it. SsaBuildTrajectoryMatrix performs the real embedding step. JacobiEigenSymmetric performs a real, exact eigendecomposition of the resulting symmetric covariance matrix using the classic cyclic Jacobi rotation method - not a power-iteration shortcut that only recovers the single dominant eigenvector - which is tractable and numerically well-behaved at the small window lengths (tens of dimensions, not thousands) this library targets. SsaDiagonalAverage performs the real reconstruction step. SsaDecompose ties the three together into one call. HONEST SCOPE NOTE, read before trusting the grouping. What is genuinely simplified here, and documented plainly in the header, is the GROUPING step: a full SSA toolkit groups eigentriples by inspecting their w-correlation to decide algorithmically which ones represent the same oscillatory mode and should be summed together - a substantial analysis step in its own right. This library instead uses a fixed grouping: component 0 (the largest eigenvalue) is always the trend, the next few (capped at four, or fewer for a small window) are always summed as "oscillation", and everything past that is summed as "noise". That fixed split works well on a series with one dominant smooth trend and one dominant cycle - the common case, and what the demo constructs - but it is a documented heuristic, not automatic mode detection. Inspect the returned eigenvalue spectrum yourself (SsaDecompose returns it) on a series with two comparably-sized cycles at different periods rather than trusting the default split blindly. The decomposition itself is exact regardless of the grouping choice: the demo verifies trend + oscillation + noise sums back to the original series to within floating-point rounding, on every bar. Only the LABEL attached to each summed group is a heuristic - the arithmetic underneath it is not. Notes and limitations - The trend/oscillation/noise grouping is a fixed heuristic, not automatic mode detection - see the honest scope note above in full. - Window length must be at least 2 and strictly less than the series length; SsaDecompose refuses cleanly (returns false) outside that range, demonstrated in the demo's edge cases. - The Jacobi eigendecomposition is exact but iterative (bounded by maxSweeps, default 100, with an early exit once off-diagonal energy is negligible) - fine at the small window lengths (tens of dimensions) SSA windows produce, not intended for large general symmetric eigenproblems. - No live account, order, or symbol-info call anywhere in this library - pure array/matrix arithmetic, no trading risk of its own.
Script · MT4 · v1.0 · Free
About Singular Spectrum Analysis
Singular Spectrum Analysis is a script for MetaTrader 4. It has 3 settings, such as Bar count, Window length and Random seed. MQL source code included.
Files in the download: SingularSpectrumAnalysis.mqh, SingularSpectrumAnalysis.txt, SingularSpectrumAnalysisDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Bar count | 80 | Synthetic series length |
| Window length | 20 | SSA embedding dimension (L) |
| Random seed | 5 | Seed for the noise component |
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 · Trend · MT4 Script · MQL4 Source Code