In this video, we are going to create an Expert Advisor that is able to pass a complete array as a parameter to different function. So let’s find out how to do that with MQL4. 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 small array. This is an integer array and we want to fill our array with a few numbers. Please keep in mind that the first array item is always index zero. These array items are like boxes and we assign values to these boxes. This is just for a simple example and it’s a good idea to create small little programs like this one to find out how things work, because now we want to find out if we are able to pass our array as a parameter to a user defined function that is called add values.
This function doesn’t exist so far, so we need to create it in a few seconds and we will get a returned result. So let’s create a chart output here. By using the comment statement. It will output the text the returned result is followed by the result that we have created, but this function doesn’t exist so far. So we need to create it now and our function will be called add values and it will get to the array as a single parameter.
And we are using this little trick here. This is for INT Integer and this little ampersands symbol is used for references, so now we can try to add a few values by adding the array indexes zero, one and two, and finally we use the return statement to return the result to the main function, and that’s about it. Well, if you have no idea what all the code here does or if this was too fast for you, maybe you want to watch one of the other videos in this basic video series.
And on our website, you can now find a link for basic courses that also contain the source codes for this basic video stuff. There is also the link for the Premium Course. This was another idea of a Premium Course member. And if you are already a Premium Course member and have an idea for a video like this one, please let me know. For now, please click on the Compile button or press F7 on your keyboard, that should work without any errors.
And if that is the case, you can click on a little button here or press F4 to go back to Metatrader. And in Metatrader we click on View, Strategy tester or press Control and R.
Please pick the new file simple array reference dot EX4.
Enable the visual mode here and start your test. Here is the Expert Advisor, and it says the returned result is fifty six. So now let’s go and check the source code. We have added up eight and 16. So this would be 24. The third item here is thirty two. And if we add all three items, the result is fifty six. So let’s actually add another value here for index three. Now recompile the code, stop the current test and restart it.
And this time it says that the returned result is one hundred and twenty. And in this little video, you have learned how to add up all the elements of an array that we have passed by a reference, and you have coded it yourself with a few lines of MQL4 code.