MQL5 — You too can become a master of this language

Daniel Jose | 7 July, 2023

Introduction

In this article, I am going to do something different from what I've been doing in earlier articles. Basically, this material is intended for those who have little or no knowledge of MQL5 or programming in general. So, the article will be like an interview with myself. in which I will explain a little bit how to learn MQL5 from scratch and to become a professional.


A little of story

Since I started writing articles for the MQL5 community, I have received a lot of feedback. Many people contact me, asking how I learned to program in MQL5 so that I can do the things that I describe in the articles. Most of these people are enthusiasts looking for a basic guide to take their first steps in programming. So, this article is dedicated to all of you, enthusiasts and novice MQL5 programmers willing to learn MQL5 programming.

Before proceeding with this topic, I need to say something. The purpose of this material is not to demotivate you but, on the contrary, to increase your motivation even more.

You might think that I must have many years of MQL5 programming experience. However, by the time of writing this article, I have been using the MQL5 language for only a year and a half. That's right, I have less than two years of experience as an MQL5 programmer. You might probably be wondering how someone with so little MQL5 programming experience could manage to do the things for MetaTrader 5 that were shown in the articles.

This is because the developers of the MQL5 programming language had a great idea of making the language look like C/C++ in many aspects. So, if a programmer has extensive C/C++ experience, it will be quite easy to learn MQL5, since there is no need to relearn all concepts, syntax and commands.

Why am I saying this? Because I have been a professional C/C++ programmer since the early 2000s, that is, I have been programming in C/C++ for over 23 years. If I had to learn a new language in order to do something for MetaTrader 5, most likely I would finally give up.

Many people think that to be a good programmer, you need to take some courses. However, to really learn to program, you shouldn't be afraid of making mistakes. You should never get attached to a code or idea. You should always look to experiment and try new things.

This may sound a little strange, since many new people always like to do the same thing and in the same way. But a person who wishes to become a great programmer is always looking for different ways to do the same thing. Also, many people do not understand that a programmer will always be inspired by others. They can take working source code developed by a more experienced programmer and change it little by little, experimenting and watching the result. This way they learn how the program is actually built.

Some people think that to learn MQL5 you have to start studying something. In fact, if anyone who doesn't have any prior knowledge asks me how to learn, I always recommend starting with the simplest. In other words, learn a little about C to really know the syntax and programming logic, as well as the commands. Once you understand the basics of the C language, start studying C++ and at the same time start practicing MQL5. The simple fact that you already have basic C knowledge will greatly help you start programming in MQL5.

Some people may think that C is obsolete, but the truth is that C is an extremely well documented and widely used language. You can find a lot of materials to learn how to use C. Please note that I'm not saying that you should delve into C. Quite the contrary, if you learn some basic commands, syntax and commands, that will be enough for you to get started with MQL5.

This may seem a little discouraging: learn C to develop advance in MQL5? Perhaps, if you put in a lot of effort and find someone willing to teach you, you can start directly with MQL5. But from my first experience and the opinions of other people, I can say that the task will not be so easy. Almost all quality material is concentrated here, in the MQL5 community. In the rest of the internet, the material is scattered and can be of poor quality, which will do more harm than good. When it comes to the C and C++ languages, there is a huge amount of excellent and very accessible material. So, it's up to you to choose where to start.


My first code in MQL5

Many people think that since I already know how to program in C/C++, I immediately started with complicated and elaborate projects in MQL5. I wish I could say that this is how it all started. However, the reality turned out to be quite different.

Whenever we take the first steps in developing our own solutions, we often start with what we think we already know. In my case, when I started to use MQL5, all I had at hand was the language documentation. I didn't even know there was a community dedicated to helping other interested programmers.

So, studying the documentation and examining source codes that come with MetaEditor, I started experimenting. Below is my first real code which I created based on what I understood.

01. //+------------------------------------------------------------------+
02. #property copyright "Daniel Jose"
03. #property version   "1.00"
04. #property description "Modify special chart properties"
05. #property script_show_inputs
06. //+------------------------------------------------------------------+
07. enum eWhat  {
08.                eScaleDate  = CHART_SHOW_DATE_SCALE,   //Time scale
09.                eScalePrice = CHART_SHOW_PRICE_SCALE   //Price scale
10.             };
11. enum eOp    {
12.                eFalse,                                //Makes hidden
13.                eTrue                                  //Makes visible
14.             };
15. //+------------------------------------------------------------------+
16. input eWhat user00 = eScaleDate;                      //Graphic property
17. input eOp   user01 = eFalse;                          //What to do?
18. //+------------------------------------------------------------------+
19. void OnStart()
20. {
21.    ChartSetInteger(0, (ENUM_CHART_PROPERTY_INTEGER) user00, user01);
22. }
23. //+------------------------------------------------------------------+

Source code of my first MQL5 program

There is nothing extraordinary or innovative. This is primarily code, the purpose of which was to verify if I correctly understood the documentation and how elements could be defined and used. The following video shows the code execution result.


