//+------------------------------------------------------------------+
//|                                       Forex Market Hours GMT.mq4 |
//|                      Copyright © 2008, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#import "kernel32.dll"
   int GetTimeZoneInformation(int& a0[]);
#import
bool show_sessions_clock=true;
extern int Shift_X = 400;
extern int Shift_Y = 10;
extern int X0=550;
extern int Y0=200;

int companion;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//----
    if(IsDllsAllowed()==false){
    Alert("DLL call is not allowed. Indicator cannot run. Please enable DLL in MT4");
     return(0);
    }
objectdelete();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
objectdelete();   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
    if(IsDllsAllowed()==true)
    {
    
   int    counted_bars=IndicatorCounted();
       companion = WindowFind("session_companion");
if (companion == -1)companion=0;
   
//----
if (show_sessions_clock) trading_session();
if (show_sessions_clock) timer();
if (show_sessions_clock) market_hours_box();

//----
  }

   for (int i=0;i<ObjectsTotal();i++)
   {
      string objn=ObjectName(i);
      if ((StringSubstr(objn,0,13)=="session_time_") || (StringSubstr(objn,0,17)=="market_hours_box_") || (objn=="time2"))
      {
         ObjectSet(objn, OBJPROP_XDISTANCE, ObjectGet(objn, OBJPROP_XDISTANCE)-X0);
         ObjectSet(objn, OBJPROP_YDISTANCE, ObjectGet(objn, OBJPROP_YDISTANCE)-Y0);
      }
   }

   return(0);
  }
//+------------------------------------------------------------------+
 void trading_session(){
  
  int i, xPos, yPos;
  
  ShowSession("session_time_tokyo",  195, 260, "Tokyo GMT");
  yPos = 249;
  for (i = 1; i <8; i++)
  {
    ShowSessionVal("session_time_tokyo2_" + i, 169, yPos, "______________________", Magenta);
    ShowSessionVal("session_time_tokyo3_" + i, 516, yPos, "__", Magenta);
    yPos += 2;
  }
  
  

  ShowSession("session_time_frankfurt",  308, 276, "Frankfurt GMT");
  yPos = 265;
  for (i = 1; i <8; i++)
  {
    ShowSessionVal("session_time_frankfurt2_" + i, 276, yPos, "________________________", Blue);
    yPos += 2;
  }
  
  ShowSession("session_time_london", 320, 217, "London GMT");
  yPos = 207;
  for (i = 1; i <8; i++)
  {
    ShowSessionVal("session_time_london_" + i, 291, yPos, "________________________", Lime);
    yPos += 2;
  }
  
  ShowSession("session_time_ny",     392, 232, "New York GMT");
  yPos = 221;
  for (i = 1; i <8; i++)
  {
    ShowSessionVal("session_time_ny_" + i, 365, yPos, "________________________", Red);
    yPos += 2;
  }

  ShowSession("session_time_sydney", 179, 245, "Sydney GMT");
  yPos = 235;
  for (i = 1; i <8; i++)
  {
    ShowSessionVal("session_time_sydney_" + i, 485, yPos, "_______", Aqua);
    ShowSessionVal("session_time_sydney2_" + i, 169, yPos, "______________", Aqua);
    yPos += 2;
  }

  xPos = 170;
  for (i = 1; i <= 24; i++)
  {
    ShowSessionVal("session_time_hour_" + i, xPos, 200, DoubleToStr(i-1,0), Silver);
    xPos += 15;
  }

}
  
void timer(){
  int x1, x2, x3;

int TimeX[] = {167, 184, 198, 213, 229, 244, 258, 273,
               289, 304, 318, 332, 349, 363, 377, 392, 
               406, 421, 436, 451, 465, 482, 497, 512};
               
   string GMTtime = TimeToStr(TimeGMT(), TIME_MINUTES|TIME_SECONDS);
   int GMTm = TimeMinute(TimeLocal()) - 0.1 * TimeZoneLocal();
   int GMTh =  TimeHour(TimeLocal()) - 1 * TimeZoneLocal();              //test
   if (GMTh == 24) GMTh = 0;
   
x1 = TimeX[GMTh];
x2 = x1 - 7;
if (GMTm <= 30 && GMTm > 15) x2 = x1;
if (GMTm >= 30 && GMTm < 45) x2 = x1 + 7;
x3 = x2 + 3;

  ShowSessionTime("session_time_current", x2, 207);
  ShowSessionTime("session_time_current2", x2, 241);
  ShowSessionVal("time2", x3, 294, "" + GMTtime + "", Silver);

}

