English

EMA counter problem

 11 trisperon 2015.03.24 23: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;    } ```
 Moderator3404 GumRai 2015.03.25 00: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);   }```
 Moderator3404 GumRai 2015.03.25 00: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.2or      <=0.2