//+------------------------------------------------------------------
//| Original idea for this indicator by San4x                                        
//+------------------------------------------------------------------
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 RoyalBlue
#property indicator_color2 OrangeRed
#property indicator_color3 RoyalBlue
#property indicator_color4 RoyalBlue
#property indicator_color5 OrangeRed
#property indicator_color6 OrangeRed
#property indicator_style1 STYLE_DOT
#property indicator_style2 STYLE_DOT
#property indicator_width3 3
#property indicator_width4 3
#property indicator_width5 3
#property indicator_width6 3

extern int offset        = 3;
extern int BreakoutPrice = PRICE_CLOSE;

double Y_HIGH[];
double Y_LOW[];
double Y_HIGHBa[];
double Y_HIGHBb[];
double Y_LOWBa[];
double Y_LOWBb[];
double state[];


int init() {
   string ind_name = "Weekly_HiLo";
   IndicatorBuffers(7);
      SetIndexBuffer(0, Y_HIGH);   SetIndexLabel(0, ind_name);
      SetIndexBuffer(1, Y_LOW);    SetIndexLabel(1, ind_name);
      SetIndexBuffer(2, Y_HIGHBa); SetIndexLabel(2, ind_name);
      SetIndexBuffer(3, Y_HIGHBb); SetIndexLabel(3, ind_name);
      SetIndexBuffer(4, Y_LOWBa);  SetIndexLabel(4, ind_name);
      SetIndexBuffer(5, Y_LOWBa);  SetIndexLabel(5, ind_name);
      SetIndexBuffer(6, state);
      IndicatorShortName(ind_name);
   return (0);
}

int deinit() {
   ObjectDelete("PrevWeekHi");
   ObjectDelete("PrevWeekLo");
   return (0);
}

int start() {
   int counted_bars=IndicatorCounted();
      if (Period()>PERIOD_MN1) return (-1);
      if(counted_bars<0)      return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars - counted_bars,Bars-1);

         ObjectCreate("PrevWeekHi", OBJ_TEXT, 0, 0, 0); ObjectSetText("PrevWeekHi", "                LWH", 7, "Arial", RoyalBlue);
         ObjectCreate("PrevWeekLo", OBJ_TEXT, 0, 0, 0); ObjectSetText("PrevWeekLo", "                LWL", 7, "Arial", OrangeRed);
         
         double last_week_high = Y_HIGH[limit]; double prev_week_high = last_week_high;
         double last_week_low  = Y_LOW[limit];  double prev_week_low  = last_week_low;
               for ( ; limit<(Bars-1); limit++) if (Y_HIGH[limit+1]!=last_week_high || Y_LOW[limit+1]!=last_week_low) break;

         if (state[limit]== 1) CleanPoint(limit,Y_HIGHBa,Y_HIGHBb);
         if (state[limit]==-1) CleanPoint(limit,Y_LOWBa,Y_LOWBb);
         
         for (int i = limit; i >= 0; i--) {
            if (High[i+1] > last_week_high) last_week_high = High[i+1];
            if (Low[i+1]  < last_week_low)  last_week_low  = Low[i+1];
      
            if (TimeDay(Time[i]) != TimeDay(Time[i+1])) {
               if(TimeDayOfWeek(Time[i])==1) {
                  prev_week_high  = last_week_high;
                  prev_week_low   = last_week_low;
                  last_week_low  = Open[i];
                  last_week_high = Open[i];
                  ObjectMove("PrevWeekHi", 0, Time[i], prev_week_high);
                  ObjectMove("PrevWeekLo", 0, Time[i], prev_week_low);
               }
            }
            
            Y_HIGHBa[i] = EMPTY_VALUE;
            Y_HIGHBb[i] = EMPTY_VALUE;
            Y_LOWBa[i]  = EMPTY_VALUE;
            Y_LOWBb[i]  = EMPTY_VALUE;
            Y_HIGH[i]   = prev_week_high;
            Y_LOW[i]    = prev_week_low;
            state[i]    = state[i+1];
               double price = iMA(NULL,0,1,0,MODE_SMA,BreakoutPrice,i);
                  if (price>prev_week_high)                  state[i]= 1;
                  if (price<prev_week_low)                   state[i]=-1;
                  if (price<prev_week_high && price>prev_week_low) state[i]= 0;
                  if (state[i]== 1) PlotPoint(i,Y_HIGHBa,Y_HIGHBb,Y_HIGH);
                  if (state[i]==-1) PlotPoint(i,Y_LOWBa,Y_LOWBb,Y_LOW);
                  
         }
         return (0);
}

void CleanPoint(int i,double& first[],double& second[]) {
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}


void PlotPoint(int i,double& first[],double& second[],double& from[]) {
   if (first[i+1] == EMPTY_VALUE)
      {
         if (first[i+2] == EMPTY_VALUE) {
                first[i]   = from[i];
                first[i+1] = from[i+1];
                second[i]  = EMPTY_VALUE;
            }
         else {
                second[i]   =  from[i];
                second[i+1] =  from[i+1];
                first[i]    = EMPTY_VALUE;
            }
      }
   else
      {
         first[i]   = from[i];
         second[i]  = EMPTY_VALUE;
      }
}