Correlation Indicator: Pearson r Against A Second Symbol

Written by Dominic Walsh · Published · Last updated

The correlation indicator measures how closely two symbols have moved together. It uses Pearson r over `InpPeriod` bars. One side is the chart symbol. The other is whatever `InpRefSymbol` names. The hard part is not the maths. It is matching bars between two symbols that do not share a history.

This build ships as a compiled .ex4 for MT4 and .ex5 for MT5; the screenshots below come from EURUSD H1.

Platforms MT4 (.ex4) + MT5 (.ex5)
Chart window Separate window (levels -0.8 / 0 / 0.8)
Plots 2: Correlation +, Correlation –
Alerts popup, push notification, email, sound
Inputs 24

What the Correlation Indicator draws on the chart

Correlation + is the aqua line drawn while r sits at or above zero.

Correlation – is the tomato line drawn below zero.

Reference lines mark plus 0.80, zero and minus 0.80.

A corner panel reports the current reading and the reference symbol it resolved.

  • Correlation +: line, aqua, width 2
  • Correlation –: line, tomato, width 2
Correlation indicator on EURUSD H1 showing Pearson r against the reference symbol
Correlation indicator on EURUSD H1 showing Pearson r against the reference symbol

How the Correlation Indicator calculates its values

The two-pass formula

r takes the sum of dx times dy. That gets divided by the square root of the two sums of squares. dx is each close minus the window mean. A one-pass version subtracts two large, close numbers and loses digits on a costly instrument. Both forms agree. This one keeps its digits.

Matching bars by time

iBarShift finds the reference bar for each chart bar time. A reference bar more than one period away gets dropped. Carrying the last known close forward would give a flat line built on nothing.

Refusing to guess

The code resolves the reference name. It adds the broker suffix from the chart symbol when it has to. It also proves the symbol quotes before using it. A window with fewer than `InpMinValidBars` `30` matched bars reports nothing.

How to read the signals

A reading near plus 1 means the two closes moved almost in step.

Near minus 1 means they moved the opposite way.

Near zero means the window shows no straight-line link.

The figure describes that window, and it shifts as the window rolls.

Alert channels in this build: popup, push notification, email and sound, one message per closed bar.

`EnableAlerts` starts on and `EnablePopup` carries the text. `EnablePushNotify`, `EnableEmail` and `EnableSound` begin false. `SoundFile` picks the clip. A message fires once per closed bar, and only when the regime changes.

Every Correlation Indicator input

Core settings

Setting What it does Default
InpRefSymbol Reference symbol (broker suffix auto-detected). Reference symbol, default USDJPY with the suffix detected automatically. USDJPY
InpPeriod Correlation window N (bars). Bars in the correlation window, default `50`. 50
InpMinValidBars Min time-aligned bars needed inside the window. Aligned bars a window must have, default `30`. 30
InpStrongBand |r| at or above this = strong link. 0.80
InpModerateBand |r| at or above this = moderate link. 0.40
InpX Panel X offset (px). Panel horizontal offset, default `14`. 14
InpY Panel Y offset (px) – clears one-click panel. Panel vertical offset, default `120` so it clears the trade panel. 120
InpMaxRows Hard cap on panel rows drawn at once. Cap on panel rows drawn, default `26`. 26
InpPriceScalePx Right margin owned by the price scale (px). 62

Display settings

Setting What it does Default
InpMaxBars Max history bars to compute (0 = all). History bars computed, default `3000`. 3000
InpShowPanel Show the correlation panel. Draws the correlation panel, default `true`. on
InpCorner Panel corner (0=UL 1=UR 2=LL 3=LR). Panel corner, default `0` for upper left. 0
InpFontSize Panel font size. 9
InpFont Panel font. Consolas
InpPanelBg Panel background (teal dark). RGB 8,24,28
InpBorderColor Panel border. teal
InpTextColor Panel text. gainsboro
InpLabelEdgePadPx Padding kept clear at the chart edges (px). 10

Alert settings

Setting What it does Default
EnableAlerts Master switch for the regime change alerts, default `true`. on
EnablePopup Terminal dialog on a regime change, default `true`. on
EnablePushNotify Phone push on a regime change, default `false`. off
EnableEmail Mail delivery on a regime change, default `false`. off
EnableSound Sound playback on a regime change, default `false`. off
SoundFile Wav clip for the correlation alert, default `alert.wav`. alert.wav
Correlation indicator inputs: InpRefSymbol, InpPeriod, InpMinValidBars
Correlation indicator inputs: InpRefSymbol, InpPeriod, InpMinValidBars

Settings that fit your chart

The window you pick changes the answer. EURUSD H1 with `InpPeriod` at `50` covers two days. That reads as a short-term link. On daily charts fifty bars covers a quarter. The two often disagree, and neither is wrong.

When the Correlation Indicator fails

Pearson r sees straight-line links only. Two symbols can be tied in a way it misses.

A fifty-bar figure moves fast, so a strong reading today means little next week.

A link is not a cause, and it is certainly not a direction.

Copy the compiled Correlation Indicator file into your MQL4/Indicators folder, and the MT5 build into MQL5/Indicators, then restart the terminal and drag it onto a chart. The step-by-step guide with screenshots is at how to install MT4 and MT5 indicators.

Download the Correlation Indicator for MT4 and MT5

The zip holds the compiled Correlation Indicator build for MT4 and MT5, plus a short README. Compiled files only, no source. Enter your email below and the download link arrives in your inbox.

Download this indicator free

Enter your email and the file is yours. You also get the full MT4 and MT5 library.

  • 1,380+ indicators
  • MT4 and MT5 files
  • No spam, unsubscribe any time

FAQ

How does the tool resolve InpRefSymbol?

It tries the name as typed first. If the broker does not quote it, the code takes the suffix from the chart symbol and adds it. So USDJPY resolves to USDJPY.r, USDJPYm or USDJPY_i, depending on how your broker names things.

Why does InpMinValidBars matter?

Because a figure built on a handful of matched bars means nothing. At `30` the window must hold at least that many bars where both symbols share a time. Below that the panel reports the shortfall instead of drawing a number.

What does InpPeriod at 50 actually describe?

The link across the last fifty bars, and nothing outside them. On EURUSD H1 that is two days. Lengthen it for a steadier reading. Shorten it to catch a link that has only just formed.

Why split the line into two plots?

So the sign shows at a glance. Correlation + carries the aqua line at or above zero. Correlation – carries tomato below it. The sign-change bar goes into both buffers, which keeps the line joined rather than broken.

Are results guaranteed?

No. Trading involves risk. Results are not guaranteed; past performance is not indicative of future results. Use the Correlation Indicator as one input. Always backtest before trading live.

External references

Dominic Walsh - Forex trader and MT4/MT5 developer

About the author

Written by Dominic Walsh, a Forex trader and MT4/MT5 indicator, Expert Advisor and script developer. Every tool on forexmt4systems.com is tested on live charts before release and ships with ready-to-use compiled MT4 (.ex4) and MT5 (.ex5) files. Learn more about the trader and developer behind this site.

How we build, test and correct every tool: Editorial & Testing Policy. Trading carries risk; see the disclaimer.

Leave a Comment