AUTO PIVOTS for MT5 – indicator MetaTrader 5

Pivots are well known price action indicator .



#property link      "
#property version   "1.00"
#property indicator_plots 0
#property indicator_chart_window
#property strict

input color WeeklyColors = Yellow;
input color MonthlyColors = Orange;

input bool Use_Sunday_Data   = true;
input bool Daily             = true;
input bool Daily_SR_Levels   = true;
input bool Weekly            = true;
input bool Weekly_SR_Levels  = false;
input bool Monthly           = true;
input bool Monthly_SR_Levels = false;

double YesterdayHigh;
double YesterdayLow;
double YesterdayClose;
double Pivot, S1, S2, S3, R1, R2, R3;

double WeekHigh;
double WeekLow;
double WeekClose;
double WeekPivot, WS1, WS2, WS3, WR1, WR2, WR3;

double MonthHigh;
double MonthLow;
double MonthClose;
double MonthPivot, MS1, MS2, MS3, MR1, MR2, MR3;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
   return(0);
  }

//--------------------------------------------------------

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ObjectDelete(0, "PivotLine");

   ObjectDelete(0, "R1_Line");
   ObjectDelete(0, "R2_Line");
   ObjectDelete(0, "R3_Line");

   ObjectDelete(0, "S1_Line");
   ObjectDelete(0, "S2_Line");
   ObjectDelete(0, "S3_Line");

//--------------------------------

   ObjectDelete(0, "PivotLabel");

   ObjectDelete(0, "R1_Label");
   ObjectDelete(0, "R2_Label");
   ObjectDelete(0, "R3_Label");

   ObjectDelete(0, "S1_Label");
   ObjectDelete(0, "S2_Label");
   ObjectDelete(0, "S3_Label");

//--------------------------------------------------------

   ObjectDelete(0, "WeekPivotLine");

   ObjectDelete(0, "WR1_Line");
   ObjectDelete(0, "WR2_Line");
   ObjectDelete(0, "WR3_Line");

   ObjectDelete(0, "WS1_Line");
   ObjectDelete(0, "WS2_Line");
   ObjectDelete(0, "WS3_Line");

//--------------------------------

   ObjectDelete(0, "WeekPivotLabel");

   ObjectDelete(0, "WR1_Label");
   ObjectDelete(0, "WR2_Label");
   ObjectDelete(0, "WR3_Label");

   ObjectDelete(0, "WS1_Label");
   ObjectDelete(0, "WS2_Label");
   ObjectDelete(0, "WS3_Label");

//--------------------------------------------------------

   ObjectDelete(0, "MonthPivotLine");

   ObjectDelete(0, "MR1_Line");
   ObjectDelete(0, "MR2_Line");
   ObjectDelete(0, "MR3_Line");

   ObjectDelete(0, "MS1_Line");
   ObjectDelete(0, "MS2_Line");
   ObjectDelete(0, "MS3_Line");

//--------------------------------

   ObjectDelete(0, "MonthPivotLabel");

   ObjectDelete(0, "MR1_Label");
   ObjectDelete(0, "MR2_Label");
   ObjectDelete(0, "MR3_Label");

   ObjectDelete(0, "MS1_Label");
   ObjectDelete(0, "MS2_Label");
   ObjectDelete(0, "MS3_Label");

  }
//---------------------------------------------------------
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   YesterdayHigh  = iHigh(_Symbol, PERIOD_D1, 1);
   YesterdayLow   = iLow(_Symbol, PERIOD_D1, 1);
   YesterdayClose = iClose(_Symbol, PERIOD_D1, 1);

   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(DayOfWeekMQL4() == 1)
        {


         YesterdayHigh  = iHigh(_Symbol, PERIOD_D1, 2);
         YesterdayLow   = iLow(_Symbol, PERIOD_D1, 2);
         YesterdayClose = iClose(_Symbol, PERIOD_D1, 2);

         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;
        }
     }

//--------------------------------------------------------
//--------------------------------------------------------

   WeekHigh  = iHigh(_Symbol, PERIOD_W1, 1);
   WeekLow   = iLow(_Symbol, PERIOD_W1, 1);
   WeekClose = iClose(_Symbol, PERIOD_W1, 1);

   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)));

