//+------------------------------------------------------------------+
//|                                 mpurdy_PBpivotPointIndicator.mq4 |
//|                                                    Matthew Purdy |
//|                                             mpurdy1973@yahoo.com |
//+------------------------------------------------------------------+

#define MAX_OBJECTS 100 
#define SUNDAY 0
#define MONDAY 1

#property copyright "Matthew Purdy"
#property link      "mpurdy1973@yahoo.com"

#property indicator_chart_window

extern bool ExtFibo = false;
extern color ExtDailyStartColor = White;
extern color ExtDailyRColor     = DodgerBlue;
extern color ExtDailyPColor     = Gold;
extern color ExtDailySColor     = Crimson;

//---- global variables
bool glob_bFirstTime = true;
string glob_objectArray[MAX_OBJECTS];
int glob_objectArrayCount = 0;

datetime glob_dailyStartTime = 0;

double glob_dailyPivot          = 0.0;
double glob_dailyPivot50Percent = 0.0;
double glob_dailyR1             = 0.0;
double glob_dailyR2             = 0.0;
double glob_dailyR3             = 0.0;
double glob_dailyS1             = 0.0;
double glob_dailyS2             = 0.0;
double glob_dailyS3             = 0.0;

double glob_dailyM0             = 0.0;
double glob_dailyM1             = 0.0;
double glob_dailyM2             = 0.0;
double glob_dailyM3             = 0.0;
double glob_dailyM4             = 0.0;
double glob_dailyM5             = 0.0;

