# Correlation Filter

**Benjamin David Hardman:**

Hello,

I have written a correlation filter in to stop my EA from opening a trade if Symbol() has a correlation over 75% or under -75% of any open positions. I've been running it on a live non-execute account for the last two weeks and there are numerous positions which have been placed which are clearly correlated.

The calculation is broken down into 2 functions:

1. Checking Correlation between X and Y

2. Scanning through open position symbols

So using these I use the following logic:

As mentioned, I still get trades such as USDJPY Long and USDCAD Short open at the same time which are clearly correlated instruments.

Any ideas?

You are checking multiple positions/symbols in a loop and yet only have a single bool trading_allowed, so the bool will only ever reflect the status of the last position/symbol checked when the loop finishes.

You need to only check the correlation and set the bool if the position/symbol is the desired one and not for each position in the loop.

**Paul Anscombe #:**

You are checking multiple positions/symbols in a loop and yet only have a single bool trading_allowed, so the bool will only ever reflect the status of the last position/symbol checked when the loop finishes.

You need to only check the correlation and set the bool if the position/symbol is the desired one and not for each position in the loop.

Ah I see. What do you think about changing it to a MAX kind of function. So returning the max or min correlation to Symbol() across all open positions and then only allow trading if the max is under 75% and the min is over -75%

**Benjamin David Hardman #:**

Ah I see. What do you think about changing it to a MAX kind of function. So returning the max or min correlation to Symbol() across all open positions and then only allow trading if the max is under 75% and the min is over -75%

your strategy, you will have to decide what you want it to do.

Yes of course.

If I wanted to find the max correlation of Symbol() vs. all open symbols, would something like this work?

double max_correlation() { double max_correlation = 0; for(int i=PositionsTotal()-1; i>=0; i--) { ulong ticket=PositionGetTicket(i); if(ticket > 0) { string symbol = PositionGetString(POSITION_SYMBOL); max_correlation = MathMax(correlation(Symbol(),symbol)) } } return max_correlation; }

**Benjamin David Hardman #:**

Yes of course.

If I wanted to find the max correlation of Symbol() vs. all open symbols, would something like this work?

MathMax requires two parameters so you need to include max_correlation as a parameter as well so that it is comparing it's current value.

but how will you know which symbol it is for at the end? you will need to do it differently with an if statement and then store the relevant symbol as well if the condition is true so that you know what to trade.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets

You agree to website policy and terms of use

Hello,

I have written a correlation filter in to stop my EA from opening a trade if Symbol() has a correlation over 75% or under -75% of any open positions. I've been running it on a live non-execute account for the last two weeks and there are numerous positions which have been placed which are clearly correlated.

The calculation is broken down into 2 functions:

1. Checking Correlation between X and Y

2. Scanning through open position symbols

So using these I use the following logic:

As mentioned, I still get trades such as USDJPY Long and USDCAD Short open at the same time which are clearly correlated instruments.

Any ideas?