//--------------------------------------------------------
//--------------------------------------------------------

   MonthHigh  = iHigh(_Symbol, PERIOD_MN1, 1);
   MonthLow   = iLow(_Symbol, PERIOD_MN1, 1);
   MonthClose = iClose(_Symbol, PERIOD_MN1, 1);

   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)
     {
      TimeToString(TimeCurrent());
      ObjectCreate(0, "PivotLine", OBJ_HLINE, 0, TimeCurrent(), Pivot);
      ObjectSetInteger(0, "PivotLine", OBJPROP_COLOR, clrMagenta);
      ObjectSetInteger(0, "PivotLine", OBJPROP_STYLE, STYLE_DASH);
      /*
       if(ObjectFind(0,"PivotLabel") != 0)
        {
         //ObjectCreate("PivotLabel", OBJ_TEXT, 0, Time[20], Pivot);
         //ObjectSetText("PivotLabel", ("Daily Pivot"), 12, "Arial", Magenta);
        }
       else
        {
         ObjectMove("PivotLabel", 0, Time[20], Pivot);
        }
        */
      ChartRedraw();

      //--------------------------------------------------------

      if(Daily_SR_Levels == true)
        {
         ObjectCreate(0, "R1_Line", OBJ_HLINE, 0, TimeCurrent(), R1);
         ObjectSetInteger(0, "R1_Line", OBJPROP_COLOR, clrRed);
         ObjectSetInteger(0, "R1_Line", OBJPROP_STYLE, STYLE_DASH);
         /*
         if(ObjectFind("R1_Label") != 0)
          {
           //ObjectCreate("R1_Label", OBJ_TEXT, 0, Time[20], R1);
           //ObjectSetText("R1_Label", "Daily R1", 12, "Arial", Red);
          }
         else
          {
           ObjectMove("R1_Label", 0, Time[20], R1);
          }
         */
         //--------------------------------------------------------

         ObjectCreate(0, "R2_Line", OBJ_HLINE, 0, TimeCurrent(), R2);
         ObjectSetInteger(0, "R2_Line", OBJPROP_COLOR, clrRed);
         ObjectSetInteger(0, "R2_Line", OBJPROP_STYLE, STYLE_DASH);
         /*
         if(ObjectFind("R2_Label") != 0)
         {
         //ObjectCreate("R2_Label", OBJ_TEXT, 0, Time[20], R2);
         //ObjectSetText("R2_Label", "Daily R2", 12, "Arial", Red);
         }
         else
         {
         ObjectMove("R2_Label", 0, Time[20], R2);
         }
         */
         //---------------------------------------------------------

         ObjectCreate(0, "R3_Line", OBJ_HLINE, 0, TimeCurrent(), R3);
         ObjectSetInteger(0, "R3_Line", OBJPROP_COLOR, clrRed);
         ObjectSetInteger(0, "R3_Line", OBJPROP_STYLE, STYLE_DASH);
         /*
         if(ObjectFind("R3_Label") != 0)
         {
         //ObjectCreate("R3_Label", OBJ_TEXT, 0, Time[20], R3);
         //ObjectSetText("R3_Label", "Daily R3", 12, "Arial", Red);
         }
         else
         {
         ObjectMove("R3_Label", 0, Time[20], R3);
         }
         */
         //---------------------------------------------------------

         ObjectCreate(0, "S1_Line", OBJ_HLINE, 0, TimeCurrent(), S1);
         ObjectSetInteger(0, "S1_Line", OBJPROP_COLOR, clrLimeGreen);
         ObjectSetInteger(0, "S1_Line", OBJPROP_STYLE, STYLE_DASH);
         /*
         if(ObjectFind("S1_Label") != 0)
         {
         ///ObjectCreate("S1_Label", OBJ_TEXT, 0, Time[20], S1);
         // ObjectSetText("S1_Label", "Daily S1", 12, "Arial", DarkBlue);
         }
         else
         {
         ObjectMove("S1_Label", 0, Time[20], S1);
         }
         */
         //---------------------------------------------------------

         ObjectCreate(0, "S2_Line", OBJ_HLINE, 0, TimeCurrent(), S2);
         ObjectSetInteger(0, "S2_Line", OBJPROP_COLOR, clrLimeGreen);
         ObjectSetInteger(0, "S2_Line", OBJPROP_STYLE, STYLE_DASH);
         /*
         if(ObjectFind("S2_Label") != 0)
         {
         //ObjectCreate("S2_Label", OBJ_TEXT, 0, Time[20], S2);
         // ObjectSetText("S2_Label", "Daily S2", 12, "Arial", DarkBlue);
         }
         else
         {
         ObjectMove("S2_Label", 0, Time[20], S2);
         }
         */
         //---------------------------------------------------------

         ObjectCreate(0, "S3_Line", OBJ_HLINE, 0, TimeCurrent(), S3);
         ObjectSetInteger(0, "S3_Line", OBJPROP_COLOR, clrLimeGreen);
         ObjectSetInteger(0, "S3_Line", OBJPROP_STYLE, STYLE_DASH);
         /*
         if(ObjectFind("S3_Label") != 0)
         {
         //ObjectCreate("S3_Label", OBJ_TEXT, 0, Time[20], S3);
         // ObjectSetText("S3_Label", "Daily S3", 12, "Arial", DarkBlue);
         }
         else
         {
         ObjectMove("S3_Label", 0, Time[20], S3);
         }
         */
        }
      ChartRedraw();
     }

