ATR_Separate_Labeled

Download script
Author: Copyright � 2008, Robert Hill
ATR_Separate_Labeled

This script is designed to measure and display market volatility by calculating the Average True Range (ATR). In simple terms, it tells you how much a particular asset (like a currency pair or stock) typically moves on average over a specific period of time.

Core Functionality

Volatility Measurement The tool looks at historical price data to determine the "true" range of movement. Rather than just looking at the difference between the high and low prices of a single candle, it also considers the closing price of the previous candle. This ensures that gaps in price (where the market jumps from one price to another instantly) are captured accurately.

Smoothing the Data To make this information useful for analysis, the script takes these individual range measurements and applies a moving average (a smoothing technique). This helps filter out "noise" and provides a clearer picture of the typical trading range over the user-defined timeframe (defaulted to 14 periods).

Visual Presentation The script provides two distinct ways to view this information:

  1. Graph Display: It creates a dedicated window at the bottom of your chart where you can see a visual line representing the ATR trend.
  2. Real-Time Labels: It automatically generates text labels on your screen. These labels clearly display the numerical value for the Current ATR and the Previous ATR, allowing you to see the immediate volatility shift without having to interpret the graph manually.

Key Features for the User

  • Automatic Scaling: The script automatically adjusts its calculations based on the specific asset's "point" value (the smallest price movement), ensuring that the data is accurate whether you are trading high-value assets or low-value ones.
  • Dynamic Label Management: The tool includes a "cleanup" system. It identifies which labels belong to your current chart and ensures that as you switch between different instruments, the old text labels are removed and replaced with the correct data for the new asset.
  • Customizable Display: Users can adjust settings such as the period of calculation, the size of the text, the color of the information, and the placement of the labels to ensure the tool fits perfectly within their workspace.

Practical Use

By using this script, you can quickly gauge market "personality." A rising ATR suggests that the market is becoming more volatile and prices are swinging wider, while a falling ATR indicates a calmer, more stable trading environment. This information is essential for setting appropriate stop-loss levels and managing risk.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
11 Views
0 Downloads
0 Favorites
ATR_Separate_Labeled
//+------------------------------------------------------------------+
//|                                              ATR_Chart_Daily.mq4 |
//|                                    Copyright © 2008, Robert Hill |
//|                                                                  |
//| Display the Average True Range on a separate window for          |
//| the past 2 periods                                               |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Robert Hill"

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

extern string  to="---Text Object Settings---";
extern int     Text_X_Offset = 20;
extern int 		CommentTxtSize = 10;
extern color 	CommentColor   = White;

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

int Comment1Y, Comment2Y;
string Comment1Label, Comment2Label;
string Object_ID = "ATRS_";
string short_name;
int IndWindow = -1;
double   myPoint;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 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);


     DeleteBadLabels();
     DeleteExistingLabels();
     SetupLabels();
     myPoint = SetPoint(Symbol());

//----
   return(0);
  }

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 

   ClearLabels();
   DeleteExistingLabels();
//----
   return(0);
  }


//+------------------------------------------------------------------+
//| Average True Range                                               |
//+------------------------------------------------------------------+
int start()
  {
   int i,counted_bars=IndicatorCounted();
//----
   if(Bars<=AtrPeriod) return(0);
   if (IndWindow < 0) IndWindow = WindowFind(short_name);
   
   if (Comment1Y < 10)
   {
     SetupLabels();
     ClearLabels();
     DeleteExistingLabels();
     SetupLabels();// Make sure label settings are OK
   }
   else
   {
     ClearLabels();
   }

//---- 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]=NormalizeDouble(iMAOnArray(TempBuffer,Bars,AtrPeriod,0,MODE_SMA,i), Digits) / myPoint;
//----

   OutputComment1ToChart("Curr ATR : " + DoubleToStr(AtrBuffer[0], 0));
   OutputComment2ToChart("Prev ATR : " + DoubleToStr(AtrBuffer[1], 0));
   return(0);
  }


double SetPoint(string mySymbol)
{
   double mPoint, myDigits;
   
   myDigits = MarketInfo (mySymbol, MODE_DIGITS);
   if (myDigits < 4)
      mPoint = 0.01;
   else
      mPoint = 0.0001;
   
   return(mPoint);
}

void ClearLabels()
{
   string mComment = " ";
   
   OutputLabelToChart(Comment1Label, Comment1Y, CommentTxtSize, CommentColor, mComment);
   OutputLabelToChart(Comment2Label, Comment2Y, CommentTxtSize, CommentColor, mComment);
}

void DeleteBadLabels()
{
   int objLabels = ObjectsTotal(OBJ_LABEL);
   string objName;
   
   if (objLabels > 0)
   {
      for (int i = objLabels; i >= 0;i--)
      {
         objName = ObjectName(i);
         if (StringFind(objName,Object_ID, 0) >= 0)
         {
// Found 2 Play object, now check for wrong Symbol

           if (StringFind(objName,Symbol(), 0) < 0)
           {
             ObjectDelete(objName);
           }
         }  
      }
   }
}

void DeleteExistingLabels()
{
   int objLabels = ObjectsTotal(OBJ_LABEL);
   string objName;
   
   if (objLabels > 0)
   {
      for (int i = objLabels; i >= 0;i--)
      {
         objName = ObjectName(i);
         if (StringFind(objName,Object_ID, 0) >= 0)
         {
// Found 2 Play object, now check for Symbol

           if (StringFind(objName,Symbol(), 0) >= 0)
           {
             ObjectDelete(objName);
           }
         }  
      }
   }
}

void SetupLabels()
{
     Comment1Y = 12;
     Comment2Y = Comment1Y + CommentTxtSize + 4;
	  Comment1Label = Object_ID + Symbol() + "_Comment1";
	  Comment2Label = Object_ID + Symbol() + "_Comment2";
}

void OutputLabelToChart(string LabelName, int LabelY, int LabelTxtSize, color LabelColor, string LabelStr)
{

  	if(ObjectFind(LabelName) != 0)
   {
   ObjectCreate(LabelName, OBJ_LABEL, IndWindow, 0, 0);
	ObjectSet(LabelName, OBJPROP_CORNER, 0);
	ObjectSet(LabelName, OBJPROP_XDISTANCE, Text_X_Offset);
	ObjectSet(LabelName, OBJPROP_YDISTANCE, LabelY);
	}
	ObjectSetText(LabelName, LabelStr, LabelTxtSize, "Arial Bold", LabelColor);
}

void OutputComment1ToChart(string mComment)
{
   OutputLabelToChart(Comment1Label, Comment1Y, CommentTxtSize, CommentColor, mComment);
}

void OutputComment2ToChart(string mComment)
{
   OutputLabelToChart(Comment2Label, Comment2Y, CommentTxtSize, CommentColor, mComment);
}

//+------------------------------------------------------------------+

Comments