
#property indicator_chart_window
#define F						0
#define L						1
#define N						2
#define S						3
#define T						4

#define OPN_SHOW				0
#define CLS_SHOW				1
#define OPN_HOUR				2
#define CLS_HOUR				3
#define OPN_CLR				0
#define CLS_CLR				1
#define OPN_LAB				0
#define CLS_LAB				1
#define MAX_SESSIONS			5		

extern int       HowManyDays	= 10;

extern string	  s1				= "FrankFort Parameters";
extern bool      F_Opn_Show 	= TRUE;
extern bool      F_Cls_Show	= TRUE;
extern int       F_Opn_Hour	= 03;
extern int       F_Cls_Hour	= 12;
extern color     F_Opn_Color	= Lime;
extern color     F_Cls_Color	= Lime;
extern string    F_Opn_Label	= "Frankfurt Open";
extern string    F_Cls_Label	= "Frankfurt Close";

extern string	  s2				= "London Parameters";
extern bool      L_Opn_Show 	= TRUE;
extern bool      L_Cls_Show	= TRUE;
extern int       L_Opn_Hour	= 07;
extern int       L_Cls_Hour	= 16;
extern color     L_Opn_Color	= Blue;
extern color     L_Cls_Color	= Blue;
extern string    L_Opn_Label	= "London Open";
extern string    L_Cls_Label	= "London Close";

extern string	  s3				= "New York Parameters";
extern bool      N_Opn_Show 	= TRUE;
extern bool      N_Cls_Show	= TRUE;
extern int       N_Opn_Hour	= 11;
extern int       N_Cls_Hour	= 19;
extern color     N_Opn_Color	= Red;
extern color     N_Cls_Color	= Red;
extern string    N_Opn_Label	= "New York Open";
extern string    N_Cls_Label	= "New York Close";

extern string	  s4				= "Sydney Parameters";
extern bool      S_Opn_Show 	= FALSE;
extern bool      S_Cls_Show	= FALSE;
extern int       S_Opn_Hour	= 17;
extern int       S_Cls_Hour	= 2;
extern color     S_Opn_Color	= Orange;
extern color     S_Cls_Color	= Orange;
extern string    S_Opn_Label	= "Sydney Open";
extern string    S_Cls_Label	= "Sydney Close";

extern string	  s5				= "Tokyo Parameters";
extern bool      T_Opn_Show 	= FALSE;
extern bool      T_Cls_Show	= FALSE;
extern int       T_Opn_Hour	= 23;
extern int       T_Cls_Hour	= 8;
extern color     T_Opn_Color	= Purple;
extern color     T_Cls_Color	= Purple;
extern string    T_Opn_Label	= "Tokyo Open";
extern string    T_Cls_Label	= "Tokyo Close";

extern int       Vline_Style		= 2;
extern int       Vline_Width		= 1;
extern int       Cls_Style		= 2;
extern int       Cls_Width		= 1;
extern bool		  VLine_Before	= TRUE;

// Indicator data
double 	dSessInfo[MAX_SESSIONS][4];
string 	sSessInfo[MAX_SESSIONS][2];
color	 	cSessInfo[MAX_SESSIONS][2];

bool		FirstTime = TRUE;
int 		BarsToProcess;
double 	PipValue;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init() 
{
	PipValue 	= (Point == 0.01 || Point == 0.0001 ) * Point + (Point == 0.001 || Point == 0.00001 ) * Point * 10; 

   IndicatorShortName(WindowExpertName());   
	dSessInfo[F][OPN_SHOW] = F_Opn_Show;
	dSessInfo[F][CLS_SHOW] = F_Cls_Show;
	dSessInfo[F][OPN_HOUR] = F_Opn_Hour;
	dSessInfo[F][CLS_HOUR] = F_Cls_Hour;
	sSessInfo[F][OPN_LAB]  = F_Opn_Label;
	sSessInfo[F][CLS_LAB]  = F_Cls_Label;
	cSessInfo[F][OPN_CLR]  = F_Opn_Color;
	cSessInfo[F][CLS_CLR]  = F_Cls_Color;
//
	dSessInfo[L][OPN_SHOW] = L_Opn_Show;
	dSessInfo[L][CLS_SHOW] = L_Cls_Show;
	dSessInfo[L][OPN_HOUR] = L_Opn_Hour;
	dSessInfo[L][CLS_HOUR] = L_Cls_Hour;
	sSessInfo[L][OPN_LAB]  = L_Opn_Label;
	sSessInfo[L][CLS_LAB]  = L_Cls_Label;
	cSessInfo[L][OPN_CLR]  = L_Opn_Color;
	cSessInfo[L][CLS_CLR]  = L_Cls_Color;
//
	dSessInfo[N][OPN_SHOW] = N_Opn_Show;
	dSessInfo[N][CLS_SHOW] = N_Cls_Show;
	dSessInfo[N][OPN_HOUR] = N_Opn_Hour;
	dSessInfo[N][CLS_HOUR] = N_Cls_Hour;
	sSessInfo[N][OPN_LAB]  = N_Opn_Label;
	sSessInfo[N][CLS_LAB]  = N_Cls_Label;
	cSessInfo[N][OPN_CLR]  = N_Opn_Color;
	cSessInfo[N][CLS_CLR]  = N_Cls_Color;
//
	dSessInfo[S][OPN_SHOW] = S_Opn_Show;
	dSessInfo[S][CLS_SHOW] = S_Cls_Show;
	dSessInfo[S][OPN_HOUR] = S_Opn_Hour;
	dSessInfo[S][CLS_HOUR] = S_Cls_Hour;
	sSessInfo[S][OPN_LAB]  = S_Opn_Label;
	sSessInfo[S][CLS_LAB]  = S_Cls_Label;
	cSessInfo[S][OPN_CLR]  = S_Opn_Color;
	cSessInfo[S][CLS_CLR]  = S_Cls_Color;
//
	dSessInfo[T][OPN_SHOW] = T_Opn_Show;
	dSessInfo[T][CLS_SHOW] = T_Cls_Show;
	dSessInfo[T][OPN_HOUR] = T_Opn_Hour;
	dSessInfo[T][CLS_HOUR] = T_Cls_Hour;
	sSessInfo[T][OPN_LAB]  = T_Opn_Label;
	sSessInfo[T][CLS_LAB]  = T_Cls_Label;
	cSessInfo[T][OPN_CLR]  = T_Opn_Color;
	cSessInfo[T][CLS_CLR]  = T_Cls_Color;

	BarsToProcess = (Time[0] - HowManyDays * 24 * 60 * 60 ) / Period();
		
	for( int i = 0; i < ObjectsTotal(); i++ ) if( StringFind( ObjectName(i), "KH_SESSIONS", 0 ) >= 0 )  ObjectDelete( ObjectName(i) );
	return(0);
}


//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit() 
{
	for( int i = 0; i < ObjectsTotal(); i++ ) if( StringFind( ObjectName(i), "KH_SESSIONS", 0 ) >= 0 )  ObjectDelete( ObjectName(i) );
	return(0);
}


//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() 
{
	int i, limit, shift;
	string name;
	static int LastBarTime = 0;   
	if( Time[0] == LastBarTime ) { return; } else { LastBarTime = Time[0]; }
 
	// Get unprocessed ticks
	int CountedBars=IndicatorCounted();
	if( CountedBars < 0 ) return (-1);
	limit = Bars - CountedBars;

	if( FirstTime ) { limit = MathMin( limit, BarsToProcess ); FirstTime = FALSE; }

	for( i = 0; i < limit; i++ )
	{
		for( int j = 0; j < MAX_SESSIONS; j++ )
		{
 			if( dSessInfo[j][OPN_SHOW] && TimeHour(Time[i]) == dSessInfo[j][OPN_HOUR] && TimeMinute(Time[i])==0) 
 			{
   			if( VLine_Before ) shift = 1; else shift = 0;
				name = sSessInfo[j][OPN_LAB]+": "+TimeToStr(Time[i+shift]);
   			DrawLine( "KH_SESSIONS_L: "+name,      Time[i+shift], cSessInfo[j][OPN_CLR]);
   			DrawTextLabel( "KH_SESSIONS_T: "+name, Time[i+shift], sSessInfo[j][OPN_LAB], cSessInfo[j][OPN_CLR], i);
 			}
 			if( dSessInfo[j][CLS_SHOW] && TimeHour(Time[i]) == dSessInfo[j][CLS_HOUR] && TimeMinute(Time[i])==0 ) 
 			{
   			if( VLine_Before ) shift=1; else shift=0;
				name = sSessInfo[j][CLS_LAB]+": "+TimeToStr(Time[i+shift]);
   			DrawLine     ( "KH_SESSIONS_T: "+name, Time[i+shift], cSessInfo[j][CLS_CLR] );
   			DrawTextLabel( "KH_SESSIONS_L: "+name, Time[i+shift], sSessInfo[j][CLS_LAB], cSessInfo[j][CLS_CLR], i );
 			}
		}
	}  
   return(0);
}
int DrawLine( string name, double time, color clr ) 
{
	if (ObjectFind(name) >= 0 ) ObjectDelete(name);
	ObjectCreate(name, OBJ_TREND, 0, time, 0, time,3); 
	ObjectSet   (name, OBJPROP_STYLE, Vline_Style );     
	ObjectSet   (name, OBJPROP_WIDTH, Vline_Width );
	ObjectSet   (name, OBJPROP_BACK,  true);
	ObjectSet   (name, OBJPROP_COLOR, clr);    
	return(0);
}
int DrawTextLabel( string name, double time, string label, color clr, int i ) 
{
	double pos = High[i] + iATR(NULL,0,5,i)*2;

	if( ObjectFind(name) >= 0 ) ObjectDelete(name);      
	ObjectCreate(name, OBJ_TEXT, 0, time, pos);    
	ObjectSet(name, OBJPROP_ANGLE, 90);
	ObjectSet(name, OBJPROP_BACK, true);
	ObjectSetText(name, label , 8, "Arial", clr);
	return(0);
}

//+------------------------------------------------------------------+