//---------------------------------------------------------
//---------------------------------------------------------
//---------------------------------------------------------

   if(Weekly == true)
     {
      ObjectCreate(0, "WeekPivotLine", OBJ_HLINE, 0, TimeCurrent(), WeekPivot);
      ObjectSetInteger(0, "WeekPivotLine", OBJPROP_COLOR, WeeklyColors);
      ObjectSetInteger(0, "WeekPivotLine", OBJPROP_STYLE, STYLE_DASH);
      /*
      if(ObjectFind("WeekPivotLabel") != 0)
      {
      // ObjectCreate("WeekPivotLabel", OBJ_TEXT, 0, Time[30], WeekPivot);
      // ObjectSetText("WeekPivotLabel", "WeeklyPivot", 12, "Arial", Aqua);
      }
      else
      {
       ObjectMove("WeekPivotLabel", 0, Time[30], WeekPivot);
      }
      */
      //--------------------------------------------------------

      if(Weekly_SR_Levels == true)
        {
         ObjectCreate(0, "WR1_Line", OBJ_HLINE, 0, TimeCurrent(), WR1);
         ObjectSetInteger(0, "WR1_Line", OBJPROP_COLOR, WeeklyColors);
         ObjectSetInteger(0, "WR1_Line", OBJPROP_STYLE, STYLE_DASHDOTDOT);
         /*
         if(ObjectFind("WR1_Label") != 0)
         {
         //  ObjectCreate("WR1_Label", OBJ_TEXT, 0, Time[30], WR1);
         // ObjectSetText("WR1_Label", " Weekly R1", 12, "Arial", Yellow);
         }
         else
         {
          ObjectMove("WR1_Label", 0, Time[30], WR1);
         }
         */
         //--------------------------------------------------------

         ObjectCreate(0, "WR2_Line", OBJ_HLINE, 0, TimeCurrent(), WR2);
         ObjectSetInteger(0, "WR2_Line", OBJPROP_COLOR, WeeklyColors);
         ObjectSetInteger(0, "WR2_Line", OBJPROP_STYLE, STYLE_DASHDOTDOT);
         /*
         if(ObjectFind("WR2_Label") != 0)
         {
         //  ObjectCreate("WR2_Label", OBJ_TEXT, 0, Time[30], WR2);
         // ObjectSetText("WR2_Label", " Weekly R2", 12, "Arial", Yellow);
         }
         else
         {
         ObjectMove("WR2_Label", 0, Time[30], WR2);
         }
         */
         //---------------------------------------------------------

         ObjectCreate(0, "WR3_Line", OBJ_HLINE, 0, TimeCurrent(), WR3);
         ObjectSetInteger(0, "WR3_Line", OBJPROP_COLOR, WeeklyColors);
         ObjectSetInteger(0, "WR3_Line", OBJPROP_STYLE, STYLE_DASHDOTDOT);
         /*
         if(ObjectFind("WR3_Label") != 0)
         {
         // ObjectCreate("WR3_Label", OBJ_TEXT, 0, Time[30], WR3);
         // ObjectSetText("WR3_Label", " Weekly R3", 12, "Arial", Yellow);
         }
         else
         {
         ObjectMove("WR3_Label", 0, Time[30], WR3);
         }
         */
         //---------------------------------------------------------

         ObjectCreate(0, "WS1_Line", OBJ_HLINE, 0, TimeCurrent(), WS1);
         ObjectSetInteger(0, "WS1_Line", OBJPROP_COLOR, WeeklyColors);
         ObjectSetInteger(0, "WS1_Line", OBJPROP_STYLE, STYLE_DASHDOTDOT);
         /*
         if(ObjectFind("WS1_Label") != 0)
         {
         // ObjectCreate("WS1_Label", OBJ_TEXT, 0, Time[30], WS1);
         // ObjectSetText("WS1_Label", "Weekly S1", 12, "Arial", SteelBlue);
         }
         else
         {
         ObjectMove("WS1_Label", 0, Time[30], WS1);
         }
         */
         //---------------------------------------------------------

         ObjectCreate(0, "WS2_Line", OBJ_HLINE, 0, TimeCurrent(), WS2);
         ObjectSetInteger(0, "WS2_Line", OBJPROP_COLOR, WeeklyColors);
         ObjectSetInteger(0, "WS2_Line", OBJPROP_STYLE, STYLE_DASHDOTDOT);
         /*
         if(ObjectFind("WS2_Label") != 0)
         {
         //  ObjectCreate("WS2_Label", OBJ_TEXT, 0, Time[30], WS2);
         // ObjectSetText("WS2_Label", "Weekly S2", 12, "Arial", SteelBlue);
         }
         else
         {
         ObjectMove("WS2_Label", 0, Time[30], WS2);
         }
         */
         //---------------------------------------------------------

         ObjectCreate(0, "WS3_Line", OBJ_HLINE, 0, TimeCurrent(), WS3);
         ObjectSetInteger(0, "WS3_Line", OBJPROP_COLOR, WeeklyColors);
         ObjectSetInteger(0, "WS3_Line", OBJPROP_STYLE, STYLE_DASHDOTDOT);
         /*
         if(ObjectFind("WS3_Label") != 0)
         {
         // ObjectCreate("WS3_Label", OBJ_TEXT, 0, Time[30], WS3);
         //ObjectSetText("WS3_Label", "Weekly S3", 12, "Arial", SteelBlue);
         }
         else
         {
         ObjectMove("WS3_Label", 0, Time[30], WS3);
         }
         */
        }
      ChartRedraw();
     }

