ATR CountBack

Download script
Author: Altered by cja
ATR CountBack

This script is designed to help traders analyze market volatility by calculating the Average True Range (ATR). In simple terms, the ATR measures how much an asset's price moves, on average, over a specific period of time. This tool is particularly useful for determining "normal" price swings, which can help in setting stop-loss levels or identifying periods of high and low market activity.

How the Logic Works

The script operates using three primary layers of logic to ensure the data it provides is accurate and relevant to your trading needs:

1. Multi-Timeframe Integration Unlike standard indicators that only look at the chart you are currently viewing, this script allows you to pull volatility data from different timeframes (such as Hourly, Daily, or Weekly charts) while staying on your current screen. It automatically identifies the correct timeframe based on your settings, allowing you to see the "big picture" of market movement without switching tabs.

2. The "CountBack" Offset A unique feature of this script is the ability to look back at historical data. By using a "CountBack" variable, the script can shift its analysis point into the past. This allows you to study what the volatility was like at a specific previous moment in time rather than just looking at the most recent price action.

3. Volatility Calculation (The Math) To determine the volatility, the script performs a two-step calculation:

  • Price Range Tracking: It identifies the highest and lowest prices reached during a specific bar. It then compares these to the closing price of the previous candle to see how far the price "stretched."
  • Smoothing: Once it has gathered these price ranges, it applies a Moving Average. This smooths out the jagged spikes in data, providing you with a clean, flowing line that represents the average volatility over your chosen period (e.g., the last 14 bars).

Summary for the User

Essentially, this tool takes raw price data from a specific timeframe, shifts it back by a chosen number of bars, calculates the range of movement for those bars, and then smooths that data into a readable line. The result is a visual representation of how "wild" or "calm" the market has been, adjusted to the specific historical point you wish to analyze.

Price Data Components
Series array that contains the highest prices of each barSeries array that contains the lowest prices of each barSeries array that contains close prices for each bar
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
15 Views
0 Downloads
0 Favorites
ATR CountBack
//+------------------------------------------------------------------+
//|                                                ATR CountBack.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Altered by cja"
#property link      "http://www.metaquotes.net/"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
//---- input parameters
extern int AtrPeriod=14;
extern int ATR_TF = 60;
extern int CountBack = 0;

//---- buffers
double AtrBuffer[];
double TempBuffer[];

string TimeFrameStr;
extern int Maxbars   = 24;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
    
    switch(ATR_TF)
   {
      case 1 : TimeFrameStr="Period M1"; break;
      case 5 : TimeFrameStr="Period M5"; break;
      case 15 : TimeFrameStr="Period M15"; break;
      case 30 : TimeFrameStr="Period M30"; break;
      case 60 : TimeFrameStr="Period H1"; break;
      case 240 : TimeFrameStr="Period H4"; break;
      case 1440 : TimeFrameStr="Period D1"; break;
      case 10080 : TimeFrameStr="Period W1"; break;
      case 43200 : TimeFrameStr="Period MN1"; break;
      default : TimeFrameStr=" Current Timeframe "; ATR_TF=0;
   }
  
   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+" )( "+TimeFrameStr+" )( "+CountBack+" )";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//----
   SetIndexDrawBegin(0,AtrPeriod);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Average True Range                                               |
//+------------------------------------------------------------------+
int start()
  {
   int i,counted_bars=IndicatorCounted();
   
    int limit;
   limit = MathMin(counted_bars,Maxbars);
   if (limit ==0)
      limit = Maxbars;
   
  //----
   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=iHigh(NULL,ATR_TF,i+CountBack);
      double low =iLow(NULL,ATR_TF,i+CountBack);
      if(i==Bars-1) TempBuffer[i]=high-low;
      else
        {
         double prevclose=iClose(NULL,ATR_TF,i+1+CountBack);
         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