Loading…

Correlation Spike Deleverager

A book spread across several symbols looks diversified right up until those symbols start moving together, and correlation between markets is not fixed - it rises toward one exactly during the stressed conditions where diversification is needed most. This computes the Pearson correlation coefficient of daily returns for every pair of symbols currently carrying an open position (matching a magic filter), averages every pairwise result into one number, and compares it against a configurable threshold. Once the average correlation crosses the threshold, every open position is trimmed by a fraction that scales from zero at the threshold up to a configurable ceiling as the average approaches full correlation - a book that is mildly over the line is trimmed lightly, one that has gone nearly fully correlated is trimmed hard. A cooldown after a trim prevents the same spike from triggering repeated trims before the book has had a chance to actually change shape. Correlation needs at least two distinct held symbols to mean anything; with fewer than two, the tool reports that plainly rather than computing a meaningless single-symbol figure. It ships in dry-run mode by default, since it reduces real open risk on a schedule, unattended. Notes and limitations - Correlation is measured on daily close-to-close returns only; a spike that shows up intraday but has not yet moved a full daily bar will not be caught until the next daily close feeds into the calculation. - Trims are partial closes only - this never adds a hedge, and a trim that would round up to closing a position fully is skipped rather than sent. - With fewer than two distinct held symbols, no correlation figure can be computed and no trim is possible, regardless of settings. - The correlation window is fixed at a maximum of 32 daily bars by the internal buffer; InpCorrelationLookback above 32 is rejected at startup rather than silently truncated.

Expert advisor · MT5 · v1.0 · Free

About Correlation Spike Deleverager

Correlation Spike Deleverager is an expert advisor (trading robot) for MetaTrader 5. It has 10 settings, such as Dry run, Magic filter and Correlation lookback. MQL source code included.

Files in the download: CorrelationSpikeDeleverager.ex5, CorrelationSpikeDeleverager.mq5, CorrelationSpikeDeleverager.txt.

Trading robots can lose money. Check every setting and test on a demo account before using real funds.

Settings

SettingDefaultWhat it does
Dry runtruetrue = report only, never actually trims a position
Magic filter0Magic number to protect; 0 = every magic number
Correlation lookback40Daily bars used to measure each pairwise correlation
Correlation threshold0.70Average pairwise correlation above which trimming begins
Max trim percent60.0Hard ceiling on how much of any one position can be trimmed in one pass
Max symbols12Safety cap on distinct symbols scanned in one pass
Scan interval sec300Seconds between correlation recalculations
Re check cooldown min60Minutes before trimming is allowed to fire again after a trim
Max retries5Attempts per partial close before giving up
Retry wait ms500Pause between attempts, milliseconds

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Experts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
  6. Test it first on a demo account or in the Strategy Tester.

Download the PDF guide

Tags: MT5 Expert Advisor · MQL5 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria