Object Oriented Programming Comes to MetaTrader 4
We have transferred the maximum possible amount of MQL5 language functions and features fully preserving MQL4 functionality. In other words, all powerful MQL5 functions, including ООP and the native code compiler, will become available in MQL4.
To achieve this, we have developed a unified compiler that automatically supports both MQL4 and MQL5 languages. MetaEditor will also become a single application both for MetaTrader 4 and MetaTrader 5 platforms. Thus, it will be possible to compile both MQL4 and MQL5 from any version.
It seems that all existing MQL4 functionality has not in fact been preserved. According to MetaQuotes once again:
This is a preliminary version for everyone's information.
Differences from the old MQL4 version:
- Changed the priority of AND/OR logical operations. Now, everything is similar to the standard C/C++
- Introduced shortened evaluation of logical expressions. Now, when a logical expression is evaluated before reaching its end, the remaining subsequent expressions are not evaluated, like in C/C++.
- Only integer values are now used in switch operator. Real values could have been used before
- A dot symbol cannot be used in variable names any more. Also, '@', '$' and '?' symbols cannot be used in variable names
- Tightened requirements for start function. Parameters could have been set in start function before. Now, all init, start, deinit, OnInit, OnStart, OnTick, OnTimer and other entry points should exactly match their signatures
- Due to expansion of keywords, such names as short, long, float, const, virtual, input, delete, new, do, char cannot be used now
- Now, imported dll functions cannot accept MQL string arrays as a parameter, like in MQL5
- Introduced predefined _Period, _Symbol, _LastError, _CriticalError, _StopFlag, _Point, _Digits, _UninitReason, _RandomSeed variable names that may come into conflict with simple variables declared under the same names in existing source files
- datetime type has become an 8-byte one, like in MQL5.
Having downloaded the beta IDE the first thing we did was compile a wide range of our existing expert advisors. Most recompiled without errors, but as suggested by the bullet points above there was one significant problem we encountered. A much loved function in one of our DLLs looks like this:
int _parse(string str, string& tokens);
and the error message the new MQL4 compiler emits when it encounters it reads as follows:
Invalid parameter for import function
This is not an entirely trivial problem to solve, especially if you don't possess the source code to the offending DLL! Nevertheless we set out undaunted to see how object oriented programming for MetaTrader 4 works in practice. That meant starting from an expert advisor originally written for MetaTrader 5. Turning to our old friend Ray the Random Robot we changed the extension of the source to his multi-currency incarnation from MQ5 to MQ4 and tried to compile it in the new IDE. That resulted (not entirely unexpectedly!) in a long list of errors including things like "CDealInfo – undeclared identifier" and "HistoryDealsTotal – function not defined". This is actually rather good news if you're an MT4 fan, since it means MetaQuotes are continuing to support the traditional MetaTrader 4 order handling functionality. After a bit of cutting and pasting from his MQL4 version, objected oriented Ray compiled with a few warnings and no errors whatsoever.
Unfortunately we have no idea at the moment whether he's capable of actually trading. The .EX4 file produced by the new compiler doesn't run in currently available versions of the MetaTrader 4 terminal. We're informed that a new version of the MT4 terminal that will run these new executables is due to arrive "next month". According to MetaQuotes when that happens:
MQL4 code protection [will be] considerably increased
which sounds like more good news. Until then, if you'd like to experiment with object oriented programming for MetaTrader 4 you can download Ray's source code from the Trading Gurus Community Forum.