//+------------------------------------------------------------------+
//|                                                 SpreadLive.mq4   |
//|                                    Copyright © 2000, Robert Hill |
//|                                                                  |
//|   Output the Current price, spread, high spread and low spread   |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Robert Hill"
#property link      "None"

#property indicator_chart_window

extern string  note1="Display the price in what corner?";
extern string  note2="Upper left=0; Upper right=1";
extern string  note3="Lower left=2; Lower right=3";
extern int     WhatCorner=1;
extern int     PriceYoffset = 25;
extern int     PriceTxtSize = 30;
extern color   PriceColor = Red;

extern int     CommentTxtSize = 12;
extern int     Adjust_Side_to_side = 20;
extern color   CommentLabel_color = LightSteelBlue;

string Object_ID = "SL_";
double myPoint;

   string PriceStr;
   double HighSpread = 0;
   double LowSpread = 999;
   double CurrentSpread;
   string HighSpreadStr;
   string LowSpreadStr;
   string CurrentSpreadStr;


// Labels

   int HighSpreadY;
   int LowSpreadY;
   int CurrentSpreadY;

   string PriceLabel;
   string HighSpreadLabel;
   string LowSpreadLabel;
   string SpreadLabel;

   string PriceLabelVal;
   string HighSpreadLabelVal;
   string LowSpreadLabelVal;
   string SpreadLabelVal;


int init() {
    myPoint = SetPoint();
    HighSpread = 0;
    LowSpread = 999;
    DeleteBadLabels();
    DeleteExistingLabels();
    SetupLabels();
   return (0);
}

int deinit() {
   ClearLabels();
   DeleteExistingLabels();
   return (0);
}

int start() {

   if (HighSpreadY < 10)
   {
     SetupLabels();
     ClearLabels();
     DeleteExistingLabels();
     SetupLabels();// Make sure label settings are OK
   }
   else
   {
    ClearLabels();
   }

   PriceStr = DoubleToStr(Bid, Digits);
   OutputPriceToChart("" + PriceStr + "", PriceColor);
   
   HighSpreadStr = "";
   LowSpreadStr = "";
   CurrentSpreadStr = "";
   CurrentSpread = (Ask - Bid) / myPoint;
   if (CurrentSpread > HighSpread) HighSpread = CurrentSpread;
   if (CurrentSpread < LowSpread) LowSpread = CurrentSpread;
   if (Digits >= 4)
   {
     HighSpreadStr = DoubleToStr(HighSpread, Digits - 4);
     LowSpreadStr = DoubleToStr(LowSpread, Digits - 4);
     CurrentSpreadStr = DoubleToStr(CurrentSpread, Digits - 4);
   }
   else
   {
     HighSpreadStr = DoubleToStr(HighSpread, Digits - 2);
     LowSpreadStr = DoubleToStr(LowSpread, Digits - 2);
     CurrentSpreadStr = DoubleToStr(CurrentSpread, Digits - 2);
   }
   OutputHighSpreadToChart("High Spread", HighSpreadStr);
   OutputLowSpreadToChart("Low Spread", LowSpreadStr);
   OutputSpreadToChart("Spread", CurrentSpreadStr);
   return (0);
}

double SetPoint()
{
   double mPoint;
   
   if (Digits < 4)
      mPoint = 0.01;
   else
      mPoint = 0.0001;
   
   return(mPoint);
}

void ClearLabels()
{
   string mComment = " ";
   
   OutputHighSpreadToChart("High Spread", mComment);
   OutputLowSpreadToChart("Low Spread", mComment);
   OutputSpreadToChart("Spread", 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()
{
     HighSpreadY = PriceYoffset + PriceTxtSize + 8;
     LowSpreadY = HighSpreadY + CommentTxtSize + 4;
     CurrentSpreadY = LowSpreadY + CommentTxtSize + 4;
	  PriceLabel = Object_ID + Symbol() + "_Price";
	  HighSpreadLabel = Object_ID + Symbol() + "_HighSpread";
	  LowSpreadLabel = Object_ID + Symbol() + "_LowSpread";
	  SpreadLabel = Object_ID + Symbol() + "_Spread";
	  HighSpreadLabelVal = Object_ID + Symbol() + "_HighSpreadVal";
	  LowSpreadLabelVal = Object_ID + Symbol() + "_LowSpreadVal";
	  SpreadLabelVal = Object_ID + Symbol() + "_SpreadVal";
}


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, WhatCorner);
	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, PriceYoffset, PriceTxtSize, 10, "Arial", mColor, mComment);
}

void OutputHighSpreadToChart(string mComment1, string mComment2)
{
   OutputLabelToChart(HighSpreadLabel, HighSpreadY, CommentTxtSize, 45, "Arial", CommentLabel_color, mComment1);
   OutputLabelToChart(HighSpreadLabelVal, HighSpreadY, CommentTxtSize, 10, "Arial Bold", Gold, mComment2);
}

void OutputLowSpreadToChart(string mComment1, string mComment2)
{
   OutputLabelToChart(LowSpreadLabel, LowSpreadY, CommentTxtSize, 45, "Arial", CommentLabel_color, mComment1);
   OutputLabelToChart(LowSpreadLabelVal, LowSpreadY, CommentTxtSize, 10, "Arial Bold", Gold, mComment2);
}

void OutputSpreadToChart(string mComment1, string mComment2)
{
   OutputLabelToChart(SpreadLabel, CurrentSpreadY, CommentTxtSize, 45, "Arial", CommentLabel_color, mComment1);
   OutputLabelToChart(SpreadLabelVal, CurrentSpreadY, CommentTxtSize, 10, "Arial Bold", Gold, mComment2);
}


