//+------------------------------------------------------------------+
//|                                                        Clock.mq4 |
//|                                                           Jerome |
//|                                               whitnell@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Jerome"
#property link      "whitnell@gmail.com"

//------------------------------------------------------------------
// Instructions
//    BrokerTZ  - Timezone of your Broker (in hours from GMT)
//    LocalTz   - Your timezone in hours from GMT
//    DST       - Is it daylight savings time?
//    ShowLocal - Set to tru to show your local time zone
//    corner    - 0 = top left, 1 = top right, 2 = bottom left, 3 = bottom right
//    topOff    - pixels from top to show the clock
//    labelColor- Color of label
//    clockColor- Color of clock
//
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red


//---- input parameters
extern double       BrokerTZ=2;
extern double       LocalTz=-4;
extern bool         DST=true;
extern bool         ShowLocal=true;
extern int          corner=1;
extern int          topOff=100;
extern bool         show12HourTime=true;
extern color        labelColor=White;
extern color        clockColor=White;

//---- buffers
double ExtMapBuffer1[];
int LondonTZ = 1;
int TokyoTZ = 9;
int NewYorkTZ = -4;
double Spread = 0.00;
int leftLabel = 90;
int leftItem = 45;

int multiplier()
{
   int multiplier;
   
   if(Digits == 2 || Digits == 4) multiplier = 1;
   if(Digits == 3 || Digits == 5) multiplier = 10;
   if(Digits == 6) multiplier = 100;   
   if(Digits == 7) multiplier = 1000;  
   
   return(multiplier);
   
}