void market_hours_box(){

  int BoxLevelY[] = { 0, 205, 219, 233, 247, 263, 279 };
  int BoxY[] = { 0, 213, 222, 231, 240, 249, 258, 263 };
   
  for (int i = 1; i <= 6; i++)
   {
     ShowMarketHoursBox("market_hours_box_level" + i, 170, BoxLevelY[i],
                        "____________________________________________________________");
   }

   for (i = 1; i <= 7; i++)
   {
     ShowBoxVert("market_hours_box_side" + i, 168, BoxY[i], Silver);
     ShowBoxVert("market_hours_box_side2" + i, 528, BoxY[i], Silver);
   }
   

int BoxX[] = { 0, 514, 499, 484, 468, 453, 438, 423, 408,
             393, 378, 363, 348, 334, 318, 305, 291, 276,
             261, 246, 231, 216, 201, 186 };
             
int Box4X[] = { 0, 168, 186, 201, 216, 231, 246, 261, 276, 291,
              305, 318, 334, 348, 363, 378, 393, 408, 423, 438, 
              453, 468, 484, 499, 514, 528 };

  for ( i = 1; i < 24; i++)
  {
    for (int j = 1; j <= 7; j++)
    {
      ShowBoxVert("market_hours_box_vert" + i*10 + j, BoxX[i], BoxY[j], DarkGray);
    }
  }
  for (i = 1; i < 26; i++) ShowBoxVert("market_hours_box_vertx" + i, Box4X[i], 279, DarkGray);
  for (i = 1; i < 26; i++) ShowBoxVert("market_hours_box_verty" + i, Box4X[i], 270, DarkGray);
    
}

  
void ShowSession(string id, int x, int y, string msg)
{
  if (ObjectFind(id) == -1 )
  {
    ObjectCreate(id, OBJ_LABEL, companion, 0, 0);
    ObjectSet(id, OBJPROP_BACK, 0);
    ObjectSet(id, OBJPROP_CORNER, 0);
  }
  ObjectSet(id, OBJPROP_XDISTANCE, x+Shift_X);
  ObjectSet(id, OBJPROP_YDISTANCE, y+Shift_Y);
  ObjectSetText(id,msg, 8,"Tahoma",White);
}


void ShowSessionVal(string id, int x, int y, string msg, color mColor)
{
  if (ObjectFind(id) == -1 )
  {
    ObjectCreate(id, OBJ_LABEL, companion, 0, 0);
    ObjectSet(id, OBJPROP_BACK, 1);
    ObjectSet(id, OBJPROP_CORNER, 0);
  }
  ObjectSet(id, OBJPROP_XDISTANCE, x+Shift_X);
  ObjectSet(id, OBJPROP_YDISTANCE, y+Shift_Y);
  ObjectSetText(id,msg, 8,"Times New Roman",mColor);
}

void ShowSessionTime(string id, int x, int y)
{
  if (ObjectFind(id) == -1 )
  {
    ObjectCreate(id, OBJ_LABEL, companion, 0, 0);
    ObjectSet(id, OBJPROP_BACK, 0);
    ObjectSet(id, OBJPROP_CORNER, 0);
    ObjectSet(id, OBJPROP_XDISTANCE, x+Shift_X);
  }
  ObjectSet(id, OBJPROP_YDISTANCE, y+Shift_Y);
  ObjectSetText(id,"|", 34,"Times New Roman",Red);
}

void ShowMarketHoursBox(string id, int x, int y, string msg)
{
  if (ObjectFind(id) == -1 )
  {
    ObjectCreate(id, OBJ_LABEL, companion, 0, 0);
    ObjectSet(id, OBJPROP_BACK, 0);
    ObjectSet(id, OBJPROP_CORNER, 0);
  }
  ObjectSet(id, OBJPROP_XDISTANCE, x+Shift_X);
  ObjectSet(id, OBJPROP_YDISTANCE, y+Shift_Y);
  ObjectSetText(id,msg, 8,"Times New Roman",Silver);
}

void ShowBoxVert(string id, int x, int y, color mColor)
{

  if (ObjectFind(id) == -1 ){
  ObjectCreate(id, OBJ_LABEL, companion, 0, 0);
  ObjectSet(id, OBJPROP_BACK, 0);
  ObjectSet(id, OBJPROP_CORNER, 0);}
  ObjectSet(id, OBJPROP_XDISTANCE, x+Shift_X);
  ObjectSet(id, OBJPROP_YDISTANCE, y+Shift_Y);
  ObjectSetText(id,"|", 8,"Times New Roman",mColor);
}

void objectdelete(){
//ObjectDelete("session_companion");
ObjectDelete("time2");

  for (int i = ObjectsTotal()-1; i >= 0; i--)
  {

   if (StringFind(ObjectName(i), "session_time_") >= 0) ObjectDelete(ObjectName(i));
   if (StringFind(ObjectName(i), "market_hours_") >= 0) ObjectDelete(ObjectName(i));

  }
}

double TimeZoneLocal() {
   int lia_0[43];
   switch (GetTimeZoneInformation(lia_0)) {
   case 0:
      Print("Error obtaining PC timezone from GetTimeZoneInformation in kernel32.dll. Returning 0");
      return (0);
   case 1:
      return (lia_0[0] / (-60.0));
   case 2:
      return ((lia_0[0] + lia_0[42]) / (-60.0));
   }
   Print("Unkown return value from GetTimeZoneInformation in kernel32.dll. Returning 0");
   return (0);
}

double TimeZoneServer() {
   int li_0 = (TimeCurrent() - TimeLocal()) / 60;
   int li_4 = MathRound(li_0 / 30.0);
   li_0 = 30 * li_4;
   return (TimeZoneLocal() + li_0 / 60.0);
}

int TimeGMT() {
   int TL = TimeLocal() - 3600.0 * TimeZoneLocal();
   int TC = TimeCurrent() - 3600.0 * TimeZoneServer();
   if (TL > TC + 300) return (TL);
   else return (TC);
}
/////////////////