MQL4 TUTORIAL – SIMPLE AVERAGE TRUE RANGE EA

video

In this video, you will see an Expert Advisor that is calculating the Average True Range value. When the value is high it will say; “it’s a strong Average True Range value”, and when the value is weak it will also say that here directly on the chart.
We can also make it visible by clicking on: “Insert/ Indicators/ Oscillators/ Average True Range”. This Oscillator is calculated, based on 14 candles, and it looks like this:
When it’s weak that would be the case when its below 0.00014, it will show the text: “Weak Average True Range Value” and when the value is rising above 0.00024 we have a strong Average True Range value. Now we want to find out how we can create an Expert Advisor that is able to calculate the indicator. To do that please click on the little button here or press F4 in your Metatrader.
Now you should see the Metaeditor and here you want to click on “File/ New/ Expert Advisor (template)” from template. Continue. I will call this one: Simple True Range EA. Continue. Continue and finish. Now you want to delete everything above the OnTick function and the two comment lines here.
We start with the Average True Range calculation so we create a variable called: AverageTrueRangeValue, it’s a double variable and we create it by using the built-in function: iATR. This function takes four parameters. The first one is: _Symbol, that variable contains the symbol name of the current chart, the second parameter is: _Period, this variable contains the value of the time frame of the current chart, the third parameter is for the number of candles and we can see that the indicator uses 14 candles. Select ATR properties and here you will see the 14 candles. The last parameter would be for the shift to the right or to the left, we don’t need that, so we use 0 here.
If the Average True Range Value is above 0.00024 we create a chart output by using the “Comment” function. It will output the text: “STRONG Average True Range Value” followed by the calculated value here. In the other case if the Average True Range Value is below 0.00014 we use the “Comment” function to output the text: “WEAK Average True Range Value” followed by the value and if the value is greater than 0.00014 and below 0.00024 we would just output the text: “Average True Range Value” and the calculated value because now its neither strong nor weak.
Please don’t forget the closing bracket here and if you are done you can click on the “Compile” button here or press F7 on your keyboard. You should have no errors and if that is the case please click here or press F4 to go back to Metatrader.
In Metatrader you want to click on: “View/ Strategy Tester” or press CTRL and R and in the Strategy Tester please select the SimpleTrueRangeEA.ex4 file and start your test.
This is how it works. Let’s switch to the candle view here, click on: “Insert/ Indicators/ Oscillators/ Average True Range” and now you can compare the value here with the calculated value above.
Okay. Now you know how to calculate the Average True Range Oscillator value for Metatrader 4 and you have coded it yourself with a few lines of mql4 code.