currency correlation code help

 
I have an EA which trades on multiple currency pair. Part of it is checking correlations and avoiding trades to limit exposure. For example if I have GBPJPY opened I don’t want my next position to contain either GBP or JPY. I’d want it to be USDCHF or AUDNZD. You get the point. Here is part of the code which does that. 
 
void OnTick()

if(Correlation_Enabled == false && IsCorrelated())return;

.

.

.

bool IsCorrelated()

  {

    string Comp = StringSubstr(CheckCurrentActiveSymbols(),0,3);  

    string Comp2 = StringSubstr(CheckCurrentActiveSymbols(),3,3);

    int Comparison = StringFind(Symbol(),Comp,0);

    int Comparison2 = StringFind(Symbol(),Comp2,3);

    if(Comparison >= 0)

    {

    return true;

    }

     else if (Comparison2 >= 3)

         {

           // Alert("Cannot place trade correlated pair");

            return true;

         } 

    return false;

  }





This doesn't work. what am I missing? any help is appreciated. 



 
chuchu777: want my next position to contain either GBP or JPY. I’d want it to be USDCHF or AUDNZD.
  1. I assume you don't mean "either" but neither.
  2. What you are checking for is only symbols beginning with GBP or ending with JPY. So you will accept EURGBP.
  3. Since you are trying to open multiple symbols, how can you filter when CheckCurrentActiveSymbols only returns one string?
Get the current chart symbol, split the base and quote currencies. Go through all open symbols and if you find either string in the open, you are done.
 
William, thanks for your reply. yes I meant neither! EURGBP is the only exception in this case and I will accept that. I am checking all pairs. The idea is for the EA to reduce exposure to any single currency. Let's say EURUSD is currently active, when the EA is ready to open the next position it would be AUDNZD, GBPJPY, NZDCHF or any of those combinations but not EURAUD, EURJPY etc which contains EUR. 
 
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. Don't double post! You already had another thread open.
              General rules and best pratices of the Forum. - General - MQL5 programming forum

 
while I kind of understand the concept of what you're saying, i am finding it hard to code it to practice. Can you help? 
 
...
   int Comparison2 = StringFind(Symbol(),Comp2, 0 );

    if(Comparison >= 0)
    {
    return true;
    }
     else if (Comparison2 >= 0 )
...
 
Airat Safin:

Thanks, doesn't look like it's working. 

 

In case when more than 1 position may be opened simultaneously such expression =>

string Comp = StringSubstr(CheckCurrentActiveSymbols(),0,3);  

string Comp2 = StringSubstr(CheckCurrentActiveSymbols(),3,3);

is incorrect because checks only 3 + 3 = 6 chars that is only 1 position

 
yes, with that it's only checking the first pair in order. If I have 4 opened positions how will that work? 
 
As a variant of solution =>


step 1> concatenate symbols of all opened positions in one buffer string => string BufferString = "" ; 

        int Length =  StringConcatenate ( BufferString , BufferString , OpenedSymbol_1 , OpenedSymbol_2 , ... ) ;
        // in cycle of course or no more than 62 at once  


step 2> parse Symbol()

        string Currency_1 = StringSubstr ( Symbol() , 0 , 3 ) ;  
        string Currency_2 = StringSubstr ( Symbol() , 3 , 3 ) ;        


step 3> search

        int Comparison  = StringFind ( BufferString , Currency_1 , 0 ) ;
        int Comparison2 = StringFind ( BufferString , Currency_2 , 0 ) ;


step 4> check

        if(Comparison >= 0)
          {
          return true;
          }
           else if (Comparison2 >= 0)
               {
                 // Alert("Cannot place trade correlated pair");
                  return true;
               } 
          return false;
        


 
Airat Safin:
As a variant of solution =>



Thanks, getting quite a few errors trying to code that, like object expected etc. Are you able to help? 

Reason: