Confusion about iClose and CopyRates data syncronization

 
So I have been reading through https://www.mql5.com/en/forum/451498 and https://www.mql5.com/ru/forum/442814/page2 and from what I understand the iClose/iOpen and things like CopyTime can return wrong values if not synced up and is therefore unreliable? But with both don't you still just check and make sure the buffer is filled like how you do it with indicators with the <= 1 right? 
double close_price = iClose(_Symbol, PERIOD_CURRENT, 1);  

   if (close_price == 0){
      Print("Warning: iClose returned 0");  
      // Obviously add some retry mechanism here 
   }

Also does iTime have this issue as well? If it does I'm going to start questioning my entire existence. 


P.S. it seems you can do stuff with SeriesInfoInteger and SERIES_SYNCHRONIZED but still that feels like a lot of extra work just to use in built functions. So if I have already built my EA with these functions should I just add this into the beginning of my OnTick function that make sure everything is synced up? 

void OnTick(){
   long is_synced = 0;
   if (!SeriesInfoInteger(_Symbol, PERIOD_CURRENT, SERIES_SYNCHRONIZED, is_synced) || is_synced != 1){
      Print("Time series not synchronized for ", _Symbol);
      // add retry mechanism when not lazy lmao
   }  
}

Or should I just start using CopyRates instead? Since I use multisymbol EAs the above code would obviously be different but I'm still confused as to why the iClose/Open/Time functions need the synchronization and CopyRates does not. Pls someone with more experience give advice on what you personally do/think is better, would greatly appreciate it. 

Differance between MqlRates and other methods ?
Differance between MqlRates and other methods ?
  • 2023.07.27
  • Ahmed_Fouda
  • www.mql5.com
is there any difference between using MqlRates and using iCLose for example...
 

You may just check error codes to make sure they are returning correct values:

History Access

 

 

ERR_HISTORY_NOT_FOUND

4401

Requested history not found

ERR_HISTORY_WRONG_PROPERTY

4402

Wrong ID of the history property

ERR_HISTORY_TIMEOUT

4403

Exceeded history request timeout

ERR_HISTORY_BARS_LIMIT

4404

Number of requested bars limited by terminal settings

ERR_HISTORY_LOAD_ERRORS

4405

Multiple errors when loading history

ERR_HISTORY_SMALL_BUFFER

4407

Receiving array is too small to store all requested data

 
Yashar Seyyedin #:

You may just check error codes to make sure they are returning correct values:

History Access

 

 

ERR_HISTORY_NOT_FOUND

4401

Requested history not found

ERR_HISTORY_WRONG_PROPERTY

4402

Wrong ID of the history property

ERR_HISTORY_TIMEOUT

4403

Exceeded history request timeout

ERR_HISTORY_BARS_LIMIT

4404

Number of requested bars limited by terminal settings

ERR_HISTORY_LOAD_ERRORS

4405

Multiple errors when loading history

ERR_HISTORY_SMALL_BUFFER

4407

Receiving array is too small to store all requested data

so either method is fine but you should still just always check appropriate error codes? In the strategy tester should I do this once in the OnInit or in the OnTick and is it the same for live? What other articles should I read about this or just in general? 

 
Casey Courtney:
So I have been reading through https://www.mql5.com/en/forum/451498 and https://www.mql5.com/ru/forum/442814/page2 and from what I understand the iClose/iOpen and things like CopyTime can return wrong values if not synced up and is therefore unreliable? But with both don't you still just check and make sure the buffer is filled like how you do it with indicators with the <= 1 right?  Also does iTime have this issue as well? If it does I'm going to start questioning my entire existence. 


P.S. it seems you can do stuff with SeriesInfoInteger and SERIES_SYNCHRONIZED but still that feels like a lot of extra work just to use in built functions. So if I have already built my EA with these functions should I just add this into the beginning of my OnTick function that make sure everything is synced up? 

Or should I just start using CopyRates instead? Since I use multisymbol EAs the above code would obviously be different but I'm still confused as to why the iClose/Open/Time functions need the synchronization and CopyRates does not. Pls someone with more experience give advice on what you personally do/think is better, would greatly appreciate it. 

If you are getting data from the chart symbol and timeframe, it's very unlikely to have outdated or wrong data (though strictly speaking it can still happen, but rarely).

If you are getting data from other symbols or timeframes, it's always possible to get outdated data (or no data) and yes you need to check the SERIES_SYNCHRONIZED.

Anyway, the best practice while coding is to ALWAYS check for error (return code of a function or whatever the method used).

