In this video, we are going to find out how we can pass a whole array as a parameter to a different function, this is possible.
So let’s find out how to do that with an Mql5. To get started, please click on a little icon here or press F4 on your keyboard.
Now you should see the Metaeditor window. And here you want to click on file, new file, Expert Advisor from template, continue. I will call this file simple array reference. Click on continue. Continue and finish. Now you can delete everything above the Ontick function and the two comment lines here. We start by creating a simple small array for integer values, because this is just a simple example to show the concept. And it makes a lot of sense to create small programs like this one to find out how things work.
So now we assign values to our array. Please remember the first value can be found when you access index zero. All the array values are whole numbers. We want to use those numbers for a simple calculation. So we want to pass the whole array to a function that is called addvalues. And please remember, usually we are going to pass parameters, but this is a whole array and the returned result should be shown on the chart. So let’s add a comment statement here that will show us the text.
The returned result is followed by the result that we have calculated. But this function doesn’t exist, so we need to create it now. And here is where we need to add this little symbol here. It’s the ampersand symbol inside of our user defined function. We want to add the values for the array for index zero, one and two. And afterwards we are going to return the results to our main function, and that’s about it. If you have no idea what all the code here does, all of this was too fast for you, you probably want to watch one of the other videos in the basic video series first. By the way, on our website, we have a link for additional courses where you can get the source codes for this basic video stuff. That’s also where you can find the link for the Premium Course. This was a suggestion of a Premium Course member, and if you are a Premium Course member and have an idea for a video like this one, please let me know.
For now, let’s click on View toolbar and press the Compile button. Or you could also press F7 on your keyboard. You shouldn’t get any errors. And if that is the case, you can click on a little icon here or press F4 to go back to Metatrader. And in Metatrader you want to click on View Strategy tester or Press Control and R. Please pick the new file simple array reference dot .ex5. Mark, the option for the Visual mode, if you can’t see that, you might need to drag this little bar upwards.
So now please click on start. And here is our returned result. It is fifty six. We have added the first three values here. That is eight plus 16. So this would be twenty four. And when we add this value, this will be fifty six. So let’s add actually another value here. Recompile the code and when we stop the test and start a new one, you should see a new value that is returned. This time it is one hundred and twenty.
So our little Expert Advisor works as expected. And in this little video you have learned how to pass an array to a user defined function and you have coded it yourself with a few lines of MQL5 code.