Introduction to MetaTrader 5 and Programming with MQL5
Create your 1 st Investment Robot with MQL5 step by step from ZERO.
All rights reserved. This e-book or any part thereof may not be reproduced or used in any way without the express written permission of the author or publisher, except for the use of short quotations in a review of the e-book.
First edition, 2018.
Author: Rafael F. V. C. Santos ( )
Specialist in strategic risk management applied to the financial market. He works with the development of automated investment strategies (Robots of investment - Expert Advisor) using machine learning and spatial statistics. Graduated in Chemical Engineering from the Federal University of Pernambuco (UFPE) - Brazil. He holds a Masters and PhD in Civil Engineering (UFPE) in the areas of characterization, modeling and statistical simulation, applied to oil wells and reservoirs. He has several articles, with the theme of applied statistics, published in national and international magazines and congresses.
Copyright2018 de Rafael F. V. C. Santos.
Chapter 1
Introduction
1.1. Digital Age
We are living in the digital age. Nowadays, information and knowledge travel through the optical fibers. Everything is in constant development and accelerated growth. This evolutionary dynamic is unprecedented in the history of mankind.
Devices and objects are becoming increasingly intelligent and independent. Nowadays, it is possible to find, for sale, smart phones and watches that recharge the battery with even the temperature of the human body. Research shows that by 2025, at least half of the US fleet will be traveling completely autonomously. That's right, the profession of driver, taxi driver and many others, are counting their days. And for the financial market this is no different. Professional traders are being replaced by algorithms.
These are just small examples of the avalanche of modifications and benefits brought in from the world shouting for change. We are changing the world and the world forcing us to change into a virtuous circle of prosperity.
1.2. Artificial Intelligence
Embedded systems are gradually dominating our daily lives. People and things are becoming more and more connected to the internet. A new intelligence is created from this new cosmos. We are opening the doors to Artificial Intelligence (AI) . Practically everyone benefits (or will benefit) in some way from the wonders brought by AI.
Many say that this new generation may bring us problems, but we must be optimistic about the uncertainties, as we are on the way to an inevitable and non-return path.
Several companies and investment funds are currently working heavily on the use of artificial intelligence to make their investment decisions. From AI, we can filter and group information and knowledge hidden in apparently meaningless numeric data and connection.
In this race, it is advantageous to dominate subjects and knowledge related to mathematics, statistics and computer programming.
1.3. High Frequency Trading - HFT
Investors and speculators in the financial market know that the main enemy to make money in this world is precisely the emotional factor. Human beings have desires, wants, greed, fears and fears that make the activity decide the best time to buy and / or sell a real challenge.
When we are working with Investment Robots, we are discussing a strategy that has been previously defined and structured so that it can be executed in a timely manner. Fortunately, robots do not get tired, stress or have any feelings that allow misunderstandings and / or biases when buying or selling.
Optical fibers allowed for a revolution in communication. In the investment world, for example, every thousandth of a second can cost millions of dollars. So-called High Frequency Traders (HFTs) operating on the world's top stock exchanges makes decisions at infinitesimal speeds and thus are threatening many professional brokers and traders.
The HFTs' decision to buy and / or sell is taken in milliseconds, up to ten to fifty times faster than a simple wink of the human eye (lasting approximately 300 milliseconds). It is practically unfeasible and unfair the operational comparison of any computational system of this category with a normal human being.
Recent data (year 2018) show that more than 50% of US financial operations are carried out by investment robots. And the results show that these robots can increase financial returns by up to 15%. There are some stock exchanges where more than 90% of operations are carried out by investment robots.
1.4. Computer Programming
Most people have the following objections when it comes to programming: it's very difficult, needs a lot of practice, has to know a lot of math, I've never been good with numbers. However, many of these objections are nothing more than a misunderstanding of this programming activity, which is as trivial as telling a brief story with beginning middle and end.
The interesting thing is that anyone able to write the steps of running a cake recipe on a sheet of paper is able to create a computer program. To program is to communicate with the outside, with the other, in order to conduct actions. For our case, this other is the machine.
We have a structure of activities to be followed and we need, for a good realization of it, an adequate sequence of order of execution. Programming is to master a language the machine understands and know how to position our deliberative needs in time and space, so that they are executed correctly.
Therefore, it is up to every curious investor and with the patience to learn something new to look at in the world of programming. This is because scheduling computers is a much simpler activity than many imagine.
1.5. What will we learn?
In this e-book we will learn how to create, step by step, an automated investment strategy. We will create an Investment Robot (Expert Advisor - EA) from scratch using the MetaTrader 5 and MQL5 programming language.
Thus, having a program written in a high-level language (MQL5) (those closest to the understanding of humans, low-level languages are closer to the understanding of machines) we will be able to leave the arduous and exhausting work of monitoring purchase and sale of a financial asset on the stock exchange for the robot.
We will spend a few hours to program a robot known as Expert Advisor and spend the rest of the time monitoring the execution of the strategy in a much quieter and safer way. That leaves more time for a contemplative and pleasurable life.
1.6. Why MetaTrader 5?
MetaTrader 5 is a powerful platform for visualizing, operating and scheduling investment strategies for various types of financial markets.
It was created in the year 2005 and continues to be improved and distributed by MetaQuotes ( https://www.metaquotes.net ). We can use MetaTrader 5 to operate in the stock markets, futures, options, currency pairs (FOREX) among others.
MetaTrader 5 was chosen to build our first EA, as it is a 'free' tool, quite powerful and complete from the point of view of the main functionalities that a trader platform should have.
Inside MetaTrader 5 we have the option to create scripts, indicators and, of course, investment robots using the MQL5 programming language. This language is very similar to C ++, Java and C #. So anyone who already knows any of them will feel extremely comfortable learning MQL5.
1.7. EA Strategy
We will develop from scratch a strategy of crossing two moving averages (one fast and one slow) with the possibility of an input filter in operations with the rather popular indicator RSI (relative strength index).
Next page