MQL5 TUTORIAL BASICS – 2 WHAT ARE FUNCTIONS

In this video we want to talk about included functions.
Whenever something is automated with MQL5 there are functions involved and we have a few functions that are already included in our Expert Advisor template here; the first one is the “OnInit” function, it’s the expert initialization function and it will be called when you drop your Expert Advisor on to a chart, so this one will only run once, it does have a return value here, basically you could check if the “OnInit” function was successful. It will return one of these values here and that would tell us if the “init” process was successful or if it failed.
We also have another function here that is called: “OnDeinit”, it is use to clean up before the Expert Advisor is closed, it doesn’t return anything, that’s what “void” is saying – “void” is used whenever something doesn’t return a value – in this case the reason is very simple, “OnDeinit” is the last thing that will run in our program and there is nothing that we can return to anything to because its closed afterwards.
This is the “OnTick” function, this function runs whenever the price changes on the chart, most of the coding logic would be triggered by the “OnTick” function and whenever you see a price change on one of your charts everything between the two brackets here will be executed.
We can now remove the rest of the template here because for our simple example we just need the “OnTick” function and we will use it to output the local time and that’s done by calling another function.
Whenever we want to see an output on our chart we use the “Comment” function, this one will output a user value on the left corner of the chart, takes so called parameters in round brackets, that could be a text, like “Hello” and this is already a complete Expert Advisor.
We now could compile it, click on the little button here to go back to Metatrader and when I take the “SimpleExpertAdvisor” and drag it on the chart and confirm it with the “OK” button it will output the text: “Hello” that we have defined here.
But the “Comment” function can do more, remember we wanted to output the local time, so let’s use the words: “The local time is ” and with a comma we can pass another parameter, in our case it’s “TimeLocal”, so let’s recompile the code again and now the Expert Advisor says: “The local time is” and we also get the output of the local time right away on our chart.
In this video you have learned about built-in functions, like the “OnTick” function that is called whenever the price changes and you have also learned how to use the “Comment” function to output a text followed by a calculated value to make Metatrader 5 print out the text: “The local time is” followed by the calculated time directly on your chart and you have done it yourself with a few lines of MQL5 code.