#property copyright "Optimized and Improved Version by tuxTrader"
#property link      "https://www.forexfactory.com/tuxtrader"
//+------------------------------------------------------------------+
//|   AutoPivotIndicator.mq4          ver 4.02           by Habeeb   |
//|                                                                  |
//|          This version solves the Sunday Bar problem.             |
//| Ver4 calculated daily pivots incorrectly when Use_Sunday_Data    |
//|        was set to "False".  Fixed in this version.               |
//+------------------------------------------------------------------+
//  some display upgrades added by Aaragorn 11/27/07


//+---------------------------------------------------------------------+
//  CHANGES by tuxTrader@ForexFactory :                                 |
//                                                                      |
//  Added "Labels_Adjust" & "Lines_Move_to_Right" settings _ 12/22/2019 |
//  Added "Lines_Shift" option.                            _ 12/23/2019 |
//  Minor bug fixes & Code Optimization                    _ 12/23/2019 |
//  Optimized "Lines_Shift" option to shift lines to right _ 12/29/2019 |
//+---------------------------------------------------------------------+
// https://forex-station.com/viewtopic.php?p=1295413522#p1295413522
// modified by banzai; 6/27/20
// thanks to jeanlouie codes and templates @ https://www.forexfactory.com/jeanlouie


#property indicator_chart_window

   extern bool             Use_Sunday_Data      = true;
   extern bool             Daily                = true;
   extern bool             Daily_SR_Levels      = true;
   extern bool             Weekly               = false;
   extern bool             Weekly_SR_Levels     = false;
   extern bool             Monthly              = false;
   extern bool             Monthly_SR_Levels    = false;
   extern int              Labels_Adjust        = 10;
   extern bool             Lines_Move_to_Right  = false;
   extern int              Lines_Shift          = 0;
extern string              note1                = "------------------------------";
extern string              LineFont             = "Arial Black";
extern int                 FontSize             = 6;
extern ENUM_LINE_STYLE     myLineStyle          = STYLE_DASH;

extern string              DailyLineColors      = "---Daily Colors-------------";
extern color               PivotLine            = clrMagenta;
extern color               R1_Line              = clrRed;
extern color               R2_Line              = clrRed;
extern color               R3_Line              = clrRed;
extern color               S1_Line              = clrGreen;
extern color               S2_Line              = clrGreen;
extern color               S3_Line              = clrGreen;

extern string              WeeklyLineColors     = "---Weekly Colors-------------";
extern color               WeekPivotLine        = clrAqua;
extern color               WR1_Line             = clrDarkOrange;
extern color               WR2_Line             = clrDarkOrange;
extern color               WR3_Line             = clrDarkOrange;
extern color               WS1_Line             = clrSteelBlue;
extern color               WS2_Line             = clrSteelBlue;
extern color               WS3_Line             = clrSteelBlue;

extern string              MonthlyLineColors    = "---Monthly Colors-------------";
extern color               MonthPivotLine       = clrAqua;
extern color               MR1_Line             = clrBlue;
extern color               MR2_Line             = clrBlue;
extern color               MR3_Line             = clrBlue;
extern color               MS1_Line             = clrSilver;
extern color               MS2_Line             = clrSilver;
extern color               MS3_Line             = clrSilver;
extern string              note2                = "------------------------------";

//template code start1
extern bool                btn_show             = true;                         //btn__show
extern ENUM_BASE_CORNER    btn_corner           = CORNER_LEFT_UPPER; // chart corner for anchoring
extern string              btn_pressed          = "Pivot on";                //btn__pressed text
extern string              btn_unpressed        = "Pivot off";              //btn__unpressed text
extern int                 btn_offset_x         = 20;                             //btn__x
extern int                 btn_offset_y         = 20;                              //btn__y
extern int                 btn_width            = 60;                          //btn__width
extern int                 btn_height           = 20;                         //btn__height
extern int                 btn_font_size        = 10;                      //btn__font size
extern color               btn_font_clr         = clrBlack;               //btn__font color
extern color               btn_bg_color         = clrGray;                  //btn__bg color
extern color               btn_border_clr       = clrWhiteSmoke;        //btn__border color
extern string              note3               = "------------------------------";
//template code end1

   double YesterdayHigh;
   double YesterdayLow;
   double YesterdayClose;
   double Day_Price[][6];
   double Pivot,S1,S2,S3,R1,R2,R3;
      
   double WeekHigh;
   double WeekLow;
   double WeekClose;
   double Weekly_Price[][6];
   double WeekPivot,WS1,WS2,WS3,WR1,WR2,WR3;
   
   double MonthHigh;
   double MonthLow;
   double MonthClose;
   double Month_Price[][6];
   double MonthPivot,MS1,MS2,MS3,MR1,MR2,MR3;

   datetime endTime=0, startTime=0;
   bool     PlotFactor=false;

