Question with 2 for()

 

Hi Everyone,

I cannot understand why the following structure doesn't work.

When I attach to chart, ONLY buffer buy has value.

To my understand, it will run the 2 sequences one by one. So the first FOR() will be fully run first  --> I have buy buffer. Then the second FOR() will be processed.

(it is for illustration of the problem, so it seems silly not using if else) 

Thank you for your help. 

SCFX 

 

      int limit=rates_total-prev_calculated;
//FIRST FOR()
for(int i=1;i<=limit; i++)
{ if(Close[i]<Open[i])
    { buy[i]=Open[i];}
}       

//SECOND FOR()
for(int j=1;j<=limit;j++)
{ if(Close[j]>Open[j])
    {sell[j]=Close[j];}
}       
   

//--- return value of prev_calculated for next call

   return(rates_total);

  }
Files:
problem.mq4  4 kb
 

Change

for(int i=1;i<=limit; i++)
//
//
for(int j=1;j<=limit;j++)

to

for(int i=1;i<limit; i++)
//
//
for(int j=1;j<limit;j++)

On first run, prev_calculated==0, so limit==Bars.

You are trying to access an array element that doesn't exist and getting an array out of range error.

Because you count up in the loop, the buy buffer is being filled before the error occurs.

 

Thanks, GumRai.

It works now! 

Yes, on first run, limit==Bars.

So it will run from i=1 to N (count up in the loop). I am not clear why a given element of the array (1-N) doesn't exist.

Can you explain it please?

SCFX 

 

You can figure it out youself if you can answer these questions.

Assume 100 bars, the lowest bar index is zero.

What is the highest bar index ? 

What is the value of Bars ?

Is the highest bar index the same as the value of Bars ?

 

Oh,

Great way to teach, SDC.

so it ranges from 0-99. value bar=100.

limit<=100 --> bar 100 is not there yet.

I got it. 

But you know, the i<=limit has not caused me any trouble before :(

Have a nice weekend.

SCFX 

 
Yes it would have worked before the recent upgrades to mql4 and MT4. Since then badly coded indicators that were previously allowed to slide because such errors were ignored by the compiler and the terminal will no longer work. They now have to be coded more accurately.
 
GumRai: Change
for(int i=1;i<=limit; i++)

to

for(int i=1;i<limit; i++)

Will NOT work. You do NOT loop from 0 (or 1) to rates_total-prev_calculated You loop from notDone to rates_total-1. rates_total-1 is the newest forming bar and 0 is the oldest bar.

See Question with for(i=0; i<=limit; i++) - MQL4 forum.

 

WHRoeder:

Will NOT work. You do NOT loop from 0 (or 1) to rates_total-prev_calculated You loop from notDone to rates_total-1. rates_total-1 is the newest forming bar and 0 is the oldest bar.

See Question with for(i=0; i<=limit; i++) - MQL4 forum.

That is only the case if you reverse the buffers and series arrays.

These are the main considerations when creating indicator loops,

  • rates_total == total number of bars
  • prev_calculated == 0 on initial load or when chart history is added
  • prev_calculated == rates-total-1 the rest of the time
  • buffer indexing direction