MQL4 TUTORIAL BASICS - 2 WHAT IS A FUNCTION

Опубликовано: 27 Январь 2019
на канале: MQL4Tutorial
12,600
151

https://mql4tutorial.com/?s=function

With MQL4 for Metatrader4 we are going to create a function in our Expert Advisor. Whenever automated trading is used, you can be sure that functions do the work - this is just a simple example...

In this video we are going to talk about functions that can be used for an Expert Advisor, an automated program that runs in Metatrader 4.

In our case we are using the Forex Trading Framework in the MQL4 version and in the last video we started by creating a simple template using the Metaeditor, so let's click on the icon for the Metaeditor or press F4.

This is the template that we have created, it's already complete I can compile it, that means it's translated for the Metatrader.

When we use Metaeditor we create an “mq4” file, that's the source code file, you could also open this file with Notepad or Notepad++ and it would be readable but if you compile the file it will create an executable file for Metatrader 4. This has the extension “ex4” and when we try to open this file in Notepad we get a lot of messy stuff because this file is already compiled and humans can no longer read it, but it's easy to read for Metatrader 4.

So the “OnTick” function is called whenever we have a price change on the chart and these two round brackets here are used for so called parameters, so whenever you see something like this in combination, it's most likely a so-called function.

You may know functions from your calculator, when you click on one of those buttons here you have called a function and it also uses the round brackets here, so let's use another one the: “Comment” function. You see as I'm typing Metaeditor is going to complete the word: “Comment” here, that is called auto-complete function, when I type: “Com” it already knows there is only one function that is already existing, so let's click on the “Comment” suggestion here and you see we get the same color as the “OnTick” function, so let's use a round bracket and I would like to see the output: “Hello MQL4” and this is a text so I need to use quotation marks and at the end of the line we are going to finish our statement by using a semicolon. This is for the compiler, so the compiler knows that this line is completely finished. It would be also possible to use multiple lines like this way, the compiler would still see everything from the last semicolon to this semicolon as a complete statement, so let's click the “Compile” button or press F7 and now the compiler tells us we don't have any errors and we don't have any warnings here, so let's drag the Expert Advisor on to a chart and when the next price change comes in we see the words: “Hello MQL4”.

Now you know how to use the “Comment” function with one parameter, you know that the semicolon is the end of a statement, you know the difference between an “mq4” file and an “ex4” file and you know how to create a text output on your chart and you have done it yourself with a few lines of MQL4 code.