ATR_exponential

Download script
Author: Copyright � 2011, Matus German
ATR_exponential

This technical indicator is designed to measure and visualize market volatility by calculating the Average True Range (ATR) using an exponential smoothing method. Unlike a standard average, which treats all historical data with equal importance, this script places a higher weight on the most recent price movements, making it more responsive to current market conditions.

How the Logic Works

The tool operates by breaking down the calculation into three distinct steps:

1. Determining Price Movement (True Range) For every candle on the chart, the script first determines the "range" of movement. To do this accurately, it looks at two different scenarios:

  • For the very first data point, it simply calculates the distance between the highest and lowest prices reached during that period.
  • For all subsequent points, it considers the difference between the current high/low and the closing price of the previous period. This ensures that "gaps" in pricing (where the market jumps from one price to another overnight or between sessions) are captured as part of the volatility measurement.

2. Applying Exponential Smoothing Once the individual range for a period is identified, the script applies an exponential multiplier. Instead of just adding up ranges and dividing by the number of periods (a simple average), it uses a specific formula based on your chosen period settings. This creates a "decay" effect where older data points have less influence on the final result than newer ones.

3. Visualizing the Result The final output is plotted as a smooth line in a separate window below your main trading chart. This line represents the "average" amount of price movement over a specific timeframe.

Key Takeaways for the User

  • Volatility Tracking: A rising line indicates that the market is becoming more volatile (larger price swings), while a falling line suggests the market is calming down or moving in a tighter range.
  • Responsiveness: Because this uses an exponential logic, the indicator will react more quickly to sudden spikes in market activity compared to a standard ATR.
  • Customization: You can adjust the "AtrPeriod" setting to determine how many previous candles the script should look back at when calculating this average volatility.
Miscellaneous
Implements a curve of type %1
3 Views
0 Downloads
0 Favorites
ATR_exponential
//+------------------------------------------------------------------+
//|                                              ATR exponential.mq4 |
//|                                    Copyright © 2011 Matus German |
//|                                        http://www.MTexperts.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Matus German"
#property link      "http://www.MTexperts.net/"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
//---- input parameters
extern int AtrPeriod=14;
                      
//---- buffers
double AtrBuffer[];
double TempBuffer[];

int counted_bars=0;
//+------------------------------------------------------------------+
//| 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 exponential("+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;
//----

   double pr=2.0/(AtrPeriod+1);
       i=Bars-2;
   if(counted_bars>2) i=Bars-counted_bars-1;
//---- main calculation loop
   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);
         }
      //if(pos==Bars-2) ExtMapBuffer[pos+1]=Close[pos+1];
      AtrBuffer[i]=TempBuffer[i]*pr+AtrBuffer[i+1]*(1-pr);
 	   i--;
     }
   return(0);
}

Comments