Is it better to use iTime/iHigh/etc instead of Time/High/etc in order to ensure forward compatibility with MQL5?

 

In MQL4, we're taught (for example) to use Time to access the array of datetimes for bars in the current period, and iTime to access the time of a bar for periods other than the one the user is currently looking at.

However, in MQL5, there is no Time array, so we're forced to use iTime.

This creates a problem when wanting to convert an MQL4 program to MQL5, as you have to go back and re-write all the syntax for Time to iTime and insert the appropriate arguments for the function.

You could write an include file for this and define a constant of Time[], then initialize the array and fill it with the values of the current period, but having to include the file and then run the initialization can become a bit annoying, mainly if you work with multiple files.

It seems like we're better off just using iTime and avoiding that hassle altogether. If there is any performance gain from using Time, it's probably negligible.

Thoughts?

 
  1. Define “better.”
  2. When you switch you can change them when you change the trading code.
  3. There is a “Time array”, when you use CopyTime
 
William Roeder #:
  1. Define “better.”
  2. When you switch you can change them when you change the trading code.
  3. There is a “Time array”, when you use CopyTime

1. in order to ensure forward compatibility with MQL5

2. You have to go back and re-write the syntax, which is what I'm trying to avoid

3. Yup. As I alluded to that in my OP, you can define a Time[] array and then initialize it and fill it. The "initialize and fill it" part refers to using SetArrayAsSeries set to true on Time[] and then using CopyTime.

With that said, after thinking it over, I already made up my mind. I just wanted to know if other people were already using iTime over Time to ensure forward compatibility. Maybe there was still a good reason to use Time[] over iTime in MQL4, but I doubt it.
 
Alexander Martinez #:

1. in order to ensure forward compatibility with MQL5

2. You have to go back and re-write the syntax, which is what I'm trying to avoid

3. Yup. As I alluded to that in my OP, you can define a Time[] array and then initialize it and fill it. The "initialize and fill it" part refers to using SetArrayAsSeries set to true on Time[] and then using CopyTime.

With that said, after thinking it over, I already made up my mind. I just wanted to know if other people were already using iTime over Time to ensure forward compatibility. Maybe there was still a good reason to use Time[] over iTime in MQL4, but I doubt it.

No need to rewrite code. Just add following code at the start of mql4 source to have compatibility with mql5.

#ifdef __MQL5__
#define DefineCopy(NAME,TYPE) \
TYPE i##NAME(string symbol, ENUM_TIMEFRAMES tf, int b) \
{ \
  TYPE result[1]; \
  return Copy##NAME(symbol, tf, b, 1, result) > 0 ? result[0] : 0; \
}
DefineCopy(Time, datetime);
DefineCopy(High, double);
DefineCopy(Low, double);
DefineCopy(Open, double);
DefineCopy(Close, double);
DefineCopy(Volume, long);
#endif

More info here: https://www.mql5.com/en/blogs/post/681230

MQL's OOP notes: Converting MetaTrader 4 indicators to MetaTrader 5
MQL's OOP notes: Converting MetaTrader 4 indicators to MetaTrader 5
  • 2017.10.11
  • www.mql5.com
It has been a long time since MetaTrader 5 was released, but MQL products for MetaTrader 4 do still prevail on mql5.com site (both in the codebase, and in the market), and in the Internet in general
 
Marcin Madrzak #:

No need to rewrite code. Just add following code at the start of mql4 source to have compatibility with mql5.

More info here: https://www.mql5.com/en/blogs/post/681230

Thanks for sharing the code and link. This is different than I did it and there's some interesting concepts at work here.

Reason: