MQL5 TUTORIAL BASICS – 10 HOW TO CODE A SIMPLE CUSTOM FUNCTION

video
play-sharp-fill

 

In this video we will use MQL5 to create a custom function that will double a counter value each time the price changes.

To do that please click on the little button here in Metatrader or press F4 on your keyboard and now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleFunction”, click on “Continue”, “Continue” and “Finish” and now you can remove everything above the “OnTick” function and let’s also delete the two comment lines.

We start by creating a static variable for our “counter”; “static” is something that you might not have seen before, in our case we use “static” to create a local variable that will only exist in our function and we want to pass the variable to another function that will be called: “DoubleTheValue” (Double The Value), it doesn’t exist so far so we need to create it and within the round braces we will pass the value of our current “counter” and afterwards to have something on the screen we will use the “Comment” function to output the words: “The counter value” and the current value of the “counter”, so when I compile the code right now I will get an error because the “DoubleTheValue” function is not defined so let’s do that.

To create a custom function you start by defining what the function is going to return – in our case the function will return an integer (int) value –, this is the name of the custom function, I call it: “DoubleTheValue” (Double The Value) and here is what the function will take as a parameter.

We pass the “counter” – that’s an integer (int) – I call it “CounterValue” in the custom function – you don’t need to use the same name – our function will need two curly braces and when I try to compile it this time we get another error because our function is not going to return anything.

So let’s first double the passed value that we have called “CounterValue”, so we take “CounterValue”, multiple it with 2 and the result will be stored in a new variable called: “DoubleValue” and now we can use the “return” operator to return the value to the calling program, that is done by using “return DoubleValue;”, so let’s compile the code one more time and now we don’t see any errors here and if that is the case you can click on the little button here or press F4 to go back to Metatrader.

And in Metatrader you want to click on: “View/ Strategy Tester” or press CTRL and R, please pick the new file: “SimpleFunction.ex5”, mark the visualization option here and start a test and now you should see the counter value is doubled each time a new tick comes in so whenever the price changes our custom function is called.

This was a very simple example, we only passed one value and we only performed one operation but you could also use the same kind of logic to do more complicated stuff, for example you could go through all the open positions, calculate the profit for a currency pair and return the value to the main function by using the “return” operator and in this little video you have learned how to create a custom function in MQL5 and you have coded it yourself with a few lines of MQL5 code.

Download “CODE - SIMPLE FUNCTION”

SimpleFunction.mq5 – Downloaded 54 times – 451.00 B