MQL5 TUTORIAL BASICS – 68 SIMPLE MOMENTUM MONITOR

video
play-sharp-fill

In this video we are going to create an expert advisor that is able to calculate the average value for an array, in this case for the momentum indicator, so let’s find out how to do that with mql5.
To get started please click on a little button 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 momentum monitor, click on continue, continue and finish.
Now you can delete everything above the ontick function and the two comment lines here.
We start with an include statement and this time we are going to include this file, it’s for math functions, this file math dot mqh will help us to calculate the average value for an array, it includes additional functions that are not part of the mql5 core language.
We start by creating a string variable called current momentum state, but we are not going to assign any values here.
Afterwards, we are going to create an array for the prices, we use array set as series for the price array to sort it from the current candle downwards and now we use the imomentum function that is included in mql5 to create the value for the momentum indicator, for the current symbol on the chart, the current period on that chart, the value should be calculated based on fourteen candles and it is calculated based on the close price.
When you click on insert, indicators, oscillators, momentum, you will see the same values here and this is how the indicator looks like.
Well, the problem with these kinds of indicators is that they do not have a fixed upper or lower value and we would like to know if the current value is above the average value, so how do we do that?
Well first we want to use copy buffer to fill our price array with data according to the imomentum definition that we have created here, we do that for the only buffer or line that the indicator has, we start with candle zero and we want to copy the prices for fourteen candles.
And now we calculate the current momentum value by looking into our price array and taking the price from candle zero. Well, that was simple.
To get the average momentum value you would usually now need to go through all the prices inside of the array, add each price to the value and divide the result by fourteen because that’s the number of elements inside of the array. Instead of doing that we use a predefined function that comes with the math library and this function is called math mean, you might notice that this function is not highlighted.
The other ones have a different color and the reason is that this one is not part of mql5, instead, it’s a sub-function that comes with a standard library and it calculates the mean value of array elements, in this case, it will calculate the average momentum value for our price array and if the current momentum value is bigger than the average momentum value that’s when we assigned the value, momentum is above average to our current momentum state.
Otherwise, if the current momentum value is below the average momentum value we would assign, momentum is below average to our current momentum state.
So let’s create a chart output for the current momentum value, the average momentum value and the current momentum state, and that’s about it.
Well, if you mark math mean and press F1 you will see that there is no entry in the current reference for mql5, you will find other functions for the maximum or the minimum value, on a website we can learn that it is a standard library, mathematics, statistics, sub-functions, so let’s try that.
We navigate from mql5 reference to math functions, standard library, sub-functions, and here we are, math mean is included, so it’s possible to find it once you know that it exists. If you are interested you can look at all the other functions that come with the math library and if you don’t understand what all the code here does or if this was too fast for you you maybe want to watch one of the other basic videos in this video series or maybe even the premium course on our website might be interesting for you. For now, please click on the compile button or press F7, you shouldn’t get any errors 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 we want to click on view, strategy tester or press control and r, please pick the new file, simple momentum monitor dot ex5, mark the option for the visual mode here and start a test.
And here we are!
Our expert advisor is able to calculate the current momentum value and it is calculating the average momentum value by using the math mean function and that is helpful to find out if the current state is below or above the average value and in this little video you have learned how to create average values by using the math library and you have coded it yourself with few lines of mql5 code.