Nested while within if loop issue

 

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!!!

 
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).
 
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--.
 
Fernando Carreiro #:

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.

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).

Hi Fernando, i'm also a newbie and interested to understand use cases for [while] loops..

if f4=Current Price, and it isnt following the change on every tick, when can we use [while] statements?

I can understand that inside the while loop f4 is 'frozen' and wont trigger onTick unless it exits the while loop first.... 

So a simple [if] statement to test condition will make this work, what are the use cases for using [while] loops?

appreciate if there are any other methods?

 
JimSingadventure #: what are the use cases for using [while] loops?

Depends on what you need to do. Here's a simple case

template<typename T>
T sma_on_array(const T& A[], int iEnd, int iBeg=0){
   return sum_on_array(A, iEnd, iBeg) / (iEnd - iBeg);
}
template<typename T>
T sum_on_array(const T& A[], int iEnd, int iBeg=0){
   T sum=0;
   while(iEnd != iBeg) sum += A[--iEnd];
   return sum;
}
Reason: