I recently completed a project that converted code I wrote from MetaTrader 4 to MT5. This post should provide some insight into the difficulties of moving back and forth between the two languages.
1) Learning about MT5 objects; their methods and members
MT5 is a quite a bit different from MT4. Compared to MT5, MT4 is a rather quaint non-object oriented ‘C’ like programming language with a limited set of statements. MT5 adds the object oriented language parts. In addition to objects, methods, and members, MT5 also adds various structures, enumerations and other constructs. Because of this I had to find the MT5 way to accomplish what was in the MT4 EA. This meant considerable time reading the language documentation, poring through newsletter examples and forum discussions when logic problems cropped up that were not related to syntax, coding, and debugging. One area in particular I had trouble with is the way historical data is returned within arrays.
2) MT5 combines multiple trades into one trade.
This is not so much a problem if the combined trades use the same stop loss and take profit. However, in this particular EA, the two trades placed on the same trade signal used different take profits. This required the storage of the first trade parameters and then monitoring when the bid or ask dictated a partial close of the now combined trade.
3) The Strategy tester had to work on an EA that accessed multiple currency pairs; and the creation of indicator handles.
The primary reason for converting this particular EA from MT4 to MT5 was to take advantage of the MT5 strategy tester, which permits backtests in multiple currency pairs. In its initial configuration the EA would test the various currency pairs to find the most favorable pair to work with every tick and then access and test numerous indicators (based on currency pair and chart periods) on the selected currency pair.
Through trial and error it was discovered that the removal of old indicator handles and the creation of new indicator handles every tick taxes memory and CPU resources to the point where the EA would exhaust those resources. After attempting various tweaks to reduce demands on these resources, I began to look for ideas from outside sources (on the Metatrader forum and from various blogs). After some forum and support ticket exchanges I decided on some approaches to try, including the use of retry loops and method calls to test for the existence of the necessary data. This worked only to a degree (with a reduced number of currency pairs).
I tried a few other approaches before finally deciding to preallocate all indicator handles in the initialization part of the EA. I had to allocate a different indicator handle for each currency pair and with each currency pair for each time period used. In the actual strategy test this greatly lengthens the EA’s initial load time but it is the only scalable solution.
4) Getting the historical data requires some manual setup.
One of the issues that delayed the discovery of the best solution in part 3 (above) is that the historical data for the various currency pairs was not present on the client terminal that the strategy tester was accessing. Many of the errors having to do with loading data for the indicators was due to this lack of data and had nothing to do with the other solutions being tried. While I feel there may be a more elegant way to programatically request this data, I have not come upon how this is done yet.
I ended up manually loading a chart of each currency pair, turned off chart autoscroll, then back spaced over the period of time being tested. Not only did I have to load a chart for each currency pair, I had to change the chart period to the 4 or 5 different periods that was to be used by the various indicators. I also had to add that indicator to the chart. In this way I was able to force the loading of historical data needed in the strategy test. Had I not been testing within a small window over a recent period of time this could prove to be a very burdensome task.
Fivos Georgiades says
Please refer to MQL5.com forum/articles/code base and dedicated programing professionals.
I am sure you wll be able to find a solution to your problem there.
FG
Shaun Overton says
Hi Fivos,
Thank you for your comment. We are dedicated programming professionals ourselves. The purpose of this post was simply to document lessons learned from our experience.
Thanks for reading our blog!
Trinity@metatrader5 says
Aside from providing brokerage services to customers in Forex market. What are other major services that Metatrader 5 can offer that Metatrader 4 don't have?
What I know the main thing that Metatrader 5 and Metatrader 4 have in common is that they are designed to provide brokerage services to customers in Forex, CFD, and Future markets. Aside from that, what else does Metatrader 5 and Metatrader 4 have in common?
Shaun Overton says
Most of the changes in MT5 are designed to improve the broker’s capabilities. MQL5 takes care of some inherent limitations of MQL4, although it’s way more complicated to program.