Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 893

 
Seric29:

There is a question like this. So, there is a loop

The 1stbreak is located not in first parentheses of the loop but in parentheses nested inside them. The 2ndbreak is even deeper nested in inner parentheses. As I understood in this casebreak simply doesn't work, to tell the truth I didn't understand if it worked or not but the program hung up and made unnecessary iterations. In order to exit a loop with a lot of conditions I added an exit flag to the loop. Maybe it seemed to me that it doesn't work, who thinks on this issue?

If the code is styled normally, you can see right away that you've made a rubbish out of it:

for(...........)
  {
   if(.........)
     {
      //что-то делаем
     }
   else
      if(.........)
        {
         break
        }
      else
        {
         break;
        }
  } 

Exactly the same:

for(...........)
  {
   if(.........)
     {
      //что-то делаем
     }
   else
      break;
  } 
 

Unable to update the candlesticks and add ticks.

Here is the code for the indicators:

 long  id_;  
 const  SYMBOL SYMB (  _Symbol  +  "Tick_exp2"  );  
 int  OnInit  ()  
   {  

     if  (SYMB.IsExist ())  
      {  
       SYMB.CloneProperties ();  
                              
       int  rates_total = candles;  

       MqlRates  Rates [], Replace [];  

       int  total_candles =  0  ;  
       for  (  int  i =  CopyRates  (  _Symbol  ,  PERIOD_M1  ,  0  , rates_total, Rates) -  1  ; i> =  0  &&!  IsStopped ();  i--)  
         {  
         MqlTick  Ticks [], ReplaceTicks [];  
         int  vv =  CopyTicksRange  (  _Symbol  , Ticks,  COPY_TICKS_ALL  ,  ulong  (Rates [i] .time) *  1000  ,  ulong  (Rates [i] .time +  61  ) *  1000  );  
          total_candles + = vv;  
         ArrayResize  (Replace, vv-  1  );  ArrayResize  (ReplaceTicks, vv-  1  );  
         for  (  int  ii =  0  ; ii <vv-  1  &&!  IsStopped ();  ii ++)  
            {  
             ReplaceTicks [ii] .bid = Ticks [ii] .bid;  
             ReplaceTicks [ii] .ask = Ticks [ii] .ask;  
             ReplaceTicks [ii] .flags =  TICK_FLAG_BID  ;  
             ReplaceTicks [ii] .last = Ticks [ii +  1  ] .bid;  
             ReplaceTicks [ii] .time = Ticks [ii] .time;  
             ReplaceTicks [ii] .time_msc = Ticks [ii] .time_msc;  

             ReplaceTicks [ii] .volume = Ticks [ii] .volume;  
             ReplaceTicks [ii] .volume_real = Ticks [ii] .volume_real;  

             Replace [ii] .time = Ticks [ii] .time;   
             Replace [ii] .open = Ticks [ii] .bid;  
             Replace [ii] .close = Ticks [ii +  1  ] .bid;  
             if  (Ticks [ii] .bid> Ticks [ii +  1  ] .bid) {Replace [ii] .high = Ticks [ii] .bid;  Replace [ii] .low = Ticks [ii +  1  ] .bid;}  
             else  {Replace [ii] .high = Ticks [ii +  1  ].  bid;  Replace [ii] .low = Ticks [ii] .bid;}  
             Replace [ii] .tick_volume =  long  (Ticks [ii] .volume);  
             Replace [ii] .real_volume =  long  (Ticks [ii] .volume_real);  
             Replace [ii] .spread =  int  (  fabs  (Ticks [ii] .bid-Ticks [ii + 1  ] .bid) *  100000  );  

            }  
        
         CustomTicksAdd  (SYMB.Name, ReplaceTicks);  
         CustomRatesUpdate  (SYMB.Name, Replace);  

         }  
       if  (SYMB.On ()) 
         {  
          id_ =  ChartOpen  (SYMB.Name,  PERIOD_CURRENT  );  
         ChartSetInteger  (id_,  CHART_AUTOSCROLL  ,  1  );  
         ChartSetInteger  (id_,  CHART_MODE  ,  2  );  
         ChartSetInteger  (id_,  CHART_COLOR_CHART_LINE  ,  clrYellow  ); 
         ChartSetInteger  (id_,  CHART_SHIFT  ,  0  ,  ChartGetInteger  (  ChartID  (),  CHART_SHIFT  ));  
         } 
          Print("Всего свечей обновлено :=> ",total_candles); 
      }  
   return  (  INIT_SUCCEEDED  );  
   } 

The indicator is then applied to check bets:

 int  OnInit  ()  
   {  

   return  (  INIT_SUCCEEDED  );  
   }  

 Int  OnCalculate  (  const  int  rates_total,  
                 const  int  prev_calculated,  
                 const  datetime  & time [],  
                 const  double  & open [],  
                 const  double  & high [],  
                 const  double  & low [],  
                 const  double  & close [],  
                 const  long  & tick_volume [],  
                 const  long  & volume [],  
                 const  int  & spread [])  
   {  

   if (newbar.IsNewBar (  "EURUSD"  ,  PERIOD_M1  ))  
    {  
       Print("Цены доступны от индикатора :=> ", rates_total);
    }  
   return  (rates_total);  
   } 

Exit:

If the candlesticks are fully updated, why does rate_total only show 50% of the candlesticks?

Please advise me.


Files:
 
Artyom Trishkin:

With normal code styling, it's immediately obvious that it's bullshit:

So in my case it may not work correctly and it really may not exit the loop or will it exit the closest loop in the body of which the condition is located anyway?

 
Seric29:

So in my case it might not work correctly and it might not actually exit the loop, or will it exit the closest loop whose body contains the condition anyway?

I don't see the loop conditions - you have a dash there:

for(...........)
 
Artyom Trishkin:

I don't see the loop condition - you have a dotted line there:

It's not about the loop condition, it's about the fact that there may be more ifs inside the if and one of those ifs will have to generate an exit, so the question is, does the depth of the condition affect whether the loop will leave or not?

     for(...........)
     {
     if(........)
       {if(.....)break; else
        if(.....)
           {
           if(....)break;}}else break;
     }  
 
Seric29:

so the question is: does the depth of the condition affect whether the loop will leave or not?

it does not, break will break the closest loop body, i.e. the one in which it is currently executed, and how many times you use if() does not matter, neither the number of iF() nor their depth

well, part of the sentence, if you want to break the body of the loop by different conditions, sometimes it's much easier to use while() and the loop interrupt flag, like this:

int i=Bars-1;
bool calc = true;
while(i>=0 && calc)
{
    if(...) calc = false;
        else
        {
        ....            
                if(...) calc = false;
        }

   i--;
}
 
Igor Makanu:

does not affect, break will break the closest loop body, i.e. the one in which it is currently executed, and how many times you use if() does not matter, neither the number of iF() nor their depth

well, part of the sentence, if you want to break a loop body by different conditions, sometimes it's much easier to use while() and a loop interrupt flag, something like this:

I see what I'm getting at. Well yes it can be complicated and sometimes it's really better to use a flag. I'm just experimenting, I see it hangs, I thought it may endlessly loop, thanks for the answers.

 
Seric29:

There is a question like this. So, there is a loop

The 1stbreak is located not in first parentheses of the loop but in parentheses nested inside them. The 2ndbreak is even deeper nested in inner parentheses. As I understood in this casebreak simply doesn't work, to tell the truth I didn't understand if it worked or not but the program hung up and made unnecessary iterations. In order to exit a loop with a lot of conditions I added an exit flag to the loop. Maybe it seemed to me that it does not work, who thinks on this issue?

     for(...........)              
     {
          if(.........)
          {
               /* что-то делаем */
          }
          else
          {
               // Непонятно зачем? Если при любом варианте выходим из цикла
               if(.........) break;
               else break;
          }
     }
I would go like this
     for(...........)              
     {
          if(/* не соответствует условие */) break;
          /* что-то делаем */
     }
Although maybe I didn't quite get the point of your idea.
 
Konstantin Nikitin:
I would do it like this
Although, maybe I don't quite get the point of your idea.

The main thing is not to get confused. There are cases when you open another loop inside a loop and need to exit both loops at once, but mql4 does not provide this only through a flag, C++ has goto operator I wonder how it works.

 
Seric29:

The main thing is not to get confused. There are cases when you open another loop inside a loop and need to exit both loops at once, but mql4 does not provide this only through a flag, C++ has goto operator I wonder how it works.

You do all loops in a function, and if you need to exit any nested one - even three hundredth - return;

Reason: