Discussing the article: "Measuring What Matters (Part 2): Building the Covariance Matrix: Eigenvalue Decomposition and Risk Factor Analysis in MQL5"

 

Check out the new article: Measuring What Matters (Part 2): Building the Covariance Matrix: Eigenvalue Decomposition and Risk Factor Analysis in MQL5.

In Part 2, we introduce a reusable CCovarianceMatrix class that computes and stores a covariance matrix from raw return series using MQL5's native Cov() method. We verify symmetry, print a labeled matrix grid, and call Eig() to obtain eigenvalues and eigenvectors. Readers see how symbols co-move and which factors drive variance, enabling clearer portfolio diagnostics and reuse in scripts or EAs.

In Part 1 of this series, we built PortfolioRiskAnalyzer.mq5 — a script that exposed a gap most multi-symbol traders never see. By computing the full covariance matrix using the native OpenBLAS .Cov() method and then performing the true portfolio variance calculation as a proper matrix multiplication — wᵀ Σ w — we showed that the true risk of holding EURUSD, GBPUSD, and XAUUSD simultaneously can be materially higher than what naive single-instrument volatility suggests.

What Part 1 produced was a single number: the hidden risk gap. That number was concrete, readable, and immediately useful. It compressed the covariance information into one output. The 3 × 3 matrix was computed by all_returns.Cov() and used for wᵀ Σ w, but it was never stored or printed. The reader could see the gap but could not yet see inside the matrix that produced it.

Part 2 changes that. We introduce the CCovarianceMatrix class—an object-oriented wrapper around OpenBLAS .Cov(). It stores the covariance matrix, verifies symmetry, and prints a labeled grid. It then calls the native MQL5 .Eig() method to obtain eigenvalues and eigenvectors, showing which factors drive risk and how each symbol loads on them.

The practical project for Part 2 is CovarianceMatrixPrinter.mq5. It builds a 3 x 3 covariance matrix, verifies symmetry, prints a labeled grid, and generates a risk-factor report that exposes the portfolio's factor structure.

Author: Kayode Michael Oyetunde