//---------------------------------------------------------
//---------------------------------------------------------
//---------------------------------------------------------

   if(Monthly == true)
     {
      ObjectCreate(0, "MonthPivotLine", OBJ_HLINE, 0, TimeCurrent(), MonthPivot);
      ObjectSetInteger(0, "MonthPivotLine", OBJPROP_COLOR, MonthlyColors);
      ObjectSetInteger(0, "MonthPivotLine", OBJPROP_STYLE, STYLE_DASH);
      /*
      if(ObjectFind("MonthPivotLabel") != 0)
      {
      // ObjectCreate("MonthPivotLabel", OBJ_TEXT, 0, Time[40], MonthPivot);
      // ObjectSetText("MonthPivotLabel", "MonthlyPivot", 12, "Arial", White);
      }
      else
      {
       ObjectMove("MonthPivotLabel", 0, Time[40], MonthPivot);
      }
      */
      //--------------------------------------------------------

      if(Monthly_SR_Levels == true)
        {
         ObjectCreate(0, "MR1_Line", OBJ_HLINE, 0, TimeCurrent(), MR1);
         ObjectSetInteger(0, "MR1_Line", OBJPROP_COLOR, MonthlyColors);
         ObjectSetInteger(0, "MR1_Line", OBJPROP_STYLE, STYLE_DASHDOTDOT);
         /*
         if(ObjectFind("MR1_Label") != 0)
         {
         // ObjectCreate("MR1_Label", OBJ_TEXT, 0, Time[40], MR1);
         // ObjectSetText("MR1_Label", " Monthly R1", 12, "Arial", Blue);
         }
         else
         {
          ObjectMove("MR1_Label", 0, Time[40], MR1);
         }
         */
         //--------------------------------------------------------

         ObjectCreate(0, "MR2_Line", OBJ_HLINE, 0, TimeCurrent(), MR2);
         ObjectSetInteger(0, "MR2_Line", OBJPROP_COLOR, MonthlyColors);
         ObjectSetInteger(0, "MR2_Line", OBJPROP_STYLE, STYLE_DASHDOTDOT);
         /*
         if(ObjectFind("MR2_Label") != 0)
         {
         // ObjectCreate("MR2_Label", OBJ_TEXT, 0, Time[40], MR2);
         //ObjectSetText("MR2_Label", " Monthly R2", 12, "Arial", Blue);
         }
         else
         {
         ObjectMove("MR2_Label", 0, Time[40], MR2);
         }
         */
         //---------------------------------------------------------

         ObjectCreate(0, "MR3_Line", OBJ_HLINE, 0, TimeCurrent(), MR3);
         ObjectSetInteger(0, "MR3_Line", OBJPROP_COLOR, MonthlyColors);
         ObjectSetInteger(0, "MR3_Line", OBJPROP_STYLE, STYLE_DASHDOTDOT);
         /*
         if(ObjectFind("MR3_Label") != 0)
         {
         // ObjectCreate("MR3_Label", OBJ_TEXT, 0, Time[40], MR3);
         // ObjectSetText("MR3_Label", " Monthly R3", 12, "Arial", Blue);
         }
         else
         {
         ObjectMove("MR3_Label", 0, Time[40], MR3);
         }
         */
         //---------------------------------------------------------

         ObjectCreate(0, "MS1_Line", OBJ_HLINE, 0, TimeCurrent(), MS1);
         ObjectSetInteger(0, "MS1_Line", OBJPROP_COLOR, MonthlyColors);
         ObjectSetInteger(0, "MS1_Line", OBJPROP_STYLE, STYLE_DASHDOTDOT);
         /*
         if(ObjectFind("MS1_Label") != 0)
         {
         //ObjectCreate("MS1_Label", OBJ_TEXT, 0, Time[40], MS1);
         //ObjectSetText("MS1_Label", "Monthly S1", 12, "Arial", Silver);
         }
         else
         {
         ObjectMove("MS1_Label", 0, Time[40], MS1);
         }
         */
         //---------------------------------------------------------

         ObjectCreate(0, "MS2_Line", OBJ_HLINE, 0, TimeCurrent(), MS2);
         ObjectSetInteger(0, "MS2_Line", OBJPROP_COLOR, MonthlyColors);
         ObjectSetInteger(0, "MS2_Line", OBJPROP_STYLE, STYLE_DASHDOTDOT);
         /*
         if(ObjectFind("MS2_Label") != 0)
         {
         //ObjectCreate("MS2_Label", OBJ_TEXT, 0, Time[40], MS2);
         //ObjectSetText("MS2_Label", "Monthly S2", 12, "Arial", Silver);
         }
         else
         {
         ObjectMove("MS2_Label", 0, Time[40], MS2);
         }
         */
         //---------------------------------------------------------

         ObjectCreate(0, "MS3_Line", OBJ_HLINE, 0, TimeCurrent(), MS3);
         ObjectSetInteger(0, "MS3_Line", OBJPROP_COLOR, MonthlyColors);
         ObjectSetInteger(0, "MS3_Line", OBJPROP_STYLE, STYLE_DASHDOTDOT);
         /*
         if(ObjectFind("MS3_Label") != 0)
         {
         //ObjectCreate("MS3_Label", OBJ_TEXT, 0, Time[40], MS3);
         //ObjectSetText("MS3_Label", "Monthly S3", 12, "Arial", Silver);
         }
         else
         {
         ObjectMove("MS3_Label", 0, Time[40], MS3);
         }
         */
        }

      ChartRedraw();
     }
//---------------------------------------------------------


   return(rates_total);
  }
//---------------------------------------------------------
int DayOfWeekMQL4()
  {
   MqlDateTime tm;
   TimeCurrent(tm);
   return(tm.day_of_week);
  }
//---------------------------------------------------------


📈 ROBOTFX MetaTrader Expert Advisors and Indicators to maximize profits and minimize the risks