Correlation Filter

 

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

double correlation (string symbol1, string symbol2)
  {
   double buf1[],buf2[];
   ArrayInitialize(buf1,0);
   ArrayInitialize(buf2,0);
   int copied1=CopyClose(symbol1,timeframe,1,period,buf1);
   int copied2=CopyClose(symbol2,timeframe,1,period,buf2);
      
   double correlation;
   
   double output = 0;
   
   if(method == Pearson && MathCorrelationPearson(buf1,buf2,correlation))
      output = correlation;
   
   return NormalizeDouble(output*100,2);
  }


2. Scanning through open position symbols

bool correlation_ok()
  {
   bool trading_allowed = true;;
   for(int i=PositionsTotal()-1; i>=0; i--)
     {
      ulong ticket=PositionGetTicket(i);
      if(ticket > 0)
        {
         string symbol = PositionGetString(POSITION_SYMBOL);
         
         if(correlation(Symbol(),symbol) > -60 && correlation(Symbol(),symbol) < 60)
         trading_allowed = true;
         else
         trading_allowed = false;
        }
     }  
   return trading_allowed;
  }  

So using these I use the following logic:

if(signal && correlation_ok() == true) enter trade

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?

 
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.

 
Paul Anscombe #:

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.

Reason: