Nested while within if loop issue

To add comments, please log in or register
palnewth
9
palnewth  

Hello,

I am kind of beginner, I have coded in mql4 a while loop within an if loop, but it seems not to work, let me put it. In below examples I just use ExpertRemove() instead of code, but issue is the same,

To begin below code works well, after one tick expert is removed, code works

      int f=6;
      if (f>(5))
         {
         ExpertRemove();
         } 


When adding while within if, EA does not stop, code does not work

      int f=6;
      if (f>(5))
         {
         while(f>4)
            {
            ExpertRemove();
            }
         }


Not even if I copy instruction ExpertRemove outside the loop,  code does not work

      ExpertRemove();
      int f=6;
      if (f>(5))
         {
         while(f>4)
            {
            ExpertRemove();
            }
         }  


Does anyone have same issue as me, can someone explain to me nested loop rules in MQL,

thanks in advance!!!

Fernando Carreiro
4448
Fernando Carreiro  
palnewth:

Hello,

I am kind of beginner, I have coded in mql4 a while loop within an if loop, but it seems not to work, let me put it. In below examples I just use ExpertRemove() instead of code, but issue is the same,

To begin below code works well, after one tick expert is removed, code works

int f=6;
if (f>(5))
  {
   ExpertRemove();
  }

When adding while within if, EA does not stop, code does not work

int f=6;
if (f>(5))
  {
   while(f>4)
     {
      ExpertRemove();
     }
  }

Not even if I copy instruction ExpertRemove outside the loop,  code does not work

ExpertRemove();
int f=6;
if (f>(5))
  {
   while(f>4)
     {
      ExpertRemove();
     }
  }
Does anyone have same issue as me, can someone explain to me nested loop rules in MQL,

thanks in advance!!!

Of course it will not work. The ExpertRemove() only comes into effect once the current event ends, for example only when you finish handling the OnTick() event. Your "while" loop example has no condition to allow it to break out of the loop and will just block execution and remain in an endless loop because the value of "f" never changes.

ExpertRemove

The function stops an Expert Advisor and unloads it from a chart.

void  ExpertRemove();
Return Value

No return value.

Note

The Expert Advisor is not stopped immediately as you call ExpertRemove(); just a flag to stop the EA operation is set. That is, any next event won't be processed, OnDeinit() will be called and the Expert Advisor will be unloaded and removed from the chart.

Also, please edit your post and use the SRC button on the post's toolbar to add source code. Don't just copy/paste as normal text (see the example above).
cxan_96
480
cxan_96  
int f=6;
if (f>(5)) {
   while(f>4) {
      ExpertRemove();
      f--;
   }
}
 You have to decrement f, otherwise the ending condition of your while loop will never be triggered. Thus f--.
To add comments, please log in or register