MQL5 PROGRAMMING – HOW TO USE FUNCTIONS

video
play-sharp-fill
In this video we take a look at functions. This here is an Expert Advisor, thus an automated program for the Metatrader 5. and whenever something is carried out automatically, then you can be quite sure that functions are used there. We want to look now once how to call or use such a function. For this we click on this small symbol in the Metatrader here above or press the F4 key. Here we see now our simple template, which we have created in the last video. And this colored marked OnTick function has two round brackets. And whenever you see something like that colored and followed by two round brackets in the meta editor, then it is a function. The OnTick function is part of MQL5. And it is called whenever the price changes on the chart. Up to now it does not contain any content. Everything between these curly brackets would be executed. But even if nothing is in between, the whole thing can be compiled here. We have received no errors and no warnings here. And compiling means that our human readable source code here is translated into machine readable text for the Metatrader. If you look at the directory, you can see here that we have a mq5 file. If I click on it with the right mouse button and open it with Notepad, then I get the human readable source text here. The ex4 file is executable and intended for the Metatrader. If you open it with Notepad, it looks like this in something like this. This makes no sense for us humans, but it is readable for our computer and the Metatrader. But now we would like to build in another function in our template. Let’s try it with the comment function. As you see, the function Comment is suggested here already after three letters. This is the so-called Autocomplete function of the Metaeditor. It suggests me here what I probably want to use. I can then click on it with the mouse. Also with this function we use round brackets. And into these round brackets we put the text Hallo MQL5, followed by a semicolon. A semicolon represents the end of a statement for the compiler. It doesn’t matter if everything before it is on a single line. We could theoretically and practically distribute this over several lines here. Recompile. And this would also work without errors. Because for the Metaeditor all this is a single statement. And if the compilation process was successful, we switch back to the meta editor with this small button or the F4 key. If we now drag our Expert Advisor here on the Chart and confirm, then we should get the text Hello MQL5 here above with the next price change. This has just happened now. And in this short video you have learned how to use a function, that a semicolon means the end of a statement, that the OnTick function is called at every price change, what is the difference between mq5 and ex5 files and how to use the Comment function to automatically display a text on the chart. And you programmed that yourself, with these few lines, here in MQL5.