//+------------------------------------------------------------------+
//|                                                  WeeklyPivot.mq4 |
//|                                                                  |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 Blue
#property indicator_color2 Black
#property indicator_color3 Black
#property indicator_color4 Black
#property indicator_color5 Black
#property indicator_color6 Black
#property indicator_color7 Black
//---- input parameters

//---- buffers
double PBuffer[];

string Pivot="WeeklyPivotPoint";

int fontsize=6;
double P;
double last_week_high, last_week_low, this_week_open, last_week_close;

//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here

   ObjectDelete("WeeklyPivot");
  

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;


//---- indicator line
   SetIndexStyle(0,DRAW_LINE,0,1,Blue);
   SetIndexBuffer(0,PBuffer);
   


//---- name for DataWindow and indicator subwindow label
   short_name="WeeklyPivotPoint";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);

//----
   SetIndexDrawBegin(0,1);
//----
 

//----
   return(0);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()

  {
   int counted_bars=IndicatorCounted();

   int limit, i;
//---- indicator calculation
if (counted_bars==0)
{

   ObjectCreate("WeeklyPivot", OBJ_TEXT, 0, 0,0);
   ObjectSetText("WeeklyPivot", "                            Weekly Pivot Point",fontsize,"Arial",Red);
   
}
   if(counted_bars<0) return(-1);

   limit=(Bars-counted_bars)-1;


for (i=limit; i>=0;i--)
{ 


   // Monday
	if ( 1 == TimeDayOfWeek(Time[i]) && 1 != TimeDayOfWeek(Time[i+1]) )
	{
		last_week_close = Close[i+1];
		this_week_open = Open[i];

		// WeeklyPivot
		P = (last_week_high + last_week_low + this_week_open + last_week_close) / 4;

  
  
   last_week_low=Low[i]; last_week_high=High[i];

	ObjectMove("WeeklyPivot", 0, Time[i],P);
   

}   
    
    last_week_high = MathMax(last_week_high, High[i]);
 	 last_week_low = MathMin(last_week_low, Low[i]);   
    PBuffer[i]=P;
    

}

//----
   return(0);
  }
//+------------------------------------------------------------------+