Novelties of the MQL5 language

November 21, 2009
By Bogdan Baltatu, MQLmagazine editor

[Versiunea romaneasca] [MQLmagazine.com in romana] [English edition]

The MetaQuotes folks brang a lot of improvements to the new language MQL5 vs the old MQL4.

Data types

They added the enum data type to the base data types and also made a more precise definition of the integer types. In this way, they added:

char , short , long , uchar , ushort , uint , ulong

This time the structures appear too:

struct, class

Operators

The introduction of the operators new si delete which are used for the creation and destruction of objects. These two operators apply to object-oriented programming.

Events

These are functions that execute when an event happens:
OnStart(), OnInit(), OnDeinit(), OnTick(), OnTimer(), OnTrade(), OnBookEvent(), OnChartEvent().

We can mention new event types:
– OnTimer() : The function is called when a timer event is triggered, which is set up by EventSetTimer();

– OnTrade() : The function is called whenever the list of orders or position changes ; that means it is called whenever an order is posted or gets executed;

– OnBookEvent() : Function is called whenever the price change for any symbol registered by MarketBookAdd() si-a schimbat pretul. In a way it is a generalized form of OnTick().

– OnChartEvent() : Function is called whenever the chart receives an event, be it given by the keyboard, mouse, or moving a graphics object by mouse.


Notes:
OnInit() basically takes over from MQL4’s init();
OnStart() reduces from the role that start() had in MQL4, remaining as main block for scripts;
OnTick() takes over the role as incoming tick event, from the start() function from MQL4;
OnDeinit() takes over old MQL4’s deinit();

Object Oriented Programming

One of the novelties of MQL5 is the Object Oriented Programming, which makes code to be easier to write, read and use. The basic OOP features are : encapsulation, inheritance, polymorphism, function overload and virtual functions.

New graphics objects

MetaQuotes comes with new graphics elements .Below is the list with the new graphics objects featured by MQL5:

OBJ_ELLIOTWAVE5 , OBJ_ELLIOTWAVE3 , OBJ_ARROW_THUMB_UP , OBJ_ARROW_THUMB_DOWN,
OBJ_ARROW_UP , OBJ_ARROW_DOWN , OBJ_ARROW_STOP , OBJ_ARROW_CHECK ,OBJ_ARROW_LEFT_PRICE,
OBJ_ARROW_RIGHT_PRICE ,OBJ_BUTTON , OBJ_CHART , OBJ_BITMAP , OBJ_BITMAP_LABEL , OBJ_EDIT.

Our opinion would be that OBJ_EDIT,OBJ_BUTTON si OBJ_CHART are the most important graphics objects. Once with OBJ_CHART comes the possibility to have chart in chart feature (to see inside a chart a different other charts). These objects are fully packed with properties, which leads to a better graphics object management.

Special variables for debugging

To simplify debugging four variables have been introduced : __LINE__ , __FILE__ ,__FUNCTION__ , __MQL5BUILD__  , which can be used to log info about current line, file , function or the MQL5 build ; these come handy in debugging as there were no such infos in MQL4 and possible errors that were generated by functions were traceable with extra logging and considerable programming effort.

The position based system that replaces the order based system

This is an MT5 feature rather than a MQL5 feature. However, since it changes the trading activity, it leaves marks over MQL5. The order selection mechanism is modified, and also a new position selection mechanism has been added

Symbol enumeration

With functions SymbolSelect() and SymbolTotal() can be made easily the list of all symbols offered by broker. Even more, with SymbolInfoInteger() a lot of symbol information can be retrieved, including the profit calculus and the margin calculus mode that tells about the symbol nature.
This existed also in MQL4’s MarketInfo(), however the symbol list was not retrievable. The symbol nature information, combined with the list, is a powerful tool in knowing which symbols are forex, which are futures, stocks or stock exchange indexes. This will have implications later in retrieving option chains by knowing the underlying.

A new way of reading/writing properties

