#define OP_BUY 0
#define OP_SELL 1
#define OP_BUYLIMIT 2
#define OP_SELLLIMIT 3
#define OP_BUYSTOP 4
#define OP_SELLSTOP 5
#define MODE_OPEN 0
#define MODE_LOW 1
#define MODE_HIGH 2
#define MODE_CLOSE 3
#define MODE_VOLUME 4
#define MODE_TIME 5
#define MODE_TRADES 0
#define MODE_HISTORY 1
#define SELECT_BY_POS 0
#define SELECT_BY_TICKET 1
#define LONG_VALUE 4
#define FLOAT_VALUE 4
#define DOUBLE_VALUE 8
#define CHART_BAR 0
#define CHART_CANDLE 1
#define MODE_ASCEND 1
#define MODE_DESCEND 2
#define MODE_BID 9
#define MODE_ASK 10
#define MODE_POINT 11
#define MODE_DIGITS 12
#define MODE_SPREAD 13
#define MODE_STOPLEVEL 14
#define MODE_LOTSIZE 15
#define MODE_TICKVALUE 16
#define MODE_TICKSIZE 17
#define MODE_SWAPLONG 18
#define MODE_SWAPSHORT 19
#define MODE_STARTING 20
#define MODE_EXPIRATION 21
#define MODE_TRADEALLOWED 22
#define MODE_MINLOT 23
#define MODE_LOTSTEP 24
#define MODE_MAXLOT 25
#define MODE_SWAPTYPE 26
#define MODE_PROFITCALCMODE 27
#define MODE_MARGINCALCMODE 28
#define MODE_MARGININIT 29
#define MODE_MARGINMAINTENANCE 30
#define MODE_MARGINHEDGED 31
#define MODE_MARGINREQUIRED 32
#define MODE_FREEZELEVEL 33
#define MODE_CLOSEBY_ALLOWED 34
#define EMPTY -1
#define MODE_MAIN 0
#define MODE_SIGNAL 1
#define MODE_PLUSDI 1
#define MODE_MINUSDI 2
#define MODE_UPPER 1
#define MODE_LOWER 2
#define MODE_GATORJAW 1
#define MODE_GATORTEETH 2
#define MODE_GATORLIPS 3
#define MODE_TENKANSEN 1
#define MODE_KIJUNSEN 2
#define MODE_SENKOUSPANA 3
#define MODE_SENKOUSPANB 4
#define MODE_CHIKOUSPAN 5
#define MODE_CHINKOUSPAN 5
#define OBJPROP_TIME1 2000
#define OBJPROP_PRICE1 2001
#define OBJPROP_TIME2 2002
#define OBJPROP_PRICE2 2003
#define OBJPROP_TIME3 2004
#define OBJPROP_PRICE3 2005
#define OBJPROP_FIBOLEVELS 2006

//+------------------------------------------------------------------+
//|                                                 ForexSession.mq4 |
//|                   2011-1-18 16:30:18 oldz.cn@qq.com         OldZ |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
// The design idea is from http://www.forexmarkethours.com/ 

#property copyright "OldZ"
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 1
#property indicator_buffers 4
#property indicator_plots 4
#property indicator_color1 clrYellow
#property indicator_color2 clrRed
#property indicator_color3 clrBlue
#property indicator_color4 clrGreen
//---- input parameters
input int       LocalGMT=-2; // Local time zone GMT offset
input int       BrokerGMT=-2; // Broker MT4's GMT offset
input bool      ShowText = false; // Show each session on the left top corner.
int      SessionLocal[8],SessionBroker[8];
//---- buffers
   double Asian[];
   double London[];
   double NewYork[];
   double Close[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
int Digits = _Digits;

//---- indicators
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,110);
   SetIndexBuffer(0,Asian,INDICATOR_DATA,true);
   SetIndexLabel(0,"Asian");
   SetIndexEmptyValue(0,0.0);
   
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,110);
   SetIndexBuffer(1,London,INDICATOR_DATA,true);
   SetIndexLabel(1,"London");
   SetIndexEmptyValue(1,0.0);
   
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,110);
   SetIndexBuffer(2,NewYork,INDICATOR_DATA,true);
   SetIndexLabel(2,"NewYork");
   SetIndexEmptyValue(2,0.0);
   
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexArrow(3,110);
   SetIndexBuffer(3,Close,INDICATOR_DATA,true);
   SetIndexLabel(3,"Close");
   SetIndexEmptyValue(3,0.0);
   
   IndicatorDigits(Digits);
   
   SessionLocal(LocalGMT);
   SessionBroker(BrokerGMT);   
  
   
//----
   return;
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit()
  {
//----
   Comment("");
//----
   return;
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int _rates_total,      // size of input time series
           const int _prev_calculated,  // bars handled in previous call
           const datetime& _time[],     // Time
           const double& _open[],       // Open
           const double& _high[],       // High
           const double& _low[],        // Low
           const double& _close[],      // Close
           const long& _tick_volume[],  // Tick Volume
           const long& _volume[],       // Real Volume
           const int& _spread[]         // Spread
)
  {

	FXD_ONCALCULATE_FAIL = false; // will be set to true if some indicator won't load

   int Bars = _rates_total;

   int    counted_bars=((_prev_calculated>0)?_prev_calculated-1:0);
         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 5 days 
   for(i=0;i<limit && iTime(_Symbol,_Period,i)>=t5 ;i++)
   {     
      hr = TimeHour(iTime(_Symbol,_Period,i));
      DrawBrokerSession(hr,i);
   }
   
    if(ShowText)
    ShowSessionLocal();
   
//----
	if (FXD_ONCALCULATE_FAIL) {return -1;}

	return _rates_total;

   return(0);
  }
//+------------------------------------------------------------------+

void SessionLocal(int LocalGMT)
   {
      int gmt_diff = 8-LocalGMT;// 
            
      SessionLocal[0] =  7 - gmt_diff;
      SessionLocal[1] =  9 - gmt_diff;
      
      SessionLocal[2] =  18 - gmt_diff;
      SessionLocal[3] =  22 - gmt_diff;
      
      SessionLocal[4] =  2 - gmt_diff;
      SessionLocal[5] =  4- gmt_diff;// 
      
      SessionLocal[6] =  11 - gmt_diff;
      SessionLocal[7] =  13- gmt_diff;
      
      for( int i=0;i<8;i++)      
         if(SessionLocal[i]<0) SessionLocal[i]+=24;
      
   }
     
void SessionBroker(int BrokerGMT)
   {
      int gmt_diff = 1-BrokerGMT;// 
            
      SessionBroker[0] =  1 - gmt_diff;
      SessionBroker[1] =  3 - gmt_diff;
      
      SessionBroker[2] =  7 - gmt_diff;
      SessionBroker[3] =  9 - gmt_diff;
      
      SessionBroker[4] =  12 - gmt_diff;
      SessionBroker[5] =  14- gmt_diff;
      
      SessionBroker[6] =  17 - gmt_diff;
      SessionBroker[7] =  18 - gmt_diff;
      
      for( int i=0;i<8;i++)      
         if(SessionBroker[i]<0) SessionBroker[i]+=24;      
   }  


void DrawBrokerSession(int hr,int i)
{

//   if(SessionBroker[0]<SessionBroker[1])  
//      if(SessionBroker[0]<=hr && hr <=SessionBroker[1]) Asian[i]=0.8;
//   if(SessionBroker[0]>SessionBroker[1]) 
//     { 
//       if(hr>=SessionBroker[0] && hr<=23)  Asian[i] = 0.8;
//       if(hr>=0 && hr<=SessionBroker[1])   Asian[i] = 0.8;
//      }
//      
//      
//   if(SessionBroker[2]<SessionBroker[3])  
//      if(SessionBroker[2]<=hr && hr <=SessionBroker[3]) London[i]=0.6;
//   if(SessionBroker[2]>SessionBroker[3]) 
//      { 
//       if(hr>=SessionBroker[2] && hr<=23)  London[i] = 0.6;
//       if(hr>=0 && hr<=SessionBroker[3])   London[i] = 0.6;
//      }
//      
//   
//   if(SessionBroker[4]<SessionBroker[5])  
//      if(SessionBroker[4]<=hr && hr <=SessionBroker[5]) NewYork[i]=0.4;
//   if(SessionBroker[4]>SessionBroker[5]) 
//       { 
//       if(hr>=SessionBroker[4] && hr<=23)  NewYork[i] = 0.4;
//       if(hr>=0 && hr<=SessionBroker[5])   NewYork[i] = 0.4;
//      }
//   
//   if(SessionBroker[6]<SessionBroker[7])  
//      if(SessionBroker[6]<=hr && hr <=SessionBroker[7]) Close[i]=0.2;
//   if(SessionBroker[6]>SessionBroker[7]) 
//       { 
//       if(hr>=SessionBroker[6] && hr<=23)  Close[i] = 0.2;
//       if(hr>=0 && hr<=SessionBroker[7])   Close[i] = 0.2;
//      }    
   
}
   
