//**********************************************************
//*   CInfo.mq4                                            *
//*   Displays the time left until the next candle close   *
//*   and the spread in absolute and relative Terms        *
//*   Written by: Totoro @ forexfactory.com                *
//*   Alert added by Hullsy - guntraderz@hotmail.com       *
//**********************************************************

#property indicator_chart_window

extern string  Part1="---- Display Settings ----";
extern bool    ShowSpread              = true;
extern bool    ShowRelative            = false;
extern string  SpreadAbsolute          = "pt";
extern string  SpreadRelative          = "per mille";
extern string  Separator               = "|";
extern color   Info_Label_Color        = White;
extern string  DisplayCorner           = "topright";
extern int     Info_font_size          = 8;
extern int     Info_X_offset           = 15;
extern int     Info_Y_offset           = 0;
extern string  Info_font_type          = "Calibri";

extern string  Part2="---- Alert Settings ----";
extern int     AlertMinsB4CandleClose  = 1;
extern bool    Use_Alert               = false;
extern string  AlertSound              = "stops.wav";

datetime LastAlertTime;
string shortname;
   
int init()
{
   shortname="Candle Info";
   IndicatorShortName(shortname);
//   Comment("New Candle Alert");
   LastAlertTime = TimeCurrent();   
   return(0);
}
int deinit()
{
   //Comment("");
   //ObjectsDeleteAll();
   ObjectDelete(shortname + "Display");  // removed ObjectsDeleteAll() 
   return(0);
}

int start()
{
  if ((Use_Alert == true) && (LastAlertTime < Time[0]))
   {
      Alert("New Candle on ",Symbol());
      LastAlertTime = Time[0];
   }	
	
	int ttc, d, h, m, s, rest;
	string spread, sp, rel;
   string textmsg;
   ttc = Time[0] + Period()*60 - TimeCurrent();
   
   rest=ttc%86400;
   d=(ttc-rest)/86400;
   ttc=rest;
   rest=ttc%3600;
   h=(ttc-rest)/3600;
   ttc=rest;
   rest=ttc%60;
   m=(ttc-rest)/60;
   s=rest;
   
   if(ShowSpread)
   {
      if(MarketInfo("EURUSD", MODE_DIGITS)==5) { sp = DoubleToStr(0.1*MarketInfo(Symbol(), MODE_SPREAD), 0); }
      else { sp = DoubleToStr(MarketInfo(Symbol(), MODE_SPREAD), 0); }
      rel = DoubleToStr(1000*(MarketInfo(Symbol(), MODE_ASK)-MarketInfo(Symbol(), MODE_BID))/MarketInfo(Symbol(), MODE_BID), 2);
      if(ShowRelative) { spread = StringConcatenate(" " + Separator + " Sp: ", sp + SpreadAbsolute + " (" + rel + " " + SpreadRelative + ")" ); }
      else             { spread = StringConcatenate(" " + Separator + " Sp: ", sp + SpreadAbsolute); }
   }

   //if(d>0)      Comment( "Close in " + d + "d " + h + "h " + m + "m " + s + "s" + spread );
   //else if(h>0) Comment( "Close in " + h + "h " + m + "m " + s + "s" + spread );
   //else if(m>0) Comment( "Close in " + m + "m " + s + "s" + spread );
   //else         Comment( "Close in " + s + "s" + spread );

   if(d>0)      textmsg="Close " + d + "d " + h + "h " + m + "m " + s + "s" + spread;
   else if(h>0) textmsg="Close " + h + "h " + m + "m " + s + "s" + spread;
   else if(m>0) textmsg= "Close " + m + "m " + s + "s" + spread;
   else         textmsg="Close " + s + "s" + spread;
   
   
   DrawAllObjects(textmsg);
   return(0);
}

void DrawAllObjects(string msg) 
{

ObjectCreate(shortname + "Display", OBJ_LABEL, 0, 0, 0);
ObjectSetText(shortname + "Display",msg,Info_font_size, Info_font_type, Info_Label_Color);
ObjectSet(shortname + "Display", OBJPROP_CORNER, CheckCorner());
ObjectSet(shortname + "Display", OBJPROP_XDISTANCE, 5+Info_X_offset);
ObjectSet(shortname + "Display", OBJPROP_YDISTANCE, 5+Info_Y_offset);

return;
}

int CheckCorner()
{
   if(DisplayCorner=="topleft")
   {
      return(0);
   }
   else if(DisplayCorner=="topright") 
   {
      return(1);
   }
   else if(DisplayCorner=="bottomleft")
   {
      return(2);
   }
   else if(DisplayCorner=="bottomright")
   {
      return(3);
   }
   else
   {
      return(0);
   }
}