//+------------------------------------------------------------------+
//|                                                 PriceDisplay.mq4 |
//|                                   Copyright © 2010, Robert Hill. |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Robert Hill."

#property indicator_chart_window

extern string I = "--- Chart Position Settings ---";
extern bool Corner_of_Chart_RIGHT_TOP = TRUE;
extern string I2 = "  Comments Settings ";
extern int PriceY = 25;
 int PriceTxtSize = 30;
extern color PriceColor = Red;

 int CommentTxtSize = 15;
extern color CommentColor = LightSteelBlue;

extern int Adjust_Side_to_side = 20;

string Object_ID = "PD_";
double myPoint;

string PriceStr, CloseStr, PriceDifStr, PricePercentStr;

// Labels

int CloseY, PriceDifY, PricePercentY;
string CloseLabel, PriceLabel, PriceDifLabel, PricePercentLabel;
string CloseLabelVal, PriceLabelVal, PriceDifLabelVal, PricePercentLabelVal;


int init() {
    myPoint = SetPoint();
    DeleteBadLabels();
    DeleteExistingLabels();
    SetupLabels();
   return (0);
}

int deinit() {
   ClearLabels();
   DeleteExistingLabels();
   return (0);
}

int start()
 {
    double myClose, PriceDif, PricePercent;
    color myColor;

   if (PriceDifY < 10)
   {
     SetupLabels();
     ClearLabels();
     DeleteExistingLabels();
     SetupLabels();// Make sure label settings are OK
   }
   else
   {
    ClearLabels();
   }

   PriceStr = DoubleToStr(Bid, Digits);
   OutputPriceToChart("" + PriceStr + "", PriceColor);
   
   CloseStr = "";
   PriceDifStr = "";
   PricePercentStr = "";
   myClose = iClose(NULL, PERIOD_D1, 1);
   CloseStr = DoubleToStr(myClose, Digits);
   PriceDif = (Bid - myClose);
   PricePercent = (PriceDif / myClose)* 100.0;
   PriceDifStr = DoubleToStr(PriceDif, Digits);
   PricePercentStr = DoubleToStr(PricePercent, Digits);
   if (Bid > myClose) myColor = Green; else myColor = Red;
   OutputCloseToChart("Close", CloseStr, myColor);
   OutputPriceDifToChart("Dif", PriceDifStr, myColor);
   OutputPricePercentToChart("Percent", PricePercentStr, myColor);
   return (0);
}

double SetPoint()
{
   double mPoint;
   
   if (Digits < 4)
      mPoint = 0.01;
   else
      mPoint = 0.0001;
   
   return(mPoint);
}

void ClearLabels()
{
   string mComment = "";
   
   OutputCloseToChart("Close", mComment, CommentColor);
   OutputPriceDifToChart("Dif", mComment, CommentColor);
   OutputPricePercentToChart("Percent", mComment, CommentColor);
}

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()
{
     CloseY = PriceY + PriceTxtSize + 8;
     PriceDifY = CloseY + CommentTxtSize + 8;
     PricePercentY = PriceDifY + CommentTxtSize + 4;
	  CloseLabel = Object_ID + Symbol() + "_Close";
	  PriceLabel = Object_ID + Symbol() + "_Price";
	  PriceDifLabel = Object_ID + Symbol() + "_PriceDif";
	  PricePercentLabel = Object_ID + Symbol() + "_PricePercent";
	  CloseLabelVal = Object_ID + Symbol() + "_CloseVal";
	  PriceDifLabelVal = Object_ID + Symbol() + "_PriceDifVal";
	  PricePercentLabelVal = Object_ID + Symbol() + "_PricePercentVal";
}


void OutputLabelToChart(string LabelName, int LabelY, int LabelTxtSize, int X_Offset, string LabelFont, color LabelColor, string LabelStr)
{

  	if(ObjectFind(LabelName) != 0)
   {
   ObjectCreate(LabelName, OBJ_LABEL, 0, 0, 0);
	ObjectSet(LabelName, OBJPROP_CORNER, Corner_of_Chart_RIGHT_TOP);
	ObjectSet(LabelName, OBJPROP_XDISTANCE, Adjust_Side_to_side + X_Offset);
	ObjectSet(LabelName, OBJPROP_YDISTANCE, LabelY);
	}
	ObjectSetText(LabelName, LabelStr, LabelTxtSize, LabelFont, LabelColor);
}

void OutputPriceToChart(string mComment, color mColor)
{
   OutputLabelToChart(PriceLabel, PriceY, PriceTxtSize, 10, "Arial", mColor, mComment);
}

void OutputCloseToChart(string mComment1, string mComment2, color mColor)
{
   OutputLabelToChart(CloseLabel, CloseY, CommentTxtSize, 90, "Arial", CommentColor, mComment1);
   OutputLabelToChart(CloseLabelVal, CloseY, CommentTxtSize, 10, "Arial", mColor, mComment2);
}

void OutputPriceDifToChart(string mComment1, string mComment2, color mColor)
{
   OutputLabelToChart(PriceDifLabel, PriceDifY, CommentTxtSize, 90, "Arial", CommentColor, mComment1);
   OutputLabelToChart(PriceDifLabelVal, PriceDifY, CommentTxtSize, 10, "Arial", mColor, mComment2);
}

void OutputPricePercentToChart(string mComment1, string mComment2, color mColor)
{
   OutputLabelToChart(PricePercentLabel, PricePercentY, CommentTxtSize, 90, "Arial", CommentColor, mComment1);
   OutputLabelToChart(PricePercentLabelVal, PricePercentY, CommentTxtSize, 10, "Arial", mColor, mComment2);
}