int glob_currentPeriod = PERIOD_D1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
Print("initializing...");
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   deleteAllObjects();

   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   double total = 0.0;
   
   double high  = 0.0;
   double low   = 0.0;
   double range = 0.0;
   int highPos  = 0;
   int lowPos   = 0;
 
   //do first time or new new day
   if(glob_bFirstTime || (TimeHour(Time[0]) == 0 && TimeMinute(Time[0]) == 0))
   {

      datetime currentTime = Time[0];
      datetime beginDay = 0;
      
      int deltaTime = (TimeHour(currentTime) * 3600) + (TimeMinute(currentTime) * 60);
      
      
      beginDay = currentTime - deltaTime;
      
      //int barShift = iBarShift(NULL, 0, Time[0], false);
      //glob_dailyStartTime = iTime(NULL, 0, barShift);
      
      glob_dailyStartTime = beginDay; 
      
      
 //Print("currentTime = " + TimeToStr(currentTime) + ", deltaTime = " + deltaTime + " : " + TimeToStr(deltaTime) + ", time = " + TimeToStr(glob_dailyStartTime));       
      calculatePivots();
      draw();
      
      glob_bFirstTime = false;      
   
   }//end if first time or new day
   
   if(Volume[0] == 1 || Period() != glob_currentPeriod)
   {
      draw();
      glob_currentPeriod = Period();
      
   }//end if need to update screen

   
   return(0);

}
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| draw                                                             |
//+------------------------------------------------------------------+
void draw()
{
   deleteAllObjects();

   ObjectCreate("dailyStartLine", OBJ_VLINE, 0, glob_dailyStartTime, 10.0, glob_dailyStartTime, 0.0);
   ObjectSet("dailyStartLine", OBJPROP_COLOR, ExtDailyStartColor);
   ObjectSet("dailyStartLine", OBJPROP_WIDTH, 1);
   //ObjectSet("dailyStartLine", OBJPROP_BACK, true);
   addObject("dailyStartLine");
   
   ObjectCreate("dailyR3Line", OBJ_TREND, 0, glob_dailyStartTime, glob_dailyR3, Time[0], glob_dailyR3);
   ObjectSet("dailyR3Line", OBJPROP_COLOR, ExtDailyRColor);
   ObjectSet("dailyR3Line", OBJPROP_WIDTH, 2);
   ObjectSet("dailyR3Line", OBJPROP_RAY, false);
   //ObjectSet("dailyR3Line", OBJPROP_BACK, true);
   addObject("dailyR3Line");

//Print("draw: glob_dailyM5 = " + glob_dailyM5);
   ObjectCreate("dailyM5Line", OBJ_TREND, 0, glob_dailyStartTime, glob_dailyM5, Time[0], glob_dailyM5);
   ObjectSet("dailyM5Line", OBJPROP_COLOR, ExtDailyRColor);
   ObjectSet("dailyM5Line", OBJPROP_WIDTH, 1);
   ObjectSet("dailyM5Line", OBJPROP_STYLE, STYLE_DOT);
   ObjectSet("dailyM5Line", OBJPROP_RAY, false);
   //ObjectSet("dailyM5Line", OBJPROP_BACK, true);
   addObject("dailyM5Line");

   ObjectCreate("dailyR2Line", OBJ_TREND, 0, glob_dailyStartTime, glob_dailyR2, Time[0], glob_dailyR2);
   ObjectSet("dailyR2Line", OBJPROP_COLOR, ExtDailyRColor);
   ObjectSet("dailyR2Line", OBJPROP_WIDTH, 2);
   ObjectSet("dailyR2Line", OBJPROP_RAY, false);
   //ObjectSet("dailyR2Line", OBJPROP_BACK, true);
   addObject("dailyR2Line");

//Print("draw: glob_dailyM4 = " + glob_dailyM4);
   ObjectCreate("dailyM4Line", OBJ_TREND, 0, glob_dailyStartTime, glob_dailyM4, Time[0], glob_dailyM4);
   ObjectSet("dailyM4Line", OBJPROP_COLOR, ExtDailyRColor);
   ObjectSet("dailyM4Line", OBJPROP_WIDTH, 1);
   ObjectSet("dailyM4Line", OBJPROP_STYLE, STYLE_DOT);
   ObjectSet("dailyM4Line", OBJPROP_RAY, false);
   //ObjectSet("dailyM4Line", OBJPROP_BACK, true);
   addObject("dailyM4Line");
   
   ObjectCreate("dailyR1Line", OBJ_TREND, 0, glob_dailyStartTime, glob_dailyR1, Time[0], glob_dailyR1);
   ObjectSet("dailyR1Line", OBJPROP_COLOR, ExtDailyRColor);
   ObjectSet("dailyR1Line", OBJPROP_WIDTH, 2);
   ObjectSet("dailyR1Line", OBJPROP_RAY, false);
   //ObjectSet("dailyR1Line", OBJPROP_BACK, true);
   addObject("dailyR1Line");
   
   
//Print("draw: glob_dailyM3 = " + glob_dailyM3);
   ObjectCreate("dailyM3Line", OBJ_TREND, 0, glob_dailyStartTime, glob_dailyM3, Time[0], glob_dailyM3);
   ObjectSet("dailyM3Line", OBJPROP_COLOR, ExtDailyRColor);
   ObjectSet("dailyM3Line", OBJPROP_WIDTH, 1);
   ObjectSet("dailyM3Line", OBJPROP_STYLE, STYLE_DOT);
   ObjectSet("dailyM3Line", OBJPROP_RAY, false);
   //ObjectSet("dailyM3Line", OBJPROP_BACK, true);
   addObject("dailyM3Line");   
   
   ObjectCreate("dailyPivotLine", OBJ_TREND, 0, glob_dailyStartTime - 36000, glob_dailyPivot, Time[0], glob_dailyPivot);
   ObjectSet("dailyPivotLine", OBJPROP_COLOR, ExtDailyPColor);
   ObjectSet("dailyPivotLine", OBJPROP_WIDTH, 2);
   ObjectSet("dailyPivotLine", OBJPROP_RAY, false);
   //ObjectSet("dailyPivotLine", OBJPROP_BACK, true);
   addObject("dailyPivotLine");
   
   ObjectCreate("dailyPivot50Line", OBJ_TREND, 0, glob_dailyStartTime, glob_dailyPivot50Percent, Time[0], glob_dailyPivot50Percent);
   ObjectSet("dailyPivot50Line", OBJPROP_COLOR, ExtDailyPColor);
   ObjectSet("dailyPivot50Line", OBJPROP_WIDTH, 1);
   ObjectSet("dailyPivot50Line", OBJPROP_STYLE, STYLE_DOT);
   ObjectSet("dailyPivot50Line", OBJPROP_RAY, false);
   //ObjectSet("dailyPivot50Line", OBJPROP_BACK, true);
   addObject("dailyPivot50Line");
     
//Print("draw: glob_dailyM2 = " + glob_dailyM2); 
   ObjectCreate("dailyM2Line", OBJ_TREND, 0, glob_dailyStartTime, glob_dailyM2, Time[0], glob_dailyM2);
   ObjectSet("dailyM2Line", OBJPROP_COLOR, ExtDailySColor);
   ObjectSet("dailyM2Line", OBJPROP_WIDTH, 1);
   ObjectSet("dailyM2Line", OBJPROP_STYLE, STYLE_DOT);
   ObjectSet("dailyM2Line", OBJPROP_RAY, false);
   //ObjectSet("dailyM2Line", OBJPROP_BACK, true);
   addObject("dailyM2Line");
   
   ObjectCreate("dailyS1Line", OBJ_TREND, 0, glob_dailyStartTime, glob_dailyS1, Time[0], glob_dailyS1);
   ObjectSet("dailyS1Line", OBJPROP_COLOR, ExtDailySColor);
   ObjectSet("dailyS1Line", OBJPROP_WIDTH, 2);
   ObjectSet("dailyS1Line", OBJPROP_RAY, false);
   //ObjectSet("dailyS1Line", OBJPROP_BACK, true);
   addObject("dailyS1Line");

//Print("draw: glob_dailyM1 = " + glob_dailyM1); 
   ObjectCreate("dailyM1Line", OBJ_TREND, 0, glob_dailyStartTime, glob_dailyM1, Time[0], glob_dailyM1);
   ObjectSet("dailyM1Line", OBJPROP_COLOR, ExtDailySColor);
   ObjectSet("dailyM1Line", OBJPROP_WIDTH, 1);
   ObjectSet("dailyM1Line", OBJPROP_STYLE, STYLE_DOT);
   ObjectSet("dailyM1Line", OBJPROP_RAY, false);
   //ObjectSet("dailyM1Line", OBJPROP_BACK, true);
   addObject("dailyM1Line");   
   
   ObjectCreate("dailyS2Line", OBJ_TREND, 0, glob_dailyStartTime, glob_dailyS2, Time[0], glob_dailyS2);
   ObjectSet("dailyS2Line", OBJPROP_COLOR, ExtDailySColor);
   ObjectSet("dailyS2Line", OBJPROP_WIDTH, 2);
   ObjectSet("dailyS2Line", OBJPROP_RAY, false);
   //ObjectSet("dailyS2Line", OBJPROP_BACK, true);
   addObject("dailyS2Line");
   
 //Print("draw: glob_dailyM0 = " + glob_dailyM0);
   ObjectCreate("dailyM0Line", OBJ_TREND, 0, glob_dailyStartTime, glob_dailyM0, Time[0], glob_dailyM0);
   ObjectSet("dailyM0Line", OBJPROP_COLOR, ExtDailySColor);
   ObjectSet("dailyM0Line", OBJPROP_WIDTH, 1);
   ObjectSet("dailyM0Line", OBJPROP_STYLE, STYLE_DOT);
   ObjectSet("dailyM0Line", OBJPROP_RAY, false);
   //ObjectSet("dailyM0Line", OBJPROP_BACK, true);
   addObject("dailyM0Line");   
   
   ObjectCreate("dailyS3Line", OBJ_TREND, 0, glob_dailyStartTime, glob_dailyS3, Time[0], glob_dailyS3);
   ObjectSet("dailyS3Line", OBJPROP_COLOR, ExtDailySColor);
   ObjectSet("dailyS3Line", OBJPROP_WIDTH, 2);
   ObjectSet("dailyS3Line", OBJPROP_RAY, false);
   //ObjectSet("dailyS3Line", OBJPROP_BACK, true);
   addObject("dailyS3Line");         
   
}//end function draw

