//+------------------------------------------------------------------+
//|                                                     BucPivot.mq4 |
//|                                    Copyright © 2007, Petr Kouril |
//|                                           http://www.jaktrade.cz |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Petr Kouril"
#property link      "http://www.bucafon.cz"
// Modifyed by Andy West 2008
#define           REVISION                "1.2"

#property indicator_chart_window

#property indicator_buffers 7
#property indicator_color1 DarkGreen         
#property indicator_color2 LimeGreen
#property indicator_color3 LimeGreen       
#property indicator_color4 LimeGreen
#property indicator_color5 Red
#property indicator_color6 Red 
#property indicator_color7 Red  
#property indicator_width1 2
#property indicator_width2 1
#property indicator_width3 1
#property indicator_width4 1
#property indicator_width5 1
#property indicator_width6 1
#property indicator_width7 1
#property indicator_style1 0
#property indicator_style2 2
#property indicator_style3 2
#property indicator_style4 2
#property indicator_style5 2
#property indicator_style6 2
#property indicator_style7 2


extern string     Label1          = "****** 0 - Monday pivot use Sunday data! ******";
extern string     Label2          = "****** 1 - Monday pivot use Friday data! ******";
extern string     Label3          = "******  2 - Monday pivot use Friday + Sunday data! ******";
bool       ShowDayPivot   = false;
bool       ShowWeekPivot  = false;
bool       ShowMonthPivot = true;
extern bool Show_Price = false;
extern int        SundayData     = 2;     // 0 - Monday pivot use Sunday data
 string PerName;                          // 1 - Monday pivot use Friday data
                                          // 2 - Monday pivot use Friday + Sunday data

//---- buffers
double            PivotBuff[], R1Buff[], R2Buff[], R3Buff[], S1Buff[], S2Buff[], S3Buff[];
int               TimeFrame;

int init()
{
  
   
 	if (ShowDayPivot)
 	{
      PerName   = "D";
      TimeFrame = PERIOD_D1;
   }
   else if (ShowWeekPivot)
   {
      PerName   = "W";
      TimeFrame = PERIOD_W1;
   }
   else if (ShowMonthPivot)
   {
      PerName   = "M";
      TimeFrame = PERIOD_MN1;
   }
   
	IndicatorBuffers(7);

   SetIndexStyle (0, DRAW_LINE);
   SetIndexBuffer(0, PivotBuff);
   SetIndexLabel (0, PerName + " Pivot");

   SetIndexStyle (1, DRAW_LINE);
   SetIndexBuffer(1, R1Buff);
   SetIndexLabel (1, PerName + " R1");

   SetIndexStyle (2, DRAW_LINE);
   SetIndexBuffer(2, R2Buff);
   SetIndexLabel (2, PerName + " R2");

   SetIndexStyle (3, DRAW_LINE);
   SetIndexBuffer(3, R3Buff);
   SetIndexLabel (3, PerName + " R3");

   SetIndexStyle (4, DRAW_LINE);
   SetIndexBuffer(4, S1Buff);
   SetIndexLabel (4, PerName + " S1");

   SetIndexStyle (5, DRAW_LINE);
   SetIndexBuffer(5, S2Buff);
   SetIndexLabel (5, PerName + " S2");

   SetIndexStyle (6, DRAW_LINE);
   SetIndexBuffer(6, S3Buff);
   SetIndexLabel (6, PerName + " S3");
   
   return(0);
}

int deinit()
{
ObjectDelete(PerName+"txtPivot");
   ObjectDelete(PerName+"txtR1");
   ObjectDelete(PerName+"txtR2");
   ObjectDelete(PerName+"txtR3");
   ObjectDelete(PerName+"txtS1");
   ObjectDelete(PerName+"txtS2");
   ObjectDelete(PerName+"txtS3");
     ObjectDelete(PerName+"txtPivotF");
   ObjectDelete(PerName+"txtR1F");
   ObjectDelete(PerName+"txtR2F");
   ObjectDelete(PerName+"txtR3F");
   ObjectDelete(PerName+"txtS1F");
   ObjectDelete(PerName+"txtS2F");
   ObjectDelete(PerName+"txtS3F");
   
   return(0);
}

