Some people like to use the momentum indicator and they say it should be a way to predict if a trend is going to continue, it should remain to be positive when an uptrend is sustained or negative when a downtrend sustained.
You can find the momentum indicator if you click on insert, indicators, oscillators and momentum.
It has a value of fourteen for the period, that’s the number of the calculated candles. It is applied to the closed price and it looks like this.
Now how can that the calculated with MQL 5? Well, first of all please click on this little button here or press the F4 key that should open your MetaEditor.
Now we click on file, new, Expert Advisor from template, continue.
We will call that simple momentum continue, continue, finish.
Now remove everything above the OnTick function and remove the two comment lines here. And as often, the first thing we need is the price array that can hold several prices of the candles on the chart.
And after that we want to define the properties for the momentum here, we will store them in iMomentum definition.
We will use the function iMomentum for the current symbol on your chart and the currently selected time period on your chart.
We want to calculate fourteen candles for the closed price of these candles. We are going to sort our price array downwards from the current candle by using arraysetasseries and with copybuffer we are going to fill our price array according to our definition we created here.
We only have one line to calculate we want to go from the current candle zero and collect the prices of three candles and now we want to get the value of the current candle and store the value in my momentum value.
This normalizedouble function will get the current candle zero from our array mypricearray and we only need two digits after the point.
Okay that’s it for the calculation. Now we want to have a chart output depending on the value… and if the value is greater than one hundred we want to output “strong momentum” and the current value.
And if it’s below 99.9 we want the output “weak momentum” on the chart followed by the calculated momentum value and if it’s between 99.9 and 100, we don’t have any special condition so we don’t need any text here and we’re only going to output the momentum value on the chart.
Okay let’s compile that Expert Advisor by clicking on this little button here or pressing the F7 key and you should see zero errors and no warnings.
So let’s click this little button here or press F4 to return to the MetaTrader. Okay please click on view strategy tester or hit control and R to bring up the strategy tester, select the simple momentum.ex5 file and mark this option for visualization.
Select any currency pair you like and click start, and this is what you should see.
Let’s speed that up a little bit, hit the pause button.
You might notice that this peak was created a little bit before this peak actually happened and you also could see the momentum indicator going mad here before the price was falling.
Most of the time the momentum indicator will be used with another indicator like a moving average. It’s up to you to find out if you want to use it.
But now you know how to program it in MQL 5 with only a few lines of MQL 5 source code.