MetaTrader 5 - MQL5 Coding Practices

 

It appears that the MQL5 is unnecessarily complicated. Majority of the functions have been replaced with complex object-oriented functions that do exactly the same things. And what's quite irritating is that some of the existing functions don't belong to any objects. MQL5 is therefore like a messy hybrid of procedure-oriented and object-oriented programming, which is a nightmare to any type of programmers.

The coding patterns established in MQL4 are no longer in MQL5. That said, the creation of indicator and EA now needs an entirely different approach. The calculations of the indicator now should be done in the OnCalculate() event handler.

After a few hours reading the MQL5 references, I still don't know how to work around the no-hedging feature (or bug). I suspect that you can run only one expert advisor per account. Running two EAs or more in MetaTrader 5 at the same time will lead to serious interference and malfunction. I am also concerned that manually modifying or closing an EA-made position could make the EA go out of sync. It could be a potential disaster if the EAs are not carefully created with this problem in mind.

It's a bit too early to tell if the extra complication is worth it or not. First impression, it appears not to be.

Looking at the plus side, the new debugger should be a life saver to MQL5 developers. Breakpoints could be set, and variables could be added to the Watch Window.

 

Here's a blob of one of the most embarrassing implementations that could be found in OOP. This is laughable. It appears those who implement MQL5 don't really know why they are moving to OOP.


long handle=ChartOpen("EURUSD",PERIOD_H12);
if(handle!=0)
{
ChartSetInteger(handle,CHART_AUTOSCROLL,false);
ChartSetInteger(handle,CHART_SHIFT,true);
ChartSetInteger(handle,CHART_MODE,CHART_LINE);
ResetLastError();
bool res=ChartNavigate(handle,CHART_END,150);
if(!res) Print("Navigate failed. Error= ",GetLastError());
ChartRedraw();
}

In OOP, it should really be like the following which improves the readability. Note that the following is not MQL5 but what MQL5 should be.


Chart ch = new Chart("EURUSD", ChartPeriod.H12);
if (ch != null) {
ch.AutoScroll = false;
ch.Shift = true;
ch.Mode = ChartMode.Line;
try {
ch.Navigate(150, ChartPosition.End);
} catch(exception ex) {
Log.WriteLine(ex.Message);
}
Chart.Current.Redraw();
}

As MQL5 now supports polymorphism, an expert third-party coder can implement all the wrapper classes (e.g. Chart, Order, etc). This could help simplify MQL5.

 

It turns out MetaQuotes have already implemented some wrapper classes including CChart, CTrade, CFile, CString, CChartObject, CArray, CIndicator, CList, etc. These classes are accessible by including the associated .mqh files. So basically, the previous messy code could be written in a clean OOP format.

This is a smart move by MetaQuotes developers indeed. They should put this in the documentation.

Anyway, it still bothers me that class property is not supported. So CChart.Mode(CHART_BARS) would be CChart.Mode = CHART_BARS instead. Trivial stuff but this is one of the OOP conventions. In fact, most of the wrapper classes contain some unconventional procedure names. CChart.Screenshot() is not right. The convention is Object.Verb(). It should be CChart.TakeScreenshot().

 

After two days of learning MQL5, shifting through each and every function, I can say that the migration from MQL4 to MQL5 will be a pain in the ass. Almost everything from trading functions, rate info, account info, to custom indicator functions is completely changed.

MQL4 indicators, custom indicators, include files and library files are not backward-compatible. Major reworks will be needed to port code from MQL4 to MQL5.

 

I noticed that some MQL4 functions are depreciated and have no replacements in MQL5. Those are:

bool IsTradeContextBusy()

int ArrayDimension()

int ArrayCopySeries()

double AccountFreeMarginMode()

void HideTestIndicators()

int WindowFind()

int FileOpenHistory()

int AccountStopoutLevel()

If you know the MQL5 equivalents of the functions, please do let me know. Thanks!

 

Seems the whole Expert Advisor programming has to be studied again for those who are in the middle like me...

Why not stick to the MQL4 and just add the debugger... this should have been better for us.