As you all already know, both MetaTrader 4 and MetaTrader 5 provide MQL Wizard
for easy code generation. Between these 2 wizards there is one important difference though. MetaTrader 5 allows you to generate ready-made Expert Advisers
, whereas MetaTrader 4 does not have such option.
This sad omission arise from the fact, that automatically generated EAs are based on the standard library (a set of MQH header files) distributed with the terminals, and the library version accompanying MetaTrader 4 lacks trading classes.
Historically the library was developed in MQL5 for the new MetaTrader 5. And only after a while, when MQL5 support was added into MetaTrader 4, the library was partially ported to MetaTrader 4 as well. But since trading API functions in MetaTrader 5 differ significantly from their ancestors in MetaTrader 4, MetaQuotes decided to not "downgrade" the trading classes of the library.
On the other hand, MetaTrader 4 is still the most popular trading platform, and it would be great to have the option to generate EAs in MetaTrader 4. Unfortunately, MetaTrader 4 is not improving anymore. Hence, the MQL5 Wizard in MetaTrader 5 remains the only option to generate EAs. BUT! What if we could compile and run the generated code in MetaTrader 4? This is surely can be done, if we complement the standard library of MetaTrader 4 with trading classes which emulate trading API of MetaTrader 5.
This sounds great, but requires some efforts. I made a start and can now post a beta version. It seems working, or ready for testing at least.
The solution comprises 2 distinctive parts:
- original file MT5Bridge.mqh, which contains all definitions and wrappers to mimic MetaTrader 5 trading API;
- collection of header files, taken from MetaTrader 5 library and adapted in such a way that they use the stuff from MT5Bridge;
The collection contains only those files, which exist in MetaTrader 5 and are missing in MetaTrader 4, so you can copy them into respective folders without being afraid of loosing some data due to file overwriting.
The collection is based on the library from MetaTrader 5 build 1545. All files can be downloaded below as a single archive MT4-SL5-b1545.zip.
Here is the list of files and folders:
While unzipping the archive, make sure you use a program which preserves the structure of subfolders (most of file managers do). All the stuff should be placed inside MQL4\Include folder.
The folders Trade, Expert, and Models are missing in MetaTrader 4, and will be created. The folder Indicators does exist and will be populated with 2 additional files.
The file MT5Bridge.mqh is attached below for download as well. It should be copied into the MQL4\Include\Trade subfolder.
After all this preparations done, you can copy any mql5-file with generated EA from MetaTrader 5 into MetaTrader 4 MQL4\Experts folder. Don't forget to change its extension to mq4. No any code modifications needed.
Then open it in MetaEditor, compile and run in MetaTrader 4.
Here is how demo expert ExpertMAPSAR.mq4 (originally ExpertMAPSAR.mq5) works in MetaTrader 4 without a single modification.
MetaTrader 4 tester report for ExpertMAPSAR EA generated by MetaTrader 5 Wizard
ExpertMAPSAR generated in MetaTrader 5 is running in MetaTrader 4
MetaTrader 4 tester log for ExpertMAPSAR EA generated by MetaTrader 5 Wizard
I'm skipping technical details here. If you want to understand programming implementation, feel free to inspect the source codes.
I don't use the standard library in my works, nor am I an expert in its codes. If you find a bug, send me a fix. The software is distributed "as is", without warranties of any kind including compliance with specific requirements and absence of errors.
All source codes are available freely. If you want to support this project, please, consider buying one of my products in the Market.
Technical details are covered in the articels: