How to use algorithmic/automated trading language for MetaTrader
If you are interested in trading, you have probably already come across the term “automatic trading system” or “adviser” in the MetaTrader (mt5, mt4) terminal. Thanks to these systems, traders have much wider trading opportunities in MetaTrader. The adviser in MetaTrader (mt5, mt4) can work day and night continuously without getting tired. In the same way, he can trade on dozens of markets at the same time, without emotions, performing only programmed actions. These properties are not always only an advantage, but this is a topic for a separate article.
An automatic trading system is essentially an algorithm written in a specific programming language that has tasks under prescribed conditions to open, close and manage positions. An Advisor in MetaTrader is a program consisting of dozens or hundreds of lines written in a programming language. Consider the MQL programming language or MetaQuotes Language of the most popular MetaTrader (mt5, mt4) software.
Algorithmic trading is a certain style of dealing in financial markets, in which a certain trading algorithm, which includes rules for opening, closing and maintaining a position, calculating the volume of a position, and others, is implemented programmatically, connects to a data source and communicates with the server through dealing requests. To put it more simply, a MetaTrader formulates the rules of his trading system, tests and configures it, and then an automatic trading system works on the market without the direct participation of a trader, who can only monitor the effectiveness of its work.
Basics of the language
MetaQuotes Language 5 (MQL5 used in MetaTrader5, 4) is a high-level object-oriented programming language (for MetaTrader5, 4 (mt5, mt4)) and is designed for writing automated dealing strategies, custom technical indicators for analyzing a variety of financial markets. It allows you not only to write a variety of expert systems designed to work in real-time but also to create your own graphical tools that help you make trading decisions in the MetaTrader platform.
MQL5 is based on the concept of the widespread C++ programming language (used in MetaTrader5, 4). Compared to MQL4, enumerations, structures, classes and event handling are added to it. Due to the expansion of the number of built-in basic types, the interaction of executable programs on MQL with other applications is maximally facilitated. The syntax of the MQL5 language is similar to the syntax of C++ or Python, and this makes it easy to transfer programs from modern programming languages to it.
Python for MetaTrader
Python is a modern high-level programming language for developing scripts and applications. Python contains many libraries for machine learning, and Python also helps in automating processes, analyzing as well as visualizing data.
The MetaTrader package for Python is designed to conveniently and quickly obtain exchange information through interprocess communication directly from the MetaTrader 5 terminal. The data obtained thanks to Python can be further used for statistical calculations and machine learning.
It is possible to use MetaTrader5 (mt5) with Python 3 on financial stock exchanges, Forex, CFDs and Futures. You can get quotes in Python from MetaTrader5, but there is no full-fledged connection between them. This wrapper was created taking into account the changes in Python 3.7. Python is now the standard for machine learning libraries. Python itself is quite slow, and therefore all machine learning libraries in C/C++ use it only for user interaction. The main idea and difference of this wrapper from the rest: data exchange between MYSQL and Python through pre-created functions. Thanks to Python, this is the fastest and most reliable method of data exchange. There is no time spent parsing and compiling Python code.
What do you need to know?
First of all, to work in MetaTrader5, 4, you need to get a basic understanding of the market. Reading literature is good for this. At this stage, you don’t have to build hypotheses and test them. You just need to get a general idea of what generally exists in the world of trading. What exactly do you need to master in MetaTrader5, 4? Basic knowledge about the functioning of the market, technical analysis (levels, figures, etc.), the functioning, calculation and purpose of various indicators, classical dealing systems. All these things can be found in any reference book on technical analysis. Also, create your own account on the site. Using the account, you can find a lot of useful information. You can also get a closer look at the tools and find out the current price for various cryptocurrencies in your account.
In the first stage, to work in MetaTrader5, 4 terminal, you need to study mql4 and Python. Most of the scripts, indicators, and Expert Advisors for the MetaTrader 4 terminal are written in this language. The language is not complicated. In principle, a month is enough for its more or less tolerable mastering to write your first Expert Advisor. Next to work in MetaTrader, it is worth studying the mql5 language for the future — at the moment, the MetaTrader5 (mt5) terminal provides an opportunity to set up automated dealing. Which language will be useful to you next? Everything will depend on your goals, but the most common among automated (algorithmic) trading are the following: C++, C#, Java, Python, MatLab, R. Python will be very useful to you since Python has separate capabilities on the platform. Python helps to transfer symbols more clearly and quickly. After studying one of these languages, you will get the opportunity to write code for your research and tools for algo trading. For any of these languages, you can find excellent open-source projects and libraries that can help you a lot.
Important basic concepts of the language:
Program execution. When executing the written program, the MetaTrader will “read” the code and, accordingly, perform the appropriate actions in the MetaTrader5, 4 (mt5, mt4) terminal.
Tick. New incoming price.
Comment. Part of the program code that is not executed. The text of the comment is indicated as follows: // and is colored gray in the code.
The compilation is the final stage of program creation. The compiler checks the code symbol within the Meta-Editor, and if it executes it without symbol errors.
Data Types
The main data types used in MQL5:
- whole data (char, short int, uchar length, ushort, uint ulong);
- Logical data (Bool);
- literals data (USHORT);
- lines data (string);
- floating-point data;
- color data;
- date and time data (datetime);
- transfers data.
Data structures and classes are complex (consolidated) data types that can work in MQL4/MQL5. Classes differ from structures in the following properties:
- the class keyword in the declaration;
- all members of the class have private access by default, and members of the structure have public access;
- class objects always have an array of virtual functions;
- The new operator can only be applied to objects of the class;
- Inheritance: A class inherits from a class, and a structure arises only from a structure.