Today we are going to do a very simplified version of the Adaptive Directional Movement Index.
Our version will calculate the current value of the IADX for each of the three lines the indicator will show.
That is the value you see if you hover with your mouse over the blue line here, right now it is 27.09.
That’s the value you see here and that’s the calculated value we also will output on our chart.
Now how can we create an Expert Advisor in MQL5 that is going to calculate the IADX value for us?
To do that, please click on the little button here or press the F4 key.
Now you should see the MetaEditor and here you want to click on File, New, Expert Advisor from Template, Continue.
I will call it Simple IADX here, Continue, Continue and Finish.
Now we want to delete everything above the Ontick function and the two comment lines here are also no longer needed.
The first thing we need is an array, so let’s create an array for several prices, it will be a double array and it will have the name My Price Array.
And now we are going to use the IADX function to create a definition for the current symbol on the chart and the currently selected period.
It will calculate IADX value for fourteen candles, because when the IADX indicator was developed in 1978 J. Welles Wilder, it was defined for fourteen days.
If you click on Insert, Indicators, Trend, Average Directional Movement Index, you will also see the fourteen candles here.
Now we want to sort the price array from the current candle downwards by using Array Set as Series for the price array we have created here.
The function Copy Buffer will now fill our price array according to the IADX definition we have created in this line for one line, that’s the zero here, for the current candle that’s the second zero and we need the price data for three candles that’s the three here to store the result in our price array.
To get the value of the current candle, we create a double variable with the name IADX value and we will use normalize double for two digits behind the dot to get the value of candle zero, that’s the current candle in our price array.
And here comes the chart output: by using the comment function we will output the text “IADX Value” and the calculated value directly on our chart.
Now and that everything is done please click on the compile button or press the F7 key on your keyboard.
You should be able to compile your code without any errors and without any warnings.
And if that did work for you, you can click on the button here or press the F4 key to go back to MetaTrader.
In MetaTrader please click on View, Strategy Tester or press control and R.
And now you should see the Strategy Tester Panel and here you want to select the Simple IADX.ex5 file.
Now let’s start the test and here is our little Expert Advisor at work.
Let’s stop it and you’ll see the first value here is 23.09 and that matches with the IADX value we have calculated here.
Let’s see if we also can get the second value here.
So let’s go back into Meta Editor and replace the zero here for the first line of the indicator with a one.
Recompile the code and start another test.
And now you will see the second value here.
If you want to get the third value you simply have to replace the one with a two, recompile the code and start again.
And now of the output here matches the third value of this indicator.
Okay, now you know how you can calculate the ADX value for each of the three lines of the Average Directional Movement Index and you have done it with a few lines of MQL5 code.