//+------------------------------------------------------------------+
//|                                                  WeeklyPivot.mq5 |
//|                     Converted from MQL4 to MQL5                  |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 7
#property indicator_plots   7

#property indicator_label1  "Weekly Pivot"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrMagenta
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2

#property indicator_label2  "W_S1"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrRoyalBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2

#property indicator_label3  "W_R1"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrCrimson
#property indicator_style3  STYLE_SOLID
#property indicator_width3  2

#property indicator_label4  "W_S2"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrRoyalBlue
#property indicator_style4  STYLE_SOLID
#property indicator_width4  2

#property indicator_label5  "W_R2"
#property indicator_type5   DRAW_LINE
#property indicator_color5  clrCrimson
#property indicator_style5  STYLE_SOLID
#property indicator_width5  2

#property indicator_label6  "W_S3"
#property indicator_type6   DRAW_LINE
#property indicator_color6  clrSeaGreen
#property indicator_style6  STYLE_SOLID
#property indicator_width6  2

#property indicator_label7  "W_R3"
#property indicator_type7   DRAW_LINE
#property indicator_color7  clrSeaGreen
#property indicator_style7  STYLE_SOLID
#property indicator_width7  2

//---- buffers
double PBuffer[];
double S1Buffer[];
double R1Buffer[];
double S2Buffer[];
double R2Buffer[];
double S3Buffer[];
double R3Buffer[];

int fontsize = 10;

double P, S1, R1, S2, R2, S3, R3;
double last_week_high, last_week_low, this_week_open, last_week_close;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0, PBuffer,  INDICATOR_DATA);
   SetIndexBuffer(1, S1Buffer, INDICATOR_DATA);
   SetIndexBuffer(2, R1Buffer, INDICATOR_DATA);
   SetIndexBuffer(3, S2Buffer, INDICATOR_DATA);
   SetIndexBuffer(4, R2Buffer, INDICATOR_DATA);
   SetIndexBuffer(5, S3Buffer, INDICATOR_DATA);
   SetIndexBuffer(6, R3Buffer, INDICATOR_DATA);

   // Initialize all buffers with EMPTY_VALUE so unset bars are blank
   ArrayInitialize(PBuffer,  EMPTY_VALUE);
   ArrayInitialize(S1Buffer, EMPTY_VALUE);
   ArrayInitialize(R1Buffer, EMPTY_VALUE);
   ArrayInitialize(S2Buffer, EMPTY_VALUE);
   ArrayInitialize(R2Buffer, EMPTY_VALUE);
   ArrayInitialize(S3Buffer, EMPTY_VALUE);
   ArrayInitialize(R3Buffer, EMPTY_VALUE);

   IndicatorSetString(INDICATOR_SHORTNAME, "WeeklyPivotPoint");
   PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, 1);

   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ObjectDelete(0, "WeeklyPivot");
   ObjectDelete(0, "Sup1");
   ObjectDelete(0, "Res1");
   ObjectDelete(0, "Sup2");
   ObjectDelete(0, "Res2");
   ObjectDelete(0, "Sup3");
   ObjectDelete(0, "Res3");
   ChartRedraw(0);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
   // Index arrays as in MQL4: index 0 = oldest bar
   ArraySetAsSeries(time,  false);
   ArraySetAsSeries(open,  false);
   ArraySetAsSeries(high,  false);
   ArraySetAsSeries(low,   false);
   ArraySetAsSeries(close, false);

   ArraySetAsSeries(PBuffer,  false);
   ArraySetAsSeries(S1Buffer, false);
   ArraySetAsSeries(R1Buffer, false);
   ArraySetAsSeries(S2Buffer, false);
   ArraySetAsSeries(R2Buffer, false);
   ArraySetAsSeries(S3Buffer, false);
   ArraySetAsSeries(R3Buffer, false);

   int limit;
   if(prev_calculated == 0)
     {
      limit = 0;

      // Create text label objects (only on first full calculation)
      if(ObjectFind(0, "WeeklyPivot") < 0)
        {
         ObjectCreate(0, "WeeklyPivot", OBJ_TEXT, 0, 0, 0);
         ObjectSetString(0, "WeeklyPivot", OBJPROP_TEXT, "                            Weekly Pivot Point");
         ObjectSetInteger(0, "WeeklyPivot", OBJPROP_FONTSIZE, fontsize);
         ObjectSetString(0, "WeeklyPivot",  OBJPROP_FONT, "Arial");
         ObjectSetInteger(0, "WeeklyPivot", OBJPROP_COLOR, clrRed);
        }
      if(ObjectFind(0, "Sup1") < 0)
        {
         ObjectCreate(0, "Sup1", OBJ_TEXT, 0, 0, 0);
         ObjectSetString(0, "Sup1",  OBJPROP_TEXT, "        wS 1");
         ObjectSetInteger(0, "Sup1", OBJPROP_FONTSIZE, fontsize);
         ObjectSetString(0, "Sup1",  OBJPROP_FONT, "Arial");
         ObjectSetInteger(0, "Sup1", OBJPROP_COLOR, clrRed);
        }
      if(ObjectFind(0, "Res1") < 0)
        {
         ObjectCreate(0, "Res1", OBJ_TEXT, 0, 0, 0);
         ObjectSetString(0, "Res1",  OBJPROP_TEXT, "        wR 1");
         ObjectSetInteger(0, "Res1", OBJPROP_FONTSIZE, fontsize);
         ObjectSetString(0, "Res1",  OBJPROP_FONT, "Arial");
         ObjectSetInteger(0, "Res1", OBJPROP_COLOR, clrRed);
        }
      if(ObjectFind(0, "Sup2") < 0)
        {
         ObjectCreate(0, "Sup2", OBJ_TEXT, 0, 0, 0);
         ObjectSetString(0, "Sup2",  OBJPROP_TEXT, "        wS 2");
         ObjectSetInteger(0, "Sup2", OBJPROP_FONTSIZE, fontsize);
         ObjectSetString(0, "Sup2",  OBJPROP_FONT, "Arial");
         ObjectSetInteger(0, "Sup2", OBJPROP_COLOR, clrRed);
        }
      if(ObjectFind(0, "Res2") < 0)
        {
         ObjectCreate(0, "Res2", OBJ_TEXT, 0, 0, 0);
         ObjectSetString(0, "Res2",  OBJPROP_TEXT, "        wR 2");
         ObjectSetInteger(0, "Res2", OBJPROP_FONTSIZE, fontsize);
         ObjectSetString(0, "Res2",  OBJPROP_FONT, "Arial");
         ObjectSetInteger(0, "Res2", OBJPROP_COLOR, clrRed);
        }
      if(ObjectFind(0, "Sup3") < 0)
        {
         ObjectCreate(0, "Sup3", OBJ_TEXT, 0, 0, 0);
         ObjectSetString(0, "Sup3",  OBJPROP_TEXT, "        wS 3");
         ObjectSetInteger(0, "Sup3", OBJPROP_FONTSIZE, fontsize);
         ObjectSetString(0, "Sup3",  OBJPROP_FONT, "Arial");
         ObjectSetInteger(0, "Sup3", OBJPROP_COLOR, clrRed);
        }
      if(ObjectFind(0, "Res3") < 0)
        {
         ObjectCreate(0, "Res3", OBJ_TEXT, 0, 0, 0);
         ObjectSetString(0, "Res3",  OBJPROP_TEXT, "        wR 3");
         ObjectSetInteger(0, "Res3", OBJPROP_FONTSIZE, fontsize);
         ObjectSetString(0, "Res3",  OBJPROP_FONT, "Arial");
         ObjectSetInteger(0, "Res3", OBJPROP_COLOR, clrRed);
        }

      // Reset tracking variables
      last_week_high = 0;
      last_week_low  = 0;
      P = S1 = R1 = S2 = R2 = S3 = R3 = 0;
     }
   else
     {
      limit = prev_calculated - 1;
     }

   for(int i = limit; i < rates_total - 1; i++)
     {
      // Detect Monday (day-of-week == 1) after a non-Monday bar
      MqlDateTime dt_cur, dt_prev;
      TimeToStruct(time[i],   dt_cur);
      TimeToStruct(time[i+1], dt_prev);

      if(dt_cur.day_of_week == 1 && dt_prev.day_of_week != 1)
        {
         last_week_close  = close[i-1 >= 0 ? i-1 : 0]; // bar just before Monday
         this_week_open   = open[i];

         // Pivot calculation (same formula as MQL4 original)
         P  = (last_week_high + last_week_low + this_week_open + last_week_close) / 4.0;
         R1 = (2 * P) - last_week_low;
         S1 = (2 * P) - last_week_high;
         R2 = P + (last_week_high - last_week_low);
         S2 = P - (last_week_high - last_week_low);
         R3 = (2 * P) + (last_week_high - (2 * last_week_low));
         S3 = (2 * P) - ((2 * last_week_high) - last_week_low);

         // Reset weekly high/low tracking for the new week
         last_week_low  = low[i];
         last_week_high = high[i];

         // Move text labels to the start of this week
         ObjectSetInteger(0, "WeeklyPivot", OBJPROP_TIME,  time[i]);
         ObjectSetDouble(0,  "WeeklyPivot", OBJPROP_PRICE, P);
         ObjectSetInteger(0, "Sup1", OBJPROP_TIME,  time[i]);
         ObjectSetDouble(0,  "Sup1", OBJPROP_PRICE, S1);
         ObjectSetInteger(0, "Res1", OBJPROP_TIME,  time[i]);
         ObjectSetDouble(0,  "Res1", OBJPROP_PRICE, R1);
         ObjectSetInteger(0, "Sup2", OBJPROP_TIME,  time[i]);
         ObjectSetDouble(0,  "Sup2", OBJPROP_PRICE, S2);
         ObjectSetInteger(0, "Res2", OBJPROP_TIME,  time[i]);
         ObjectSetDouble(0,  "Res2", OBJPROP_PRICE, R2);
         ObjectSetInteger(0, "Sup3", OBJPROP_TIME,  time[i]);
         ObjectSetDouble(0,  "Sup3", OBJPROP_PRICE, S3);
         ObjectSetInteger(0, "Res3", OBJPROP_TIME,  time[i]);
         ObjectSetDouble(0,  "Res3", OBJPROP_PRICE, R3);
        }

      // Track weekly high/low as we move forward
      if(last_week_high == 0 || high[i] > last_week_high) last_week_high = high[i];
      if(last_week_low  == 0 || low[i]  < last_week_low)  last_week_low  = low[i];

      // Fill buffers with current pivot values
      PBuffer[i]  = (P  != 0) ? P  : EMPTY_VALUE;
      S1Buffer[i] = (S1 != 0) ? S1 : EMPTY_VALUE;
      R1Buffer[i] = (R1 != 0) ? R1 : EMPTY_VALUE;
      S2Buffer[i] = (S2 != 0) ? S2 : EMPTY_VALUE;
      R2Buffer[i] = (R2 != 0) ? R2 : EMPTY_VALUE;
      S3Buffer[i] = (S3 != 0) ? S3 : EMPTY_VALUE;
      R3Buffer[i] = (R3 != 0) ? R3 : EMPTY_VALUE;
     }

   ChartRedraw(0);
   return(rates_total);
  }
//+------------------------------------------------------------------+