ATR_Chart_Labeled

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

#property indicator_chart_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 ChartBuffer[];
double AtrBuffer[];
double TempBuffer[];

int Comment1Y, Comment2Y;
string Comment1Label, Comment2Label;
string Object_ID = "ATRC_";
string short_name;
double   myPoint;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 1 additional buffer used for counting.
   IndicatorBuffers(3);
//---- indicator line
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0, ChartBuffer);
   SetIndexBuffer(1,AtrBuffer);
   SetIndexBuffer(2,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 (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, 0, 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

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---