//+------------------------------------------------------------------+
//|                                                    mondayway.mq4 |
//|                                                alexsilver@gmx.de |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 6

#property indicator_width1 3     
#property indicator_width2 3
#property indicator_width3 2
#property indicator_width4 2
#property indicator_width5 2
#property indicator_width6 2

#property indicator_color1 Aqua 
#property indicator_color2 DeepPink
#property indicator_color3 Blue
#property indicator_color4 Yellow
#property indicator_color5 Blue
#property indicator_color6 Yellow

extern int first_4h_candle = 1; 
extern bool use_open_close_instead_of_High_Low = false;
extern double targetfactor_bull = 1;
extern double sl_factor_bull = 1;
double targetfactor_bear = 1;
double sl_factor_bear = 1;
extern int extrapips = 50;
double extra_pip = 0; 
extern bool original_sl = false;
extern int begin_drawing_at_bar = 1;


double inside[];
double outside[];
double buystop[];
double sellstop[];
double t1[];
double t2[];

double lastMondayHigh;
double lastMondayLow;


//+------------------------------------------------------------------------------------------------------------+
int init(){
   extra_pip = extrapips*MarketInfo(NULL,MODE_POINT);
   targetfactor_bear = targetfactor_bull;
   sl_factor_bear = sl_factor_bull;

   
   SetIndexBuffer(0, inside);
   SetIndexBuffer(1, outside);
   SetIndexBuffer(2, buystop);
   SetIndexBuffer(3, sellstop);
   SetIndexBuffer(4, t1);
   SetIndexBuffer(5, t2);
   
   SetIndexStyle (0, DRAW_LINE);
   SetIndexStyle (1, DRAW_LINE);
   SetIndexStyle (2, DRAW_LINE);
   SetIndexStyle (3, DRAW_LINE);
   SetIndexStyle (4, DRAW_LINE);
   SetIndexStyle (5, DRAW_LINE);
   
   SetIndexLabel (0,"-");  
   SetIndexLabel (1,"-");
   SetIndexLabel (2,"High");  
   SetIndexLabel (3,"Low");
   SetIndexLabel (2,"-");  
   SetIndexLabel (3,"-");
   
   IndicatorDigits(Digits);
   
   return(0);
}
//+------------------------------------------------------------------------------------------------------------+
int start(){
   int limit = Bars - IndicatorCounted() -2;  
   
   if( limit < 1 )
       limit = 1;
   
   for( int n=limit; n>=begin_drawing_at_bar; n-- ){
      if(last_monday_values(n)){       // if values are valid
         
         if (iHigh(NULL,0,n)>(lastMondayHigh+extra_pip)){
            inside[n] = iHigh(NULL,0,n);
         } else inside[n] = EMPTY_VALUE;
         
         if (iLow(NULL,0,n)<(lastMondayLow-extra_pip)){
            outside[n] = iLow(NULL,0,n);
         } else outside[n] = EMPTY_VALUE;
         
         buystop[n]= lastMondayHigh+extra_pip;
         sellstop[n]= lastMondayLow-extra_pip;
         
         t1[n] = precision(lastMondayHigh+(lastMondayHigh-lastMondayLow)*targetfactor_bull+extra_pip,Digits);
         t2[n] = precision(lastMondayLow -(lastMondayHigh-lastMondayLow)*targetfactor_bear-extra_pip,Digits);
         
      }
      else{                                                
         inside[n] = EMPTY_VALUE;
         outside[n]= EMPTY_VALUE;
         buystop[n]= EMPTY_VALUE;
         sellstop[n]= EMPTY_VALUE;
         t1[n] = EMPTY_VALUE;
         t2[n] = EMPTY_VALUE;
      }
   }

   return(0);
}


bool last_monday_values (int shift){
   bool valid=true;
   
   // find date according to shift
   datetime start = iTime(NULL,0,shift);
   int weekdaynow = TimeDayOfWeek(start);
   
   // find first candle this week
   bool this_week = true;
   int monshift = iBarShift(NULL,PERIOD_H4,start,false); // start with current hour..
   int monshift_start = monshift;
   
   while (this_week){
      datetime testdate = iTime(NULL,PERIOD_H4,monshift);
      //Print("monshift h1 ",monshift);
      if ((TimeDayOfWeek(testdate)>4)&&(TimeHour(testdate)>16)) this_week = false;
      if (monshift>1250) this_week = false;
      monshift++;
   }
   monshift--; //this is shift of the first candle
   monshift-= first_4h_candle;
   if (monshift_start>=monshift) valid = false;
   
   // put high and low values in buffer
   if(use_open_close_instead_of_High_Low){
      lastMondayHigh = High_close_or_open(monshift,PERIOD_H4);
      lastMondayLow  = Low_close_or_open(monshift,PERIOD_H4);
   } else {
      lastMondayHigh = iHigh(NULL,PERIOD_H4,monshift);
      lastMondayLow = iLow(NULL,PERIOD_H4,monshift);
   }
   
   return valid;
}


double High_close_or_open (int shift, int timeframe){
   if (iOpen(NULL,timeframe,shift)>iClose(NULL,timeframe,shift)) return iOpen(NULL,timeframe,shift);
   else return iClose(NULL,timeframe,shift);
}

double Low_close_or_open (int shift, int timeframe){
   if (iOpen(NULL,timeframe,shift)<iClose(NULL,timeframe,shift)) return iOpen(NULL,timeframe,shift);
   else return iClose(NULL,timeframe,shift);
}

double precision (double inpd,int digits){
   double out = 0;
   int helper = 0;
   int divisor = MathPow(10,digits);
   inpd*=divisor;
   helper = (int) inpd;      //dicard by doing type conversion
   out = (double) helper;
   out/= (double) divisor; 
   return NormalizeDouble(out,digits);
}