Component Risk Decomposition
Ranking open positions by exposure size answers "what is big", not "what is risky". Two correlated positions can each look moderate individually while together carrying most of a book's real volatility; an uncorrelated position with a similar dollar size can carry almost none. Only a covariance-aware decomposition tells the two apart. Portfolio standard deviation is a positively-homogeneous degree-one function of the position weights, so Euler's theorem applies directly: sigma_p = sum_i(w_i * MarginalRisk_i), where MarginalRisk_i is (Sigma . w)_i / sigma_p - the covariance matrix times the weight vector, divided by portfolio stddev. That identity is what makes "component risk" (w_i * MarginalRisk_i per position) a genuine decomposition rather than an approximation: CRD_EulerResidual confirms in the demo that the component risks sum EXACTLY back to total portfolio volatility, with only floating-point rounding as residual. MQL4 has no dynamic 2D array, so the covariance matrix is a flat 1D array in row-major order - element (i, j) lives at index i*n+j. Every function validates the flat array's actual size against n*n before touching it, since a wrong-length flat array is a silent out-of-bounds read rather than a compile-time error. CRD_BuildCovariance additionally builds the matrix FOR the caller from per-position volatilities and a correlation matrix, since a correlation matrix (bounded -1..1, unit diagonal) is usually the more intuitive input than a raw covariance matrix. Notes and limitations - CRD_MAX_POSITIONS (20) bounds a single decomposition - raise it in the header if a book genuinely carries more open symbols at once. - CRD_BuildCovariance validates only that the correlation matrix's diagonal is 1.0 - it does not check that the matrix is a valid (positive semi-definite) correlation matrix overall. A hand-typed correlation matrix with an internally inconsistent off-diagonal set can still pass the diagonal check and produce a decomposition; feed correlations estimated from real return data, not guessed numbers. - A flat all-zero-weight book returns zero risk for every position rather than dividing by zero - this is the correct answer (no exposure, no risk to attribute), not a fallback for an error condition. - This header decomposes volatility, not tail risk (VaR/CVaR) - a position that is small under a normal-volatility assumption can still carry outsized tail risk the covariance matrix alone does not capture.
Script · MT4 · v1.0 · Free
About Component Risk Decomposition
Component Risk Decomposition is a script for MetaTrader 4. MQL source code included.
Files in the download: ComponentRiskDecomposition.mqh, ComponentRiskDecomposition.txt, ComponentRiskDecompositionDemo.mq4.
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: MT4 Script · MQL4 Source Code