//+---------------------------------------------------------------+
//|                                                    MTF info   |
//|                                   http://www.currencylabs.com |
//|                                                 (C) _mikkom   |
//+---------------------------------------------------------------+
#property copyright "Mikko"
#property link      "noneOfYOurBusiness"

#property indicator_chart_window
#property indicator_buffers 0

string name;
extern int gmtPlus = -1;

int init() {

   IndicatorBuffers(0);

   name = "Mikko mtf info";
   IndicatorShortName(name);

   return(0);
}
  
int deinit() {
   clear();
   return(0);
}
     
int start() {
   int period[9];
   int untilClose[9];
   int lastMove[10];
   int lastValue[10]; // int to disable display of decimals
   
   period[0] = PERIOD_M1; // NOTE: Not used
   period[1] = PERIOD_M5;
   period[2] = PERIOD_M15;
   period[3] = PERIOD_M30;
   period[4] = PERIOD_H1;
   period[5] = PERIOD_H4;
   period[6] = PERIOD_D1;
   period[7] = PERIOD_W1;
   period[8] = PERIOD_MN1;
             
   for(int j=1; j<=8; j++) {        
      int per = period[j];
      /** Doesn't work correctly! **/
      lastValue[j] = (iClose(Symbol(),per,0)-iOpen(Symbol(),per,0))/Point;
      untilClose[j] = period[j]*60-(TimeCurrent()-iTime(Symbol(),per,0));
   } 

   clear();

   for(int i=1; i<=8; i++) {
         int move = lastValue[i];
    
         string id;
         int win = 0;
         
         double hh = (WindowPriceMax(win)-WindowPriceMin(win))/10;

         id =  name+"_time_"+i;
         ObjectCreate(id, OBJ_LABEL, win, 0, 0);
         ObjectSetText(id, timeRemaining(untilClose[i]), 8);
         ObjectSet(id, OBJPROP_CORNER, 1);
         ObjectSet(id, OBJPROP_XDISTANCE, 85);
         ObjectSet(id, OBJPROP_YDISTANCE, 10 + (8-i) * 15);         

         id =  name+"_value_"+i;
         ObjectCreate(id, OBJ_LABEL, win, 0, 0);
         ObjectSetText(id, ""+lastValue[i], 8);
         ObjectSet(id, OBJPROP_CORNER, 1);
         ObjectSet(id, OBJPROP_XDISTANCE, 10);
         ObjectSet(id, OBJPROP_YDISTANCE, 10 + (8-i) * 15);         

         id =  name+"_title_"+i;
         ObjectCreate(id, OBJ_LABEL, win, 0, 0);
         ObjectSetText(id, period_text(period[i]), 8);
         ObjectSet(id, OBJPROP_CORNER, 1);
         ObjectSet(id, OBJPROP_XDISTANCE, 45);
         ObjectSet(id, OBJPROP_YDISTANCE, 10 + (8-i) * 15);         
         
         if(move > 0) {
            ObjectSet(name+"_time_"+i, OBJPROP_COLOR, Green);
            ObjectSet(name+"_value_"+i, OBJPROP_COLOR, Green);
            ObjectSet(name+"_title_"+i, OBJPROP_COLOR, Green);
            //SetIndexStyle(i-1,DRAW_LINE,EMPTY,1,Green);
         } else if(move < 0) {
            ObjectSet(name+"_time_"+i, OBJPROP_COLOR, Red);
            ObjectSet(name+"_value_"+i, OBJPROP_COLOR, Red);
            ObjectSet(name+"_title_"+i, OBJPROP_COLOR, Red);
            //SetIndexStyle(i-1,DRAW_LINE,EMPTY,1, Red);
         } else {
            ObjectSet(name+"_time_"+i, OBJPROP_COLOR, Gray);
            ObjectSet(name+"_value_"+i, OBJPROP_COLOR, Gray);
            ObjectSet(name+"_title_"+i, OBJPROP_COLOR, Gray);
         }
  }
  
   datetime gmt = TimeCurrent() + gmtPlus * 60 * 60;

   id = "time";
   ObjectCreate(id, OBJ_LABEL, win, 0, 0);
   ObjectSetText(id, digits2(TimeHour(gmt))+":"+digits2(TimeMinute(gmt))+":"+digits2(TimeSeconds(gmt)), 8);
   ObjectSet(id, OBJPROP_CORNER, 1);
   ObjectSet(id, OBJPROP_XDISTANCE, 10);
   ObjectSet(id, OBJPROP_YDISTANCE, 10 + 9 * 15);       
   ObjectSet(id, OBJPROP_COLOR, White);

   // Session times from: http://www.babypips.com/school/market_hours.html
     
   id = "session_london";
   ObjectCreate(id, OBJ_LABEL, win, 0, 0);
   ObjectSetText(id, openTime(gmt,8,16)+" London 8:00 - 17:00", 8);
   ObjectSet(id, OBJPROP_CORNER, 1);
   ObjectSet(id, OBJPROP_XDISTANCE, 10);
   ObjectSet(id, OBJPROP_YDISTANCE, 10 + 11 * 15);         
  
   if(!weekend(gmt) && TimeHour(gmt) >= 8 && TimeHour(gmt) < 17) {
      ObjectSet(id, OBJPROP_COLOR, White);
   } else {
      ObjectSet(id, OBJPROP_COLOR, Gray);
   } 
  
   id = "session_us";
   ObjectCreate(id, OBJ_LABEL, win, 0, 0);
   ObjectSetText(id, openTime(gmt,13,22)+" Nyc 13:00 - 22:00", 8);
   ObjectSet(id, OBJPROP_CORNER, 1);
   ObjectSet(id, OBJPROP_XDISTANCE, 10);
   ObjectSet(id, OBJPROP_YDISTANCE, 10 + 12 * 15);       
   if(!weekend(gmt) && TimeHour(gmt) >= 13 && TimeHour(gmt) < 22) {
      ObjectSet(id, OBJPROP_COLOR, White);
   } else {
      ObjectSet(id, OBJPROP_COLOR, Gray);
   } 

   id = "session_sydney";
   ObjectCreate(id, OBJ_LABEL, win, 0, 0);
   ObjectSetText(id, openTime(gmt,22,8)+" Sydney 22:00 - 8:00", 8);
   ObjectSet(id, OBJPROP_CORNER, 1);
   ObjectSet(id, OBJPROP_XDISTANCE, 10);
   ObjectSet(id, OBJPROP_YDISTANCE, 10 + 13 * 15);       
   if(!weekend(gmt) && (TimeHour(gmt) >= 22 || TimeHour(gmt) < 8)) {
      ObjectSet(id, OBJPROP_COLOR, White);
   } else {
      ObjectSet(id, OBJPROP_COLOR, Gray);
   } 

   id = "session_tokyo";
   ObjectCreate(id, OBJ_LABEL, win, 0, 0);
   ObjectSetText(id, openTime(gmt,0,9)+" Tokyo 0:00 - 9:00", 8);
   ObjectSet(id, OBJPROP_CORNER, 1);
   ObjectSet(id, OBJPROP_XDISTANCE, 10);
   ObjectSet(id, OBJPROP_YDISTANCE, 10 + 14 * 15);
   if(!weekend(gmt) && TimeHour(gmt) >= 0 && TimeHour(gmt) < 9) {
      ObjectSet(id, OBJPROP_COLOR, White);
   } else {
      ObjectSet(id, OBJPROP_COLOR, Gray);
   }

  
   return(0);
}

void clear() {
   for(int i = 0; i <= 8; i++) {
      ObjectDelete(name+"_time_"+i);
      ObjectDelete(name+"_value_"+i);
      ObjectDelete(name+"_title_"+i);
   }
   ObjectDelete("session_tokyo");
   ObjectDelete("session_london");
   ObjectDelete("session_us");
   ObjectDelete("time");
}

string period_text(int p) {
   switch(p) {
      case PERIOD_M1: return("M1"); break;
      case PERIOD_M5: return("M5"); break;
      case PERIOD_M15: return("M15"); break;
      case PERIOD_M30: return("M30"); break;
      case PERIOD_H1: return("H1"); break;
      case PERIOD_H4: return("H4"); break;
      case PERIOD_D1: return("D1"); break;
      case PERIOD_W1: return("W1"); break;
      case PERIOD_MN1: return("MN1"); break;
   }
}

string digits2(int num) {
   if(num < 10)
      return("0"+num);
   else
      return(num);
}

string openTime(datetime gmt, int openHour, int closeHour) {
   if(!weekend(gmt) && openHour > closeHour && (TimeHour(gmt) <= closeHour || TimeHour(gmt) > openHour)) {      
      return ((closeHour-TimeHour(gmt)-1)+":"+digits2(60-TimeMinute(gmt)));
   } else
   if(!weekend(gmt) && TimeHour(gmt) >= openHour && TimeHour(gmt) < closeHour) {
      return ((closeHour-TimeHour(gmt)-1)+":"+digits2(60-TimeMinute(gmt)));
   }
   return("closed");
}

bool weekend(datetime gmt) {
   return(TimeDayOfWeek(gmt) == 0 || TimeDayOfWeek(gmt) == 6);
}

string timeRemaining(datetime t) {
   if(t/60/60 > 1) {
      return (DoubleToStr(t/60/60,0)+":"+DoubleToStr(t/60%60,0)+":"+DoubleToStr(t%60,0));
   } else {
      return (DoubleToStr(t/60,0)+":"+DoubleToStr(t%60,0));
   }
}