//+------------------------------------------------------------------+
//|                                                                  |
//|                         PivotsWeekly.mq4                          |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Traderathome - public domain code"
#property link      "None" 

/*-------------------------------------------------------------------+
PivotsWeekly: 

It provides for color and line style selection of pivot lines.  It 
provides for color, font size and font style selection of line labels, 
and the ability to move line labels left and right of the chart center.  
The chart center is automatically adjusted as you increase/decrease 
the chart density.  Included is the ability to independently turn 
on/off the price display in the line labels and the chart margin.
Pivot lines can be calculated using either the "Daily" pivot formula 
(default), or the "Fibonacci" formula which also has two added levels.  
Included are MidPivot lines which can be turned on/off.

The first item in the Indicator Window is the indicator "On" switch
with default of "true".  To turn the indicator display "Off" you do
not have to remove the indicator from your chart indicators list.
Instead, just edit the indicator to select "false" to turn it off.
Your indicator settings for that chart are preserved while "Off".
 
                                     - Traderathome, March 20, 2009
----------------------------------------------------------------------
v2: Altered code so all items displayed except for the Period 
Separators and Separator Labels go into the background relative to 
other charting items.  This prevents the annoying breakup of other 
chart study lines.
                                     - Traderathome, April 11, 2009                                     
---------------------------------------------------------------------*/

#property  indicator_chart_window

extern bool   Indicator_On?                 = true;
extern int    Weekly_or_Fibonacci_Pivots_12  = 2;
extern bool   Margin_Prices?                = false;
extern bool   Line_Prices?                  = true;
extern int    Move_Labels_PerCent_Left      = 0;
extern int    Labels_Fontsize               = 10; 
extern int    Labels_Fontstyle_123          = 1;
extern color  Labels_color                  = Gray;
extern color  Resistances                   = Maroon;
extern color  CentralPivot                  = Blue;
extern color  Supports                      = DarkGreen;
extern color  MidPivots                     = C'85,85,0';
extern bool   Show_MidPivots?               = true;
extern int    Pivots_LineStyle_01234        = 2;     
extern int    SolidLine_Thickness_01234     = 1;



//+-------------------------------------------------------------------+
//| Custom indicator initialization function                          |
//+-------------------------------------------------------------------+
int init()
   {
   return(0);
   }
     
//+-------------------------------------------------------------------+
//| Custom indicator deinitialization function                        |
//+-------------------------------------------------------------------+  
int deinit()
   {    
   int obj_total= ObjectsTotal();  
   for (int k= obj_total; k>=0; k--) 
      {
      string name= ObjectName(k);    
      if (StringSubstr(name,0,14)=="[PivotsWeekly]") { ObjectDelete(name); }
      }   
   return(0);
   }
   