//template code start2
   bool Daily2, Daily_SR_Levels2, Weekly2, Weekly_SR_Levels2, Monthly2, Monthly_SR_Levels2;
   string aButtonName = "Pivot_button";
//template code end2
//-------------------------------------------------------- 
int OnInit()
  {
//template code start3  
   if(btn_show){CreateButton();}
   if(!btn_show){ObjectDelete(0,aButtonName);}
//template code end3
   return(0);
  }
//-------------------------------------------------------- 
void OnDeinit(const int reason)
{
   //template code start4
   ObjectDelete(0,aButtonName);
   //template code end4

   switch(reason)
   {
      case REASON_CHARTCHANGE : break;
      case REASON_CHARTCLOSE  : break;
      case REASON_RECOMPILE   : break;
      case REASON_CLOSE       : break;
      default                 :
      {
        DeleteObjectsByString("Line",0);
        DeleteObjectsByString("Label",0);
      }
   }  
   WindowRedraw();
}
//--------------------------------------------------------- 
int start()
{
//template code start5
      if(!ObjectGetInteger(0,aButtonName,OBJPROP_STATE)){
//template code end5

   ArrayCopyRates(Day_Price,(Symbol()), 1440);

   YesterdayHigh  = Day_Price[1][3];
   YesterdayLow   = Day_Price[1][2];
   YesterdayClose = Day_Price[1][4];
   
   Pivot = ((YesterdayHigh + YesterdayLow + YesterdayClose)/3);

   R1 = (2*Pivot)-YesterdayLow;
   S1 = (2*Pivot)-YesterdayHigh;

   R2 = Pivot+(R1-S1);
   S2 = Pivot-(R1-S1);
   
   R3 = (YesterdayHigh + (2*(Pivot-YesterdayLow)));
   S3 = (YesterdayLow - (2*(YesterdayHigh-Pivot)));  
  
  
if (Use_Sunday_Data == false)
 {   
   while (DayOfWeek() == 1)
      {  
       YesterdayHigh  = Day_Price[2][3];
       YesterdayLow   = Day_Price[2][2];
       YesterdayClose = Day_Price[2][4];
   
       Pivot = ((YesterdayHigh + YesterdayLow + YesterdayClose)/3);

       R1 = (2*Pivot)-YesterdayLow;
       S1 = (2*Pivot)-YesterdayHigh;

       R2 = Pivot+(R1-S1);
       S2 = Pivot-(R1-S1);
   
       R3 = (YesterdayHigh + (2*(Pivot-YesterdayLow)));
       S3 = (YesterdayLow - (2*(YesterdayHigh-Pivot)));
       break;
      }
//template code start6
}   
//template code end6

 }
//--------------------------------------------------------
ArrayCopyRates(Weekly_Price, Symbol(), 10080);

WeekHigh  = Weekly_Price[1][3];
WeekLow   = Weekly_Price[1][2];
WeekClose = Weekly_Price[1][4];

WeekPivot = ((WeekHigh + WeekLow + WeekClose)/3);

      WR1 = (2*WeekPivot)-WeekLow;
      WS1 = (2*WeekPivot)-WeekHigh;

      WR2 = WeekPivot+(WR1-WS1);
      WS2 = WeekPivot-(WR1-WS1);

      WS3 = (WeekLow - (2*(WeekHigh-WeekPivot)));
      WR3 = (WeekHigh + (2*(WeekPivot-WeekLow)));
//--------------------------------------------------------
//--------------------------------------------------------
ArrayCopyRates(Month_Price, Symbol(), 43200);

MonthHigh  = Month_Price[1][3];
MonthLow   = Month_Price[1][2];
MonthClose = Month_Price[1][4];

MonthPivot = ((MonthHigh + MonthLow + MonthClose)/3);

      MR1 = (2*MonthPivot)-MonthLow;
      MS1 = (2*MonthPivot)-MonthHigh;

      MR2 = MonthPivot+(MR1-MS1);
      MS2 = MonthPivot-(MR1-MS1);

      MS3 = (MonthLow - (2*(MonthHigh-MonthPivot)));
      MR3 = (MonthHigh + (2*(MonthPivot-MonthLow)));
//--------------------------------------------------------
//--------------------------------------------------------

if (Daily==true)
{
 DrawLine("PivotLine","PivotLabel","DP",Pivot,PivotLine);
 if (Daily_SR_Levels==true)
 {
   DrawLine("R1_Line","R1_Label","DR1",R1,R1_Line);
   DrawLine("R2_Line","R2_Label","DR2",R2,R2_Line);
   DrawLine("R3_Line","R3_Label","DR3",R3,R3_Line);

   DrawLine("S1_Line","S1_Label","DS1",S1,S1_Line);
   DrawLine("S2_Line","S2_Label","DS2",S2,S2_Line);
   DrawLine("S3_Line","S3_Label","DS3",S3,S3_Line);
 }
}
//---------------------------------------------------------
//---------------------------------------------------------
//---------------------------------------------------------

if (Weekly==true)
{
 DrawLine("WeekPivotLine","WeekPivotLabel","WP",WeekPivot,WeekPivotLine);

 if (Weekly_SR_Levels==true)
 {
   DrawLine("WR1_Line","WR1_Label","WR1",WR1,WR1_Line);
   DrawLine("WR2_Line","WR2_Label","WR2",WR2,WR2_Line);
   DrawLine("WR3_Line","WR3_Label","WR3",WR3,WR3_Line);

   DrawLine("WS1_Line","WS1_Label","WS1",WS1,WS1_Line);
   DrawLine("WS2_Line","WS2_Label","WS2",WS2,WS2_Line);
   DrawLine("WS3_Line","WS3_Label","WS3",WS3,WS3_Line);
 }
}
//---------------------------------------------------------
//---------------------------------------------------------
//---------------------------------------------------------

if (Monthly==true)
{
 DrawLine("MonthPivotLine","MonthPivotLabel","MP",MonthPivot,MonthPivotLine);

 if (Monthly_SR_Levels==true)
 {
   DrawLine("MR1_Line","MR1_Label","MR1",MR1,MR1_Line);
   DrawLine("MR2_Line","MR2_Label","MR2",MR2,MR2_Line);
   DrawLine("MR3_Line","MR3_Label","MR3",MR3,MR3_Line);

   DrawLine("MS1_Line","MS1_Label","MS1",MS1,MS1_Line);
   DrawLine("MS2_Line","MS2_Label","MS2",MS2,MS2_Line);
   DrawLine("MS3_Line","MS3_Label","MS3",MS3,MS3_Line);
 }
}
   return(0);
}
//---------------------------------------------------------
//---------------------------------------------------------
void DrawLine(string LineName, string LabelName, string LabelText, double Price, color COLOR)
{
   startTime=0;  endTime=0;
   if(Lines_Move_to_Right)  { startTime = Time[0]+(Period()*60*Lines_Shift);  endTime = startTime + (Period()*60*1);                 PlotFactor=true; }
   else                     { startTime = iTime(Symbol(),PERIOD_D1,-1);       endTime = iTime(Symbol(),PERIOD_D1,0)+(PERIOD_D1*60);  PlotFactor=true; }

  ObjectCreate(LineName, OBJ_TREND,0,startTime,Price,endTime,Price);
  ObjectSet   (LineName, OBJPROP_TIME1, startTime);
  ObjectSet   (LineName, OBJPROP_TIME2, endTime);
  ObjectSet   (LineName, OBJPROP_COLOR, COLOR);
  ObjectSet   (LineName, OBJPROP_STYLE, myLineStyle);
  ObjectSet   (LineName, OBJPROP_RAY,   PlotFactor);
  ObjectSet   (LineName, OBJPROP_SELECTABLE, false);
  ObjectSet   (LineName, OBJPROP_SELECTED, false);  
  ObjectSet   (LineName, OBJPROP_HIDDEN, true);
  ObjectMove  (LineName, 1, endTime, Price);
  ObjectMove  (LineName, 0, startTime, Price);

  if(ObjectFind(LabelName) != 0)
  {
   ObjectCreate(LabelName, OBJ_TEXT, 0, Time[0]+(Period()*60*Labels_Adjust), Price);
   ObjectSetText(LabelName, LabelText, FontSize, LineFont, COLOR);
  }
  else
  {
   ObjectMove(LabelName, 0, Time[0]+(Period()*60*Labels_Adjust), Price);
   ObjectMove(LineName, 0, startTime,Price);

  }

  ObjectsRedraw();
  WindowRedraw();
}
//---------------------------------------------------------
//---------------------------------------------------------
void DeleteObjectsByString(string Search,int SubWin, int ObjectType=-1) {
   int TotalObject = ObjectsTotal(0,SubWin,-1);
   for (int i = TotalObject; i >= 0 ; i--)
   {
      if (StringFind(ObjectName(0, i, SubWin, ObjectType), Search, 0) > -1)
      {
         ObjectDelete(0,ObjectName(0,i,SubWin,-1));
      }      
   }
}
//---------------------------------------------------------
//template start codes7
void CreateButton()
{
   ButtonCreate(NULL,aButtonName,0,btn_offset_x,btn_offset_y,btn_width,btn_height,btn_corner,btn_unpressed,"Arial",btn_font_size,btn_font_clr,btn_bg_color,btn_border_clr,false,true,false,false,0);
}
//+------------------------------------------------------------------+
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
{
   if (id == CHARTEVENT_OBJECT_CLICK && sparam==aButtonName) 
   { 
      if(ObjectGetInteger (0,aButtonName,OBJPROP_STATE)){
         ObjectSetString  (0,aButtonName,OBJPROP_TEXT,btn_pressed);
         DeleteObjectsByString("Line",0);
         DeleteObjectsByString("Label",0);
         
         Daily2 = Daily;  
         if (Daily == true) Daily = false;     
          
         Daily_SR_Levels2 = Daily_SR_Levels;
         if (Daily_SR_Levels == true) Daily_SR_Levels = false;      

         Weekly2 = Weekly;
         if (Weekly == true) Weekly = false;      

         Weekly_SR_Levels2 = Weekly_SR_Levels;
         if (Weekly_SR_Levels == true) Weekly_SR_Levels = false;      

         Monthly2 = Monthly;
         if (Monthly == true) Monthly = false;      

         Monthly_SR_Levels2 = Monthly_SR_Levels;
         if (Monthly_SR_Levels == true) Monthly_SR_Levels = false;      
         }

      if(!ObjectGetInteger(0,aButtonName,OBJPROP_STATE)){
         ObjectSetString  (0,aButtonName,OBJPROP_TEXT,btn_unpressed);
            Daily               = Daily2;
            Daily_SR_Levels     = Daily_SR_Levels2;
            Weekly              = Weekly2;
            Weekly_SR_Levels    = Weekly_SR_Levels2;
            Monthly             = Monthly2;
            Monthly_SR_Levels   = Monthly_SR_Levels2;
            start();
         }
   }
}
//+------------------------------------------------------------------+
bool ButtonCreate(const long              chart_ID   = 0,               // chart's ID
                  const string            name       = "Button",            // button name
                  const int               sub_window = 0,             // subwindow index
                  const int               x          = 0,                      // X coordinate
                  const int               y          = 0,                      // Y coordinate
                  const int               width      = 50,                 // button width
                  const int               height     = 18,                // button height
                  const int               mycorner   = CORNER_LEFT_UPPER,
                  const string            text       = "Button",            // text
                  const string            font       = "Arial",             // font
                  const int               font_size  = 10,             // font size
                  const color             clr        = clrBlack,             // text color
                  const color             back_clr   = C'236,233,216',  // background color
                  const color             border_clr = clrNONE,       // border color
                  const bool              state      = false,              // pressed/released
                  const bool              back       = false,               // in the background
                  const bool              selectable = true,      //object can be selected        
                  const bool              selection  = false,          // highlight to move
                  const bool              hidden     = false,              // hidden in the object list
                  const long              z_order    = 0)                // priority for mouse click
  {
   ResetLastError();
   ObjectCreate    (chart_ID,name,OBJ_BUTTON,          sub_window,0,0);
   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,   x);
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,   y);
   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,       width);
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,      mycorner);
   ObjectSetString (chart_ID,name,OBJPROP_TEXT,        text);
   ObjectSetString (chart_ID,name,OBJPROP_FONT,        font);
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,    font_size);
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,       clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,     back_clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BORDER_COLOR,border_clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,        back);
   ObjectSetInteger(chart_ID,name,OBJPROP_STATE,       state);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,  selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,    selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,      hidden);
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,      z_order);
   return(true);
  }
//+------------------------------------------------------------------+
//template end codes7
