Why Do You Need MQL5?
There may be many reasons why you have decided to study the modern MQL5 trading strategies' programming language, and we welcome your decision! Experienced users can easily navigate in the language documentation, as well as in the variety of articles and services presented here. But, if you have just discovered the MetaTrader 5 client terminal, many things may seem to be unusual and confusing at first.
So, what benefits are there to knowing MQL5? Perhaps, you have decided to study a present-day OOP (object-oriented programming) language. After studying MQL5, you can easily master other high-level languages, such as C++, С#, Java and so on. Of course, that does not mean that they are very similar, but their basics have much in common.
Or maybe, you already know one of these languages and have an idea to make your own trading robot or information-analytical system for working on financial markets. In this case, you will easily master the specialized MQL5 language created specifically for that purpose.
Or perhaps, you already know MQL4 actively used for making various trading robots and indicators for the popular MetaTrader 4 trading terminal? Then, you just have to make a slight effort to see the whole power of the new MQL5 language and all the benefits of the new MetaEditor 5 development environment.
There may be plenty of reasons to study MQL5, and we want to give you some tips on where to start and what to pay attention to. So, let us begin.
The Language Possibilities and Features
MetaQuotes Language 5 (MQL5) is developed by MetaQuotes Software Corp. based on their long experience in the creation of online trading platforms of several generations. The following are the language's main advantages.
- Syntax is as close to C++ as possible. That allows MQL5 to easily adopt applications written in other languages.
- The operating speed of MQL5 programs is almost as high as that of С++ programs.
- Rich, built-in features for creating technical indicators, graphic objects and user interfaces.
- Built-in OpenCL support.
- A big standard library and a lot of examples in the Code Base.
- Paralleling mathematical optimization tasks for dozens and thousands individual threads without having to write a special code.
For Newcomers in Programming
If you do not have experience programming in high-level languages, you can take any C++ manual and use it as an example when studying MQL5 language basics (syntax, data types, variables, operators, functions, OOP and so on). MQL5 developers sought to ensure the maximum compatibility of its features with the highly popular C++ language.
Experience shows that it is possible to learn MQL5 from scratch within a couple of months, while less than a year may be enough for some users to study and reveal all its features. See Limitless Opportunities with MetaTrader 5 and MQL5, and perhaps it will inspire you to create something really great.
For MQL4 Users
At first, you may find the new approach to creating indicators uncomfortable. Lots of new event handling functions may astonish you, while C-like syntax and the new data types may initially seem to be unusual.
But, after a little while, you will appreciate all the advantages of MQL5 over MQL4. And, how do you like the rich possibilities for working with charts and graphic objects and the ability to draw any image like on the canvas? You will be able to try all that after getting acquainted with MQL5.
For Professional Programmers
If you code in any of the present-day languages, it will be easy enough for you to quickly master MQL5. You already know ООP and the event model. Now, you only have to learn the following specific functions deliberately meant for algorithmic trading.
- Chart Operations.
- Trade Functions.
- Getting Market Information.
- Custom Indicators.
- Object Functions.
- and others.
There are also the following slight differences in the language syntax implemented for reasons of safe code writing and optimal application operating time.
- No pointer arithmetic. MQL5 pointers are, in fact, descriptors.
- No exceptions.
- Arrays of any type are always passed by reference.
- Arrays cannot have more than four dimensions.
- Arrays and objects cannot be returned from the functions, but it is possible to return an object pointer.
- No goto operator.
If you have not performed trading operations before, you may have some questions concerning trading terms and the Strategy Tester when writing your trading robot. The articles section contains useful publications to help you with that, such as the following publications.
- The Fundamentals of Testing in MetaTrader 5
- Orders, Positions and Deals in MetaTrader 5
- Trade Events in MetaTrader 5
- Speed Up Calculations with the MQL5 Cloud Network
Therefore, MQL5 language is not a problem for a professional programmer. The main issue is acquaintance with trading and related concepts.
MetaTrader 5 Terminal Installation
MetaTrader 5 terminal web installer can be downloaded from the official website via the link https://download.mql5.com/cdn/web/metaquotes.software.corp/mt5/mt5setup.exe. MetaTrader 5 terminal installation is easy enough. All is done in a few clicks. But, we recommend that you install the terminal on any drive but the one wherethe Windows operating system is installed. The reason is that Microsoft has implemented the new system for user actions control starting with Windows Vista, UAC.
Thus, if you are not experienced in system administration or you do not like to navigate through many hidden folders, specify the terminal installation folder away from Program Files to allow the data terminal to be stored in the same directory as the MetaTrader 5 terminal. For example, install the terminal on drive D:\, if the operating system is installed on drive C:\.
More information about the differences in the modes of MetaTrader 5 operation that depend on the installation path can be found in Getting started → For Advanced User → Platform Start in the built-in user guide.
Indicators, Scripts and Expert Advisors
MQL5 language realizes three basic program types. Each type is best suited for solving its specific tasks, such as the following.
- A script is a program designed for a single launch on a price chart. Once the execution reaches the end of the predefined OnStart() handler, the script is completed and unloaded from the chart. The OnStart() function is designed only for scripts. This is the only launch point in which the executable code must be located. A script may contain an infinite loop with short pauses between iterations and thus operate on a chart until it is forcibly stopped. Only one script can be executed at a time on each chart.
- An indicator is a program for calculating value arrays on the basis of price data. Special arrays for storing indicator values are called indicator buffers. The number of allowed buffers in one indicator is practically unlimited.
Each chart can simultaneously have multiple indicators including a few copies of the same indicator. The functions for working with indicator properties are available only from the indicators. They are not available from scripts or Expert Advisors. A program is considered to be an indicator, if the OnCalculate() handler has been detected in it. The indicator lifetime is unlimited. It is executed until it is removed from the chart. The OnCalculate() function can be called only in indicators.
- An Expert Advisor is another type of a program with unlimited lifetime. It also can be located on a chart for as much time as necessary. Expert Advisors usually have the OnTick() event handler, which clearly indicates that we are dealing with a source code of an Expert Advisor. Only one Expert Advisor at a time can be located and, therefore, executed on one chart.
The primary goal of Expert Advisors is the automation of trading systems. But, they can also have exclusively service functions, such as the implementation of a graphical interface for manual trading, and/or the current market situation analysis and visual representation and so on.
It would be better to start learning MQL5 basics from writing scripts, using examples from the MQL5 documentation or the Code Base. Then, you can start working with object functions and experimenting with trade operations on a demo account.
The next stage is writing your own custom indicators and analyzing examples from the code case and articles. By the time you master the indicators, you will be ready for learning event-handling functions.
The final goal is the creation of simple Expert Advisors and proving them against historical data using the Strategy Tester in MetaTrader 5 terminal. There are many articles in Experts, Tester and Trading Systems devoted to this subject.
And, of course, we should mention the most exciting feature concerning MQL5 programming, development of custom modules for Expert Advisors via the MQL5 Wizard. You can find many articles on this subject, and the Code Base contains a variety of ready-made MQL5 Wizard modules.
An MQL5 program works only when some events occur. They may represent downloading and initializing an MQL5 program, new tick arrival (symbol price change), changing chart properties, changing a symbol or a timeframe on a chart, pending order actuation and so forth.
Therefore, the event model allows you to write interactive programs in the simplest way. There are great possibilities for writing custom graphical panels and creating convenient user interface that can suit your needs. Built-in functions for working with graphics allow creation of full-featured and well-designed applications.
The ability to generate custom events using the EventChartCustom() function for any active chart in MetaTrader 5 allows the creation of complex interactive systems. Event trapping and handling are performed by the OnChartEvent() function. The mentioned features are shown in The Player of Trading Based on Deal History and EventChartCustom() function example.
Debugging and User Guide
The MetaTrader 5 terminal and MetaEditor 5 contain well-documented built-in user guides that can be accessed by pressing F1. All documentation is updated automatically via LiveUpdate. Also, this user guide is available online in several languages on the official site of the MetaTrader 5 trading platform.
- https://www.metatrader5.com/en/terminal/help is the MetaTrader 5 terminal user guide.
- https://www.metatrader5.com/en/metaeditor/help is the MetaEditor 5 development environment user guide.
The most important learning source is MQL5 documentation, which is represented not only online at https://www.mql5.com/en/docs but is also available for downloading in CHM and PDF formats in multiple languages.
The client terminal and MetaEditor 5 are closely integrated with each other. You can always switch to another application by pressing F4. This is a very convenient feature when editing code, especially if you are working with several terminals simultaneously.
Any MQL5 program can be debugged in the terminal right from the editor by pressing F5. The chart will be opened automatically, and your program (a script, an indicator or an Expert Advisor) will be launched on it. When debugging scripts, you should also consider that they are uploaded on their own after OnStart() operation completion. Therefore, the debugging process automatically completes at that stage, and the "debug" chart is closed without saving all your graphical objects made by the script. Thus, put a breakpoint or Sleep() with a very large value before the return() operator at the very end of a script.
The debugging mode is necessary both for detecting errors in your program and for studying and learning MQL5. Besides using breakpoints in your code, there is a special DebugBreak() function, which works only if a program is in the debugging mode.
And, of course, we should mention the powerful search engine integrated into MetaTrader 5, which allows searching any necessary data not only in a source file or a folder, but also on the MQL5.community website (Articles, Forum and Code Base).
Obtained results can be filtered by the necessary categories. Therefore, the development environment provides not only the built-in user guide to the editor and MQL5 language but also the ability to find useful materials on the mql5.com website.
The MetaEditor 5 development environment offers programmers plenty of convenient features to simplify code writing and debugging. What else do programmers need apart from the debugging feature? Code profiling, of course. Profiling is gathering the application features, such as the execution time of its individual fragments (functions, lines), in a convenient form.
Profiling allows you to quickly detect the most time-consuming parts in your application. You can evaluate the implemented changes in terms of operation speed to choose the most efficient algorithms. Professional developers are well aware of what they can do using this feature, while newcomers can examine their programs in a new light.
The screenshot above shows profiling of the code shown at the forum (https://www.mql5.com/en/forum/7134). Try code profiling by downloading the code from the mentioned forum thread.
MQL5 Storage: Store and Manage Your Work in a Uniform Way
Another interesting and convenient feature for MQL5 programming is your personal MQL5 source codes storage. Using it, you will always have direct access to your files via MetaEditor 5 from anywhere in the world. You can store not only MQL5 programs but also C++ sources (cpp, h), as well as BMP and WAV source files.
You can add and extract your codes, revert changes — in short, you can do anything that modern SVN systems have to offer. In addition to working with MQL5 Storage directly from MetaEditor 5, you can use any external client that supports Subversion 1.7, like Tortoise SVN.
The Life of Indicators, Charts and Graphic Objects
All the previous experience has been considered when developing MetaTrader 5. Therefore, some features may seem to be unusual at first. For example, the efficient model is used for indicator calculation — an indicator represents a calculation part. Many Expert Advisors,
scripts and other indicators can use results of one indicator. It also means that if one indicator is set on several charts with the same symbol and timeframe, the calculation will be performed in a single calculation entity. This approach saves both time and memory.
Also, the values of one indicator can be calculated using the values of another one or using the arrays' values in MQL5. That allows obtaining complex indicator calculations in a unified and simple way. As it has been mentioned before, the possibilities of indicator values graphical representation in MQL5 language are really immense.
All operations concerning chart properties and graphic objects management are asynchronous ones. That prevents users from wasting time while waiting for the terminal video system to display changes in colors, sizes and so on. If you want to get immediate results of executing the functions from Object functions or Chart operations, call ChartRedraw() for the chart forced redraw. Otherwise, the chart will be redrawn automatically by the terminal at the first opportunity.
You can buy or sell, place pending orders to buy/sell under some definite terms or delete an existing pending order. If OrderSend() has been executed successfully, the trade request execution result is fixed in the MqlTradeResult structure.
At first, you do not need to check the correctness of MqlTradeRequest structure filling when studying MQL5. The standard library has a special CTrade class for performing trade operations. This class has been designed to simplify the work of MQL5 programmers.
|Working with orders|
Places the pending order with set parameters.
Modifies the pending order parameters.
Deletes the pending order.
|Working with positions|
Opens the position with set parameters.
Modifies the position parameters.
Closes the position by.
Opens a long position with specified parameters.
Opens a short position with specified parameters.
Places the pending order of Buy Limit type (buy at the price lower than current market price) with specified parameters.
Places the pending order of Buy Stop type (buy at the price higher than current market price) with specified parameters.
Places the pending order of Sell Limit type (sell at the price higher than current market price) with specified parameters.
Places the pending order of Buy Stop type (sell at the price lower than current market price) with specified parameters
An example of a CTrade class application can be found in the MACD Sample training Expert Advisor from the terminal standard delivery. The Expert Advisor can be found at <terminal_directory>\MQL5\Experts\Examples\MACD. Some other useful classes for working with orders, positions, deals and so on can be found in Trade classes section along with CTrade.
MetaTrader 5 Strategy Tester
MetaTrader 5 not only allows trading on various financial markets using trading robots, but also provides the ability to check their profitability and stability over different parts of history. To achieve this, Strategy Tester has been implemented into the terminal.
It should be considered that the terminal acts like an execution manager, distributing tasks to individual services called agents when testing/optimizing an Expert Advisor. Thus, tests are performed as communication sessions between the terminal and the agents. The tester sends tasks to agents and gets execution results in return.
Messages of the tester and the agents are placed to the journal. When testing, the agents can send a very large number of messages generated in an Expert Advisor by the Print() and Alert() functions. Therefore, not all the messages from the agents are displayed in the journall some of them can be skipped. This is done to prevent testing slowdown because of the necessity to display all messages.
Therefore, the journal is stored separately at the <terminal_folder>\tester\logs\, while detailed logs with all messages are saved in the appropriate folders of tester agents. Keep this in mind when searching detailed logs for test analysis. Fortunately, the tester has a special logs viewer where you can find the logs for a definite interval.
Apart from testing, there is also the optimization mode of an Expert Advisor's input parameters, in which the tester can use dozens, hundreds or even thousands of tester agents (for example, from MQL5 Cloud Network). In this case, messages sending and display by Print() and Alert() functions are completely suppressed not to increase the volume of the outgoing traffic in the direction of the tester and to save space on a PC hard disk where tester agents are located. The only exception is made for the OnInit() function. The function can use Print() to send the message that can clarify a reason for unsuccessful initialization or refusal to perform a test using the ExpertRemove() function for technical reasons.
You can find more interesting information in Tester. We believe that you will appreciate the Strategy Tester possibilities in MetaTrader 5 client terminal.
Pushing the Boundaries
Whoever you are, you will discover new opportunities after studying MQL5. That may include better understanding of programming languages, new insight into trading or acquaintance with new technologies. The new MetaTrader 5 terminal includes so many new features that we probably will not be able to find even a single developer who has managed to reach its whole potential up to now.
In this article, we still have not mentioned a lot of exciting things including convenient work with DLLs, downloading programs from the Code Base into the editor, their one-click launch in the terminal and much more. If you are not afraid of reading the long list of the terminal features, then you are welcome to do so in MetaTrader 5: More Than You Can Imagine!
We wish you all well and hope to see you among the permanent members of MQL5.community!
Translated from Russian by MetaQuotes Software Corp.
Original article: https://www.mql5.com/ru/articles/447