//+------------------------------------------------------------------+
//|                                                        Pivot.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
extern ENUM_TIMEFRAMES TimeFrame       =PERIOD_W1;
extern string          Label_Font      ="Arial";
extern int             Label_Font_Size =10;
extern int             Label_Location  =20;
extern color           R_238_2_Color   =Yellow;
extern color           R_223_6_Color   =Yellow;
extern color           R_200_0_Color   =Aqua;
extern color           R_138_2_Color   =Yellow;
extern color           R_123_6_Color   =Yellow;
extern color           R_100_0_Color   =Aqua;
extern color           R_61_8_Color    =Red;
extern color           R_38_2_Color    =Magenta;
extern color           PIVOT_Color     =Yellow;
extern color           S_38_2_Color    =Magenta;
extern color           S_61_8_Color    =Lime;
extern color           S_100_0_Color   =Aqua;
extern color           S_123_6_Color   =Yellow;
extern color           S_138_2_Color   =Yellow;
extern color           S_200_0_Color   =Aqua;
extern color           S_223_6_Color   =Yellow;
extern color           S_238_2_Color   =Yellow;

double LastWeek_high = 0;
double LastWeek_low  = 0;
double LastWeek_close= 0;

double P=0;
double Q=0;
double H8,H7,H6,H5,H4,H3,H2,H1,L1,L2,L3,L4,L5,L6,L7,L8;
double nQ=0;
double nD=0;
double D=0;
double rates_d1[2][6];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   H8=0; H7=0; H6=0; H5=0; H4=0; H3=0; H2=0; H1=0; L1=0; L2=0; L3=0; L4=0; L5=0; L6=0; L7=0; L8=0;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   ObjectDelete("WP Label");
   ObjectDelete("WP Line");
   ObjectDelete("H8 Label");
   ObjectDelete("H8 Line");
   ObjectDelete("H7 Label");
   ObjectDelete("H7 Line");
   ObjectDelete("H6 Label");
   ObjectDelete("H6 Line");
   ObjectDelete("H5 Label");
   ObjectDelete("H5 Line");
   ObjectDelete("H4 Label");
   ObjectDelete("H4 Line");
   ObjectDelete("H3 Label");
   ObjectDelete("H3 Line");
   ObjectDelete("H2 Label");
   ObjectDelete("H2 Line");
   ObjectDelete("H1 Label");
   ObjectDelete("H1 Line");
   ObjectDelete("L1 Label");
   ObjectDelete("L1 Line");
   ObjectDelete("L2 Label");
   ObjectDelete("L2 Line");
   ObjectDelete("L3 Label");
   ObjectDelete("L3 Line");
   ObjectDelete("L4 Label");
   ObjectDelete("L4 Line");
   ObjectDelete("L5 Label");
   ObjectDelete("L5 Line");
   ObjectDelete("L6 Label");
   ObjectDelete("L6 Line");
   ObjectDelete("L7 Label");
   ObjectDelete("L7 Line");
   ObjectDelete("L8 Label");
   ObjectDelete("L8 Line");

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//---- TODO: add your code here
//---- exit if period is greater than daily charts
   if(Period()>1440)
     {
      Print("Error - Chart period is greater than 1 day.");
      return(-1); // then exit
     }
//---- Get new daily prices
   ArrayCopyRates(rates_d1,Symbol(),TimeFrame);

   LastWeek_close = rates_d1[1][4];
   LastWeek_high  = rates_d1[1][3];
   LastWeek_low   = rates_d1[1][2];
//---- Calculate Pivots
   Q = (LastWeek_high - LastWeek_low);
   P = (LastWeek_high + LastWeek_low + LastWeek_close) / 3;

   H8 = (Q*2.382)+P;
   H7 = (Q*2.236)+P;
   H6 = (Q*2.00)+P;
   H5 = (Q*1.382)+P;
   H4 = (Q*1.236)+P;
   H3 = (Q*1.00)+P;
   H2 = (Q*0.618)+P;
   H1 = (Q*0.382)+P;

   L1 = P-(Q*0.382);
   L2 = P-(Q*0.618);
   L3 = P-(Q*1.00);
   L4 = P-(Q*1.236);
   L5 = P-(Q*1.382);
   L6 = P-(Q*2.00);
   L7 = P-(Q*2.236);
   L8 = P-(Q*2.382);

   if(Q>5)
     {
      nQ=Q;
     }
   else
     {
      nQ=Q*10000;
     }
   if(D>5)
     {
      nD=D;
     }
   else
     {
      nD=D*10000;
     }
   Comment("PRV High= ",LastWeek_high,"    PRV Range= ",nQ,"\nPRV Low= ",LastWeek_low,"     PRV Close= ",LastWeek_close);

//---- Set Line Labels on chart window
//---- Pivot Lines
   if(ObjectFind("WP Label")!=0)
     {
      ObjectCreate("WP Label",OBJ_TEXT,0,Time[10],P);
      ObjectSetText("WP Label","PIVOT",Label_Font_Size,Label_Font,PIVOT_Color);
     }
   else
     {
      ObjectMove("WP Label",0,Time[Label_Location],P);
     }
//---  Draw  Pivot Lines on chart
   if(ObjectFind("WP Line")!=0)
     {
      ObjectCreate("WP Line",OBJ_HLINE,0,Time[40],P);
      ObjectSet("WP Line",OBJPROP_STYLE,STYLE_DASHDOTDOT);
      ObjectSet("WP Line",OBJPROP_WIDTH,0);
      ObjectSet("WP Line",OBJPROP_COLOR,PIVOT_Color);
     }
   else
     {
      ObjectMove("WP Line",0,Time[Label_Location],P);
     }
//---------------------------------------------------------------------------------
   if(ObjectFind("H8 Label")!=0)
     {
      ObjectCreate("H8 Label",OBJ_TEXT,0,Time[10],H8);
      ObjectSetText("H8 Label","R 238.2 %",Label_Font_Size,Label_Font,R_238_2_Color);
     }
   else
     {
      ObjectMove("H8 Label",0,Time[Label_Location],H8);
     }
//---------------------------------------------------------------------------------     
   if(ObjectFind("H7 Label")!=0)
     {
      ObjectCreate("H7 Label",OBJ_TEXT,0,Time[10],H7);
      ObjectSetText("H7 Label","R 223.6 %",Label_Font_Size,Label_Font,R_223_6_Color);
     }
   else
     {
      ObjectMove("H7 Label",0,Time[Label_Location],H7);
     }
//---------------------------------------------------------------------------------     
   if(ObjectFind("H6 Label")!=0)
     {
      ObjectCreate("H6 Label",OBJ_TEXT,0,Time[10],H6);
      ObjectSetText("H6 Label","R 200.0 %",Label_Font_Size,Label_Font,R_200_0_Color);
     }
   else
     {
      ObjectMove("H6 Label",0,Time[Label_Location],H6);
     }
//---------------------------------------------------------------------------------
   if(ObjectFind("H5 Label")!=0)
     {
      ObjectCreate("H5 Label",OBJ_TEXT,0,Time[10],H5);
      ObjectSetText("H5 Label","R 138.2 %",Label_Font_Size,Label_Font,R_138_2_Color);
     }
   else
     {
      ObjectMove("H5 Label",0,Time[Label_Location],H5);
     }
//---------------------------------------------------------------------------------
   if(ObjectFind("H4 Label")!=0)
     {
      ObjectCreate("H4 Label",OBJ_TEXT,0,Time[10],H4);
      ObjectSetText("H4 Label","R 123.6 %",Label_Font_Size,Label_Font,R_123_6_Color);
     }
   else
     {
      ObjectMove("H4 Label",0,Time[Label_Location],H4);
     }
//---------------------------------------------------------------------------------     
   if(ObjectFind("H3 Label")!=0)
     {
      ObjectCreate("H3 Label",OBJ_TEXT,0,Time[10],H3);
      ObjectSetText("H3 Label","R 100.0 %",Label_Font_Size,Label_Font,R_100_0_Color);
     }
   else
     {
      ObjectMove("H3 Label",0,Time[Label_Location],H3);
     }
//---------------------------------------------------------------------------------     
   if(ObjectFind("H2 Label")!=0)
     {
      ObjectCreate("H2 Label",OBJ_TEXT,0,Time[10],H2);
      ObjectSetText("H2 Label","R 61.8 %",Label_Font_Size,Label_Font,R_61_8_Color);
     }
   else
     {
      ObjectMove("H2 Label",0,Time[Label_Location],H2);
     }
//---------------------------------------------------------------------------------     
   if(ObjectFind("H1 Label")!=0)
     {
      ObjectCreate("H1 Label",OBJ_TEXT,0,Time[10],H1);
      ObjectSetText("H1 Label","R 38.2 %",Label_Font_Size,Label_Font,R_38_2_Color);
     }
   else
     {
      ObjectMove("H1 Label",0,Time[Label_Location],H1);
     }
//---------------------------------------------------------------------------------     
//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------     
   if(ObjectFind("L1 Label")!=0)
     {
      ObjectCreate("L1 Label",OBJ_TEXT,0,Time[10],L1);
      ObjectSetText("L1 Label","S 38.2 %",Label_Font_Size,Label_Font,S_38_2_Color);
     }
   else
     {
      ObjectMove("L1 Label",0,Time[Label_Location],L1);
     }
//---------------------------------------------------------------------------------     
   if(ObjectFind("L2 Label")!=0)
     {
      ObjectCreate("L2 Label",OBJ_TEXT,0,Time[10],L2);
      ObjectSetText("L2 Label","S 61.8 %",Label_Font_Size,Label_Font,S_61_8_Color);
     }
   else
     {
      ObjectMove("L2 Label",0,Time[Label_Location],L2);
     }
//---------------------------------------------------------------------------------
   if(ObjectFind("L3 Label")!=0)
     {
      ObjectCreate("L3 Label",OBJ_TEXT,0,Time[10],L3);
      ObjectSetText("L3 Label","S 100.0 %",Label_Font_Size,Label_Font,S_100_0_Color);
     }
   else
     {
      ObjectMove("L3 Label",0,Time[Label_Location],L3);
     }
//---------------------------------------------------------------------------------
   if(ObjectFind("L4 Label")!=0)
     {
      ObjectCreate("L4 Label",OBJ_TEXT,0,Time[10],L4);
      ObjectSetText("L4 Label","S 123.6 %",Label_Font_Size,Label_Font,S_123_6_Color);
     }
   else
     {
      ObjectMove("L4 Label",0,Time[Label_Location],L4);
     }
//---------------------------------------------------------------------------------
   if(ObjectFind("L5 Label")!=0)
     {
      ObjectCreate("L5 Label",OBJ_TEXT,0,Time[10],L5);
      ObjectSetText("L5 Label","S 138.2 %",Label_Font_Size,Label_Font,S_138_2_Color);
     }
   else
     {
      ObjectMove("L5 Label",0,Time[Label_Location],L5);
     }
//---------------------------------------------------------------------------------
   if(ObjectFind("L6 Label")!=0)
     {
      ObjectCreate("L6 Label",OBJ_TEXT,0,Time[10],L6);
      ObjectSetText("L6 Label","S 200.0 %",Label_Font_Size,Label_Font,S_200_0_Color);
     }
   else
     {
      ObjectMove("L6 Label",0,Time[Label_Location],L6);
     }
//---------------------------------------------------------------------------------
   if(ObjectFind("L7 Label")!=0)
     {
      ObjectCreate("L7 Label",OBJ_TEXT,0,Time[10],L7);
      ObjectSetText("L7 Label","S 223.6 %",Label_Font_Size,Label_Font,S_223_6_Color);
     }
   else
     {
      ObjectMove("L7 Label",0,Time[Label_Location],L7);
     }