//+-------------------------------------------------------------------+
//| Custom indicator start function                               |
//+-------------------------------------------------------------------+   
int start()
   {
   if (Indicator_On? == false){return(0);}
   int CalcPeriod;
	string pLineStr,pStr,p,s1Str,S1LineStr,s1,r1Str,R1LineStr,r1;
	string s2Str,S2LineStr,s2,r2Str,R2LineStr,r2; 
	string s3Str,S3LineStr,s3,r3Str,R3LineStr,r3;
	string s4Str,S4LineStr,s4,r4Str,R4LineStr,r4;
	string s5Str,S5LineStr,s5,r5Str,R5LineStr,r5;
	string mrLineStr;

      CalcPeriod = 10080;
      if( Weekly_or_Fibonacci_Pivots_12  == 1)
         { 
         pLineStr  = "WPV ";
         S1LineStr = "WS1 ";
         R1LineStr = "WR1 ";
         S2LineStr = "WS2 ";
         R2LineStr = "WR2 ";
         S3LineStr = "WS3 ";
         R3LineStr = "WR3 ";
         mrLineStr = "Wm";
         }        
      if( Weekly_or_Fibonacci_Pivots_12  == 2)   
         {
         pLineStr  = "WFPV ";
         S1LineStr = "WFS1 ";
         R1LineStr = "FR1 ";
         S2LineStr = "WFS2 ";
         R2LineStr = "WFR2 ";
         S3LineStr = "WFS3 ";
         R3LineStr = "WFR3 ";
         S4LineStr = "WFS4 ";
         R4LineStr = "WFR4 ";
         S5LineStr = "WFS5 ";
         R5LineStr = "WFR5 ";
         mrLineStr = "Wm";
         }
         
   int shift,dow; 
	double HiPrice,LoPrice,Range,ClosePrice,Pivot,R1,S1,R2,S2,R3,S3,R4,S4,R5,S5;
   shift	= iBarShift(NULL,CalcPeriod,Time[0])+ 1;	// default = one period ago	 
	datetime StartTime	= iTime(NULL,CalcPeriod,shift);	
	   switch(dow)
	   {
	   case 5:	 
         //dowStr = "Sunday  ";
         break;
      case 0:
         shift	= iBarShift(NULL,CalcPeriod,Time[0])+2;	// two periods ago or Friday value	 
         //dowStr = "Monday  ";     
         break;
      case 1:
         //dowStr = "Tuesday  ";
         break;
      case 2:
         //dowStr = "Wednesday  ";
         break;
      case 3:
         //dowStr = "Thursday  ";
         break;
      case 4:
         //dowStr = "Friday  "; 
         break; 
      } 	 
  
  
 	ClosePrice  = NormalizeDouble(iClose(NULL,CalcPeriod,shift),4);
   HiPrice     = NormalizeDouble (iHigh(NULL,CalcPeriod,shift),4); 
   LoPrice     = NormalizeDouble (iLow(NULL,CalcPeriod,shift),4);
   Range       = HiPrice -  LoPrice;
   Pivot       = NormalizeDouble((HiPrice+LoPrice+ClosePrice)/3,4);
      
   if( Weekly_or_Fibonacci_Pivots_12  == 2)
      {
      R1 = Pivot + (Range  * 0.382);
      S1 = Pivot - (Range  * 0.382); 
      R2 = Pivot + (Range  * 0.618);
      S2 = Pivot - (Range  * 0.618);
      R3 = Pivot +  Range ;  
      S3 = Pivot -  Range ;
      R4 = Pivot + (Range  * 1.618);
      S4 = Pivot - (Range  * 1.618);
      R5 = Pivot + (Range  * 2.618);
      S5 = Pivot - (Range  * 2.618);
      }
   else
      {   
      R1 = (2*Pivot)-LoPrice;
      S1 = (2*Pivot)-HiPrice;
      R2 = Pivot+(R1-S1);
      S2 = Pivot-(R1-S1); 
      R3 = ( 2.0 * Pivot) + ( HiPrice - ( 2.0 * LoPrice ) );
      S3 = ( 2.0 * Pivot) - ( ( 2.0 * HiPrice ) - LoPrice );
      }

   drawLine("R5", R5, Resistances); 
   drawLabel( R5LineStr,R5,Labels_color);
   drawLine("R4", R4, Resistances);
   drawLabel(R4LineStr,R4,Labels_color);
   drawLine("R3", R3, Resistances); 
   drawLabel( R3LineStr,R3,Labels_color);
   drawLine("R2", R2, Resistances);
   drawLabel(R2LineStr,R2,Labels_color);
   drawLine("R1", R1, Resistances);
   drawLabel(R1LineStr,R1,Labels_color);
   drawLine("PIVOT", Pivot, CentralPivot);
   drawLabel(pLineStr, Pivot,Labels_color);
   drawLine("S1", S1, Supports);
   drawLabel(S1LineStr,S1,Labels_color);
   drawLine("S2", S2, Supports);
   drawLabel(S2LineStr,S2,Labels_color);
   drawLine("S3", S3, Supports);
   drawLabel(S3LineStr ,S3,Labels_color);
   drawLine("S4", S4, Supports);
   drawLabel(S4LineStr,S4,Labels_color);
   drawLine("S5", S5, Supports);
   drawLabel(S5LineStr ,S5,Labels_color);

   if(Show_MidPivots?)
      {
      drawLine("MR5", (R4+R5)/2, MidPivots); 
      drawLabel(mrLineStr+"R5",(R4+R5)/2,Labels_color);
      drawLine("MR4", (R3+R4)/2, MidPivots); 
      drawLabel(mrLineStr+"R4",(R3+R4)/2,Labels_color);
      drawLine("MR3", (R2+R3)/2, MidPivots); 
      drawLabel(mrLineStr+"R3",(R2+R3)/2,Labels_color);
      drawLine("MR2", (R1+R2)/2, MidPivots); 
      drawLabel(mrLineStr+"R2",(R1+R2)/2,Labels_color); 
      drawLine("MR1", (Pivot+R1)/2, MidPivots); 
      drawLabel(mrLineStr+"R1",(Pivot+R1)/2,Labels_color);
      drawLine("MS1", (Pivot+S1)/2, MidPivots); 
      drawLabel(mrLineStr+"S1",(Pivot+S1)/2,Labels_color);
      drawLine("MS2", (S1+S2)/2, MidPivots); 
      drawLabel(mrLineStr+"S2",(S1+S2)/2,Labels_color);
      drawLine("MS3", (S2+S3)/2, MidPivots); 
      drawLabel(mrLineStr+"S3",(S2+S3)/2,Labels_color);
      drawLine("MS4", (S3+S4)/2, MidPivots); 
      drawLabel(mrLineStr+"S4",(S3+S4)/2,Labels_color);
      drawLine("MS5", (S4+S5)/2, MidPivots); 
      drawLabel(mrLineStr+"S5",(S4+S5)/2,Labels_color);
      }
   
       
   return(0);
   } 

