In this video, we are going to
create an entry module for this oscillator here. This is called the momentum oscillator.
It is shown on a separate chart below the candle chart.
So let’s find out how to calculate that with MQL5.
In this video we want to create an entry signal module for the so-called momentum oscillator.
That is once again an indicator that will be drawn on a separate chart window below the candle chart, because oscillators create values that are different from the price values on the candle chart.
To get started we create a new file named CheckEntry_Momentum.mq5 in the same directory where the main file and the other entry files are located.
This file will contain the function CheckEntry which will return a string variable for the calculated signal.
Let’s first create the signal. That is a variable of the type string, so it can contain text values.
For now we leave it empty as we need to calculate the value as follows.
We want to store some price data in an array, so we create that first.
To define the momentum oscillator, we can use the function iMomentum.
That is included in MQL5 and it needs a few parameters that we need to define.
Parameter 1 is for the symbol on the chart.
The second parameter is for the period on that chart.
Parameter 3 is the number of candles that we want to use for the calculation.
In our case, we will use 14 candles.
That is also the default value that you will see when you drag the momentum indicator onto one of your charts.
Finally, we use PRICE_CLOSE to calculate the value based on the close prices.
Afterwards we use Array set as series. That will sort the array from the current candle downwards.
And now we can use CopyBuffer to fill the array with data.
We do it for the definition that we have created, for buffer 0 and we want to do it from candle 0 and for three candles.
The result is stored in the price array.
And now that we have done that, we can calculate the current value for the momentum.
All we need to do is to look at the value for candle 0 in our array.
As the momentum indicator has two digits behind the dot, we use Normalize Double and the value 2 to create a formatted output.
Now let’s calculate the signal.
If the momentum value is below 100 we consider that to be a buy signal.
So we assign the word buy to our signal.
Otherwise, when the momentum value is above 100, that’s when we want to assign the word sell to our variable, because that is when we want to sell.
At the end of the function we use return to return the calculated result to our main module.
Now please save your entry file if you not already have done that.
Compilation is not necessary, it will be compiled together with the main module.
Therefore we open our main file and go to the include section where we can outcomment other entry signals by adding two slashes.
Instead of the old entry signal, we now add an include statement for our new file CheckEntry_Momentum.mq5.
Afterwards we compile the whole thing, by pressing F7.
You can also click on the compile button on the toolbar.
That should work without any error messages.
Otherwise please check out the basic videos about compilation or maybe the Premium course on our website might be interesting for you.
OK, that’s it for the momentum oscillator calculation.
I hope it works for you.
Thank you for watching and I will see you in the next video.