Correlated Scenario Sampler
Drawing each instrument's random return independently and summing them into a portfolio-level scenario silently assumes zero correlation between every pair of instruments. That is wrong for almost any real basket, and it understates tail risk whenever the basket is actually positively correlated - which is the normal case for most FX majors moving together against broad USD strength or weakness, or for related index/commodity pairs. The standard fix, implemented here, is a Cholesky decomposition of the target correlation matrix. CholeskyDecompose factors a symmetric positive-definite correlation (or covariance) matrix into a lower triangular matrix L such that L multiplied by its own transpose reproduces the original matrix. Once L is known, applying it to a vector of independent standard-normal draws produces a correlated vector that reproduces the target correlation structure - the whole point of the exercise. The library also refuses cleanly rather than producing silent garbage: a correlation matrix that is not genuinely positive-definite (for example, one assembled from three pairwise correlations that cannot simultaneously be true of any real covariance structure) is detected during decomposition and CholeskyDecompose returns false rather than continuing with a nonsensical factor. The demo deliberately constructs one such impossible matrix to show the refusal happening. Matrices are passed as flat row-major arrays because MQL4 cannot resize a second array dimension at runtime - only the first dimension of an array can be dynamic. An n by n matrix element at (row, col) therefore lives at index row*n+col throughout this library. Notes and limitations - Uses MathRand() as its uniform source via a local Box-Muller transform - adequate for scenario generation and demonstration, not a cryptographic-quality generator. - CholeskyDecompose only accepts a genuinely positive-definite matrix. A correlation matrix built from mismatched pairwise estimation windows (each pair correlated over a different date range, for example) can easily fail this test - assemble every pairwise correlation from one consistent window before passing it in. - The generated scenarios have unit variance per asset, matching the input correlation matrix directly. Multiply each column by that asset's own volatility afterward to turn a standardised shock into an actual return scenario. - No live account, order, or symbol-info call anywhere in this library - it is pure array/matrix arithmetic and carries no trading risk of its own, only whatever risk a caller's downstream use of the scenarios introduces.
Script · MT5 · v1.0 · Free
About Correlated Scenario Sampler
Correlated Scenario Sampler is a script for MetaTrader 5. It has 2 settings, such as Num scenarios and Random seed. MQL source code included.
Files in the download: CorrelatedScenarioSampler.mq5.mqh, CorrelatedScenarioSampler.txt, CorrelatedScenarioSamplerDemo.ex5, CorrelatedScenarioSamplerDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Num scenarios | 5000 | How many correlated draws to generate |
| Random seed | 42 | Seed for repeatable output |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT5 Script · MQL5 Source Code