//lptuyen

#property indicator_chart_window

extern string s1 = "Change font colors automatically? True = Yes";
extern bool Bid_Ask_Colors = FALSE;
extern string s2 = "Default Font Color";
extern color FontColor = White;
extern string s3 = "Font Size";
extern int FontSize = 10;
extern string s4 = "Font Type";
extern string FontType = "Tahoma";
extern string s5 = "Display the price in what corner?";
extern string s6 = "Upper left=0; Upper right=1";
extern string s7 = "Lower left=2; Lower right=3";
extern int DockCorner = 3;
extern string spdtxt = "Ticks to average Spread over: (1 or more)";
extern int spdpd = 100;

extern color ClockColor = White;
extern int size = 10;

double cBid, sprd=0, osprd, spdarray[], spdmax=0;
int digitNo;
string mPrice, sText, mText, hText, mSpread;
int rTimeM, rTimeS;

int init()
{
   digitNo=Digits;
   if (Digits==3 || Digits==5) digitNo=Digits-1;
   sprd=MarketInfo(Symbol(),MODE_SPREAD);
   ArrayResize(spdarray, spdpd);
   return (0);
}

int deinit()
{
   ObjectDelete("Market_Price_Label");
   ObjectDelete("time");
   ObjectDelete("spread");
   return (0);
}

int start()
{
   if (Bid_Ask_Colors == TRUE)//According to Up/Down
   {
      if (Bid > cBid) FontColor = LawnGreen;
      if (Bid < cBid) FontColor = Red;
      cBid = Bid;
   }
   osprd=sprd;
   sprd=MarketInfo(Symbol(),MODE_SPREAD);
   spdarray[0]=sprd;
   spdmax=0;
   for(int i=0;i<=spdpd-1;i++) if(spdarray[i]>spdmax) spdmax=spdarray[i];
   sprd=((spdpd-1)*osprd+sprd)/spdpd;
   mPrice="Bid="+DoubleToStr(Bid, digitNo)+"   ";
   if(spdpd>1) mSpread = "Avg("+spdpd+") Spread="+DoubleToStr(sprd,1)+" pips.  Max("+spdpd+") Spread="+DoubleToStr(spdmax,1)+" pips.   ";
   else mSpread = "Spread="+DoubleToStr(sprd,1)+" pips.   ";
   for(i=spdpd-1;i>=1;i--) spdarray[i]=spdarray[i-1];
     
   //Clock
   int thisbarminutes = Period();
    int thisbarseconds = thisbarminutes*60; 
    int seconds= thisbarseconds - (TimeCurrent()-Time[0]); // seconds left in bar 
    
    int minutes= MathFloor(seconds/60);
    int hours  = MathFloor(seconds/3600);
    
    minutes = minutes -  hours*60  ;
    seconds = seconds - minutes*60 - hours*3600; 
    
   
   sText=DoubleToStr(seconds,0);
   if(StringLen(sText)<2) sText="0"+sText;
   mText=DoubleToStr(minutes,0);
   if(StringLen(mText)<2) mText="0"+mText;
   hText=DoubleToStr(hours,0);
   if(StringLen(hText)<2) hText="0"+hText;
      
   ObjectCreate("Market_Price_Label", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("Market_Price_Label", mPrice, FontSize, FontType, FontColor);
   ObjectSet("Market_Price_Label", OBJPROP_CORNER, DockCorner);
   ObjectSet("Market_Price_Label", OBJPROP_XDISTANCE, 1);
   ObjectSet("Market_Price_Label", OBJPROP_YDISTANCE, 1); 
   ObjectSet("Market_Price_Label", OBJPROP_BACK, False); 
   
   ObjectCreate("spread", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("spread", mSpread, FontSize, FontType, FontColor);
   ObjectSet("spread", OBJPROP_CORNER, DockCorner);
   ObjectSet("spread", OBJPROP_XDISTANCE, 1);
   ObjectSet("spread", OBJPROP_YDISTANCE, 30); 
   ObjectSet("spread", OBJPROP_BACK, False); 

   ObjectCreate("time", OBJ_LABEL, 0, 0, 0);
   if(Period()<240) ObjectSetText("time", "Time Remaining = "+mText + ":" + sText+"   ", size, "Arial", ClockColor);
   else ObjectSetText("time", "Time Remaining = "+hText + ":" + mText + ":" + sText+"   ", size, "Arial", ClockColor);
   ObjectSet("time", OBJPROP_CORNER, DockCorner);
   ObjectSet("time", OBJPROP_XDISTANCE, 1);
   ObjectSet("time", OBJPROP_YDISTANCE, 60);
   ObjectSet("time", OBJPROP_BACK, False); 

   return (0);
}