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);
}

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



Sample





Analysis



Market Information Used:

Series array that contains the highest prices of each bar
Series array that contains the lowest prices of each bar
Series array that contains close prices for each bar


Indicator Curves created:

Implements a curve of type DRAW_LINE


Indicators Used:

Moving average indicator


Custom Indicators Used:

Order Management characteristics:

Other Features: