Help to Advise "k+=2" and Remove "Case" Codes

 

Hi coders, need your advice for codes.


I have a Multi-Symbols EA and for exit section, the codes are as follows :-


   bool SequenceBreak=false;

   if(OrderType()==OP_BUY && OrderMagicNumber()==mnBUY) {
   string ExitSequence=StringSubstr(ExitSig(sym,cTkt,cOpTm,cOpPx,cLot,cTPPx,cSLPx),12,12);
   // Output (ExitSequence)= -0-0-0-1

      for(int k=0; k<StringLen(ExitSequence); k+=2)
      if(StringSubstr(ExitSequence,k,1)=="-") {
      int temCase=(int)StringToInteger(StringSubstr(ExitSequence,k+1,1));

      switch(temCase) {
      case 1: {
      if(MarketInfo(sym,MODE_TRADEALLOWED)==true) {

         //Print("Cs BUY "+sym+" ("+TimeToStr(TmNow)+"/Tkt "+IntegerToString(cTkt)+") /ExitSigVal:"+ExitSig(sym,cTkt,cOpTm,cOpPx,cLot,cTPPx,cSLPx)+
         //"/k:"+IntegerToString(k)+"/temCase:"+IntegerToString(temCase)+" /ExitSequence:"+ExitSequence);
         //Output : Cs BUY AUDCHF (2025.04.14 14:13/Tkt 285304721) /ExitSigVal:+0+0+0+0+0-0-0-0-0-1/k:6/temCase:1 /ExitSequence:-0-0-0-1

         if(!OrderClose(cTkt,cLot,cBid,10)) Print("Error BUY CloseOrder : ",GetLastError());
         else SequenceBreak=true;}
         break;}
         default : break;}
      if(SequenceBreak) break;}}

I think the Print Output above "Cs BUY AUDCHF (2025.04.14 14:13/Tkt 285304721) /ExitSigVal:+0+0+0+0+0-0-0-0-0-1/k:6/temCase:1 /ExitSequence:-0-0-0-1" should be able to enlighten you how the whole exit section works.


I have the following questions needing your advice and seeking your help :-


1) For the loop "for(int k=0; k<StringLen(ExitSequence); k+=2)", could you please explain how the "k+=2" works ? If you can provide some illustrations to explain would be the best. I'm a beginner, I can only understand k++ or k-- only.


2) In this exit section, it uses only one "case 1 (switch)", I think it is unnecessary / redundant. Could you please help me to modify the codes so that :-


  a) No more "case 1 (switch) + break", i.e. to remove the relevant codes totally


  b) Must keep the ExitSig(sym,cTkt,cOpTm,cOpPx,cLot,cTPPx,cSLPx) codes as it is required in other functions.


Finally, as long as [StringSubstr(ExitSequence,k,1)=="-"] finds any "-" in the ExitSequence, the OrderClose should close the BUY orders immediately. The codes must ensure that the relevant BUY order are confirmed has been closed only then stop looping the OrderClose.


Thank you.

AUDCHF (Australian Dollar vs Swiss Franc): Live Forex Charts
AUDCHF (Australian Dollar vs Swiss Franc): Live Forex Charts
  • 2025.04.14
  • www.mql5.com
AUDCHF exchange rate has changed by 0.47% for today. During the day, the instrument was traded at a low of 0.51218 and at a high of 0.51838
 

k+=2 is same as saying "Add 2 to the previous calculation of k.

ie if k is 0, then you have k+=2, then, the 2nd iteration of k will be 0 + 2.

3rd iteration of k will be 2 + 2 = 4;

4th k is 4 + 2 = 6;

 
Michael Charles Schefe #:

k+=2 is same as saying "Add 2 to the previous calculation of k.

ie if k is 0, then you have k+=2, then, the 2nd iteration of k will be 0 + 2.

3rd iteration of k will be 2 + 2 = 4;

4th k is 4 + 2 = 6;

Thanks Michael. How about for my question 2 : the removal of the "case 1 (switch) + break" ? Would you be able to help ? Thanks