The functions working with properties are now divided on data types, according to the data type they work with.
For instance MQL4’s MarketInfo() is now divided in SymbolInfoInteger(), SymbolInfoDouble(), SymbolInfoString() ; also SymbolInfoTick() for current prices;
The functions working with the account, which were one per solicited element in MQL4, are organized in a similar manner: AccountInfoInteger(), AccountInfoDouble(), AccountInfoString() (there were 16 account functions in MQL4, for comparison);
For the running program, MQL5InfoInteger(), MQL5InfoString() (similar functions were not existant in MQL4);
For data series : SeriesInfoInteger();
For charts : ChartGetDouble(), ChartGetInteger(),ChartGetString() , with their writing complements ChartSetDouble(), ChartSetString(), ChartSetSymbolPeriod() , near a series of functions inherited from MQL4’s window functions;
For indicators : IndicatorSetDouble(), IndicatorSetInteger(), IndicatorSetString();
To establish indicator plotting parameters: PlotIndexGetInteger(), PlotIndexSetDouble(), PlotIndexSetInteger(), PlotIndexSetString;
For graphics objects : ObjectGetDouble(), ObjectGetInteger(), ObjectGetString() , also the newer ObjectGetTimeByValue(), ObjectGetValueByTime ; also for writing : ObjectSetDouble(), ObjectSetInteger(), ObjectSetString().

A new way to address data series

Gone are functions like iHigh(), iHighest(), iLow(), iLowest(), iOpen(), iClose(), iBars(), iBarShift() from MQL4.
I said that appeared the function SeriesInfoInteger(). But this one returns only general properties.
To return data from data series now the function being used is CopyRates().

Un new way to construct and address indicators

This subject is too complicated for the present article. However, all indicators, including custom indicators, have the addressing manner changed. This time, functions don’t return values anymore, but handlers. It is recommendable that a new handler to be created for every indicator called with some parameters, one time. Then data can be extracted , datele pot fi extrase, pentru orice bara, cu ajutorul functiei CopyBuffer() , that can use same handler over and over again.

Sure we can’t cover in an article all the new relevant stuff brought by MQL5.
But these will appear in time, with the development of trading strategies written in MQL5.

4 Responses to “ Novelties of the MQL5 language ”

  1. Michal on July 17, 2010 at 8:26 pm

    Hi,
    Could somebody explain me and other beginner programers how to get to price in MQL5? I mean in MQL4 it’s for example Open[5], Close[100], High[0] etc. But i dont understand new system which is in MQL5 :/. If i think correct i have to create array something like double Open[1000]; and than save prices in this variables? -> plz help :)
    I will very gratefull for some example step by step.

  2. Bogdan Caramalac, MQLmagazine sr.editor on July 17, 2010 at 10:27 pm

    Hi Michal,

    Well, it is your option whether you declare or not arrays so big. Thing is, you have to declare at least one-element array.
    To get what you want, which is not prices, rather history access, you need to use the CopyRates() function.
    For that, you have to declare an MqlRates array. The MqlRates is a structure itself containg high, low, close prices etc.
    So when you get info with CopyRates you get them all in a single array.

    CopyRates disregards the “as series” setup of your array. It will begin with the latest bar on the nth-1 position in the array, and will continue to the oldest bar which will be on 0 position in the array. Run this script for instance on a chart of your choice:

    void OnStart()
    {
    MqlRates Rates[3];
    ArraySetAsSeries(Rates,false); //set it to true also and retry
    CopyRates(Symbol(),Period(),0,3,Rates);
    Print(“Close[0]=”,Rates[0].close,” Open[0]=”,Rates[0].open);
    Print(“Close[1]=”,Rates[1].close,” Open[1]=”,Rates[1].open);
    Print(“Close[2]=”,Rates[2].close,” Open[2]=”,Rates[2].open);
    }

    Check all the syntaxes of CopyRates - it also allows selection based on dates, not only bar indexes.

  3. Michal on July 18, 2010 at 9:55 am

    Thank You very much for Your answer!
    I think i’m much closer to understend that.
    I have to check that in a program yet but i think it will be o.k. :) .
    Examples and programming are the most important to really understand :P so i have to just try :P
    I hope it will be easy :D and it the same way for scripts, indicators and EA.

    Your Regards
    Michal

  4. Michal on July 18, 2010 at 10:54 am

    Best* regards :)