ATR
Download scriptThis tool is designed to measure and visualize market volatility by calculating the "Average True Range" (ATR). In simple terms, it helps traders understand how much an asset's price typically moves over a specific period, allowing for a better grasp of the market's current "mood"—whether it is calm and stable or aggressive and erratic.
How the Logic Works
The script follows a three-step process to determine the volatility level:
1. Measuring Individual Price Movement Instead of just looking at how much a price moved from its opening to its closing point within a single candle, the logic looks for the "True Range." To do this, it compares the current candle's high and low prices against the previous candle's closing price. By checking both the current range and the gap between the previous close and the current move, the tool ensures that "gaps" in the market (where the price jumps from one level to another instantly) are accounted for in the calculation.
2. Creating a Temporary Data Set The script collects these individual measurements for every candle on the chart. This creates a secondary layer of data that represents the raw distance traveled by the price during each specific time frame, regardless of whether the price trended up or down.
3. Smoothing the Data Because market prices can be "noisy" (fluctuating wildly from one second to the next), looking at a single candle's range isn't very helpful for long-term planning. To solve this, the script takes the raw measurements gathered in step two and applies a Moving Average. By averaging these movements over a user-defined period (defaulted to 14 units), the tool produces a smooth, continuous line.
The Final Result
The output is a smooth line displayed in a separate window below your main price chart. When this line is high, it indicates that the market is experiencing significant swings and higher volatility. When the line is low, it suggests that the market is trading in a tighter, more stable range. Traders often use this information to set appropriate stop-loss levels or to decide whether the current market conditions are suitable for their specific trading strategy.
//+------------------------------------------------------------------+
//| ATR.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
//---- input parameters
extern int AtrPeriod=14;
//---- buffers
double AtrBuffer[];
double TempBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
//---- 1 additional buffer used for counting.
IndicatorBuffers(2);
//---- indicator line
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,AtrBuffer);
SetIndexBuffer(1,TempBuffer);
//---- name for DataWindow and indicator subwindow label
short_name="ATR("+AtrPeriod+")";
IndicatorShortName(short_name);
SetIndexLabel(0,short_name);
//----
SetIndexDrawBegin(0,AtrPeriod);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Average True Range |
//+------------------------------------------------------------------+
int start()
{
int i,counted_bars=IndicatorCounted();
//----
if(Bars<=AtrPeriod) return(0);
//---- initial zero
if(counted_bars<1)
for(i=1;i<=AtrPeriod;i++) AtrBuffer[Bars-i]=0.0;
//----
i=Bars-counted_bars-1;
while(i>=0)
{
double high=High[i];
double low =Low[i];
if(i==Bars-1) TempBuffer[i]=high-low;
else
{
double prevclose=Close[i+1];
TempBuffer[i]=MathMax(high,prevclose)-MathMin(low,prevclose);
}
i--;
}
//----
if(counted_bars>0) counted_bars--;
int limit=Bars-counted_bars;
for(i=0; i<limit; i++)
AtrBuffer[i]=iMAOnArray(TempBuffer,Bars,AtrPeriod,0,MODE_SMA,i);
//----
return(0);
}
//+------------------------------------------------------------------+
Comments