# Question with for(i=0; i&lt;=limit; i++)

267

Good morning,

I write a simple EA below to run my indicator

if (lastCandle != Time[0])
{
if(iCustom(NULL,0,"MY_INDICATOR",0,1)==2)
sl=Close[1]-0.01;
tp2=Close[1]+0.01;
tp1=Close[1]+0.01;
}

I find that this trading will run correctly if in my indicator there is an EQUAL sign on limit condition

int limit=rates_total-prev_calculated;
if(limit<0) limit=2;
......
for(int i=0;i<=limit;i++)

Without that EQUAL sign, it still trade but the entry is wrong in many occasion.

Can you tell me why it is like that?

Many thanks,

SCFX

2584

It is really not possible to say without seeing the rest of the indicator code.

267

The indicator (MY_INDICATOR) that feed to the trading EA is

int limit=rates_total-prev_calculated;
if(limit<0) limit=2;
if(prev_calculated>0)
limit++;

//-----------------------------------------------------------------------------------------

for(int i=1;i<limit;i++)
{ cobom4roc[i]=
iCustom(NULL,0,"H_roc",H_roc_windowshort,200,0,i) +
iCustom(NULL,0,"H_roc",H_roc_windowshort,200,0,i)
;
if(combo4roc[i]>0.01) feedbuffer[i]=2;}

And the original H_roc indicator is:

int limit=rates_total-prev_calculated;
if(limit<0) limit=2;
if(prev_calculated>0)
limit++;

for(int i=1;i<=limit   && (i+shortwindow)<rates_total;i++)
{ roc_short=Close[i]/Close[i+shortwindow]-1;

}
15598

scfx: I find that this trading will run correctly if in my indicator there is an EQUAL sign on limit condition
int limit=rates_total-prev_calculated;
if(limit<0) limit=2;
1.  I'd recommend ignoring those (and open[],close[],etc.) and just set buffers as series and count down (using Open[], Close[], etc) using the simpler: int ic = IndicatorCounted(); if(ic < LOOKBACK) ic = LOOKBACK; for(iBar = Bars - 1 - ic; iBar >= 0; iBar--){ ...

2. When you count up useing rates_total and prev_calculated ... return(rates_total) there are two possibilities. If prev_calculated==0 you must loop [0..rates_total-1] (pos<rates_total or all bars) but if prev_calculated != 0 you loop from [prev_calculated-1 .. rates_total - 1] (recalculating the previously forming bar next time.) You have to check in calculating limit. You can NOT use rates_total - prev_calculated. That is your "EQUAL sign on limit condition" problem.
3. If you return(rates_total-1) than you can just use [prev_calculated .. rates_total -1] for both cases.
267

Thank you, WHRoeder.

SCFX

2584

WHRoeder:
1.  I'd recommend ignoring those (and open[],close[],etc.) and just set buffers as series and count down (using Open[], Close[], etc) using the simpler:

2. When you count up useing rates_total and prev_calculated ... return(rates_total) there are two possibilities. If prev_calculated==0 you must loop [0..rates_total-1] (pos<rates_total or all bars) but if prev_calculated != 0 you loop from [prev_calculated-1 .. rates_total - 1] (recalculating the previously forming bar next time.) You have to check in calculating limit. You can NOT use rates_total - prev_calculated. That is your "EQUAL sign on limit condition" problem.
3. If you return(rates_total-1) than you can just use [prev_calculated .. rates_total -1] for both cases.
3) I dont think the return value of OnCalculate does anything. You can return(0) and it still functions the same as return(rates_total).
15598

 SDC: 3) I dont think the return value of OnCalculate does anything. You can return(0) and it still functions the same as return(rates_total). Then it needs to be reported as broken. as OnCalculate says:During the function call, the prev_calculated parameter contains a value returned by OnCalculate() during previous call.