void ShowSessionLocal()
   {
      Comment("\n","Forex market session on local time (GMT ",LocalGMT,")   ",TimeToStr(TimeLocal(),TIME_SECONDS),"\n","Sydney   session:  ",SessionLocal[0]+":00 ----",SessionLocal[1]+":59","\n","Tokyo    session:  ",SessionLocal[2]+":00 ----",SessionLocal[3]+":59","\n","London   session:  ",SessionLocal[4]+":00 ----",SessionLocal[5]+":59","\n","NewYork  session:  ",SessionLocal[6]+":00 ----",SessionLocal[7]+":59","\n");              
   }


//== fxDreema MQL4 to MQL5 Converter ==//

//-- Global Variables
int FXD_SELECTED_TYPE = 0; // Indicates what is selected by OrderSelect(), 1 for trade, 2 for pending order, 3 for history trade
ulong FXD_SELECTED_TICKET = 0; // The ticket number selected by OrderSelect()
bool FXD_ONCALCULATE_FAIL = false; // Flag that causes OnCalculate() to return nothing if some indicator is used and failed to load, which means that indicator calculations must be repeated


//-- Functions
string TimeToStr(datetime value, int mode = TIME_DATE|TIME_MINUTES)
{
	return TimeToString(value, mode);
}

int TimeHour(datetime date)
{
	MqlDateTime tm;
	TimeToStruct(date,tm);
	return(tm.hour);
}

void SetIndexStyle(
	int index,
	int type,
	int style=EMPTY,
	int width=EMPTY,
	color clr=clrNONE)
{
	if(width>-1)
		PlotIndexSetInteger(index,PLOT_LINE_WIDTH,width);
	if(clr!=CLR_NONE)
		PlotIndexSetInteger(index,PLOT_LINE_COLOR,clr);
	switch(type)
	{
		case 0:
			PlotIndexSetInteger(index,PLOT_DRAW_TYPE,DRAW_LINE); break;
		case 1:
			PlotIndexSetInteger(index,PLOT_DRAW_TYPE,DRAW_SECTION); break;
		case 2:
			PlotIndexSetInteger(index,PLOT_DRAW_TYPE,DRAW_HISTOGRAM); break;
		case 3:
			PlotIndexSetInteger(index,PLOT_DRAW_TYPE,DRAW_ARROW); break;
		case 4:
			PlotIndexSetInteger(index,PLOT_DRAW_TYPE,DRAW_ZIGZAG); break;
		case 12:
			PlotIndexSetInteger(index,PLOT_DRAW_TYPE,DRAW_NONE); break;

		default:
			PlotIndexSetInteger(index,PLOT_DRAW_TYPE,DRAW_LINE);
	}
	switch(style)
	{
		case 0:
			PlotIndexSetInteger(index,PLOT_LINE_STYLE,STYLE_SOLID); break;
		case 1:
			PlotIndexSetInteger(index,PLOT_LINE_STYLE,STYLE_DASH); break;
		case 2:
			PlotIndexSetInteger(index,PLOT_LINE_STYLE,STYLE_DOT); break;
		case 3:
			PlotIndexSetInteger(index,PLOT_LINE_STYLE,STYLE_DASHDOT); break;
		case 4:
			PlotIndexSetInteger(index,PLOT_LINE_STYLE,STYLE_DASHDOTDOT); break;

		default: return;
	}
}

void SetIndexArrow(int index, int code)
{
	PlotIndexSetInteger(index, PLOT_ARROW, code);
}

bool SetIndexBuffer( 
   int                    index,
   double                 &buffer[],
   ENUM_INDEXBUFFER_TYPE  data_type,
   bool set_as_series
   )
{
	bool ret = SetIndexBuffer(index, buffer, data_type);
	if (ret) {
		ArraySetAsSeries(buffer, true);
	}
	
	return ret;
}

void SetIndexLabel(int index, string text)
{
	PlotIndexSetString(index, PLOT_LABEL, text);
}

void SetIndexEmptyValue(int index, double value)
{
	PlotIndexSetDouble(index, PLOT_EMPTY_VALUE, value);
}

void IndicatorDigits(int digits)
{
	IndicatorSetInteger(INDICATOR_DIGITS, digits);
}

//== fxDreema MQL4 to MQL5 Converter ==//