MetaQuotes Language MQL4/MQL5
This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)(Learn how and when to remove this template message)
|Filename extensions||.ex4, .mq4, .mqh, .ex5, .mq5|
MQL4 (MetaQuotes Language 4) and MQL5 (MetaQuotes Language 5) are integrated programming languages designed for developing trading robots, technical market indicators, scripts and function libraries within the MetaTrader software.
The primary objective of MQL4 and MQL5 is automation of trading and facilitation of operational analysis. MQL4 and MQL5 comprises an extensive codebase source code library used for developing trading robots.
On July 1, 2005 MetaQuotes Software released MetaTrader 4 - a platform for trading on financial markets. MQL4 - an object-oriented programming language, was written specifically for work on this platform. Initially, its syntax was based on C. Another platform, MetaTrader 5, was released in 2010 together with MQL5 - a new language created for it.
The release of MetaTrader 4 600 build in 2014 was a major update of the language. That brought it to the level of the more modern MQL5. The development environment MetaEditor became unified for both languages.
The updated MQL4 features new graphical objects and new functions borrowed from MQL5 and used for analyzing charts. The protection system was completely modified for the executable files EX4. The MQL5 Standard library was transferred almost entirely with the only exception of the trade subsystem, which required adaptation. New data types were added and the micro substitution system expanded (conditional compiling #ifdef, #ifndef, #else, #endif), allowing the use of code from other languages based on С/C++. Structures, classes and object pointers were added as well (it should be noted that object pointers in MQL4/MQL5 are not similar in every way to the ones in С++). The updated MQL4 features the mode of strict compiling, which prevents a lot of potential programming errors. In MQL5 this mode is default and cannot be disabled.
Both languages support nearly all standards of object-oriented programming except multiple inheritance: encapsulation and extensibility of types, inheritance, polymorphism, overload, virtual functions.
To develop programs in MQL4 and MQL5, MetaEditor - a compiler embedded in the development environment - was created. It is integrated in the MetaTrader 4/MetaTrader 5 trading terminals. MetaEditor allows convenient editing of program source code, automatic generating projects by a template, profiling code and remotely developing joint applications in conjunction with other people.
Differences between MQL4 and MQL5
One of the fundamental differences is the trading system setup. MQL4 is used for developing trading programs based on the order system and MQL5 is utilized to implement a positional system. In the MetaTrader 5 trading terminal there are strict delimitations between the concepts of position, order and deal. An order is a request to execute a trading operation, which may result in a trading deal. A position is the aggregate deals on a certain financial instrument.
MQL5 comprises an expanded list of trading functions for work with open orders, a list of open positions, order history and deal history. In MQL4, different functions such as OrderSend(), OrderClose(), OrderCloseBy(), OrderModify(), OrderDelete() were initially incorporated for performing each trade operation. These functions can be used to open/close and delete pending orders.
In MQL5 all trading operations are carried out by sending trade requests using only one function - OrderSend(). As a parameter, the function gets passed a request either to place a pending order, or to open by the market, or to cancel a previously placed order. Introducing the new function OrderSendAsync() to MQL5 enabled asynchronous trading operations.
An important novelty in MQL5 is Depth of Market and a new event type of handling Depth of Market information.
MQL4/MQL5 aims to directly address traders' needs and requirements. It was developed for writing trading programs and is used only for that purpose. Functions for performing trade operations OrderSend(), OrderClose(), OrderCloseBy(), OrderModify(), OrderDelete() have been initially incorporated in the language and are used for changing the balance of a trading account.
There are four program types that can be written in MQL4/MQL5.
- Expert Advisors. Automatic systems trading by specified parameters and following a coded algorithm. Occurrence of a previously specified event like receiving a new tick, an alert about a new trading operation or even pressing a button or clicking a mouse, triggers the Expert Advisor to perform a programmed action.
- Custom Indicators. Written by users, they are used along the ready-made indicators integrated in the terminals. Their function is mostly analytical. Indicators do not perform trading nor carry out operations that slow down the interface stream such as sending emails or performing a random delay. The main task of indicators is to monitor a situation, reflect and interpret it and then submit to a trader for analysis or to alert a trader for a specific profitability.
- Scripts. A script is a program intended for a single execution of some actions. The start event is the only event type processed by the script.
- Custom Function Libraries. In addition, there is an opportunity to create include files (#include). Include files allow you to include most frequently used functions and classes without directly pasting their source code into the program. Using functions and classes simplifies creating, debugging and compiling because when using dynamic libraries, functions load only when they are called directly.
The MQL4 and MQL5 Syntax and their Difference from С++
The syntax of the languages is similar to the one of C++, however there are exceptions. MQL4 and MQL5 do not feature pointer arithmetic. The goto operator is also missing in the MQL languages as well as a possibility to declare anonymous enumeration and multiple inheritance.
- Text formatting. Any number of space symbols such as spaces, tabs, empty strings can be used to make the code more readable and convenient to work with. There are exceptions, however. A line break symbol must not be used immediately after a hash and space symbols cannot be used inside constants, identifiers and key words.
- Comments. As with the C/C++, MQL4/MQL5 comments can be both single-line and multi-line. A single-line comment starts with the // symbols and ends with the new line character. Multi-line comments start with the /* symbols and end with */ ones. They cannot be nested.
- Identifiers. Identifiers are used as names for variables and functions. The length of an identifier cannot exceed 63 characters. The following characters can be used in writing an identifier: numbers 0-9, Latin upper and lower case letters Z, recognized as different characters and the underscore character (_). A number can not be used as the first character.
Main data types used in MQL4/MQL5:
- integers (char, short, int, long, uchar, ushort, uint, ulong);
- Boolean or logical (bool);
- literals (ushort);
- strings (string);
- floating point (double, float);
- color (color);
- date and time (datetime);
- enumeration (enum).
Structures and classes are a complex (abstract) data type that can be operated in MQL4/MQL5. Classes differ from structures in the following characteristics:
- the keyword class at declaration;
- all class members have private access by default whereas structure members have public access;
- class objects always have a table of virtual functions when structures cannot have it;
- the new operator can be applied only to class objects. It cannot be applied to structures;
- inheritance: a class inherit from a class only and a structure derives only from a structure.
Operations and expressions
All common operations - arithmetic, bool, binary etc. are present in MQL4/MQL5. The precedence of operations corresponds to that adopted in С++.
This section needs expansion. You can help by adding to it. (January 2015)
The major disadvantage of the languages of the MetaQuotes programs is the impossibility of creating independent applications as each language is attached to its corresponding platform (Meta -Trader/-Editor 4/Meta -Trader/-Editor/-Tester 5) and the files with extensions .EX4 and .EX5 only work in these platforms. It facilitates automation of trading actions.
- "MetaTrader 4 Official Release". MetaQuotes Software Corp. July 1, 2005. Retrieved August 23, 2017.
- "MetaTrader 5: Official Release". MetaQuotes Software Corp. June 1, 2010. Retrieved August 23, 2017.
- "MetaQuotes Releases Build 600: Marketplaces, MT5 and Regulation in the Balance". Finance Magnates. February 4, 2014. Retrieved August 23, 2017.
- "Updated MQL4". MQL4 Reference. Retrieved August 23, 2017.
- "MetaTrader - investiˇcná platforma pre obchodovanie" (PDF). Is.muni.cz. Retrieved 14 November 2014.
- "Evaluating the Effectiveness and Sensitivity of Forex Trading Robots" (PDF). Unitec.researchbank.ac.nz. Retrieved 14 November 2014.
- "Forex Analysis and Money Management : Interactive Qualifying Project" (PDF). Wpi.edu. Retrieved 14 November 2014.
- "INVESTMENT AND TRADING : An Interactive Qualifying Project Report Submitted to the Faculty of WORCESTER POLYTECHNIC INSTITUTE" (PDF). Wpi.edu. Retrieved 14 November 2014.