//+------------------------------------------------------------------+
//|                                                        Pivot.mq4 |
//|                                          Monday Fixed by TaXiRaN |
//|                               Copyright © 2004, Poul_Trade_Forum |
//|                                                         Aborigen |
//|                                          http://forex.kbpauk.ru/ |
//+------------------------------------------------------------------+
#property copyright "Poul Trade Forum"
#property link      "http://forex.kbpauk.ru/"

#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 Orange
#property indicator_color2 DodgerBlue
#property indicator_color3 DodgerBlue
#property indicator_color4 Maroon
#property indicator_color5 Maroon
#property indicator_color6 Green
#property indicator_color7 Green
#property indicator_color8 Magenta

#define DEBUG 0

#define PIVOT_DIGITS 4
#define P  0
#define S1 1
#define R1 2
#define S2 3
#define R2 4
#define S3 5
#define R3 6
#define numPoints 7

double PBuffer[];
double S1Buffer[];
double R1Buffer[];
double S2Buffer[];
double R2Buffer[];
double S3Buffer[];
double R3Buffer[];
double todaysOpen[];

extern bool   E_PlotP=TRUE;
extern bool   E_Plot1=TRUE;
extern bool   E_Plot2=TRUE;
extern bool   E_Plot3=TRUE;
extern bool   E_PlotOpen=TRUE;

int deinit() {
   return(0);
}

int init() {
   IndicatorBuffers(8);
      
   SetIndexBuffer(0,PBuffer);
   SetIndexLabel (0,"Pivot");
   SetIndexStyle (0,DRAW_LINE);

   SetIndexBuffer(1,S1Buffer);
   SetIndexLabel (1,"S1");
   SetIndexStyle (1,DRAW_LINE);

   SetIndexBuffer(2,R1Buffer);
   SetIndexLabel (2,"R1");
   SetIndexStyle (2,DRAW_LINE);

   SetIndexBuffer(3,S2Buffer);
   SetIndexLabel (3,"S2");
   SetIndexStyle (3,DRAW_LINE);

   SetIndexBuffer(4,R2Buffer);
   SetIndexLabel (4,"R2");
   SetIndexStyle (4,DRAW_LINE);

   SetIndexBuffer(5,S3Buffer);
   SetIndexLabel (5,"S3");
   SetIndexStyle (5,DRAW_LINE);

   SetIndexBuffer(6,R3Buffer);
   SetIndexLabel (6,"R3");
   SetIndexStyle (6,DRAW_LINE);

   SetIndexBuffer(7,todaysOpen);
   SetIndexLabel (7,"Open");
   SetIndexStyle (7,DRAW_LINE);

   IndicatorShortName("Pivot Point");
   SetIndexDrawBegin(0,1);

   return(0);
}

int start() {
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);

   int limit=(Bars-counted_bars)-1;
   for (int i=limit; i>=0;i--) {
      if (TimeDayOfWeek(Time[i+1])!=0 && TimeDay(Time[i])!=TimeDay(Time[i+1])) {
         double yesterdayHigh =GetDailyHigh (Time[i],1);
         double yesterdayLow  =GetDailyLow  (Time[i],1);
         double yesterdayClose=GetDailyClose(Time[i],1);
         double vector[numPoints];
         
         CalcFloorPivots(vector,yesterdayHigh,yesterdayLow,yesterdayClose);
      }

      if(E_PlotP)    PBuffer[i]  =vector[P];
      if(E_Plot1)    {S1Buffer[i]=vector[S1];R1Buffer[i]=vector[R1];}
      if(E_Plot2)    {S2Buffer[i]=vector[S2];R2Buffer[i]=vector[R2];}
      if(E_Plot3)    {S3Buffer[i]=vector[S3];R3Buffer[i]=vector[R3];}
      if(E_PlotOpen) todaysOpen[i]=GetDailyOpen (Time[i]);
   }

   return(0);
}

//+----------------------------------------------------+
//| Return Daily High, regardless of chart timeframe.  |
//| If shift is 0, todays high is returned. If shift is|
//| 1, yesterday's high is returned, etc.              |
//+----------------------------------------------------+
double GetDailyHigh(datetime d, int shift=0) {
  double result = 0.0;  
  result=iHigh(Symbol(),PERIOD_D1,iBarShift(Symbol(),PERIOD_D1,d)+shift);
  return (result);
}

//+----------------------------------------------------+
//| Return Daily Low, regardless of chart timeframe.   |
//| If shift is 0, todays low is returned. If shift is |
//| 1, yesterday's low is returned, etc.               |
//+----------------------------------------------------+
double GetDailyLow(datetime d, int shift=0) {
  double result = 0.0;  
  result=iLow(Symbol(),PERIOD_D1,iBarShift(Symbol(),PERIOD_D1,d)+shift);
  return (result);
}

//+----------------------------------------------------+
//| Return Daily Close, regardless of chart timeframe. |
//| If shift is 0, todays close is returned. If shift  |
//| is 1, yesterday's close is returned, etc.          |
//+----------------------------------------------------+
double GetDailyClose(datetime d, int shift=0) {
  double result = 0.0;  
  result=iClose(Symbol(),PERIOD_D1,iBarShift(Symbol(),PERIOD_D1,d)+shift);
  return (result);
}

//+----------------------------------------------------+
//| Return Daily Open, regardless of chart timeframe.  |
//| If shift is 0, todays open is returned. If shift   |
//| is 1, yesterday's open is returned, etc.           |
//+----------------------------------------------------+
double GetDailyOpen(datetime d, int shift=0) {
  double result = 0.0;  
  result=iOpen(Symbol(),PERIOD_D1,iBarShift(Symbol(),PERIOD_D1,d)+shift);
  return (result);
}

//+----------------------------------------------------+
//| Calculate "Floor" pivots.                          |
//| eg http://www.earnforex.com/pivot-points-calculator|
//+----------------------------------------------------+
void CalcFloorPivots(double &vector[],
                double high, double low, double close) {
   vector[P]  = (high+low+close)/3;
   vector[R1] = (2*vector[P])-low;
   vector[S1] = (2*vector[P])-high;
   vector[R2] = vector[P]+(high-low);
   vector[S2] = vector[P]-(high-low);
   vector[R3] = (2*vector[P])+(high-(2*low));
   vector[S3] = (2*vector[P])-((2* high)-low);

   if(DEBUG==1)Print("Using High=", DoubleToStr(high, PIVOT_DIGITS),
                     ", Low=",      DoubleToStr(low,  PIVOT_DIGITS),
                     ", Close=",    DoubleToStr(close,PIVOT_DIGITS),
                     ", R3=",DoubleToStr(vector[R3],PIVOT_DIGITS),
                     ", R2=",DoubleToStr(vector[R2],PIVOT_DIGITS),
                     ", R1=",DoubleToStr(vector[R1],PIVOT_DIGITS),
                     ", P=", DoubleToStr(vector[P], PIVOT_DIGITS),
                     ", S1=",DoubleToStr(vector[S1],PIVOT_DIGITS),
                     ", S2=",DoubleToStr(vector[S2],PIVOT_DIGITS),
                     ", S3=",DoubleToStr(vector[S3],PIVOT_DIGITS));
}