//+------------------------------------------------------------------+
//|                                                          asd.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   ObjectCreate("SessionsInfo", OBJ_RECTANGLE_LABEL, 0, 0, 0);
   ObjectSet("SessionsInfo", OBJPROP_CORNER, 0);
   ObjectSet("SessionsInfo", OBJPROP_XDISTANCE, 0);
   ObjectSet("SessionsInfo", OBJPROP_YDISTANCE, 20);
   ObjectSetInteger(0,"SessionsInfo",OBJPROP_XSIZE,150); 
   ObjectSetInteger(0,"SessionsInfo",OBJPROP_YSIZE,95);    
   ObjectSet("SessionsInfo", OBJPROP_BACK, false);
   ObjectSet("SessionsInfo", OBJPROP_SELECTABLE, false);
   ObjectSetInteger(0,"SessionsInfo",OBJPROP_BGCOLOR,clrNavy); 
   EventSetTimer(1);
//---
   return(INIT_SUCCEEDED);
  }
  
void OnDeinit(const int reason) {
   Comment("");
   ObjectDelete("SessionsInfo");
   EventKillTimer();
};

//+------------------------------------------------------------------+
//| 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 value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   bool market_open = MarketInfo(_Symbol, MODE_TRADEALLOWED);;
   datetime last_quote_time = (datetime)SymbolInfoInteger(_Symbol, SYMBOL_TIME);
   datetime dtstart[],dtend[];
   int cnt=0;
   ArrayResize(dtstart,1);
   ArrayResize(dtend,1);
   while (SymbolInfoSessionTrade(_Symbol,DayOfWeek(),cnt,dtstart[cnt],dtend[cnt])) { cnt++; ArrayResize(dtstart,cnt+1); ArrayResize(dtend,cnt+1); };
   string sessions="";
   bool found=false;
   int dist_sec=-1;
   datetime lastsessionend=0;
   datetime timecurrent=TimeCurrent();
   bool noquotes = false;
   if (timecurrent - last_quote_time > 5*60) noquotes = true;
   for (int i=0;i<cnt;i++) {
   //Print(dtstart[i]," ",dtend[i]);
      dtstart[i]=StrToTime(TimeToStr(dtstart[i],TIME_MINUTES));
      dtend[i]=StrToTime(TimeToStr(dtend[i],TIME_MINUTES));
      //if (TimeHour(dtend[i]) == 0 && TimeMinute(dtend[i]) == 0) dtend[i] += 86400; // move 00:00 to next day
      if (dtend[i] < dtstart[i]) dtend[i] += 86400;
      lastsessionend=dtend[i];
      string time="";
      if (timecurrent >= dtstart[i] && timecurrent < dtend[i]) { 
         found=true;
         time=calctime(dtend[i],timecurrent);
         if (time != "") {
            time="     "+time+" to close";
            } //else found=false; // time is outside the range
         }
      if ((i==0 || timecurrent >= dtend[i-1]) && timecurrent < dtstart[i]) { 
         time=calctime(dtstart[i],timecurrent);
            time="     "+time+" to open";
         }
      sessions += TimeToStr(dtstart[i],TIME_MINUTES)+" - "+TimeToStr(dtend[i],TIME_MINUTES)+ time+"\n";
   }
   if (!found) market_open = false;
   /*string time2=calctime(lastsessionend,TimeCurrent());
   if (time2 != "") {
      time2=time2+" to today's market close"+"\n";
      }*/
   string comment = "\n";
   comment += "Market is "+(market_open?"open"+(noquotes?" - no quotes":""):"closed") + "\n";
   comment += "\n" + sessions;
   //comment += "\n"+time2;
   Comment(comment);
  }
//+------------------------------------------------------------------+
string calctime(datetime endtime,datetime current) {
   int h,m,s;
   int dist_sec=(int)(endtime-current);
   string time="";
   if (dist_sec>=0) {
      h = (int)MathFloor(dist_sec/3600);
      m = (int)MathFloor((dist_sec - h*3600) / 60);
      s = dist_sec - h*3600 - m*60;
      time=IntegerToString(h,2,'0')+":"+IntegerToString(m,2,'0')+":"+IntegerToString(s,2,'0');
      }
   return time;
}