How to Extract those Symbols Without Any Open Trades ?

 

Hi coders, good day to you. Let's say :-

I've 4 Symbols with Open Trades now :-

   string opSym[]={"AUDCHF","CADCHF","EURUSD","GBPJPY"};


My EA has a total of 12 trading symbols readily for trading (set up in user input variable) :-

   string trSym[]={"AUDCAD","AUDCHF","AUDJPY","CADCHF","CADJPY","EURCAD","EURGBP","EURUSD","GBPAUD","GBPJPY","USDCAD","USDJPY"};


I created a new array :-

   string nonOpSym[];
   ArrayResize(nonOpSym,0);


The purpose of the nonOpSym[] is to store the symbols without any open trades. Therefore, the expected output should be the balance of 8 symbols as follows :-

   string nonOpSym[]={"AUDCAD","AUDJPY","CADCHF","EURCAD","EURGBP","GBPAUD","USDCAD","USDJPY"};


Now, how am I going to filter out the symbols of opSym[] from trSym[] in order to get the nonOpSym[] ?


I tried the following way but failed :-

   for(int h=0; h<ArraySize(opSym); h++) {
      for(int i=0; i<ArraySize(trSym); i++) {
      if(opSym[h]!=trSym[i])  continue;
                              {ArrayResize(nonOpSym,ArraySize(nonOpSym)+1);
                              nonOpSym[ArraySize(nonOpSym)-1]=trSym[i];}
      }

   }


Could you please help to correct my loop ? Thanks

 

Not tested, not compiled, just typed.

template<typename T>
int ArrayFind(const T& array[], T value){
    for(int iA=ArraySize(array) - 1; iA >= 0; --iA) if(array[iA] == value) return iA;
    return EMPTY;
}
   string nonOpSym[]; int count=0;
   for(int iSym = ArraySize(trOpSym) - 1; iSym >= 0; --iSym)  if(ArrayFind(opSym, trOpSym[iSym]) == EMPTY){
       ArrayResize(nonOpSym, count+1); nonOpSym[count++] = trOpSym[iSym];
   }
Not tested, not compiled, just typed.

or

Not tested, not compiled, just typed.

#define INDEX uint
template<typename Ti, typename To>
INDEX    remove_copy(const Ti& inp[], INDEX iBeg, INDEX iEnd,
                           To& out[], INDEX oBeg, const Ti& value){
   for(; iBeg != iEnd; ++iBeg){
      if(!(inp[iBeg] == value) )                out[oBeg++] = inp[iBeg];
   }
   return oBeg;
}
///////////////
   string nonOpSym[]; int iEnd = ArrayCopy(nonOpSym, trSym);
   for(iOp=ArraySize(opSym) - 1; iOp >= 0; --iOp) iEnd =  remove-copy(nonOpSym, 0, iEnd, nonOpSym, 0, opSym[iOp]);
   // Process nonOpSym[0 … iEnd-1] 

Not tested, not compiled, just typed.

 
William Roeder #:

Not tested, not compiled, just typed.

Not tested, not compiled, just typed.

or

Not tested, not compiled, just typed.

Not tested, not compiled, just typed.

Thanks,for the option 1, would it be possible not to create a function "int ArrayFind(const T& array[], T value)" but everything will be settled in just one loop ? Thanks

 
Chin Min Wan #:

Thanks,for the option 1, would it be possible not to create a function "int ArrayFind(const T& array[], T value)" but everything will be settled in just one loop ? Thanks

     string opSym[]={"AUDCHF","CADCHF","EURUSD","GBPJPY"};
      string trSym[]={"AUDCAD","AUDCHF","AUDJPY","CADCHF","CADJPY","EURCAD","EURGBP","EURUSD","GBPAUD","GBPJPY","USDCAD","USDJPY"};
      string nonOpSym[];
      ArrayResize(nonOpSym,ArraySize(trSym)-ArraySize(opSym));
      int n = 0;
      for(int h=0; h<ArraySize(trSym); h++) 
         {
            int match = 0;
            for (int i=0;i<ArraySize(opSym);i++)
               {
                  if (opSym[i]==trSym[h])
                     {
                        match++;
                     }
               }          
            if (match<=0)
               {
                  nonOpSym[n] = trSym[h];
                  n++;
               }
         }
       ArrayPrint(nonOpSym); 

 "AUDCAD" "AUDJPY" "CADJPY" "EURCAD" "EURGBP" "GBPAUD" "USDCAD" "USDJPY"

 
Chin Min Wan #: would it be possible not to create a function but everything will be settled in just one loop ?

Of course, but why would you want to obfuscate your code?

 
Sardion Maranatha #:

 "AUDCAD" "AUDJPY" "CADJPY" "EURCAD" "EURGBP" "GBPAUD" "USDCAD" "USDJPY"

Dear Sardion, thanks. I think your way of coding should be the solution to run one loop and get the answer.

For the ...

int n = 0;

If I'm not wrong, I think the purpose is a bool flag.

 
William Roeder #:

Of course, but why would you want to obfuscate your code?

Different coders would have different style of coding, you have nothing wrong. Running one loop to get the answer without creating a new function : Sardion has already given the solution, I prefer his style.
Reason: