//+------------------------------------------------------------------+
//|                                                  WeeklyPivot.mq4 |
//|                                                                  |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 Magenta
#property indicator_color2 RoyalBlue
#property indicator_color3 RoyalBlue
#property indicator_color4 Crimson
#property indicator_color5 Crimson
#property indicator_color6 SeaGreen
#property indicator_color7 SeaGreen

extern int Line_Width=2;
extern bool AlertsOn = true;
extern bool PivotLineCrossAlert = true;
extern bool R1LineCrossAlert = true;
extern bool R2LineCrossAlert = true;
extern bool R3LineCrossAlert = true;
extern bool S1LineCrossAlert = true;
extern bool S2LineCrossAlert = true;
extern bool S3LineCrossAlert = true;
//---- input parameters

//---- buffers
double PBuffer[];
double S1Buffer[];
double R1Buffer[];
double S2Buffer[];
double R2Buffer[];
double S3Buffer[];
double R3Buffer[];
string Pivot="MonthlyPivotPoint",Sup1="M_S 1", Res1="M_R 1";
string Sup2="M_S 2", Res2="M_R 2", Sup3="M_S 3", Res3="M_R 3";
int fontsize=10;
double P,S1,R1,S2,R2,S3,R3;
double last_month_high, last_month_low, this_month_open, last_month_close;
double prevtime=0;

//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here

   ObjectDelete("MPivot");
   ObjectDelete("MSup1");
   ObjectDelete("MRes1");
   ObjectDelete("MSup2");
   ObjectDelete("MRes2");
   ObjectDelete("MSup3");
   ObjectDelete("MRes3");   

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;


//---- indicator line
   SetIndexLabel(0,"Monthly Pivot");
   SetIndexStyle(0,DRAW_LINE,0,Line_Width,Magenta);
   SetIndexLabel(1,"Monthly Support 1");
   SetIndexStyle(1,DRAW_LINE,0,Line_Width,PowderBlue);
   SetIndexLabel(2,"Monthly Resistance 1");
   SetIndexStyle(2,DRAW_LINE,0,Line_Width,Coral);
   SetIndexLabel(3,"Monthly Support 2");
   SetIndexStyle(3,DRAW_LINE,0,Line_Width,Blue);
   SetIndexLabel(4,"Monthly Resistance 2");
   SetIndexStyle(4,DRAW_LINE,0,Line_Width,Crimson);
   SetIndexLabel(5,"Monthly Support 3");
   SetIndexStyle(5,DRAW_LINE,0,Line_Width,Navy);
      SetIndexLabel(6,"Monthly Resistance 3");
   SetIndexStyle(6,DRAW_LINE,0,Line_Width,FireBrick);
   SetIndexBuffer(0,PBuffer);
   SetIndexBuffer(1,S1Buffer);
   SetIndexBuffer(2,R1Buffer);
   SetIndexBuffer(3,S2Buffer);
   SetIndexBuffer(4,R2Buffer);
   SetIndexBuffer(5,S3Buffer);
   SetIndexBuffer(6,R3Buffer);


//---- name for DataWindow and indicator subwindow label
   short_name="MonthlyPivotPoint";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);

//----
   SetIndexDrawBegin(0,1);
//----
 

//----
   return(0);
  }
void AlertCrosses()
{
 /*  
   extern bool PivotLineCrossAlert = true;
   extern bool R1LineCrossAlert = true;
   extern bool R2LineCrossAlert = true;
   extern bool R3LineCrossAlert = true;
   extern bool S1LineCrossAlert = true;
   extern bool S2LineCrossAlert = true;
   extern bool S3LineCrossAlert = true;

      buf0[i] = p;
      buf1[i] = r1;
      buf2[i] = r2;
      buf3[i] = r3;
      buf4[i] = s1;
      buf5[i] = s2;
      buf6[i] = s3;
*/

   if(PivotLineCrossAlert && Low[0] < P && High[0] > P) 
   AlertOnce("Pivot Tocuh - Price has tocuhed the Pivot line  @ "+Symbol()+"-"+PeriodToText(),0);

   if(R1LineCrossAlert && Low[0] < R1 && High[0] > R1) 
   AlertOnce("R1 Tocuh - Price has tocuhed the R1 line  @ "+Symbol()+"-"+PeriodToText(),1);
   
   if(R2LineCrossAlert && Low[0] < R2 && High[0] > R2) 
   AlertOnce("R2 Tocuh - Price has tocuhed the R2 line  @ "+Symbol()+"-"+PeriodToText(),2);
   
   if(R3LineCrossAlert && Low[0] < R3 && High[0] > R3) 
   AlertOnce("R3 Tocuh - Price has tocuhed the R3 line  @ "+Symbol()+"-"+PeriodToText(),3);
   
   if(S1LineCrossAlert && Low[0] < S1 && High[0] > S1) 
   AlertOnce("S1 Tocuh - Price has tocuhed the S1 line  @ "+Symbol()+"-"+PeriodToText(),4);
   
   if(S2LineCrossAlert && Low[0] < S2 && High[0] > S2) 
   AlertOnce("S2 Tocuh - Price has tocuhed the S2 line  @ "+Symbol()+"-"+PeriodToText(),5);
   
   if(S3LineCrossAlert && Low[0] < S3 && High[0] > S3) 
   AlertOnce("S3 Tocuh - Price has tocuhed the S3 line  @ "+Symbol()+"-"+PeriodToText(),6);
}
bool AlertOnce(string alert_msg, int ref)
{  
   static int LastAlert[10];
   
   if( LastAlert[ref] == 0 || LastAlert[ref] < Bars)
   {
      Alert(alert_msg);
      LastAlert[ref] = Bars;
      return (true);
   }
   return(true);
}  
string PeriodToText()
{
   switch (Period())
   {
      case 1:
            return("M1");
            break;
      case 5:
            return("M5");
            break;
      case 15:
            return("M15");
            break;
      case 30:
            return("M30");
            break;
      case 60:
            return("H1");
            break;
      case 240:
            return("H4");
            break;
      case 1440:
            return("D1");
            break;
      case 10080:
            return("W1");
            break;
      case 43200:
            return("MN1");
            break;
   }
  return("");
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
DrawPivots();
if (AlertsOn) AlertCrosses();
   
   return(0);
  }
//+------------------------------------------------------------------+
int DrawPivots()

  {
   int counted_bars=IndicatorCounted();
if(Period()>PERIOD_D1)
{
return(0);
}
   int limit, i;
//---- indicator calculation
if (counted_bars==0)
{
if(1==0)
{
   ObjectCreate("MonthlyPivot", OBJ_TEXT, 0, 0,0);
   ObjectSetText("MonthlyPivot", "                                                 Monthly Pivot",fontsize,"Arial",Red);
   ObjectCreate("MSup1", OBJ_TEXT, 0, 0, 0);
   ObjectSetText("MSup1", "                                    MS 1",fontsize,"Arial",Red);
   ObjectCreate("MRes1", OBJ_TEXT, 0, 0, 0);
   ObjectSetText("MRes1", "                                    MR 1",fontsize,"Arial",Red);
   ObjectCreate("MSup2", OBJ_TEXT, 0, 0, 0);
   ObjectSetText("MSup2", "                                    MS 2",fontsize,"Arial",Red);
   ObjectCreate("MRes2", OBJ_TEXT, 0, 0, 0);
   ObjectSetText("MRes2", "                                    MR 2",fontsize,"Arial",Red);
   ObjectCreate("MSup3", OBJ_TEXT, 0, 0, 0);
   ObjectSetText("MSup3", "                                    MS 3",fontsize,"Arial",Red);
   ObjectCreate("MRes3", OBJ_TEXT, 0, 0, 0);
   ObjectSetText("MRes3", "                                    MR 3",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_month_close=iClose(Symbol(),PERIOD_MN1,1);
this_month_open=iOpen(Symbol(),PERIOD_MN1,0);

		// WeeklyPivot
		P = (last_month_high + last_month_low + this_month_open + last_month_close) / 4;

   R1 = (2*P)-last_month_low;
   S1 = (2*P)-last_month_high;
   R2 = P+(last_month_high - last_month_low);
   S2 = P-(last_month_high - last_month_low);
   R3 = (2*P)+(last_month_high-(2*last_month_low));
   S3 = (2*P)-((2* last_month_high)-last_month_low); 
  
   last_month_low=iLow(Symbol(),PERIOD_MN1,1);
    last_month_high=iHigh(Symbol(),PERIOD_MN1,1);

	ObjectMove("MonthyPivot", 0, Time[i],P);
   ObjectMove("MSup1", 0, Time[i],S1);
   ObjectMove("MRes1", 0, Time[i],R1);
   ObjectMove("MSup2", 0, Time[i],S2);
   ObjectMove("MRes2", 0, Time[i],R2);
   ObjectMove("MSup3", 0, Time[i],S3);
   ObjectMove("MRes3", 0, Time[i],R3);

}   
    
    last_month_high = MathMax(last_month_high, High[i]);
 	 last_month_low = MathMin(last_month_low, Low[i]);   
 	   last_month_low=iLow(Symbol(),PERIOD_MN1,1);
    last_month_high=iHigh(Symbol(),PERIOD_MN1,1);
    PBuffer[i]=P;
    S1Buffer[i]=S1;
    R1Buffer[i]=R1;
    S2Buffer[i]=S2;
    R2Buffer[i]=R2;
    S3Buffer[i]=S3;
    R3Buffer[i]=R3;

}

//----
   return(0);
  }