In MQL5, all "iFunction" should be avoided, the CopyXXX functions are faster (or equal depending of the context). 

 
Alain Verleyen #:
In MQL5, all "iFunction" should be avoided, the CopyXXX functions are faster (or equal depending of the context).

iXXX() are just wrappers around CopyXXX(). iXXX() were not originally supposed to exist in MQL5, but were later added due to numerous requests from those who had difficulties with the transition from MQL4 to MQL5.

If I remember correctly and I'm not mixing anything up.

[edit]

I clearly remember numerous requests to add some of the iXXX functions (which are in MQL4) to MQL5. And MQ fulfilled these requests eventually. But I can't remember which iXXX functions were added because of the requests. Most likely the following: iTime, iClose, iHigh ...

Does anyone remember which iXXX functions were not in MQL5 originally?

 
Vladislav Boyko #:

iXXX() are just wrappers around CopyXXX(). iXXX() were not originally supposed to exist in MQL5, but were later added due to numerous requests from those who had difficulties with the transition from MQL4 to MQL5.

If I remember correctly and I'm not mixing anything up.

[edit]

I clearly remember numerous requests to add some of the iXXX functions (which are in MQL4) to MQL5. And MQ fulfilled these requests eventually. But I can't remember which iXXX functions were added because of the requests. Most likely the following: iTime, iClose, iHigh ...

Does anyone remember which iXXX functions were not in MQL5 originally?

Is that important ?

There was no iXXX function at all in the original MQL5, not even iBarShift.

 
Alain Verleyen #:
Is that important ?

No, it's not important. I just added that the iXXX functions were redundant from the start and ideally should never have been introduced.

 
Alain Verleyen #:

If you are getting data from the chart symbol and timeframe, it's very unlikely to have outdated or wrong data (though strictly speaking it can still happen, but rarely).

If you are getting data from other symbols or timeframes, it's always possible to get outdated data (or no data) and yes you need to check the SERIES_SYNCHRONIZED.

Anyway, the best practice while coding is to ALWAYS check for error (return code of a function or whatever the method used).

In MQL5, all "iFunction" should be avoided, the CopyXXX functions are faster (or equal depending of the context). 

So for multisymbol EAs its better to use CopyXXX instead of iFunctions because I don't have to check SERIES_SYNCHRONIZED or should I check it anyway and how often should I check it? 

Why is it better to use CopyXXX instead of iFunctions? I am definitely going to use CopyRates and stuff now but WHY specifically is it better? If I have to check SERIES_SYNCHRONIZED anyway even with CopyXXX then why can't I just use the iFunctions? 

 
Casey Courtney #:

So for multisymbol EAs its better to use CopyXXX instead of iFunctions because I don't have to check SERIES_SYNCHRONIZED or should I check it anyway and how often should I check it? 

Why is it better to use CopyXXX instead of iFunctions? I am definitely going to use CopyRates and stuff now but WHY specifically is it better? If I have to check SERIES_SYNCHRONIZED anyway even with CopyXXX then why can't I just use the iFunctions? 

I thought I already answered to these questions. 

- It's better to use CopyXXX functions because they are more efficient (or equal at worst) than iFunctions. See also Vladislav post #4. It's easy to check and demonstrate, I will not do it.

- You should always use SERIES_SYNCHRONIZED, because even if it changes nothing 95% of the time, it will avoid you trouble in the remaining 5%. You never know in advance, so use it, always.

- CopyRates is rarely the best choice. Actually, I never used it, it's not in my codebase. Why ? Because it's the less efficient CopyXXX function. Use CopySeries or the specific function for what you need CopyClose, CopyTime, etc...

- ALWAYS check for error.

WHY :

  • In general, having better performance is always preferable, I hope I don't need to explain that.
  • And what matters even more its accuracy, you don't want to work with outdated or wrong data.
 
Alain Verleyen #:

I thought I already answered to these questions. 

- It's better to use CopyXXX functions because they are more efficient (or equal at worst) than iFunctions. See also Vladislav post #4. It's easy to check and demonstrate, I will not do it.

- You should always use SERIES_SYNCHRONIZED, because even if it changes nothing 95% of the time, it will avoid you trouble in the remaining 5%. You never know in advance, so use it, always.

- CopyRates is rarely the best choice. Actually, I never used it, it's not in my codebase. Why ? Because it's the less efficient CopyXXX function. Use CopySeries or the specific function for what you need CopyClose, CopyTime, etc...

- ALWAYS check for error.

WHY :

  • In general, having better performance is always preferable, I hope I don't need to explain that.
  • And what matters even more its accuracy, you don't want to work with outdated or wrong data.

Thanks I understand now