Download MetaTrader 5
To add comments, please log in or register
Join MQL5 Cloud Network. Millions of traders are waiting for this!
Stefano Specia
44
Stefano Specia 2015.03.24 22:56 
Hi guys, I want to write a simple loop that counts how many times some part of a candle (be it Low or High) touches the 200-p EMA.
Unfortunately whenever I start the program, it automatically exits without doing anything. 

Any suggestions? 
Thank you!

double EMA[];


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int init(){



return(0);}
int start()
  {
  int i,emaCount;
      
      
   
   for(i=Bars;i>=0;i--)
     {
      emaCalc(i,emaCount);
      if(i==0)Alert("EMA: ",emaCount);
      
     }

   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void emaCalc(int i,int emaCount)
  {
   
   EMA[i]=iMA(NULL,0,200,0,MODE_EMA,PRICE_CLOSE,i);
   if((High[i]==EMA[i]) || (Low[i]==EMA[i]) || ((High[i]-EMA[i])>=-0.2) || ((High[i]-EMA[i])<=0.2) || ((Low[i]-EMA[i])>=-0.2) || ((Low[i]-EMA[i])<=0.2))
   emaCount+=1;
   }
Keith Watford
Moderator
7887
Keith Watford 2015.03.24 23:38  
int start()
  {
  int i,emaCount;
      
      
   ArrayResize(EMA,Bars);
   for(i=Bars-1;i>=0;i--)
     {
      emaCalc(i,emaCount);
      if(i==0)Alert("EMA: ",emaCount);
      
     }

   return(0);
  }
Keith Watford
Moderator
7887
Keith Watford 2015.03.24 23:43  

emaCount should be declared Globally or passed to the function by reference. It is always a good idea to set it to zero before any calculations

High[i]-EMA[i])>=-0.2) || ((High[i]-EMA[i])<=0.2

 You will find that ANY number you can think of will ALWAYS be 

either >=-0.2

or      <=0.2

Stefano Specia
44
Stefano Specia 2015.03.25 14:19  
Thank you Keith for your help, anyway I still get 0 as Alert. (Ema: 0).
/
To add comments, please log in or register