MQL5 Tutorial – Simple Momentum Expert Advisor with MQL5



  1. Introduction to the Momentum Indicator in Trading (00:00 – 00:25) Discussing the use of the momentum indicator to predict trend continuation, with an explanation of how to find and set it up in a trading platform.
  2. Opening MetaEditor and Creating a New Expert Advisor (00:25 – 00:57) Instructions on opening MetaEditor and creating a new expert advisor named “Simple Momentum.”
  3. Setting Up a Price Array and Momentum Properties (00:57 – 01:24) Steps to set up a price array to hold candle prices and defining the properties for the momentum indicator.
  4. Sorting the Price Array and Filling with Momentum Data (01:24 – 02:02) Using ArraySetAsSeries and CopyBuffer to sort the price array and fill it with data from the momentum indicator.
  5. Calculating and Storing the Momentum Value (02:02 – 02:29) Calculating the momentum value for the current candle and storing it in a variable.
  6. Creating Chart Output Based on Momentum Value (02:29 – 03:11) Coding the expert advisor to output different texts on the chart based on the momentum value.
  7. Compiling and Testing the Expert Advisor in MetaTrader (03:11 – 03:39) Compiling the expert advisor and testing it in MetaTrader’s strategy tester with visualization.
  8. Observing the Momentum Indicator’s Behavior and Peaks (03:39 – 04:24) Observing how the momentum indicator behaves in relation to price movements and discussing its potential use with other indicators.

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.

Download “MQL5 Simple Momentum Expert Advisor from MQL5 Tutorial”

SimpleMomentum.txt – Downloaded 393 times – 945.00 B