//+-------------------------------------------------------------------------------------------------------------+
//|                                                                 Copyright 2014, William Kreider (Madhatt30) |
//|                                                                                                             |
//| Broker_Time_Offset fix a2 and enhancements by file45 - https://login.mql5.com/en/users/file45/publications  |
//+-------------------------------------------------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property strict
#property indicator_chart_window

enum FontSelect 
{
   Arial=0,
   Times_New_Roman=1,
   Courier=2
};
enum ChartWindow
{
   Main_Chart_Window = 0, // Main chart window
   First_Separate_Window = 1, // 1st Separate window
   Second_Seperate_Window = 2, // 2nd Separate window
   Third_Separate_Window = 3, // 3rd Separate window
   Fourth_Separate_Window = 4, // 4th Separate window
   Fith_Separate_Window = 5, // 5th Separate window
   Sixth_Separate_Window = 6 // 6th Separate window
};   
  
//--- input parameter
input int Broker_Time_Offset = 1; // Broker Time adjustment (0 = GMT, 1 = GMT + 1 etc)
input ChartWindow  windexx = 0;  // Display Window
input FontSelect selectedFont = 0; // Font Select
input int textSize = 14;  // Font Size
input bool bold = false;  // Font Bold
input color fntcolor = clrLime;  // Font Color
input int XDistance = 20;  // Left - Right
input int YDistance = 20;  // Up - Down
input ENUM_BASE_CORNER corner = 1;  // Corner 

long           thisChart;
int            iFontType;
string         sBoldType;
string         sFontType;
int            timeOffset;
datetime       ServerLocalOffset;
datetime       prevTime,myTime,localtime;
bool           newBar = false;
//int windexx=WindowIsVisible(0);
//int windexx;

ENUM_ANCHOR_POINT corner_loc;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   switch(corner)
   {
      case 0: corner_loc = ANCHOR_LEFT_UPPER; break;
      case 1: corner_loc = ANCHOR_LEFT_LOWER; break;
      case 2: corner_loc = ANCHOR_RIGHT_LOWER; break;
      case 3: corner_loc = ANCHOR_RIGHT_UPPER;
   }          
  
//--- indicator buffers mapping
   EventSetTimer(1);
   thisChart = ChartID();
   if(bold){
      sBoldType=" Bold";
   }else if(!bold){
      sBoldType="";
   }
   iFontType=selectedFont;
   Comment("");
   switch(iFontType){
      case 0: sFontType="Arial" + sBoldType; break;
      case 1: sFontType="Times New Roman" + sBoldType; break;
      case 2: sFontType="Courier" + sBoldType; break;
   }
   ObjectCreate(thisChart,"BarTimer",OBJ_LABEL,windexx,XDistance,YDistance);

   datetime srvtime,tmpOffset;
   // Use RefreshRates to get the current time from TimeCurrent
   // Otherwise you'll just get the last known time
   RefreshRates();
   
   srvtime = TimeCurrent();
   // Modified
   localtime = TimeLocal()+TimeGMTOffset();
   if(TimeHour(srvtime)>TimeHour(localtime)){
      // Server Time is still ahead of us
      int newOffset = TimeHour(srvtime)-TimeHour(localtime);
      ServerLocalOffset = (newOffset*60*60);
   }else if(TimeHour(srvtime)<TimeHour(localtime)){
      // Server Time is Behind us
      int newOffset = TimeHour(localtime)-TimeHour(srvtime);
      ServerLocalOffset = (newOffset*60*60);
   }else{
      // No modification required
      ServerLocalOffset = srvtime;
   }
   localtime = TimeLocal()-ServerLocalOffset;
   
   tmpOffset = TimeSeconds(srvtime) - TimeSeconds(localtime);
   if(tmpOffset < 30 && tmpOffset >= 0){
      timeOffset = TimeSeconds(srvtime) - TimeSeconds(localtime);
   }
   return(INIT_SUCCEEDED);
}
  
void OnDeinit(const int reason)
  {
   EventKillTimer();
   ObjectDelete("BarTimer");
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   return(rates_total);
}
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
{
   if(Period() >0 && Period() <1440)
   {
      localtime = TimeLocal()+(TimeGMTOffset()+((60*60)*Broker_Time_Offset));
      if(ObjectFind(thisChart,"BarTimer")>=0)
      {
         ObjectCreate(0,"BarTimer",OBJ_LABEL,windexx,0,0);
         ObjectSetInteger(0,"BarTimer",OBJPROP_ANCHOR,corner_loc);
         ObjectSet("BarTimer",OBJPROP_CORNER,corner);
         ObjectSet("BarTimer",OBJPROP_XDISTANCE,XDistance);
         ObjectSet("BarTimer",OBJPROP_YDISTANCE,YDistance);
         ObjectSetText("BarTimer",TimeToStr(Time[0]+Period()*60-localtime-timeOffset,TIME_SECONDS ),textSize,sFontType,fntcolor);
     } 
   }
}

