Knowing how to create indicators in MQL for MetaTrader is very important for a serious trader. This tutorial will teach you how to create and modify the ATR Average True Range for MetaTrader.
Many traders think that coding in MQL is complicated, but it’s not true.
Standard Average True Range for MT4 in MQL
Step 1: Declare the #property
Firstly, declare the #property. You can optionally insert the copyright information and the version of your indicator.
You can show the MetaTrader indicator in the chart window or a separate window. Average True Range for MetaTrader stays in a separate window. Finally, you insert the number of buffers that is Zero because you have only one array.
Array? Buffers? Don’t be overwhelmed! This part of the code in MQL is the same for many indicators.
Step 2: Declare the input and the variable
After that, we go to declare the input; we have only one extern input, ATR_Period. Average True Range ATR of period 14 uses the last close price in the previous 14 candles.
The “double” variable is the array ATR that, in MQL, corresponds to Buffer 0. In this array, the program will write all ATR values.
extern int ATR_Period = 14; // Insert the ATR period – standard is 14
double ATR; // BUFFER 0
Step 3: Initialize the custom indicator function
Now you have to initialize the custom MetaTrader Indicator and insert the string to draw your ATR.
In the “SetIndexBuffer“, insert the ATR array with the “0” value because the buffer for the ATR array is zero.
In the “SetIndexStyle“, decide the style of your indicator, the width, and the color.
Step 4: Custom indicator iteration function
In this part of the MQL program, you have to insert the OnCalculate function and the “for loop” operation, this part of code is the same for most MetaTrader Indicators:
Finally, write the part of the MQL program that calculates and plots your ATR Average True Range for MetaTrader.
That’s all. Finally, attach the MQL MetaTrader Indicator to the chart, and this is the result:
Change the color and the width in MetaTrader
This MQL indicator shows what you need, but sometimes you need more.
You can modify the style of your indicator; for example we can modify the color and the size of the line:
You have modified the color in DarkBlue and enlarge the width from one to three.
Smoothed Average True Range for MetaTrader
Say you want to flatten your MetaTrader Indicator. Now what? To smooth and Average True Range for MetaTrader in MQL, you can create a moving average of the ATR array. For best results, use a 5-periods moving average.
Change the buffer to 1:
#property indicator_buffers 1
Declare another variable and invert, because you want to draw only the first “MA_ATR”:
double MA_ATR; // BUFFER 0
double ATR; // BUFFER 1
The SetIndex for MA_ATR became buffer 0 and the ATR became buffer 1:
To create a 5-period moving average of your indicator, write this:
for(int i=1; i<limit; i++)
ATR[i] = iATR(NULL,0,ATR_Period,i);
for(int i=1; i<limit; i++)
MA_ATR[i] = iMAOnArray(ATR,0,5,0,MODE_EMA,i);
The smoothed ATR is red, so you can clearly see the difference with the standard green:
Volatility phases with Average True Range
The Average True Range for MetaTrader calculates the volatility; let’s say you want to know if volatility is increasing or decreasing.
With a little complicated MQL code, you can use a Moving Average to identify if the volatility is high or low and change the indicator’s color.
We don’t show the code because it’s too long to explain here, but we attach the MetaTrader Indicator at the end of this tutorial.
Also, with a little MQL code change, you could modify the style of your MetaTrader Indicator, and you can plot a histogram that shows you if the volatility is high or low.
Our indicator with Histogram style: