//+-------------------------------------------------------------------+
//|                                                  ForexSession.mq4 |
//|                    2011-1-18 16:30:18 oldz.cn@qq.com         OldZ |
//+-------------------------------------------------------------------+
//|                                              Forex Session v2.mq4 |
//|29-9-2011-Redesign,Fixed session bar, added broker GMT time by cmkm|
//+-------------------------------------------------------------------+
#property copyright "cmkm-2011,[thhudu@yahoo.com]"

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 1
#property indicator_buffers 4
#property indicator_color1 DodgerBlue
#property indicator_color2 Gold
#property indicator_color3 Green
#property indicator_color4 OrangeRed
//---- input parameters
extern int       LocalGMT  = 7; 
extern bool      Show_Info  = true; 

int fontsize =8;
string fontname = "Comic Sans MS";
int lineWidth=2;

int LocalSyd[8],LocalTo[8],LocalLon[8],LocalNew[8];
int BrokerSyd[8],BrokerTo[8],BrokerLon[8],BrokerNew[8];
//---- buffers

double Sydney[];
double Tokyo[];
double London[];
double NewYork[];
//+---------------------------------------------+
int init()
  {  
   IndicatorShortName("Forex Session v2 |");
//---- indicators
   SetIndexStyle(0,DRAW_ARROW,STYLE_SOLID,lineWidth);
   SetIndexArrow(0,167);  
   SetIndexBuffer(0,Sydney);
   SetIndexLabel(0,"Sydney");
   SetIndexEmptyValue(0,0.0);
   
   SetIndexStyle(1,DRAW_ARROW,STYLE_SOLID,lineWidth);
   SetIndexArrow(1,167);
   SetIndexBuffer(1,Tokyo);
   SetIndexLabel(1,"Tokyo");
   SetIndexEmptyValue(1,0.0);
   
   SetIndexStyle(2,DRAW_ARROW,STYLE_SOLID,lineWidth);
   SetIndexArrow(2,167);
   SetIndexBuffer(2,London);
   SetIndexLabel(2,"London");
   SetIndexEmptyValue(2,0.0);
   
   SetIndexStyle(3,DRAW_ARROW,STYLE_SOLID,lineWidth);
   SetIndexArrow(3,167);
   SetIndexBuffer(3,NewYork);
   SetIndexLabel(3,"NewYork");
   SetIndexEmptyValue(3,0.0);
   
   IndicatorDigits(Digits);

//----
   return(0);
  }
//+---------------------------------------------+

int deinit()
  { 
//----
ObjectDelete("Local Time");
ObjectDelete("Broker Time");
ObjectDelete("Sydney");
ObjectDelete("Tokyo");
ObjectDelete("London");
ObjectDelete("NewYork");
ObjectDelete("period");
//---- 
   return(0);
  }
//+---------------------------------------------+

int start()
  {   int BrokerGMT=  LocalGMT-((TimeLocal()-CurTime())/3600);
      SessionLocal(LocalGMT);
      SessionBroker(BrokerGMT);
   
   int    counted_bars=IndicatorCounted();
         if(counted_bars<0) return(-1);
         if(counted_bars>0) counted_bars--;
   int   limit = Bars-counted_bars;
//----
   int i;
   int hr;
   datetime t5 = iTime(NULL,PERIOD_D1,4);// 4 : draw 4 days 
   for(i=0;i<limit && Time[i]>=t5 ;i++)
   {     
      hr = TimeHour(Time[i]);
      DrawBrokerSession(hr,i);
   } 
    if(Show_Info==True)    {     //on / off show text
   
   setLabel("Local Time","Local Time : (GMT "+LocalGMT+")   "+(TimeToStr(TimeLocal(),TIME_MINUTES)),White,1,10,5,false,fontsize,fontname);
   setLabel("Broker Time","Broker Time : (GMT "+BrokerGMT+")   "+(TimeToStr(CurTime(),TIME_MINUTES)),White,1,10,20,false,fontsize,fontname);
   setLabel("Sydney","Sydney   ( "+LocalSyd[0]+":00 ---- "+(LocalSyd[1])+":00 )",indicator_color1,1,10,40,false,fontsize,fontname);
   setLabel("Tokyo","Tokyo    ( "+LocalTo[2]+":00 ---- "+(LocalTo[3])+":00 )",indicator_color2,1,10,55,false,fontsize,fontname);
   setLabel("London","London  ( "+LocalLon[4]+":00 ---- "+(LocalLon[5])+":00 )",indicator_color3,1,10,70,false,fontsize,fontname);
   setLabel("NewYork","NewYork  ( "+LocalNew[6]+":00 ----  "+(LocalNew[7])+":00 )",indicator_color4,1,10,85,false,fontsize,fontname); 
   setLabel("period",":: 9 hours / Session ::",PaleGreen,4,155,2,false,fontsize,fontname); }

   return(0);
  }
//+---------------------------------------------+

void SessionLocal(int LocalGMT)
   {   
      LocalSyd[0] =  22 + LocalGMT;   //Local session time = GMT + LocalGMT
      LocalSyd[1] =  7 + LocalGMT;

      LocalTo[2] =  24 + LocalGMT;
      LocalTo[3] =  9 + LocalGMT;

      LocalLon[4] =  8 + LocalGMT;
      LocalLon[5] =  17 + LocalGMT;

      LocalNew[6] =  13 + LocalGMT;
      LocalNew[7] =  22 + LocalGMT;
      
      if (LocalSyd[0]>24){LocalSyd[0]=LocalSyd[0]-24;} 
      if (LocalSyd[1]>24){LocalSyd[1]=LocalSyd[1]-24; }
      
      if (LocalTo[2]>24){LocalTo[2]=LocalTo[2]-24;}
      if (LocalTo[3]>24){LocalTo[3]=LocalTo[3]-24; }
      
      if (LocalLon[4]>24){LocalLon[4]=LocalLon[4]-24;}
      if (LocalLon[5]>24){LocalLon[5]=LocalLon[5]-24; }
      
      if (LocalNew[6]>24){LocalNew[6]=LocalNew[6]-24;}
      if (LocalNew[7]>24){LocalNew[7]=LocalNew[7]-24; }
   }
//+---------------------------------------------+
void SessionBroker( int BrokerGMT)
   { 
      BrokerSyd[0] =  22 + BrokerGMT;   //Broker session time = GMT + BrokerGMT
      BrokerSyd[1] =  7 + BrokerGMT;

      BrokerTo[2] =  24 + BrokerGMT;
      BrokerTo[3] =  9 + BrokerGMT;

      BrokerLon[4] =  8 + BrokerGMT;
      BrokerLon[5] =  17 + BrokerGMT;

      BrokerNew[6] =  13 + BrokerGMT;
      BrokerNew[7] =  22 + BrokerGMT;
      
      if (BrokerSyd[0]>24){BrokerSyd[0]=BrokerSyd[0]-24;}
      if (BrokerSyd[1]>24){BrokerSyd[1]=BrokerSyd[1]-24; }
      
      if (BrokerTo[2]>24){BrokerTo[2]=BrokerTo[2]-24;}
      if (BrokerTo[3]>24){BrokerTo[3]=BrokerTo[3]-24; }
      
      if (BrokerLon[4]>24){BrokerLon[4]=BrokerLon[4]-24;}
      if (BrokerLon[5]>24){BrokerLon[5]=BrokerLon[5]-24; }
      
      if (BrokerNew[6]>24){BrokerNew[6]=BrokerNew[6]-24;}
      if (BrokerNew[7]>24){BrokerNew[7]=BrokerNew[7]-24; }      
   }  
//+---------------------------------------------+
void DrawBrokerSession(int hr,int i)
{
   if(BrokerSyd[0]<BrokerSyd[1])                                     //if (openTime < closeTime):
      if(BrokerSyd[0]<=hr && hr <=(BrokerSyd[1]-1)) Sydney[i]=0.8;   //-> draw From OpenTime to CloseTime (at 0.8 position)

   if(BrokerSyd[0]>BrokerSyd[1])                                     //if (openTime > closeTime):
     { 
       if(hr>=BrokerSyd[0] && hr<=23)  Sydney[i] = 0.8;              //->     draw From OpenTime to 23h (at 0.8)
       if(hr>=0 && hr<=(BrokerSyd[1]-1))   Sydney[i] = 0.8;          //-> and draw From 0h to CloseTime (at 0.8)
      }
//+----- 
   if(BrokerTo[2]<BrokerTo[3])  
      if(BrokerTo[2]<=hr && hr <=(BrokerTo[3]-1)) Tokyo[i]=0.6;

   if(BrokerTo[2]>BrokerTo[3]) 
      { 
       if(hr>=BrokerTo[2] && hr<=23)  Tokyo[i] = 0.6;
       if(hr>=0 && hr<=(BrokerTo[3]-1))   Tokyo[i] = 0.6;
      }
//+-----  
   if(BrokerLon[4]<BrokerLon[5])  
      if(BrokerLon[4]<=hr && hr <=(BrokerLon[5]-1)) London[i]=0.4;
   if(BrokerLon[4]>BrokerLon[5]) 
       { 
       if(hr>=BrokerLon[4] && hr<=23)  London[i] = 0.4;
       if(hr>=0 && hr<=(BrokerLon[5]-1))   London[i] = 0.4;
      }
//+-----
   if(BrokerNew[6]<BrokerNew[7])  
      if(BrokerNew[6]<=hr && hr <=(BrokerNew[7]-1)) NewYork[i]=0.2;
   if(BrokerNew[6]>BrokerNew[7]) 
       { 
       if(hr>=BrokerNew[6] && hr<=23)  NewYork[i] = 0.2;
       if(hr>=0 && hr<=(BrokerNew[7]-1))   NewYork[i] = 0.2;
      }    
}           
//+----------------Session Name ---------------------------------+
void setLabel(string name, string text, color col, int corner,
            int x, int y, bool back = false, int fontsize = 12, 
            string fontname = "MS Sans Serif") {
   if (ObjectFind(name)==-1){
      // aaa
      ObjectCreate(name, OBJ_LABEL, WindowOnDropped(),1, 0, 0);
      ObjectSetText(name, text, fontsize, fontname, col);
      ObjectSet(name, OBJPROP_CORNER, corner);
      ObjectSet(name, OBJPROP_BACK,back);
      ObjectSet(name, OBJPROP_XDISTANCE, x);
      ObjectSet(name, OBJPROP_YDISTANCE, y);          
   }else{
      ObjectSetText(name, text, fontsize, fontname, col);
      ObjectSet(name, OBJPROP_CORNER, corner);
      ObjectSet(name, OBJPROP_BACK,back);
      ObjectSet(name, OBJPROP_XDISTANCE, x);
      ObjectSet(name, OBJPROP_YDISTANCE, y); }
                                               }
                   

   


