//+------------------------------------------------------------------+
//|                                           WEEKLY_Pivot_Range.mq4 |
//|                      Copyright © 2006, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
#property link      " Code modified by cja" 

#property indicator_chart_window

#property indicator_buffers 7
#property indicator_color1  Silver
#property indicator_width1 5
#property indicator_color2  Blue
#property indicator_width2 4
#property indicator_color3  Blue
#property indicator_width3 4
#property indicator_color4  Blue
#property indicator_width4 4
#property indicator_color5  Blue
#property indicator_width5 4
#property indicator_color6  Blue
#property indicator_width6 4
#property indicator_color7  Blue
#property indicator_width7 4


extern bool LineTEXT =false;

//---- input parameters

//---- buffers
double PBuffer[];
double S1Buffer[];
double S2Buffer[];
double S3Buffer[];
double R1Buffer[];
double R2Buffer[];
double R3Buffer[];
//double UP1Buffer[];
//double DN1Buffer[];

string Pivot = "Pivot Point", Support1="Support1", Support2="Support2", Support3="Support3", Resistance1="Resistance1", Resistance2="Resistance2", Resistance3="Resistance3";//Range_DN = "Range_DN", Range_UP = "Range_UP";

double P, S1,S2,S3,R1,R2,R3;//UP, DN;
double LastHigh, LastLow, x,RANGE;


//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{

   ObjectDelete("Pivot Point");
   ObjectDelete("Support1");   
   ObjectDelete("Support2");   
   ObjectDelete("Support3");
   ObjectDelete("Resistance1");      
   ObjectDelete("Resistance2");      
   ObjectDelete("Resistance3");      
//	ObjectDelete("Range_DN");
//	ObjectDelete("Range_UP");

	//----
	return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{  string short_name,short_name1,short_name2,short_name3,short_name4,short_name5,short_name6;
	//---- indicator line
	SetIndexStyle(0, DRAW_LINE);
	SetIndexStyle(1, DRAW_LINE,STYLE_DASH);
	SetIndexStyle(2, DRAW_LINE,STYLE_DASH);
	
	SetIndexStyle(3, DRAW_LINE,STYLE_DASH);
	SetIndexStyle(4, DRAW_LINE,STYLE_DASH);
	SetIndexStyle(5, DRAW_LINE,STYLE_DASH);
	SetIndexStyle(6, DRAW_LINE,STYLE_DASH);

	
	SetIndexBuffer(0, PBuffer);
	SetIndexBuffer(1, S1Buffer);
	SetIndexBuffer(2, S2Buffer);
	SetIndexBuffer(3, S3Buffer);
	SetIndexBuffer(4, R1Buffer);
	SetIndexBuffer(5, R2Buffer);
	SetIndexBuffer(6, R3Buffer);
//	SetIndexBuffer(1, DN1Buffer);
//	SetIndexBuffer(2, UP1Buffer);
	
// Indicator name
	 short_name="WeeklyPivotPoint";
	 short_name1="WeeklySupport1";
	 short_name2="WeeklySupport2";
	 short_name3="WeeklySupport3";
	 short_name4="WeeklyResistance1";
	 short_name5="WeeklyResistance2";
	 short_name6="WeeklyResistance3";
	 
	 
//	 short_name2="WeeklyDownRange";
//	 short_name3="WeeklyUpRange";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
   SetIndexLabel(1,short_name1);
   SetIndexLabel(2,short_name2);
   SetIndexLabel(3,short_name3);
   SetIndexLabel(4,short_name4);
   SetIndexLabel(5,short_name5);
   SetIndexLabel(6,short_name6);
	
	//----
	SetIndexDrawBegin(0,1);
	IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
	
	if (LineTEXT == true)
	{
	ObjectCreate("Pivot", OBJ_TEXT, 0, 0, 0);
	ObjectSetText("Pivot", "                                         PIVOT", 7, "Arial",Silver);
	
	ObjectCreate("Support1", OBJ_TEXT, 0, 0, 0);
	ObjectSetText("Support1", "                                         Support1", 7, "Arial",Silver);

	ObjectCreate("Support2", OBJ_TEXT, 0, 0, 0);
	ObjectSetText("Support2", "                                         Support2", 7, "Arial",Silver);
	

	ObjectCreate("Support3", OBJ_TEXT, 0, 0, 0);
	ObjectSetText("Support3", "                                         Support3", 7, "Arial",Silver);


	ObjectCreate("Resistance1", OBJ_TEXT, 0, 0, 0);
	ObjectSetText("Resistance1", "                                         Resistance1", 7, "Arial",Silver);



	ObjectCreate("Resistance2", OBJ_TEXT, 0, 0, 0);
	ObjectSetText("Resistance2", "                                         Resistance2", 7, "Arial",Silver);


	ObjectCreate("Resistance3", OBJ_TEXT, 0, 0, 0);
	ObjectSetText("Resistance3", "                                         Resistance3", 7, "Arial",Silver);

	
/*	ObjectCreate("Range_DN", OBJ_TEXT, 0, 0, 0);
	ObjectSetText("Range_DN", "                                     RANGE DOWN", 7, "Arial", Silver);
	ObjectCreate("Range_UP", OBJ_TEXT, 0, 0, 0);
	ObjectSetText("Range_UP", "                                     RANGE UP", 7, "Arial", Silver);
*/	
   }
	//----
	return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
	int counted_bars = IndicatorCounted();
	int limit, i;


	if(counted_bars == 0)
	{
		x = Period();
		if(x > 240) 
			return(-1);

	}
	if(counted_bars < 0) 
		return(-1);
	
	limit = (Bars - counted_bars) - 1;
	//----
	for(i = limit; i >= 0; i--)
	{ 
		if(High[i+1] > LastHigh) 
			LastHigh = High[i+1];
		//----
		if(Low[i+1] < LastLow) 
			LastLow=Low[i+1];
			if ( 1 == TimeDayOfWeek(Time[i]) && 1 != TimeDayOfWeek(Time[i+1]) )
		{
		   PBuffer[i+1] = EMPTY_VALUE;
		   S1Buffer[i+1] = EMPTY_VALUE;
		   S2Buffer[i+1] = EMPTY_VALUE;
		   S3Buffer[i+1] = EMPTY_VALUE;
		   R1Buffer[i+1] = EMPTY_VALUE;
		   R2Buffer[i+1] = EMPTY_VALUE;
		   R3Buffer[i+1] = EMPTY_VALUE;
		   
//		   DN1Buffer[i+1] = EMPTY_VALUE;
//		   UP1Buffer[i+1] = EMPTY_VALUE;
		   
			RANGE = (LastHigh - LastLow)/2;
			P = (LastHigh + LastLow + Close[i+1]) / 3; 
//			UP = (P + RANGE); //deleted by ziv
//			DN = (P - RANGE); //deleted by ziv 
		
S1 = 2*P -  LastHigh;		
S2 =   P - (LastHigh - LastLow);
S3 = 2*P - (LastHigh - LastLow);
R1 = 2*P -  LastLow;//R1 = 2*P +  LastLow;
R2 =   P + (LastHigh - LastLow);
R3 = 2*P + (LastHigh - LastLow);
/*==added by ziv==
R1 = 2*PP -  yesterday_low;
         S1 = 2*PP -  yesterday_high;
         R2 =   PP + (yesterday_high - yesterday_low);
         S2 =   PP - (yesterday_high - yesterday_low);
         R3 = 2*PP + (yesterday_high - 2*yesterday_low);
         S3 = 2*PP - (2*yesterday_high - yesterday_low);
         //R2 = PP - S1 + R1;
         //S2 = PP - R1 + S1;

*/

			
		
			LastLow = Open[i]; 
			LastHigh = Open[i];
			
         ObjectMove("Pivot", 0, Time[i-0], P);
         ObjectMove("Support1", 0, Time[i-0], S1);
         ObjectMove("Support2", 0, Time[i-0], S2);
         ObjectMove("Support3", 0, Time[i-0], S3);
         ObjectMove("Resistance1", 0, Time[i-0], R1);
         ObjectMove("Resistance2", 0, Time[i-0], R2);
         ObjectMove("Resistance3", 0, Time[i-0], R3);
//         ObjectMove("Range_DN", 0, Time[i-0], DN);
//         ObjectMove("Range_UP", 0, Time[i-0], UP);
        
		}
		PBuffer[i] = P;
		S1Buffer[i] = S1;
		S2Buffer[i] = S2;
		S3Buffer[i] = S3;
		R1Buffer[i] = R1;
		R2Buffer[i] = R2;
		R3Buffer[i] = R3;
//		DN1Buffer[i] = DN;
//		UP1Buffer[i] = UP;
	
	}

	//----
	return(0);

}
//+------------------------------------------------------------------+

