MQL5 PROGRAMMING – CREATE A SIMPLE FUNCTION

video
play-sharp-fill

In this video we would like to take a look at how you can program your own function with MQL5. In this case we have a function double, which doubles the counter value whenever the price changes here. And we want to have a look how to do this with MQL5. To do this, we click on this little symbol up here in the Meta Trader or press the F4 key. This will call up the Meta Editor and here we click on File, New, Expert Advisor from template, Next. I will give the name simpleFunction, click on Next, Next and Finish. And now everything above this OnTick function can be deleted here. And also the two comment lines are removed. We start by creating a static variable in the OnTick function. For this we use this addition static here. If you mark it once and press the F1 key, then you learn that it is a so-called modifier. Static variables keep their validity within the lifetime of a function. Now we want to call a new function with the value of this variable, which should be called double the value and which gets the value of our counter variable and so that we see what happens, we want to display the value on the chart afterwards. We do this with the Comment command, which should show us the text CounterValue and the determined value for our counter directly on the chart. If we compile the whole thing now, then we get a message here below that doubling the value is not yet defined. So we have to create this function now. The first is the value of the function that is returned. In our case we want to double integers, so we return integer values because we don’t need decimal places. Then we define the name of the function. in this case double value. You could also choose another name here, but double value describes quite well what the function should do. And here in round brackets we take the value of our counter, which we have passed as integer up here. Here the parameter we pass to the function is called counter, here below the parameter we take is called counter value. This need not be identical. Our function now needs two curly brackets. If this would have been declared so far, but if we now click on compile, then we get the message that not all control paths return a value and that is because we have not done anything in the function yet. So we change that now. We simply multiply the passed counter value with the factor 2 to double it. That means counter value times 2 is assigned to the variable double value and then we use return to return our double value as result to the main function. If you highlight return and press F1 you will see that this is an operator, it stops the running function and returns control to the calling program. In this case we come back here and the result from our function is assigned to the variable counter and the whole thing should be traceable here using the comment function on the chart. So we compile again. This time there will be no errors and no warnings and if that’s the case for you, please click on this icon up here or press F4 to return to the Meta Trader. And in MetaTrader we click on View, Strategy Tester or press CTRL+R. Here please select the new file simpleFunction.ex5. Please also mark the option for visualization and start a test. And here we see our output. Our counter value has the value 8 16 32 at every tick, so every incoming price change our value is added here. Of course you could now do much more complicated things in our function. We have processed only one parameter here, which we have doubled. But in such a function you could, for example, go through all open positions. See if they belong to a currency pair and then calculate the profit for that currency pair and return it to the main module using return. But for this example, that should be enough. Because in this short video you have learned how to program your own function and you have done it yourself with these few lines here in MQL5.