Knowing how to create indicators in MQL for MetaTrader is very important for a serious trader. With this tutorial, you will learn how to create and modify the Average True Range (ATR) in MetaTrader.

Many traders think that coding in MQL is complicated, but it’s not true.

 

Let’s start.

Standard Average True Range in MQL

mql code Average True Range for MetaTrader

Step 1: Declare the #property

Firstly, declare the #property. You optionally can 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 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 code in MQL is the same for many indicators.

MetaTrader MQL code property part

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.

int OnInit()
{

SetIndexBuffer(0, ATR);
SetIndexStyle(0,DRAW_LINE,0,1,clrRed);
SetIndexLabel(0,”ATR”);
return(INIT_SUCCEEDED);
}


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:

MetaTrader MQL Code const part for Average True Range

MetaTrader Code for Average True Range in mql

Finally, write the part of the MQL program that calculates and plot your Average True Range ATR

MetaTrader Code for calculation of Average True Range in mql

That’s all. Finally, attach the MQL MetaTrader Indicator to the chart, and this is the result:

average true range MQL MetaTrader

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:

int OnInit()
{

SetIndexBuffer(0, ATR);
SetIndexStyle(0,DRAW_LINE,0,3,clrDarkBlue);
SetIndexLabel(0,”ATR”);

You have modify the color in DarkBlue and enlarge the width from one to three.

average true range custom metatrader indicator in MQL

Smoothed Average True Range

Say you want to flatten your MetaTrader Indicator. Now what? To smooth an Average True Range 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:

SetIndexBuffer(0, MA_ATR);
SetIndexStyle(0,DRAW_LINE,0,3,clrDarkRed);
SetIndexLabel(0,”MA_ATR”);

SetIndexBuffer(1, ATR);

To create a 5-period moving average of your indicator, just 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:

average true range smoothed metatrader indicator

Volatility phases with Average True Range

The Average True Range 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.

average true range with volatility phases metatrader indicator in mql

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:

average true range volatility phases histogram metatrader indicator

 

Download the MQL MetaTrader Indicator for free!!

Average True Range Bundle for MetaTrader

 

 

Editors’ Recommendations:

 

 

 

 

 

 

Leave a Reply