MQL4 TUTORIAL BASICS – 68 SIMPLE MOMENTUM MONITOR

video

In this video we are going to create an expert advisor that is able to calculate the average value for the momentum indicator, 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 momentum monitor, click on continue, continue and finish.
Now you can delete everything above the ontick function and the two comment lines here.
First, we want to create a variable called, current momentum state, it’s a string variable so it can hold text but so far we don’t assign any values here.
Afterwards, we are going to calculate the current momentum value by using the included imomentum function for the current symbol on the chart and the currently selected period on that chart.
We want to calculate the results based on fourteen candles, the calculation should be done using the close price and for the current value want to see the value for candle zero.
Now if you mark this function and press F1 you will see that we have a shift value, this is the last parameter and they say it’s the shift relative to the current bar the given amount of periods ago, so basically if we change this value to thirteen we would get the calculation for candle fourteen but we don’t want to repeat this line fourteen times.
So let’s create another array called, momentum values, and this one can hold fourteen values for fourteen candles. We also want to create a variable called, average momentum value, the initial value will be zero and the third thing that we need is a variable that is called, momentum values summary.
Let’s assign also zero to that one and now we use a for loop to go through all the elements in our array, we start with an index value of thirteen, that might look a little bit confusing because we said that we want to calculate fourteen candles.
Well, the problem is that an index for an array starts with zero, so the first candle would be candle zero and candle number fourteen would be index thirteen, so basically what this says is, start from index thirteen – which is the last candle – and until you reach the index zero – which is the current candle – do what’s inside the for loop and subtract one from the current index number until you are finished.
And now we can use this index number here to calculate a temporary momentum value for each of the fourteen candles. The rest in this line is basically the same and we use the momentum values summary and add the current temporary momentum value that we have calculated in this line to this summary until there are no candles left and finally we calculate the average momentum value by dividing the summary by the number of candles.
And if the current momentum value is bigger than the calculated average momentum value, that’s when the momentum is above the average value and now we assign, momentum is above average to the variable that is called, current momentum state.
Otherwise, if the current momentum value is below the average momentum value, that’s when we assign the sentence, momentum is below average to our current momentum state.
Finally, we use the comment function to create an output on our chart for the current momentum value, the average momentum value for the last fourteen candles and the current momentum state.
Well, if this was too fast for you or if you don’t have any idea what all the code here does maybe you want to watch one of the other videos in this basic video series first or maybe even the premium course on our website might be interesting for you. For now, we click on the compile button or press F7, I didn’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 pick a clean chart, I prefer to see the line chart.
Now we click on insert, indicators, oscillators, momentum, we will go with the default values, period fourteen for fourteen candles, the calculation should be done based on the close price, so let’s click on okay and now you should see the momentum indicator in a separate window below.
Now let’s right-click into the chart, select template, save template and save this one as tester dot tpl because this is the template that you see when you start a strategy test, let’s click on save, you can override the current value and if you don’t see the strategy tester here please click on view, strategy tester or press control and r.
Now, please choose the new file, simple momentum monitor dot ex4, mark the option for the visual mode and start a test.
Here we are! The expert advisor is running, currently, it says the momentum is above average, we have a calculated value for the average value, and we have the current momentum value for the indicator so our little expert advisor works and in this little video you have learned how to calculate the average value for the momentum indicator and you have coded it yourself with a few lines of mql4 code.