Array out of range error for not existing bars - page 2

 
Marco vd Heijden:

You have to check on forehand if the bar exists and when it does you can continue and read it's value.

But when it does not exist don't try to read it because it will go out of range and unload the indicator.

Ok Thank you. But how to check if it exists? That's my problem.
 
Seng Joo Thio:

You don't understand.

'i' will cause error when you use it access an array directly (when i is out of range). But iOpen() and iClose() are function calls, and within that function calls, all necessary safeguards were incorporated to prevent error condition so that the return is 0 (worst case) rather than error.

Thank you. I'll check it and use iClose instead of Close to see the difference.
 

Check how many bars there are by calling iBars(.... and then see if the bar that you are trying to read fit's within that range.

If there are only 100 bars you will get array out of range when trying to read bar 101..

A better solution would be to do your loops according to the amount of available bars because that way it will always match the number of available bars.
 
Afshid: No, The problem is the [i] itself. You can not use Open[i], Close[i] or anything about [i]. Cause it doesn't exist.
  1. They do exist. There are no missing ones. All i in the range [0 … Bars-1] are fine.

    There may be missing periods (This is what your image shows,) but there are no missing bars. If there are no ticks during a specific candle period there is no bar. There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific.) requires knowledge of when your broker stops and starts (not necessary the same as the market.)
              "Free-of-Holes" Charts - MQL4 Articles
              No candle if open = close ? - MQL4 and MetaTrader 4 - MQL4 programming forum

  2. There is no problem with the code you posted. The problem is in the range(s) you pass to them.
 
Seng Joo Thio:

What if, instead of Open[i]/Close[i], use iOpen()/iClose() instead? These functions will return value 0 if the bar does not exist - this way you can do a filtering, if necessary, of the return values before incorporating them into your calculation.

Thank you. It worked without previous error.

The answer was using iClose instead of Close.

 
Afshid:

Thank you. It worked without previous error.

The answer was using iClose instead of Close.

It didn't work, it just changed the problem. Instead of array exceeded, you got zero prices from your iClose. Stop accessing past Bars-1.
Reason: