//**********************************************************
//*   CInfo.mq4                                            *
//*                                                        *
//*   Displays the time left until the next candle close   *
//*   and the spread in absolute and relative Terms        *
//*                                                        *
//*   Written by: Totoro @ forexfactory.com                *
//**********************************************************

#property indicator_chart_window

extern bool   ShowSpread     = true;
extern bool   ShowRelative   = true;
extern string SpreadAbsolute = "pt";
extern string SpreadRelative = "per mille";
extern string Separator      = "|";

extern string Part2="---- Display Settings -----";                                                         
extern color Info_Label_Color = White;
extern string DisplayCorner = "topright";
extern int Info_font_size = 12;
extern int Info_X_offset = 0;
extern int Info_Y_offset = 0;
extern string Info_font_type = "Arial Bold";
string shortname;
   
int init()
{
   shortname="CInfo_V2";
   IndicatorShortName(shortname);
   return(0);
}
int deinit()
{
   //Comment("");
   ObjectsDeleteAll();
   return(0);
}

int start()
{
	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 + " Spread: ", sp + SpreadAbsolute + " (" + rel + " " + SpreadRelative + ")" ); }
      else             { spread = StringConcatenate(" " + Separator + " Spread: ", 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 in " + d + "d " + h + "h " + m + "m " + s + "s" + spread;
   else if(h>0) textmsg="Close in " + h + "h " + m + "m " + s + "s" + spread;
   else if(m>0) textmsg="Close in " + m + "m " + s + "s" + spread;
   else         textmsg="Close in " + 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, 7+Info_X_offset);
ObjectSet(shortname + "Display", OBJPROP_YDISTANCE, 13+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);
   }
}