11 KiB
MQL
MetaQuotes Language 4 (MQL4) is a built-in language for programming trading strategies. This language is developed by MetaQuotes Software Corp. based on their long experience in the creation of online trading platforms. Using this language, you can create your own Expert Advisors that make trading management automated and are perfectly suitable for implementing your own trading strategies. Besides, using MQL4 you can create your own technical indicators (custom indicators), scripts and libraries.
Programs written in MetaQuotes Language 4 have different features and purposes:
Expert Advisor is a mechanical trading system linked up to a certain chart. An Expert Advisor starts to run when an event happens that can be handled by it: events of initialization and deinitialization, event of a new tick receipt, a timer event, depth of market changing event, chart event and custom events. An Expert Advisor can both inform you about a possibility to trade and automatically trade on an account sending orders directly to a trade server. Expert Advisors are stored in terminal_directory\MQL4\Experts. Custom Indicator is a technical indicator written independently in addition to those already integrated into the client terminal. Like built-in indicators, they cannot trade automatically and are intended for implementing of analytical functions only. Custom indicators are stored in terminal_directory\MQL4\Indicators Script is a program intended for a single execution of some actions. Unlike Expert Advisors, scripts do not process any actions, except for the start event (this requires the OnStart handler function in a script). Scripts are stored in terminal_directory\MQL4\Scripts Library is a set of custom functions intended for storing and distributing frequently used blocks of custom programs. Libraries cannot start executing by themselves. Libraries are stored in terminal_directory\MQL4\Libraries Include File is a source text of the most frequently used blocks of custom programs. Such files can be included into the source texts of Expert Advisors, scripts, custom indicators, and libraries at the compiling stage. The use of included files is more preferable than the use of libraries because of additional burden occurring at calling library functions. Include files can be stored in the same directory as a source file - in this case the #include directive with double quotes is used. Another place to store include files is terminal_directory\MQL4\Include, in this case the #include directive is used with angle brackets.
Language Basics
The MetaQuotes Language 4 (MQL4) is an object-oriented high-level programming language intended for writing automated trading strategies, custom technical indicators for the analysis of various financial markets. It allows not only to write a variety of expert systems, designed to operate in real time, but also create their own graphical tools to help you make trade decisions.
MQL4 is based on the concept of the popular programming language C++. The language has enumerations, structures, classes and event handling. By increasing the number of embedded main types, the interaction of executable programs in MQL4 with other applications through dll is now as easy as possible. MQL4 syntax is similar to the syntax of C++, and this makes it easy to translate into it programs from modern programming languages.
Constants, Enumerations and Structures
To simplify the program writing and to make program texts more convenient for perception, the MQL4 language provides predefined standard constants and enumerations. Besides that, service structures are used for storing information.
Standard constants are similar to macros and are of int type.
The constants are grouped by their purposes:
- Chart constants: are used when working with price charts: opening, navigation, setting parameters;
- Objects constants: are intended for processing graphical objects that can be created and displayed in charts;
- Indicators constants: are used for working with standard and custom indicators;
- Environment state: constants describe properties of a MQL4-program, show information about a client terminal, financial instrument and current account;
- Trade constants: allow to specify a variety of information in the course of trading;
- Named constants: are constants of the MQL4 language;
- Data structures: describe data storage formats used;
- Codes of errors and warnings: describe compiler messages and trading server answers to trade requests;
- In/out constants: are designed for working with file functions and displaying messages on the screen by the MessageBox() function.
MQL4 Programs
For the mql4-program to operate, it must be compiled (Compile button or F7 key). Compilation should pass without errors (some warnings are possible; they should be analyzed). At this process, an executable file with the same name and with EX4 extension must be created in the corresponding directory, terminal_dir\MQL4\Experts, terminal_dir\MQL4\indicators or terminal_dir\MQL4\scripts. This file can be run.
Operating features of MQL4 programs are described in the following sections:
- Program running: order of calling predefined event-handlers.
- Client terminal events: description of events, which can be processed in programs.
- Call of imported functions: description order, allowed parameters, search details and call agreement for imported functions.
- Runtime errors: getting information about runtime and critical errors.
Expert Advisors, custom indicators and scripts are attached to one of opened charts by Drag'n'Drop method from the Navigator window.
For an expert Advisor to stop operating, it should be removed from a chart. To do it select "Expert list" in chart context menu, then select an Expert Advisor from list and click "Remove" button. Operation of Expert Advisors is also affected by the state of the "AutoTrading" button.
In order to stop a custom indicator, it should be removed from a chart.
Custom indicators and Expert Advisors work until they are explicitly removed from a chart; information about attached Expert Advisors and Indicators is saved between client terminal sessions.
Scripts are executed once and are deleted automatically upon operation completion or change of the current chart state, or upon client terminal shutdown. After the restart of the client terminal scripts are not started, because the information about them is not saved.
Maximum one Expert Advisor, one script and unlimited number of indicators can operate in one chart.
How MQL Programs Run
Right after a program is attached to a chart, it is uploaded to the client terminal memory, as well as global variable are initialized. If some global variable of the class type has a constructor, this constructor will be called during initialization of global variables.
After that the program is waiting for an event from the client terminal. Each mql4-program should have at least one event-handler, otherwise the loaded program will not be executed. Event handlers have predefined names, parameters and return types.
| Type | Function Name | Params | App Type | Comment |
|---|---|---|---|---|
| int | OnInit | none | Expert Advisors, indicators and scripts | Init event handler. It allows to use the void return type. |
| void | OnDeinit | const int reason | Expert Advisors, indicators and scripts | Deinit event handler. |
| void | OnStart | none | scripts | Start event handler. |
| int | OnCalculate | const int rates_total,const int prev_calculated,const datetime &Time[],const double &Open[],const double &High[],const double &Low[],const double &Close[],const long &TickVolume[],const long &Volume[],const int &Spread[] | indicators | Calculate event handler for all prices. |
| void | OnTick | none | Expert Advisors | NewTick event handler. While the event of a new tick receipt is being processed, no other events of this type are received. |
| void | OnTimer | none | Expert Advisors and indicators | Timer event handler |
| double | OnTester | none | Expert Advisors | Tester event handler. |
| void | OnChartEvent | const int id,const long &lparam,const double &dparam,const string &sparam | Expert Advisors and indicators | ChartEvent event handler. |
Libraries do not handle any events.
Functions prohibited in Indicators and Expert Advisors
Indicators, scripts and Expert Advisors are executable programs written in MQL4. They are designed for different types of tasks. Therefore there are some restrictions on the use of certain functions, depending on the type of program. The following functions are prohibited in indicators:
All functions designed for indicators are prohibited in Expert Advisors and scripts:
The library is not an independent program and is executed in the context of the MQL4 program that has called it: script, indicator or Expert Advisor. Accordingly, the above restrictions apply to the called library.