Problem with this simple code

 

Hi, I am fairly new to MQL4 programming.Was doing ok but I am completely perplexed by this bit of code. All I want is to find on a one minute chart, the last 60 candles and compare the closing price to the price of the 300ema, and then set up a counter of how many of those closing candles are above the comparable 300 moving average. I have set up a for loop counting down from 60 to 1, and then an if statement with a variable CounterUP that keeps count of how many candles are above ema. And then an alert at the end which should print how many.

The for statement does not seem to work properly. Any suggestions would be welcome.

Thanks


void OnTick()

  {
//---
   
   club();
   
  }
//+------------------------------------------------------------------+


   void club()
   {
   int CounterUP = 0;
   int y;
   double a=Close[y];
   double b=iMA(NULL,0,300,0,1,0,y);

   
   for(y=60;y>0;y--)
   {   
      
      if(a>b)
      {
     
       CounterUP++;  
       
      }
   }  
      
   Alert("Up=",CounterUP);   
    
   return;   
   
   
   }
 
  1. When you post code please use the SRC button! Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  3. int y;
    double a=Close[y];
    double b=iMA(NULL,0,300,0,1,0,y);
    Y has a random value, therefor a and b are also.

  4.  for(y=60;y>0;y--){   
          if(a>b){ CounterUP++;  }
       }  
    A and b never change inside the loop, so CounterUp can only be zero or 60.

  5. Don't double post!
              General rules and best pratices of the Forum. - General - MQL5 programming forum

 

Thanks for that!

Sorry about double posting!

just realised after it was in the wrong section.