//+-------------------------------------------------------------------+
//| Subroutine to draw line labels                                    |                                                        
//+-------------------------------------------------------------------+
void drawLabel(string text, double level, color Color)
    {
    if (Move_Labels_PerCent_Left < 0)   {Move_Labels_PerCent_Left =0;}
    if (Move_Labels_PerCent_Left > 100) {Move_Labels_PerCent_Left =100;}
    int A = WindowFirstVisibleBar();
    int L = A*(Move_Labels_PerCent_Left)/100;
    if (L > A - (A/60) ) {L = A - (A/60);} //stop label from disappearing on left
    if (Time[L] == 0 ) {L = 0;}            //stop label from disappearing on right

    string Font;
    string name = "[PivotsWeekly] " + text + " Label"; 
    if (Line_Prices? == true) {text= text + ":  "+DoubleToStr(level, Digits);}
    
    
    if (Labels_Fontsize >12){Labels_Fontsize = 12;}
    string space = "                                          ";
    if (Move_Labels_PerCent_Left == 0){text = space + text;}
    
    if(ObjectFind(name) != 0)
       {
       if (Labels_Fontstyle_123  <= 1){Font = "Arial";}
       if (Labels_Fontstyle_123  == 2){Font = "Arial Bold";}
       if (Labels_Fontstyle_123  >= 3){Font = "Arial Black";}
       ObjectCreate(name, OBJ_TEXT, 0, Time[L], level);       
       ObjectSetText(name, text, Labels_Fontsize, Font, Color);
       ObjectSet(name, OBJPROP_BACK, true);
       }
       else
       {
       ObjectMove(name, 0, Time[L], level);
       }            
    }

//+-------------------------------------------------------------------+
//| Sub-routing to draw lines                                         |                                                        
//+-------------------------------------------------------------------+
void drawLine(string text, double level, color Color)
    {
    int AA = WindowFirstVisibleBar(); 
    int a = Pivots_LineStyle_01234;
    int b = SolidLine_Thickness_01234;
    int c =1; if (a==0)c=b;

    string name= "[PivotsWeekly] " + text + " Line";
    int Z= OBJ_TREND;
    if(  Margin_Prices? == true){Z = OBJ_HLINE;}   
    if(ObjectFind(name) != 0)
       {
       ObjectCreate(name, Z, 0, AA, level, Time[0], level);                            
       ObjectSet(name, OBJPROP_STYLE, a);              
       ObjectSet(name, OBJPROP_WIDTH, c); 
       ObjectSet(name, OBJPROP_COLOR, Color);
       ObjectSet(name, OBJPROP_BACK, true);                 
       }
       else
       {      
       ObjectMove  (name, 1, Time[0],level);
       ObjectMove  (name, 0, AA, level);       
       }   
    }

//+----------------------- End of Program ----------------------------+