In this video we are going to create a custom function, in our case the function will double a value each time when the price changes so let’s find out how to do that.
First we want to click on a little icon here or press a F4 in Metatrader 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 delete everything above the “OnTick” function and the two comment lines here.
First we want to create a static variable for the counter, it will be called: “counter”, it’s an integer (int) variable and the modifier: “static” is used to declare a local variable. A local variable is only available inside of one function, when you highlight the word: “static” and press F1 you will see that the “MQL4 Reference” says that the storage class of “static” defines a static variable, the static modifier is indicated before the data type and it exists throughout the function lifetime.
Now we want to call an external function to double the value, the function we call will be named: “DoubleTheValue” (double the value) and we will pass the “counter” from the static variable here.
Let’s create an output for the current value by using the “Comment” function, it will output the words: “Counter Value” and the current value of the “counter” on our screen but if we compile the code right now we will get an error that says that the double the value (DoubleTheValue) function is not defined, so let’s create that function.
The first thing is always to define what kind of value will be returned by the function, in our case we are going to double whole numbers, so we will return an integer value (int), this is the name of the function, I have called it: double the value (DoubleTheValue) but you could use any other name and inside the round braces here we pass the value of the “counter” to the new function, here is where we pass it when we call the function: double the value (DoubleTheValue) and here is where we get the value inside of the function and you can see that we use another name here, you don’t need to call it “counter” because this is just the value of the “counter” that has been passed here.
Let’s add two curly braces so this would be the declaration of the custom function but when we compile now we will see that “not all control paths return a value” and that is because we didn’t do anything inside of the function and we have not returned a result, so let’s take the counter value (CounterValue), multiply it by 2 and store the result in a new variable called: double value (DoubleValue) and afterwards we are going to return the result by using the “return” command, actually it’s a “return operator”, it terminates the current function execution and returns the control to the calling program and that’s about it.
So let’s compile the code one more time, now you should not get any errors and no warnings and if that is the case you can click on the little button here or press F4 to go back to Metatrader.
And inside of Metatrader we want to click on: “View/ Strategy Tester” or press CTRL and R, please pick the new file: “SimpleFunction.ex4”, enable the visual mode here and start a test and now you should see the counter value and a doubled value that comes up every time the price changes, this is a very simple example we only do one thing inside of the custom function and we only pass one parameter, you could perform much more complicated stuff but in this short video you have learned how to create a custom function with MQL4 and you have done it yourself with a few lines of MQL4 code.