# EMA counter problem

44

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

}

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;
}
```
Moderator
10614

```int start()
{
int i,emaCount;

ArrayResize(EMA,Bars);
for(i=Bars-1;i>=0;i--)
{
emaCalc(i,emaCount);

}

return(0);
}```
Moderator
10614

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

44

Thank you Keith for your help, anyway I still get 0 as Alert. (Ema: 0).