Skip to content
All library documents

Measuring Portfolio Risk with Covariance Matrices in MQL5

Article MQL5 articles

Summary

The article shows why individual asset volatility is insufficient for measuring multi-instrument portfolio risk: it describes each instrument’s variability but omits how returns move together. It introduces sample covariance and arranges pairwise covariances and individual variances into a matrix. Portfolio variance is then calculated from the weights and the full covariance matrix, including cross terms that can raise risk when assets move together or lower it when they offset one another.

A practical MQL5 script is presented to fetch multi-symbol returns, calculate covariance, and compare a naive variance estimate with one that accounts for covariance, using matrix operations backed by OpenBLAS. The article gives an example in which the gap between the estimates can be material during directional markets, and emphasizes that the result changes with the chosen sample period and market conditions. It is an introductory risk measurement tool; covariance is historical and does not guarantee future relationships or losses.

Key ideas

  • Single-instrument standard deviation does not describe relationships among portfolio holdings.
  • A covariance matrix combines each instrument’s variance with pairwise return relationships.
  • Portfolio variance includes cross terms that can increase or decrease total risk.
  • The MQL5 example uses matrix operations to compare naive and covariance-aware portfolio estimates.
  • Estimated risk depends on the lookback period and the prevailing correlation environment.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.