Demo video

This is something extremely basic. But being able to create this code in a completely autonomous way, without anyone's help, except for documentation and source codes from MetaEditor, showed me that I can do something else. All I had to do was to adapt my existing knowledge of C and C++ to MQL5. This is where things really started to progress.

Maybe, looking at this simple code above, you don't find anything valuable in it. But as usual, the first task of any beginner in a programming language is to create code known as "HELLO WORLD", where the idea is to display a message on the screen.

But I'm way past the phase of the "HELLO WORLD" code. So, I went to try something a little more complicated. However, because I didn't have that much experience and understanding of how MQL5 actually worked, I started by developing a script. It was something much easier for me, given the little experience and knowledge of how things actually happened.

Only after some time I started to realize that MQL5 is not actually a procedural language, but an event-based one. This discovery radically changed my approach to programming and code analysis. Wanting to improve my skills, I started studying and reading articles here in the MQL5 community, and I realized that although many could get their code to work, they were learning MQL5 in a relatively basic way, at least in my opinion. Therefore, I began to develop and experiment with more complex solutions that would explore the capabilities of the MetaTrader 5 platform and the MQL5 language in more depth.

And at some point I decided to start sharing the results of my experiments. This is how my first article appeared. It is one thing to build code and there is another thing to explain how it is built and designed. I knew that both experienced and beginners would read these articles.

The articles have undergone changes both in form and content. I did this so that even those with little experience could understand the information. And even if you cannot immediately absorb all the knowledge presented in the material, over time, as you improve your programming skills, you will be able to correctly assimilate what I am trying to convey in a particular article.

Knowledge needs to be shared. I see no glory in dying with all your wisdom without leaving behind something valuable, something that could change someone's worldview, awaken to something that many do not realize, because they always keep their heads down.

But let's get back to the code. I want to show you that even a few lines of code can contain much more information and knowledge than a thousand lines of code. Although originally, all the information, which is not part of the C/C++ syntax, and therefore MQL5, was not in English. But this is just a mere detail. Let's see this little code and understand how it works. And mainly: Why it works.


Analyzing the code

Lines two, three and four could be dropped from the code. In fact, they serve MetaTrader 5 only to inform the user about the purpose of the code. Being the user, you can simply remove these lines from your learning code.

Line five is very important because without it, MetaTrader 5 will not understand that the script can be customized by the user through some kind of input structure, which in this case is 'inputs' shown on lines 16 and 17. We'll get there soon.

Now comes something that many may not understand. Especially, if they only studied MQL5 or if they are coming from some other language very different from C/C++. I am talking about the enumerations, which are present in lines 07 and 11.

Most likely you can be confused when looking at such constructions. You may even imagine that they are something too complicated for you. But precisely because MQL5 was built to bring in C/C++ programmers, that makes it so pleasant to be used by those who already have even a small experience in C/C ++. But let's understand the enumerations, at least superficially.

An enumeration can be roughly understood as a sequence of data where you can assign value to them or not. When you don't assign a value, the count will always start with the first possible value which in this case is zero. Therefore, the enumeration defined in line 11 follows the same principle as the value 0 or 1. In C / C++, as in MQL5, the zero value is considered false, and the value of one is considered true.

Therefore, line 12, where the eFalse value is defined, we have the indication of zero, and in line 13, which is the second value of the enumeration, we will have the value of one. If we had more things defined, they would follow the sequence, so the next would be two, three, and so on. This is the simplest case.

I want to draw your attention to something else. At the end of line 12 we have something that would be a comment in C/C++. We also have the same type of things in other lines. Let's focus on line 12, but this will also apply to all other places.

If we look at the script when it is launched on the chart, we will see Figure 01. In it, I highlight what exactly matches the comment on line 12.

Figure 01

Figure 01 - User interaction window


Such things greatly simplify MQL5 coding. In other languages, we would have to do a few things to get what is done here in MQL5 in such a simple and practical way. Therefore, you need to understand that writing a comment to the code is the right thing to do, because it allows you not only to make the code more understandable, but also to customize the message that will be displayed to the user.

Let's now take a step back to line 07 where we have another enumeration. However, this enumeration is different from the one I just explained. These are not entirely different things: the difference is that now we assign values to each of the elements. That is, the compiler will use the values we specified, which in this case are constants defined in MQL5. You might ask: Why not use constants directly instead of assigning them to an enumeration? The reason is the ease of implementation. If we were to use constants directly, we would have to create something else, such as an array or a structure, and this is much more difficult. Using my approach, you can insert any value in any position or sequence. And if some constant of interest appears in the sequence I declare, I don't really need to assign or declare that constant; all I have to do is add more content to the enumeration, and the language will take care of ensuring that the correct values are used and assigned appropriately.

