//+------------------------------------------------------------------+
//| Template.mq4
//| Based on 
//| !!! this file is created with Tab size 4 and no space indentation
//+------------------------------------------------------------------+
#property copyright "Comer"
#property link      ""

#property indicator_chart_window

#property indicator_buffers 0

#define MAX_RETRY			10
#define DEFAULT_FONT_SIZE	10
#define DEFAULT_FONT_NAME	"Arial"

//---- input parameters
extern string	Timeframe				= "D1";

extern int		Label_Righ_Shift_bars	= 20;

extern int		Pivot_Line_Style		= STYLE_SOLID;
extern int		Pivot_Line_Width		= 1;
extern int		SR_Line_Style			= STYLE_DOT;
extern int		SR_Line_Width			= 1;

extern color	Pivot_Color			=	Aqua;
extern color	Support_Color		=	LimeGreen;
extern color	Resistance_Color	=	DarkOrange;

int timeframe	= EMPTY_VALUE;

datetime lastOpen = 0;
string pivotObj;
string r1Obj;
string r2Obj;
string r3Obj;
string s1Obj;
string s2Obj;
string s3Obj;

int init() {
	string msg;
	
	if( Timeframe == "M1" )
		timeframe = PERIOD_M1; //	1 minute
	else
	if( Timeframe == "M5" )
		timeframe = PERIOD_M5; //	5 minutes
	else
	if( Timeframe == "M15" )
		timeframe = PERIOD_M15; //	15 minutes
	else
	if( Timeframe == "M30" )
		timeframe = PERIOD_M30; //	30 minutes
	else
	if( Timeframe == "H1" )
		timeframe = PERIOD_H1; //	1 hour
	else
	if( Timeframe == "H4" )
		timeframe = PERIOD_H4; //	4 hour
	else
	if( Timeframe == "D1" )
		timeframe = PERIOD_D1; //	Daily
	else
	if( Timeframe == "W1" )
		timeframe = PERIOD_W1; //	Weekly
	else
	if( (Timeframe == "MN")
	||	(Timeframe == "MN1") )
		timeframe = PERIOD_MN1; //	Monthly	
	else {
		timeframe = EMPTY_VALUE;
		msg = "Unknown Timeframe! \'" + Timeframe + "\' ***";
		Print( msg );
		Alert( "Fatal Error: ", msg );
		return (-1);
	}

	// load timeframe - if it can not be loaded - abort
	int i;
	for(i = 0; (i < MAX_RETRY) && (iOpen( NULL, timeframe, 1 ) == 0); i++ )
     	Sleep( 2000 );
	
	if( i == MAX_RETRY ) {
		timeframe = EMPTY_VALUE;
		msg = "*** Data can not be loaded for \'" + Timeframe + "\' ***";
		Alert( "Fatal Error: ", msg );
		return (-1);
	}
		
	return (0);
}

int deinit() {
	delete( pivotObj );
	
	delete( r1Obj );
	delete( r2Obj );
	delete( r3Obj );
	
	delete( s1Obj );
	delete( s2Obj );
	delete( s3Obj );

	return(0);
}

int start() {
	if( timeframe == EMPTY_VALUE )
		return (-1);

	processBar();
   
	return(0);
}

void processBar() {
	datetime open = iOpen( NULL, timeframe, 1 );
	if( lastOpen == open )	// not changed - nothing to do
		return;
	
	lastOpen = open;
	
	double high 	= iHigh	( NULL, timeframe, 1 );
	double low		= iLow	( NULL, timeframe, 1 );
	double close	= iClose( NULL, timeframe, 1 );
   
	double pivot	= (high + low + close) / 3.0;
	
	double r1		= (2.0*pivot) - low;
	double r2		= pivot + (high-low);
	double r3		= high + 2.0*(pivot-low);
	
	double s1		= (2.0*pivot) - high;
	double s2		= pivot - (high-low);
	double s3		= low - 2.0*(high-pivot);
	
	pivotObj = draw( "Pivot",	pivot, Pivot_Color,		Pivot_Line_Style, Pivot_Line_Width );
	
	s1Obj = draw( "S1",		s1, Support_Color,	SR_Line_Style, SR_Line_Width );
	s2Obj = draw( "S2",		s2, Support_Color,	SR_Line_Style, SR_Line_Width );
	s3Obj = draw( "S3",		s3, Support_Color,	SR_Line_Style, SR_Line_Width );
	
	r1Obj = draw( "R1",		r1, Resistance_Color,	SR_Line_Style, SR_Line_Width );
	r2Obj = draw( "R2",		r2, Resistance_Color,	SR_Line_Style, SR_Line_Width );
	r3Obj = draw( "R3",		r3, Resistance_Color,	SR_Line_Style, SR_Line_Width );
}

string draw( string name, double price, color col, int line, int width ) {
	string objectName = name + " " + Timeframe;
	
	line ( name, objectName, price, col, line, width );
	label( name, objectName, price, col, line, width );
	
	return (objectName);
}

void delete( string objectName ) {
	ObjectDelete( objectName );
	ObjectDelete( objectName + " Label" );
}

void line( string name, string objectName, double price, color col, int line, int width ) {
	datetime time = iTime( NULL, timeframe, 0 );

	if( ObjectFind( objectName ) != -1 )
		ObjectMove	( objectName, 0, time, price );
	else {
		ObjectCreate( objectName, OBJ_TREND,		0, time, price, TimeCurrent(), price );
		ObjectSet	( objectName, OBJPROP_COLOR,	col );
		ObjectSet	( objectName, OBJPROP_STYLE,	line );
		ObjectSet	( objectName, OBJPROP_WIDTH,	width );
	}
	
}

void label( string name, string objectName, double price, color col, int line, int width ) {
	datetime	time		= iTime( NULL, timeframe, 0 );
	datetime	x			= TimeCurrent() + Period()*60*Label_Righ_Shift_bars;

	objectName = objectName + " Label";
	
	if( ObjectFind( objectName ) != -1 )
		ObjectMove		( objectName, 0, x, price );
	else {
		string msg = Timeframe + " " + name;
		for( int i = StringLen(msg); i > 0; i--)
			msg = " " + msg;

		ObjectCreate	( objectName, OBJ_TEXT, 0, x, price );
		ObjectSet		( objectName, OBJPROP_COLOR, col );
		ObjectSetText	( objectName, msg, DEFAULT_FONT_SIZE, DEFAULT_FONT_NAME );
	}
}


