With MQL5 for Metatrader5 you can calculate the Standard Deviation Indicator to use it in your own Expert Advisor – for example to combine the standard deviation value with an simple moving average to open a trade when the expected volatility changes…
In this video we are talking about the Average True Range Indicator and we are going to create a little Expert Advisor to calculate if we have a strong or a weak Average True Range value.
This is a sideways market and you see that nothing moves, and when nothing moves, the ATR value you see here down below has a low value.
And here we have bigger movements the ATR value rises, and if you are somebody who wants to get in when the Average True Range is very high, you would wait with your trading entry until the value starts to rise because you can expect the markets to make big movements after very slow periods.
And now the ATR value rises and if it rises a little bit more, we would see it here on the chart. If it’s low it says weak Average True Range value, and if the value is high it would say strong Average True Range value.
The Average True Range indicator is also an indicator created by J. Welles Wilder Jr., who created the RSI and the Parabolic SAR indicator.
You will find these indicators in other videos of this channel. The ATR indicator will measure the volatility, in our case for fourteen candles.
Now how can we create an Expert Advisor for this little indicator? To do that please hit this little button over here to bring up the Meta Editor.
And here you want to click on File, New, Expert Advisor from Template, Continue and we will call it Simple Average True Range.
Click on Continue, Continue, Finish, and remove everything above the OnTick function. You can also remove these two comment lines and the first thing we need to create is an array that will hold our price data.
And now we want to define our Expert Advisor by using the iATR function that is built in with MQL 5 for the current symbol on the chart and the selected period on the chart, and we will use fourteen candles because this is what Wilder recommended. And now we would use the function arraysetasseries to sort our price array from the current candle downwards and after that we will use the copybuffer function to fill our price array.
According to our definition we have created here for the first line of the indicator from the current candle zero for three candels.
And now we want to get the value of the current candle, that’s candle zero, from the Average True Range value and we will use normalizedouble to get the five digits we need after the point.
And now we need to chart output if the Average True Range value is greater than this, we want to output the text strong average range value and the value.
And if it’s below this value we want to output the text weak Average True Range value and the value, and if it’s between this value and this value we don’t have a signal so we don’t want any text on the chart and we will just output the calculated value here.
So let’s compile the code and that worked without any errors and any warnings.
So let’s click this little button over here to change back to MetaTrader and here we want to select View, Strategy Tester and you can also hit the control and the R key.
That will bring up the Strategy Tester Panel here and there you want to select the Simple AverageTrueRange.ex5 file.
Please mark the visualization option don’t here and click on start. And now you should see it running. Please compare the value down here with the calculated value here, it should always match.
Okay now you know how to calculate the Average True Range indicator to use it in one of your Expert Advisors and you did it with a few lines of MQL 5 code.