# while in indicator crashing platform

169
2014.01.13 13:22

relative newbie trying to code an indicator that marks bars that are completely below the 20 EMA (i.e. high below 20 EMA) and only mark the first instance since there was a whole bar above the MA (i.e. low above MA) and alos the opposite (ie first whole bar above the MA). When I put in while loops to try and determine the shifts of the most recent bars above and below the MA the platform crashes. I'm not sure what I am doing wrong. I may need to separate the while loops from the for function calculating Range used in positioning of arrows but not sure how to do this. Suggestions for what I am doing wrong. Thanks

```int start()
{
if(time==Time[0])return(0);  time=Time[0];    // once per bar

int limit, i, counter;
double Range, AvgRange;
int counted_bars=IndicatorCounted();

//---- check for possible errors
if(counted_bars<0) return(-1);

//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;

limit=Bars-counted_bars;

//Calculate positons of indicator arrows above or below bars
for(i = 0; i < limit; i++)
{
counter=i;
Range=0;
AvgRange=0;

for (counter=i ;counter<=i+9;counter++)
{
AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
}
Range=AvgRange/10;

// use while to determine shift of most recent bar where Low was greater than MA
int CountLowAboveMA=2;
while(Low[i+CountLowAboveMA] <= iMA(NULL,0,20,0,1,0,i))
{
CountLowAboveMA++;
}
int ShiftLowAboveMA = CountLowAboveMA;

// use while to determine shift of most recent bar where High was less than MA
int CountHighBelowMA=2;
while(High[i+CountHighBelowMA] >= iMA(NULL,0,20,0,1,0,i))
{
CountHighBelowMA++;
}
int ShiftHighBelowMA = CountHighBelowMA;

// determine if Low is above MA and if this is the first low above the MA. print arrow on chart if true.
if (Low[i+1] > iMA(NULL,0,20,0,1,0,i) && ShiftHighBelowMA < ShiftLowAboveMA)
{
PunchUp[i+1] = Low[i+1] - Range*0.5;
}

// determine if High is below MA and if this is the first high below the MA. iprint arrow on chart if true.
if (High[i+1] < iMA(NULL,0,20,0,1,0,i) && ShiftLowAboveMA > ShiftHighBelowMA)
{
PunchDn[i+1] = Low[i+1] - Range*0.5;
}
}

return(0);
}
```
Moderator
18002
2014.01.13 13:31
strathmoreraven:

relative newbie trying to code an indicator that marks bars that are completely below the 20 EMA (i.e. high below 20 EMA) and only mark the first instance since there was a whole bar above the MA (i.e. low above MA) and alos the opposite (ie first whole bar above the MA). When I put in while loops to try and determine the shifts of the most recent bars above and below the MA the platform crashes. I'm not sure what I am doing wrong. I may need to separate the while loops from the for function calculating Range used in positioning of arrows but not sure how to do this. Suggestions for what I am doing wrong. Thanks

Probably your loops take too much time . . . you cannot decelerate the interface thread. Limit limit on the first run of the indicator to 100 bars and see how it goes . . .

14737
2014.01.13 15:12
```      int CountLowAboveMA=2;
while(Low[i+CountLowAboveMA] <= iMA(NULL,0,20,0,1,0,i))
{
CountLowAboveMA++;
}
```
What happens when i = Bars - 1? Low[i+n] is zero, ALWAYS, because it is off the chart. The Condition is ALWAYS true. Infinite loop.