int start()
{
   int counted_bars = IndicatorCounted();
   if (counted_bars<0) return(-1);
   if (counted_bars>0) counted_bars--;
   int i, limit = Bars-counted_bars;

   double Cl, Hi, Lo;
   for (i = (limit-1); i >= 0; i--)
	{
	   GetCHL(i, Cl, Hi, Lo);
	   
      PivotBuff[i] = ((Hi + Lo + Cl)/3);

      R1Buff[i]    = (2 * PivotBuff[i]) - Lo;
      S1Buff[i]    = (2 * PivotBuff[i]) - Hi;

      R2Buff[i]    = PivotBuff[i] + (R1Buff[i] - S1Buff[i]);
      S2Buff[i]    = PivotBuff[i] - (R1Buff[i] - S1Buff[i]);

      S3Buff[i]    = (Lo - (2 * (Hi - PivotBuff[i])));
      R3Buff[i]    = (Hi + (2 * (PivotBuff[i] - Lo)));
     
       
       SetText(PerName+"txtPivot", PerName+"-P", Time[i], PivotBuff[i], indicator_color1);
       SetText(PerName+"txtR1", PerName+"-R1", Time[i], R1Buff[i], indicator_color2);
       SetText(PerName+"txtR2", PerName+"-R2", Time[i], R2Buff[i], indicator_color3);
       SetText(PerName+"txtR3", PerName+"-R3", Time[i], R3Buff[i], indicator_color4);
       SetText(PerName+"txtS1", PerName+"-S1", Time[i], S1Buff[i], indicator_color5);
       SetText(PerName+"txtS2", PerName+"-S2", Time[i], S2Buff[i], indicator_color6);
       SetText(PerName+"txtS3", PerName+"-S3", Time[i], S3Buff[i], indicator_color7);
       
       if(Show_Price)
{
SetPrice(PerName+"txtPivotF", Time[i],PivotBuff[i], indicator_color1);
SetPrice(PerName+"txtR1F", Time[i],R1Buff[i], indicator_color2);
SetPrice(PerName+"txtR2F", Time[i],R2Buff[i], indicator_color3);
SetPrice(PerName+"txtR3F", Time[i],R3Buff[i], indicator_color4);
SetPrice(PerName+"txtS1F", Time[i],S1Buff[i], indicator_color5);
SetPrice(PerName+"txtS2F", Time[i],S2Buff[i], indicator_color6);
SetPrice(PerName+"txtS3F", Time[i],S3Buff[i], indicator_color7);

}
    
      
	}
}

void GetCHL(int Shift, double &Cl, double &Hi, double &Lo)
{
   double Cl1, Hi1, Lo1;
   int PeriodShift = iBarShift(Symbol(), TimeFrame, Time[Shift]);

   if ((TimeFrame == PERIOD_D1) && (TimeDayOfWeek(Time[Shift]) == 0) && (SundayData != 0)) PeriodShift++;  // Sobotni pivoty zkopiruji patecni
      
   Cl = iClose(Symbol(), TimeFrame, PeriodShift + 1);
   Hi = iHigh (Symbol(), TimeFrame, PeriodShift + 1);
   Lo = iLow  (Symbol(), TimeFrame, PeriodShift + 1);

   if ((TimeFrame == PERIOD_D1) && (TimeDayOfWeek(Time[Shift]) == 1))
   {
      if (SundayData == 1)
      {
         Cl = iClose(Symbol(), TimeFrame, PeriodShift + 2);
         Hi = iHigh (Symbol(), TimeFrame, PeriodShift + 2);
         Lo = iLow  (Symbol(), TimeFrame, PeriodShift + 2);
      }
      
      if (SundayData == 2)
      {
         Hi1 = iHigh (Symbol(), TimeFrame, PeriodShift + 2);
         Lo1 = iLow  (Symbol(), TimeFrame, PeriodShift + 2);
      
         if (Hi1 > Hi) Hi = Hi1;
         if (Lo1 < Lo) Lo = Lo1;
         
      }
   }

}
void SetText(string name, string txt, datetime Tm, double Prc, color clr)
  {
   if(ObjectFind(name) == -1)
     {
       ObjectCreate(name, OBJ_TEXT, 0, 0, 0);
       ObjectSetText(name, txt, 7, "Times New Roman", clr);
            }
   else
     {
       ObjectSet(name, OBJPROP_TIME1, Tm);
       ObjectSet(name, OBJPROP_PRICE1, Prc);
       ObjectSetText(name, txt, 7, "Times New Roman", clr);
        
     } 
  }
   //+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SetPrice(string name, datetime Tm, double Prc, color clr)
  {
   if(ObjectFind(name) == -1)
     {
       ObjectCreate(name, OBJ_ARROW, 0, Tm, Prc);
       ObjectSet(name, OBJPROP_COLOR, clr);
       ObjectSet(name, OBJPROP_WIDTH, 1);
       ObjectSet(name, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
     }
   else
     {
       ObjectSet(name, OBJPROP_TIME1, Tm);
       ObjectSet(name, OBJPROP_PRICE1, Prc);
       ObjectSet(name, OBJPROP_COLOR, clr);
       ObjectSet(name, OBJPROP_WIDTH, 1);
       ObjectSet(name, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
     } 
  }