See how having a solid, well-built foundation in a language like C/C++ helps us when writing MQL5 code. What we show here is not often found in MQL5 code. In fact, I have noticed that many resort to much more complex and time-consuming constructions in order to achieve the same that they got here. And note that I ended up writing just one line of code (line 21).

Now pay attention to the following: in line 21 itself, which is actually the only one that will be executed, we need to do an explicit type conversion. This is necessary for the compiler to understand it and, accordingly, correctly generate the final code. But why is explicit conversion so important? The reason is in the compiler. It doesn't really matter to the processor, because we're only using numbers, and that's what the processor actually sees. Types are not of interest to the processor, it just observes numbers. But the second argument of line 21, despite being a numerical value, is not exactly what is expected by the ChartSetInteger function. This function expects a certain type, not a numerical value. Even if the value represents the expected type, i.e. the value is exactly the constant we are using, the compiler will see this as an error. So, to fix this, we explicitly force the numerical value to be converted to a type expected by the ChartSetInteger function. Thus, the compiler will be able to understand that we are aware of the values we are passing to the function.


A few thoughts on Python

Before closing our conversation, dear reader and novice programmer, I want to share some final thoughts, the results of my experience and knowledge gained over several years of working with various programming languages. Ny purpose is to prevent any future frustrations that may arise when trying to perform certain actions in MetaTrader 5.

Although our main topic is MQL5, the following question is often asked: whether or not to study Python. Especially for those who are starting to learn programming to trade in the financial markets, the question arises whether it is worth deepening their knowledge of Python to work with MetaTrader 5. This question is very relevant, since learning Python is much easier and faster than going through the difficulties of learning C, then C++, and finally getting a solid and consolidated foundation for professional work with MQL5. However, there is one point to keep in mind so that you do not think that you have wasted your time learning (or not learning) Python.

The Python programming language (although some prefer to refer to it as a scripting language) is not comparable to what can be done in MQL5 - perhaps that is why some do not consider it a programming language, but we will not enter into this debate here. If your goal is to learn and be able to generate codes just to test, verify and analyze something in a pure mathematical way, then the shortest and easiest solution is to learn Python. This is because you it is easier to run backtests and the like in Python, interacting directly with MetaTrader 5, than trying to do the same thing using MQL5.

It may seem strange to some, but with a little training and a lot of dedication, you can easily create a Python script that can do the same thing that a programmer with much more experience can do with MQL5. Many times, the programmer would have to create indicators, Expert Advisors, in addition to having to configure the strategy tester, to verify whether a given operating system, the so called SETUP, is profitable or not. Not to mention the risk of putting everything at risk by not setting everything up properly.

One might think that when using MetaTrader 5, we have access to charts and a beautiful presentation of results, which will make it easier for us to analyze. However, consider how much knowledge you will need to program all the functions needed to perform a simple backtest directly in MetaTrader 5.

Working with Python, you can use the package to access MetaTrader 5 data. Using everything that can be done in Python, it is easier to carry out the same research that an MQL5 programmer does. Once the research is completed, you can alternatively export the data to Excel or another program to do all the graphical representation that can be done in MetaTrader 5, making it easier to analyze the results.

I need to add some brief explanation here. Why use Python instead of Excel correctly since the results would be plotted in Excel? The reason is simple: VBA (Visual Basic for Application) does not give us direct access to the same data that we would have through Python, given the use of MetaTrader 5 data. To do this in Excel, you should either create a DLL to support such things or export lots of files in CVS format in order to get the same data.

Once inside Excel, you will have perhaps even more work to produce all the calculations that would be done in Python. On the downside, if you decide to change the database, you'll have to do all the work again in Excel. But if you're using Python, all you need to do is instruct Python to change the database, and it will provide this directly to MetaTrader 5 without any extra effort on your part.

So, my advice is to try to learn as much as you can, because each tool has its advantages and disadvantages. Although Python is very useful, it does not allow you to create an indicator to be displayed on a chart or an Expert Advisor to help you trade. This can only be implemented through MQL5. Therefore, there is no better or worse tool here. They complement each other and serve different purposes. In any case, study both things, because you never know when you will need to use one or the other.


Conclusion

Despite all the simplicity involved here, you must always remember the following: You are just starting out. Don't try to do something complicated right away. Try to create something that works. Even though it may seem completely useless at first glance, start with something simple.

Start experimenting with the functions little by little. Study the C/C++ language to expand your MQL5 programming capabilities. Always practice and try to do things in your own way, differently from what many always do, because no matter how complicated a task may seem, there is always the way to do it better.

Learn to simplify your codes, be patient and persistent. Don't give up if you find a problem that's a little beyond your knowledge. In such a situation, learn even more and try to create a solution based on what you already know. If you have already solved a problem, look for a better solution for it. Your ultimate reward will be vast and deep knowledge, a wealth that no one can take away from you. Knowledge can be borrowed but it can never be stolen.

Be humble and try to share your knowledge with others. By doing so, you learn new things and also show new possibilities for those who are focused another solutions. That's all for today.