string TimeToString( datetime when ) {
   if ( !show12HourTime )
      return (TimeToStr( when, TIME_MINUTES ));
      
   int hour = TimeHour( when );
   int minute = TimeMinute( when );
   
   string ampm = " AM";
   
   string timeStr;
   if ( hour >= 12 ) {
      hour = hour - 12;
      ampm = " PM";
   }
      
   if ( hour == 0 )
      hour = 12;
   timeStr = DoubleToStr( hour, 0 ) + ":";
   if ( minute < 10 )
      timeStr = timeStr + "0";
   timeStr = timeStr + DoubleToStr( minute, 0 );
   timeStr = timeStr + ampm;
   
   return (timeStr);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   int dstDelta=-1;
   if ( DST )
      dstDelta = 0;
   

   
   datetime brokerTime = CurTime();
   datetime GMT = brokerTime - (BrokerTZ+dstDelta)*3600;
   datetime local = GMT + (LocalTz + dstDelta) * 3600;
   datetime london = GMT + (LondonTZ + dstDelta) * 3600;
   datetime tokyo = GMT + (TokyoTZ + dstDelta) * 3600;
   datetime newyork = GMT + (NewYorkTZ + dstDelta) * 3600;
   
   //Print( brokerTime, " ", GMT, " ", local, " ", london, " ", tokyo, " ", newyork  );
   string GMTs = TimeToString( GMT );
   string locals = TimeToString( local );
   string londons = TimeToString( london );
   string tokyos = TimeToString( tokyo );
   string newyorks = TimeToString( newyork );
   string brokers = TimeToString( CurTime() );
   
   //string timeText = " Local: " + locals +  " GMT: " +GMTs +  " | London:" +londons + 
   //   " | New York: " +newyorks + " | Tokyo: " + tokyos ;
   
   //ObjectSetText( "times", timeText, 10, "Verdana", clockColor );
   if ( ShowLocal ) {
      ObjectSetText( "locl", "Local:", 8, "Verdana", labelColor );
      ObjectSetText( "loct", locals, 8, "Verdana", clockColor );
   }
   ObjectSetText( "gmtl", "GMT:", 8, "Verdana", labelColor );
   ObjectSetText( "gmtt", GMTs, 8, "Verdana", clockColor );
   ObjectSetText( "nyl", "New York:", 8, "Verdana", labelColor );
   ObjectSetText( "nyt", newyorks, 8, "Verdana", clockColor );
   ObjectSetText( "lonl", "London:", 8, "Verdana", labelColor );
   ObjectSetText( "lont", londons, 8, "Verdana", clockColor );
   ObjectSetText( "tokl", "Tokyo:", 8, "Verdana", labelColor );
   ObjectSetText( "tokt", tokyos, 8, "Verdana", clockColor );
   ObjectSetText( "brol", "Broker:", 8, "Verdana", labelColor );
   ObjectSetText( "brot", brokers, 8, "Verdana", clockColor );
//---- spread

   Spread=NormalizeDouble(((Ask-Bid)/Point)/multiplier(),1);
   ObjectSetText("Spread Monitor1","Spread:", 8, "Verdana", labelColor);
   ObjectSetText("Spread Monitor2",DoubleToStr(Spread ,1),8, "Verdana", clockColor);
   
   ObjectSetText("barcount", timeBarLeft(), 8, "Verdana", clockColor);
        
//----
   return(0);
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int ObjectMakeLabel( string n, int xoff, int yoff ) {
   ObjectCreate( n, OBJ_LABEL, 0, 0, 0 );
   ObjectSet( n, OBJPROP_CORNER, corner );
   ObjectSet( n, OBJPROP_XDISTANCE, xoff );
   ObjectSet( n, OBJPROP_YDISTANCE, yoff );
   ObjectSet( n, OBJPROP_BACK, true );
}

int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   
   int top=topOff;
   
   if (show12HourTime)
   {
      leftLabel = 110;
   }

   if ( ShowLocal ) {
      ObjectMakeLabel( "locl", leftLabel, top+15 );
      ObjectMakeLabel( "loct", leftItem, top+15 );
   }
   
//---- spread monitor
   
   ObjectMakeLabel( "barcount", leftItem, top-90 );
   ObjectMakeLabel( "Spread Monitor1", leftLabel, top-75 );   
   ObjectMakeLabel( "Spread Monitor2", leftItem, top-75 );
   ObjectMakeLabel( "brol", leftLabel, top-60 );
   ObjectMakeLabel( "brot", leftItem, top-60 );
   ObjectMakeLabel( "tokl", leftLabel, top-45 );
   ObjectMakeLabel( "tokt", leftItem, top-45 );
   ObjectMakeLabel( "lonl", leftLabel, top-30 );
   ObjectMakeLabel( "lont", leftItem, top-30 );
   ObjectMakeLabel( "nyl", leftLabel, top-15 );
   ObjectMakeLabel( "nyt", leftItem, top-15 );
   ObjectMakeLabel( "gmtl", leftLabel, top );
   ObjectMakeLabel( "gmtt", leftItem, top );




  
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   ObjectDelete( "barcount");
   ObjectDelete( "locl" );
   ObjectDelete( "loct" );
   ObjectDelete( "nyl" );
   ObjectDelete( "nyt" );
   ObjectDelete( "gmtl" );
   ObjectDelete( "gmtt" );
   ObjectDelete( "lonl" );
   ObjectDelete( "lont" );
   ObjectDelete( "tokl" );
   ObjectDelete( "tokt" );
   ObjectDelete( "brol" );
   ObjectDelete( "brot" );
//---- spread monitor
   ObjectDelete( "Spread Monitor1" );   
   ObjectDelete( "Spread Monitor2" );    
//----
   return(0);
  }

string timeBarLeft()
{

   int time = Time[0]+Period()*60-CurTime();
   int seconds = time%60;
   int minutes = time/60;
   int hours = time/60/60;
 
   
   if (hours >= 1)
   {
      return(pluralTime(hours, "Hour") + ", " + pluralTime(minutes%60, "Minute") + ", " + pluralTime(seconds,"second"));
   }
   else if (minutes >= 1)
   { 
      return(pluralTime(minutes, "Minute") + ", " + pluralTime(seconds, "second"));
   }
   else
   {
      return(pluralTime(seconds, "Second"));
   }

}

string pluralTime(int time, string word)
{
   if (time == 1)
   {
      return(time + " " + word);
   }
   
   return(time + " " + word + "s");
}