Increment varibles while condition is met

 

I am trying to count the number of bars while a condition is met. 

In the chart below I want to count the number of red bars (when aray is < 0).


I want to make an EA but first I am trying to visulise it to ensure its working correctly.  I have tried to plot it in an indicator which has added another layer confusion for me.


The indicator is working correctly untill I add my while loop then MT4 literally stops working. This is the loop I have added. 

while(Squeeze[i]==1){
        if(new_bar()==false)continue;
        else if(new_bar()==true){
           for(int t=0; t<iBars(Symbol(),0)-1; t++){
              if(Squeeze[i]!=0){
                 t++;
                 continue;
              }else if(Squeeze[i]==0){
                 t=0;
                 break;
              }
           }
        }
      }


//------------------------------------------------------------

bool new_bar(){
      bool is_new_bar = false;
      datetime current_time    = TimeCurrent();
      datetime new_candle_time = iTime(Symbol(), Period(), 0);
      if (current_time == new_candle_time){
         is_new_bar = true;
      }
      return(is_new_bar);
}

But this is in inside another loop for the other arrays in the chart.

for(int i=limit; i>=0; i--) //main indicator FOR loop
     {
      // ======================
      // Indicator Calculations
      // ======================
      double MA_Hi = iMA(Symbol(),0,Keltner_Period,0,MODE_SMA,PRICE_HIGH,i);
      double MA_Lo = iMA(Symbol(),0,Keltner_Period,0,MODE_SMA,PRICE_LOW,i);
      double Kelt_Mid_Band=iMA(Symbol(),0,Keltner_Period,0,MODE_SMA,PRICE_TYPICAL,i);
      double Kelt_Upper_Band = Kelt_Mid_Band + ((MA_Hi - MA_Lo)*Keltner_Mul);
      double Kelt_Lower_Band = Kelt_Mid_Band - ((MA_Hi - MA_Lo)*Keltner_Mul);
      double Boll_Upper_Band = iBands(Symbol(),0, Boll_Period,Boll_Dev,0,PRICE_CLOSE, MODE_UPPER,i);
      double Boll_Lower_Band = iBands(Symbol(),0, Boll_Period,Boll_Dev,0,PRICE_CLOSE, MODE_LOWER,i);
      

      // ======================
      // Buffer Calculations
      // ======================
      Momentum[i]=(Close[i]-Close[i+Momentum_Period]);                                                      //blue dotted line on the chart 

      if(Boll_Upper_Band>=Kelt_Upper_Band || Boll_Lower_Band<=Kelt_Lower_Band)                              //Bollinger bands outside Keltner bands(Green bars on the chart)
        {
         Pos_Diff[i]=(MathAbs(Boll_Upper_Band-Kelt_Upper_Band)+MathAbs(Boll_Lower_Band-Kelt_Lower_Band));
         Squeeze[i] = 0;
        }
      else
        {
         Pos_Diff[i]=0;
        }

      if(Boll_Upper_Band<Kelt_Upper_Band && Boll_Lower_Band>Kelt_Lower_Band)                                   // SQUEEZE Condition- Bollinger bands are inside Keltner bands(red bars on the chart) 
        {
        int h;
         Neg_Diff[i]= -(MathAbs(Boll_Upper_Band-Kelt_Upper_Band)+MathAbs(Boll_Lower_Band-Kelt_Lower_Band));
         Squeeze[i] = 1;
        }
      else
        {
         Neg_Diff[i]=0;
        }
    
     /*
      while(Squeeze[i]==1){                                                       //while in squeeze condition
        if(new_bar()==false)continue;                                             //Calulate once per bar (function below)
        else if(new_bar()==true){
           for(int t=0; t<iBars(Symbol(),0)-1; t++){                              //incrementing loop to add 1 to each bar that is negative
              if(Squeeze[i]!=0){                                                  // if squeeze condition increment t and continue to next interation
                 t++;
                 continue;
              }else if(Squeeze[i]==0){                                            // if not squeeze condition t resets to 0 and break out of while loop
                 t=0;
                 break;
              }
           }
        }
      }
      */
   }


I have attached the whole file below. I've commentted out my while loop.  Please beware if you activate the while loop it literally stops MT4 so you will have to restart the programe. 

I have read documentation and the book but I have obviously misunderstood something (or everything!). Can someone tell me where I am going wrong?

Can you use incrementing and decrementing loops inside each other? 

I have been working with EAs so i may be misunderstanding arrays?

Is there a way to do this without using a while loop? 

My understanding is while and for loops can do pretty much the same thing but while loops are better when you don't know the total increments needed in a loop.

Help with any of these questions will be greatly appreciated.

Thanks 

Files:
 
Bruno Harris: The indicator is working correctly untill I add my while loop then MT4 literally stops working.

Indicators can not stop, they can not sleep, all loops must finish and the indicator must exit.

  1. Remove the new bar code.
  2. while(Squeeze[i]==1){
    You do not modify i or Squeeze[i] inside the loop. Therefor you have an infinite loop.
 
William Roeder:

Indicators can not stop, they can not sleep, all loops must finish and the indicator must exit.

  1. Remove the new bar code.
  2. You do not modify i or Squeeze[i] inside the loop. Therefor you have an infinite loop.

Hi William thanks for your reply.

New bar code is deleted.

Sorry I mis spoke about the the program stopping. When I ran the while loop Windows shows this message.


Am I right in thinking this is because (as you mentioned) the while loop is infinite and the program can’t leave the loop.

 

As for point two I thought I understood the logic but maybe I have misunderstood.

I will explain my thinking below. Sorry if this post is long but if I decribe MY understanding  hopefully it will be easier for someone to read a tell me where i've gone wrong 

If i add an extra break there is no more error messages but this doesn't modify the expression directly in the operators. 

while(Squeeze[i]>0){
         for(int t=1; t<iBars(Symbol(),0)-2; t++){
              if(Squeeze[t]!=0){
                 continue;
              }else if(Squeeze[t]==0){
                 break;
              }
         }
         break;
}

Am i right in thinking. 

the for loop:

for(int i=limit; i>=0; i--)

Starts from the the last bar working forward 

when the while condition is met

while(Squeeze[i]>0){

the while loop activates and

If the expression is true, the operator is executed until the expression becomes false (from the MQL4 book).

So as you said I need to modify the expression in the loop. I understand this doesn’t modify the expression directly but I’m I right thinking that the first break exits the for loop the second break exits the while loop to check if the while condition is true again. Running through the while loop until the condition is false. 

After the second  break; does it then check is the while condition is true or does it go straight to the for loop again?

 


If the expression is to be modified by the operators.

In order to look back through the bars the for loop should be related to i.

So if I

int t=1;

     while(Squeeze[i+t]>0){
         for( t=1; t<iBars(Symbol(),0)-2; t++){
              if(Squeeze[i+t]!=0){
                 continue;

              }else if(Squeeze[i+t]==0){
                 break;
              }
         }
         break;
     }

  When the previous bar to i is in condition the for loop will look back t bars from i (incrementing each time) when it finds a bar where squeeze = 0 it will break the for loop and then break the while loop.

As I mention there are no error messages but I am still unable to plot t incrementing during the squeeze condition. I would also like to see what i'm missing from my logic.

Again any help is greatly appreciated. 

Files:
Reason: