//+------------------------------------------------------------------+
//|                                                       eclock.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "www.forex-tsd.com"
#property indicator_chart_window

extern ENUM_TIMEFRAMES  TimeFrame    = PERIOD_CURRENT;
extern string           UniqueID     = "iClock1";
extern color            colorNormal  = clrLimeGreen;
extern color            colorLate    = clrRed;
extern int              XPos         = 10;
extern int              YPos         = 10;
extern ENUM_BASE_CORNER Corner       = CORNER_RIGHT_LOWER;
extern int              fontSize     = 10;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()   { EventSetMillisecondTimer(250);            return(0); }
int deinit() { EventKillTimer(); ObjectDelete(UniqueID); return(0); }
int start()  {                                           return(0); }
void OnTimer() { ShowClock();}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void ShowClock()
{
   datetime TimeArray[];
   int    theColor;
   string time;
 
   RefreshRates();

   //
   //
   //
   //

   int mul = (TimeFrame<1) ? _Period : TimeFrame;
   int diff = (int)MathRound((TimeCurrent()-TimeLocal())/3600.0)*3600;
         time = getTime((int)iTime(NULL,TimeFrame,0)+mul*60-(int)TimeLocal()-diff,theColor);

         //
         //
         //
         //
         //
                  
         ObjectCreate(UniqueID,OBJ_LABEL,0,0,0);
            ObjectSet(UniqueID,OBJPROP_CORNER,Corner);
            ObjectSet(UniqueID,OBJPROP_XDISTANCE,XPos);
            ObjectSet(UniqueID,OBJPROP_YDISTANCE,YPos);
            ObjectSetText(UniqueID,timeFrameToString(TimeFrame)+" "+time,fontSize, "Arial bold", theColor);
}

//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
//
//
//
//
//

string getTime(int times,int& theColor)
{
   string time = "";
   int    seconds;
   int    minutes;
   int    hours;
   
   //
   //
   //
   //
   //
   
   if (times < 0) {
         theColor = colorLate; times = MathAbs(times);}
   else  theColor = colorNormal;
   seconds = (times%60);
   hours   = (times-times%3600)/3600;
   minutes = (times-seconds)/60-hours*60;

   //
   //
   //
   //
   //
   
   if (hours>0)
   if (minutes < 10)
         time = StringConcatenate(time,hours,":0");
   else  time = StringConcatenate(time,hours,":");
         time = StringConcatenate(time,minutes);
   if (seconds < 10)
         time = StringConcatenate(time,":0",seconds);
   else  time = StringConcatenate(time,":" ,seconds);
   return(time);
}

//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   if (tf==PERIOD_CURRENT) tf=_Period;
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}
