Author: � 2008, Kris
ATR_Bands

This tool is a specialized technical analysis indicator designed to help traders visualize market price movement and volatility in real-time. Unlike standard fixed indicators, this script creates "self-adjusting" envelopes that expand and contract based on how much the market is actually moving at any given moment.

How it Works: The Core Logic

The script functions by calculating three distinct lines on your chart to provide a complete picture of price action:

1. The Central Trend Line (Moving Average) At the heart of the tool is a smoothed average of the closing prices over a specific period. This acts as the "baseline" or equilibrium point. By using an Exponential Moving Average, the tool ensures that recent price action carries more weight, allowing the baseline to react quickly to new trends while still filtering out minor market "noise."

2. The Volatility Measurement (ATR) The defining feature of this script is its use of the Average True Range (ATR). Rather than using a standard deviation (which can sometimes be skewed by extreme outliers), this tool looks at the average distance between highs, lows, and closes over a set timeframe. This provides a realistic measurement of the market's current "breath" or intensity.

3. The Dynamic Envelopes (Upper and Lower Bands) The script takes the baseline (the moving average) and projects two outer boundaries—an upper and a lower band.

  • Expansion: When the market becomes highly volatile, the ATR value increases, causing the bands to widen significantly. This signals that price swings are becoming larger.
  • Contraction: When the market settles into a period of low activity or consolidation, the ATR decreases, pulling the bands closer to the baseline.

Practical Application for Traders

By combining these three elements, the indicator provides a visual map of "normal" market behavior:

  • Trend Identification: The central line helps you see the primary direction of the price.
  • Volatility Awareness: By observing the width of the bands, you can instantly tell if the market is entering a high-stress period or a calm trading range.
  • Boundary Analysis: Because the bands adjust automatically to volatility, they serve as dynamic "ceilings" and "floors." When the price pushes toward or breaks through these self-adjusting envelopes, it often indicates that the market is moving beyond its typical behavior for the current environment.
Indicators Used
Moving average indicatorIndicator of the average true range
Miscellaneous
Implements a curve of type %1
11 Views
0 Downloads
0 Favorites
ATR_Bands
//+------------------------------------------------------------------+
//|                                                    ATR_Bands.mq4 |
//| ATR Bands are self-adjusting enveloppes, based on the volatility |
//|        Based on BB indicator, modified by kris.pivo[at]gmail.com |
//+------------------------------------------------------------------+
#property copyright "© 2008, Kris"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 C'150,150,150'
#property indicator_color2 C'080,080,080'
#property indicator_color3 C'080,080,080'
//---- indicator parameters
extern int    Bands_Period   =  21;
extern int    Bands_Shift    =   0;
extern int    ATR_Period     =  14;
extern double ATR_Multiplier = 1.5;
//---- buffers
double MovingBuffer[];
double UpperBuffer[];
double LowerBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle( 0,DRAW_LINE,STYLE_SOLID,2,indicator_color1);
   SetIndexBuffer(0,MovingBuffer);
   SetIndexStyle( 1,DRAW_LINE,STYLE_DOT,  1,indicator_color2);
   SetIndexBuffer(1,UpperBuffer);
   SetIndexStyle( 2,DRAW_LINE,STYLE_DOT,  1,indicator_color3);
   SetIndexBuffer(2,LowerBuffer);
//----
   SetIndexDrawBegin(0,Bands_Period+Bands_Shift);
   SetIndexDrawBegin(1,Bands_Period+Bands_Shift);
   SetIndexDrawBegin(2,Bands_Period+Bands_Shift);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Bollinger Bands                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int    i,k,counted_bars=IndicatorCounted();
   double TrueRange;
   double sum,oldval,newres;
//----
   if(Bars<=Bands_Period) return(0);
//---- initial zero
   if(counted_bars<1)
      for(i=1;i<=Bands_Period;i++)
        {
         MovingBuffer[Bars-i]=EMPTY_VALUE;
         UpperBuffer[Bars-i]=EMPTY_VALUE;
         LowerBuffer[Bars-i]=EMPTY_VALUE;
        }
//----
   int limit=Bars-counted_bars;
   if(counted_bars>0) limit++;
   for(i=0; i<limit; i++)
      MovingBuffer[i]=iMA(NULL,0,Bands_Period,Bands_Shift,MODE_EMA,PRICE_CLOSE,i);
//----
   i=Bars-Bands_Period+1;
   if(counted_bars>Bands_Period-1) i=Bars-counted_bars-1;
   while(i>=0)
     {
      sum=0.0;
      k=i+Bands_Period-1;
      oldval=MovingBuffer[i];
      TrueRange=ATR_Multiplier*iATR(NULL,0,ATR_Period,Bands_Shift);
      UpperBuffer[i]=oldval+TrueRange;
      LowerBuffer[i]=oldval-TrueRange;
      i--;
     }
//----
   return(0);
  }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
//                                       THE END                                        |
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+

Comments