//+------------------------------------------------------------------+
//| deleteAllObjects                                                 |
//+------------------------------------------------------------------+
void deleteAllObjects()
{
   for(int i = 0; i < glob_objectArrayCount; i++)
      ObjectDelete(glob_objectArray[i]);
      
   glob_objectArrayCount = 0;
      
}//end function deleteAllObjects
//--------------------------------------------------------------------

//+------------------------------------------------------------------+
//| addObject                                                        |
//+------------------------------------------------------------------+
void addObject(string sObj)
{
   if(glob_objectArrayCount <= MAX_OBJECTS)
   {
      glob_objectArray[glob_objectArrayCount] = sObj;
      glob_objectArrayCount++;
      
   }//end if room for more objects
      
}//end function deleteAllObjects
//--------------------------------------------------------------------

//+------------------------------------------------------------------+
//| calcSR                                                           |
//+------------------------------------------------------------------+
void calcSR(double high, double low, double pivot, bool bFibo, double &s1, double &s2, double &s3, double &r1, double &r2, double &r3)
{
    if(bFibo)
    {
       s1 = pivot - (0.382 * (high - low));
       s2 = pivot - (0.618 * (high - low));
       s3 = pivot - (1.000 * (high - low));
       r1 = pivot + (0.382 * (high - low));
       r2 = pivot + (0.618 * (high - low));
       r3 = pivot + (1.000 * (high - low));       

    }//end if fibo pivots
    else
    {
       s1 = (2 * pivot) - high;
       r1 = (2 * pivot) - low;
       
       s2 = pivot - (r1 - s1);
       r2 = pivot + (r1 - s1);
       
       s3 = low - (2 * (high - pivot));       
       r3 = high + (2 * (pivot - low));

    }//end else normal pivots 

}//end function calcSR
//--------------------------------------------------------------------

//+------------------------------------------------------------------+
//| calculatePivots                                            |
//+------------------------------------------------------------------+
void calculatePivots()
{
   double high  = 0.0;
   double low   = 1000.0;
   double close = 0.0;
   datetime time = 0;
   
   
   
   //calculate daily pivot
   high  = iHigh(NULL, PERIOD_D1, 1);
   low   = iLow(NULL, PERIOD_D1, 1);
   close = iClose(NULL, PERIOD_D1, 1);    
   time  = iTime(NULL, PERIOD_D1, 1);
  
//Print("time = " + TimeToStr(time) + ", day of week = " + TimeDayOfWeek(time));
//Print("time day of week = " + TimeDayOfWeek(time));

   if(TimeDayOfWeek(time) == SUNDAY)
   {
      double friHigh = iHigh(NULL, PERIOD_D1, 2);
      double friLow  = iLow(NULL, PERIOD_D1, 2);

//Print("high = " + high + ", low = " + low + ", friHigh = " + friHigh + ", friLow = " + friLow); 
      
      high = MathMax(friHigh, high);
      low  = MathMin(friLow, low); 
 
   }//end if Sunday - concat price with previous friday

   glob_dailyPivot          = (MathRound(((high + low + close) / 3.0) / Point)) * Point;
   glob_dailyPivot50Percent = (MathRound(((high + low) / 2.0) / Point)) * Point;
   
   calcSR(high, low, glob_dailyPivot, ExtFibo, glob_dailyS1, glob_dailyS2, glob_dailyS3, glob_dailyR1, glob_dailyR2, glob_dailyR3);
   
   glob_dailyM0 = ((glob_dailyS2 - glob_dailyS3) / 2) + glob_dailyS3;
   glob_dailyM1 = ((glob_dailyS1 - glob_dailyS2) / 2) + glob_dailyS2;
   glob_dailyM2 = ((glob_dailyPivot - glob_dailyS1) / 2) + glob_dailyS1;
   glob_dailyM3 = ((glob_dailyR1 - glob_dailyPivot) / 2) + glob_dailyPivot;
   glob_dailyM4 = ((glob_dailyR2 - glob_dailyR1) / 2) + glob_dailyR1;
   glob_dailyM5 = ((glob_dailyR3 - glob_dailyR2) / 2) + glob_dailyR2;
   
   
/*

   //calculate weekly pivot
   high  = iHigh(NULL, PERIOD_W1, 1);
   low   = iLow(NULL, PERIOD_W1, 1);
   close = iClose(NULL, PERIOD_W1, 1);
   
   glob_weeklyPivot          = (MathRound(((high + low + close) / 3.0) / Point)) * Point;
   glob_weeklyPivot50Percent = (MathRound(((high + low) / 2.0) / Point)) * Point;

   calcSR(high, low, glob_weeklyPivot, ExtFibo, glob_weeklyS1, glob_weeklyS2, glob_weeklyS3, glob_weeklyR1, glob_weeklyR2, glob_weeklyR3);
   
   //calculate monthly pivot
   high  = iHigh(NULL, PERIOD_MN1, 1);
   low   = iLow(NULL, PERIOD_MN1, 1);
   close = iClose(NULL, PERIOD_MN1, 1);
   
   glob_monthlyPivot          = (MathRound(((high + low + close) / 3.0) / Point)) * Point;
   glob_monthlyPivot50Percent = (MathRound(((high + low) / 2.0) / Point)) * Point;
   
   calcSR(high, low, glob_monthlyPivot, ExtFibo, glob_monthlyS1, glob_monthlyS2, glob_monthlyS3, glob_monthlyR1, glob_monthlyR2, glob_monthlyR3);

*/
}//end function calculatePivots
//--------------------------------------------------------------------