//---------------------------------------------------------------------------------/
   if(ObjectFind("L8 Label")!=0)
     {
      ObjectCreate("L8 Label",OBJ_TEXT,0,Time[10],L8);
      ObjectSetText("L8 Label","S 238.2 %",Label_Font_Size,Label_Font,S_238_2_Color);
     }
   else
     {
      ObjectMove("L8 Label",0,Time[Label_Location],L8);
     }
//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
//---- Draw Camarilla Lines on Chart
   if(ObjectFind("H8 Line")!=0)
     {
      ObjectCreate("H8 Line",OBJ_HLINE,0,Time[40],H8);
      ObjectSet("H8 Line",OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet("H8 Line",OBJPROP_WIDTH,1);
      ObjectSet("H8 Line",OBJPROP_COLOR,R_238_2_Color);
     }
   else
     {
      ObjectMove("H8 Line",0,Time[40],H8);
     }
//---------------------------------------------------------------------------------
   if(ObjectFind("H7 Line")!=0)
     {
      ObjectCreate("H7 Line",OBJ_HLINE,0,Time[40],H7);
      ObjectSet("H7 Line",OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet("H7 Line",OBJPROP_WIDTH,1);
      ObjectSet("H7 Line",OBJPROP_COLOR,R_223_6_Color);
     }
   else
     {
      ObjectMove("H7 Line",0,Time[40],H7);
     }
//---------------------------------------------------------------------------------
   if(ObjectFind("H6 Line")!=0)
     {
      ObjectCreate("H6 Line",OBJ_HLINE,0,Time[40],H6);
      ObjectSet("H6 Line",OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet("H6 Line",OBJPROP_WIDTH,1);
      ObjectSet("H6 Line",OBJPROP_COLOR,R_200_0_Color);
     }
   else
     {
      ObjectMove("H6 Line",0,Time[40],H6);
     }
//---------------------------------------------------------------------------------
   if(ObjectFind("H5 Line")!=0)
     {
      ObjectCreate("H5 Line",OBJ_HLINE,0,Time[40],H5);
      ObjectSet("H5 Line",OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet("H5 Line",OBJPROP_WIDTH,1);
      ObjectSet("H5 Line",OBJPROP_COLOR,R_138_2_Color);
     }
   else
     {
      ObjectMove("H5 Line",0,Time[40],H5);
     }
//---------------------------------------------------------------------------------
   if(ObjectFind("H4 Line")!=0)
     {
      ObjectCreate("H4 Line",OBJ_HLINE,0,Time[40],H4);
      ObjectSet("H4 Line",OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet("H4 Line",OBJPROP_WIDTH,1);
      ObjectSet("H4 Line",OBJPROP_COLOR,R_123_6_Color);
     }
   else
     {
      ObjectMove("H4 Line",0,Time[40],H4);
     }
//---------------------------------------------------------------------------------
   if(ObjectFind("H3 Line")!=0)
     {
      ObjectCreate("H3 Line",OBJ_HLINE,0,Time[40],H3);
      ObjectSet("H3 Line",OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet("H3 Line",OBJPROP_WIDTH,1);
      ObjectSet("H3 Line",OBJPROP_COLOR,R_100_0_Color);
     }
   else
     {
      ObjectMove("H3 Line",0,Time[40],H3);
     }
//---------------------------------------------------------------------------------
   if(ObjectFind("H2 Line")!=0)
     {
      ObjectCreate("H2 Line",OBJ_HLINE,0,Time[40],H2);
      ObjectSet("H2 Line",OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet("H2 Line",OBJPROP_WIDTH,1);
      ObjectSet("H2 Line",OBJPROP_COLOR,R_61_8_Color);
     }
   else
     {
      ObjectMove("H2 Line",0,Time[40],H2);
     }
//---------------------------------------------------------------------------------
   if(ObjectFind("H1 Line")!=0)
     {
      ObjectCreate("H1 Line",OBJ_HLINE,0,Time[40],H1);
      ObjectSet("H1 Line",OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet("H1 Line",OBJPROP_WIDTH,1);
      ObjectSet("H1 Line",OBJPROP_COLOR,R_38_2_Color);
     }
   else
     {
      ObjectMove("H1 Line",0,Time[40],H1);
     }
//---------------------------------------------------------------------------------
   if(ObjectFind("L1 Line")!=0)
     {
      ObjectCreate("L1 Line",OBJ_HLINE,0,Time[40],L1);
      ObjectSet("L1 Line",OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet("L1 Line",OBJPROP_WIDTH,1);
      ObjectSet("L1 Line",OBJPROP_COLOR,S_38_2_Color);
     }
   else
     {
      ObjectMove("L1 Line",0,Time[40],L1);
     }
//---------------------------------------------------------------------------------
   if(ObjectFind("L2 Line")!=0)
     {
      ObjectCreate("L2 Line",OBJ_HLINE,0,Time[40],L2);
      ObjectSet("L2 Line",OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet("L2 Line",OBJPROP_WIDTH,1);
      ObjectSet("L2 Line",OBJPROP_COLOR,S_61_8_Color);
     }
   else
     {
      ObjectMove("L2 Line",0,Time[40],L2);
     }
//---------------------------------------------------------------------------------
   if(ObjectFind("L3 Line")!=0)
     {
      ObjectCreate("L3 Line",OBJ_HLINE,0,Time[40],L3);
      ObjectSet("L3 Line",OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet("L3 Line",OBJPROP_WIDTH,1);
      ObjectSet("L3 Line",OBJPROP_COLOR,S_100_0_Color);
     }
   else
     {
      ObjectMove("L3 Line",0,Time[40],L3);
     }
//---------------------------------------------------------------------------------
   if(ObjectFind("L4 Line")!=0)
     {
      ObjectCreate("L4 Line",OBJ_HLINE,0,Time[40],L4);
      ObjectSet("L4 Line",OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet("L4 Line",OBJPROP_WIDTH,1);
      ObjectSet("L4 Line",OBJPROP_COLOR,S_123_6_Color);
     }
   else
     {
      ObjectMove("L4 Line",0,Time[40],L4);
     }
//---------------------------------------------------------------------------------
   if(ObjectFind("L5 Line")!=0)
     {
      ObjectCreate("L5 Line",OBJ_HLINE,0,Time[40],L5);
      ObjectSet("L5 Line",OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet("L5 Line",OBJPROP_WIDTH,1);
      ObjectSet("L5 Line",OBJPROP_COLOR,S_138_2_Color);
     }
   else
     {
      ObjectMove("L5 Line",0,Time[40],L5);
     }
//---------------------------------------------------------------------------------
   if(ObjectFind("L6 Line")!=0)
     {
      ObjectCreate("L6 Line",OBJ_HLINE,0,Time[40],L6);
      ObjectSet("L6 Line",OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet("L6 Line",OBJPROP_WIDTH,1);
      ObjectSet("L6 Line",OBJPROP_COLOR,S_200_0_Color);
     }
   else
     {
      ObjectMove("L6 Line",0,Time[40],L6);
     }
//---------------------------------------------------------------------------------
   if(ObjectFind("L7 Line")!=0)
     {
      ObjectCreate("L7 Line",OBJ_HLINE,0,Time[40],L7);
      ObjectSet("L7 Line",OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet("L7 Line",OBJPROP_WIDTH,1);
      ObjectSet("L7 Line",OBJPROP_COLOR,S_223_6_Color);
     }
   else
     {
      ObjectMove("L7 Line",0,Time[40],L7);
     }
//---------------------------------------------------------------------------------
   if(ObjectFind("L8 Line")!=0)
     {
      ObjectCreate("L8 Line",OBJ_HLINE,0,Time[40],L8);
      ObjectSet("L8 Line",OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet("L8 Line",OBJPROP_WIDTH,1);
      ObjectSet("L8 Line",OBJPROP_COLOR,S_238_2_Color);
     }
   else
     {
      ObjectMove("L8 Line",0,Time[40],L8);
     }
//---------------------------------------------------------------------------------
//---- End Of Program
   return(0);
  }
//+------------------------------------------------------------------+
