In this video, we are going to talk about this Indicator here; it is the Average Directional Movement Index. Basically it’s a trend confirmation oscillator. Whenever the line here is rising that’s a trend confirmation; in our case for a short trend that is developing on the chart here and as soon as the IADX Indicator is falling, you see, the trend is interrupted here, now its rising again and we see an uptrend and when the IADX Indicator starts to decline – like in this case – the uptrend is interrupted.
Now how can we create an Expert Advisor that is able can calculate the Average Directional Index in MQL4?
To do that please click on the little button here or press F4 on your keyboard. Now you should see the Metaeditor window and here you want to click on “File/ New/ Expert Advisor (template)” from template. “Continue” (Next). I’ll call this one Simple IADX (SimpleIADX), click on “Continue” (Next) “Continue” (Next), and “Finish”, and now you can delete everything above the “OnTick” function and the two comment lines here.
In the first step we want to create a signal, especially a string variable for the signal that will also be called signal and we don’t assign any value because we are going to calculate that later.
Let’s calculate the IADX value (IADXValue) by using the function: “iADX” that comes with Metatrader 4. If you mark it and press F1 the reference will tell you that it needs some parameters; the first one is for the symbol on the chart, the second one for the time frame you use on the chart, the third one is for the period for the calculation. If you right-click on the Indicator and select “ADX properties” you will see that the period is 14 and we calculate the values based on the close price so that’s what we use here; 14 and PRICE_CLOSE for the close price.
This parameter is for the buffer, so what is a buffer?
You see the Indicator produces 3 lines here, let’s hit the pause button and the value for the blue line is 29.93555 – that’s exactly what you see here. The 2 dotted lines have a different value and you could calculate the 2 other values if you exchange buffer 0 with buffer 1 or buffer 2.
The last parameter here is the shift value – that’s also 0. We want to calculate the current IADX Indicator for the current value (IADXValue) here and to get the last IADX value (LastIADXValue) we use exactly the same parameters accept we use a 1 to get the last value. If we use a 10 instead we would get the value for 10 candles before. If the IADX value (IADXValue) is higher now so IADX value (IADXValue) is greater than last IADX value (LastIADXValue) we want to output: “IADX is rising” so that’s what we assign to our signal variable. In the other case if the IADX value (IADXValue) is lower now so the current IADX value (IADXValue) is below the last IADX value (LastIADXValue) we assign the text: “IADX is falling” to our signal.
Let’s create a screen output by using the “Comment” function. It would output the text; “IADXValue:”, the calculated value and in a new line a text: “Signal:” and the calculated signal we have created here or here. So, that’s basically it, let’s click on “Compile” or press F7 on your keyboard.
That worked without any errors so now we can click here or press F4 to go back to Metatrader.
In Metatrader we are going to use a little trick; please click on “Insert/ Indicators/ Trend/ Average Directional Movement Index”, click on “OK”, leave the parameters as they are, click on “OK” and now you should see the Average Directional Movement Index in a separate window below the candles and because Wikipedia says: “The ADX does not indicate a trend direction or momentum and is only used to calculate the trend strength” I will insert another Indicator for a trend. Let’s use a Moving Average for 14 candles because the ADX Indicator here is also calculated based on 14 candles, let’s choose an SMA (Simple Moving Average) this is apply to the close price, I use red and click on “OK”. Now we use a little trick; right-click the chart, select “Template/ Save Template” and save it as “tester.tpl” because that’s the name that Metatrader 4 is going to use for the back-test. Click on “Save/ Overwrite”, now please click on click on “View/ Strategy Tester” or press CTRL and R, this should bring up the “Strategy Tester” panel here.
Here you want to select the “SimpleIADX.ex4” file from our little Expert Advisor we just have created.
Let’s click on “Start” and whenever you see a trend change here the trend confirmation signal is changing; like here, and now you know how to create an Expert Advisor to calculate the IADX value and you can use it in your own automated system to confirm and validate any trend and you have coded it yourself with a few lines of MQL4 code.
Download “MQL4 Tutorial - Simple IADX Expert Advisor”SimpleIADX.txt – Downloaded 968 times – 801 B