Loading…

Correlation Aware Exposure Guard

Five 1% risk trades on five different symbol names are not automatically five independent bets. EURUSD, GBPUSD and EURGBP longs together are close to one large concentrated position on EUR, and nothing in a typical position-sizing routine notices, because each ticket is sized and risk-checked in isolation. This library measures Pearson correlation of historical returns between a candidate symbol and every symbol already held, and hands back a plain allow/block decision plus the single worst offending pair - so the log says exactly why an entry was refused, not just that it was. The correlation and basket functions are pure: they take an explicit list of symbols, not the live order pool, so the whole thing can be exercised and unit-tested without a single real position open. A thin convenience wrapper reads the actual open-order pool for the caller who wants that directly. Correlation is computed from percentage returns on closed bars of a timeframe you choose - daily is the sane default for a portfolio-level check, since intraday correlation on thin symbols is noisy enough to trigger false blocks. The library does not pick that timeframe for you, because how much history is enough is a judgement call that depends on the strategy, not something a library can decide. Notes and limitations - Correlation is estimated from a finite historical sample and is not stable - pairs that moved together last quarter can decouple without warning, most often exactly during the stress event a correlation guard exists to catch. - Needs at least 3 overlapping samples between two symbols to produce a number; fewer than that is treated as "no evidence", not zero correlation. - A symbol whose MarketInfo/history (MQL4) or SymbolInfo/history (MQL5) the terminal has not synchronised yet will simply be skipped rather than reported as an error - check the Journal if a symbol is unexpectedly absent from the matrix. - Pure computation only. Nothing here sends an order; the caller decides what "too correlated" means and what to do about it.

Script · MT4 · v1.0 · Free

About Correlation Aware Exposure Guard

Correlation Aware Exposure Guard is a script for MetaTrader 4. It has 4 settings, such as History bars, Max correlation and Max basket size. MQL source code included.

Files in the download: CorrelationRiskGuard.mqh, CorrelationRiskGuard.txt, CorrelationRiskGuardDemo.mq4.

Settings

SettingDefaultWhat it does
History bars200Bars of history per symbol
Max correlation0.70Entries blocked above this absolute correlation
Max basket size6How many Market Watch symbols to sample
Magic filter-1Magic number filter for the live-position check

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: EURUSD · GBPUSD · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria