//+------------------------------------------------------------------+
//|                                                 Testing days.mq4 |
//|                      Copyright © 2006, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, LightKeeper"


#property indicator_chart_window

#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Blue

//buffers
string strDayOfWeek;
int i;
int intWeek;
string strName[15000], strLimit[15000], strLimitLabel[15000];        
string strGo[15000], strSL[15000]; //weekly low line name
int intW; //Week Number
double WH, WL, PWH, WC, WH2, WH3, WL2, WL3, WC2, WC3, WO, WO2, WO3,SL, FridayHigh, FridayLow, FridayClose; 
double FridayMid;        
double Target, Entry;
string Direction;  
double lineLen;
double CurrentClose;
//double PP;
int linelen;
double ToLose;
double ToWin;
double length;
string strWeekHighText[15000]; //weekly high label 
string strWeekLowText [15000]; //weekly low label
string strLineNameH[15000]; // Weekly high line name
string strLineNameM[15000]; // Weekly mid point
string strPivotName[15000]; //name for pivot line
string Times[15000];
string flagDisplayed;
string strMondayNotBeen;
extern color entrycolor=Aqua;
extern color targetcolor=Yellow;
extern color stoplosscolor=Red;
extern double WinLoseRatio = 0;

//extern bool showMid=true;
//extern bool boolPivot=true;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {


   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
  
  ObjectsDeleteAll();
   return(0);
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()

  {


//// if not hourly or 4H chart, there's no lines (for now)
linelen=0;

///// 4 H CHART FOUND -------- set lines to the right length
if (Time[1]-Time[2]==14400)
   {linelen=26;}
if (Time[1]-Time[2]==187200)
   {linelen=26;}

//// 1H chart found ---------- set lines to the right length
if (Time[1]-Time[2]==3600)
   {linelen=110;}
   


intW = 0; 
flagDisplayed="no";
i=Bars;  //Bars;
   while(i>=0)
     {
  
  
//----
   strDayOfWeek=TimeDayOfWeek(Time[i]);
  
  
  ////************* just to work out the Time of each chart period
  // Times[i]="time " + i;
  // ObjectCreate(Times[i], OBJ_TEXT, 0, Time[i], High[i] );
  // ObjectSetText(Times[i], " "+(Time[i]-Time[i+1]) , 9, "Times New Roman", Red);
   
   
   if (strDayOfWeek != "1") {
        strMondayNotBeen="yes";
     
         
         }
   
   //it's Friday, calculate Friday's Hi, Lo and Median
   if (strDayOfWeek=="5") {
      //Hi
      if (High[i]>FridayHigh){FridayHigh=High[i];}
      if (FridayLow==0){FridayLow=Low[i];}
      if (Low[i]<FridayLow){FridayLow=Low[i];}
      FridayClose=Close[i];
   
   
   }
   
   
   //it's monday   
   if ((strDayOfWeek=="1")&&(strMondayNotBeen=="yes")) {  // Only draw lines once on each Monday
      //reset friday hi and Low
      FridayMid=FridayLow+((FridayHigh-FridayLow)/2);
      FridayHigh=0;
      FridayLow=0;
      
      
      WL3=WL2;
      WL2=WL;
      WH3=WH2;
      WH2=WH;
      WC3=WC2;
      WC2=WC;
      WC=FridayClose;
      WO3=WO2;
      WO2=WO;
      WO=Open[i];
      
      strLineNameH[i]="Weekly High -"+i;
      ObjectCreate(strLineNameH[i], OBJ_TREND, 0, Time[i], 0,  Time[i], WH*2);
      ObjectSet (strLineNameH[i], OBJPROP_RAY, 0);
      
      //Range of previous week was lower than range of week before it
      if (MathAbs(WH3-WL3)>MathAbs(WH2-WL2)){
         //if last week was bearish we go SHORT 
         if (WO2>WO){
            Entry=WH2-((WH2-WL2)/2);
            Target=WL2;
            SL=WH2;
            Direction="Go short Trend";
             
         }else{
            Entry=WH2-((WH2-WL2)/2);
            Target=WH2;
            SL=WL2;
            Direction="Go Long Trend";
         
         
         }
      
      
      
      }
      
      
      //Range of previous week was higher than range of week before it
      if (MathAbs(WH3-WL3)<MathAbs(WH2-WL2)){
            
      
      // last week was bearish (we go long)
         if (WO2>WO){
            SL=WL2;
            Target=WO+((WH2-WL2)/2);
            Entry=FridayMid;
            Direction="Go Long Fade";
         }else{
         
            SL=WH2;
            Target=WO-((WH2-WL2)/2);
            Entry=FridayMid;
            Direction="Go Short Fade";
         
         
         
         }
         }
            
         //does target meet our requirements?
         ToLose=MathAbs(SL-Entry);
         ToWin=MathAbs(Target-Entry);   
         
         
         if (ToWin>ToLose*WinLoseRatio){
            if (i>=linelen) {length=Time[i-linelen];}else{length=Time[0];}
            strGo[i]="Entry point - " + WO2 + " " + WC2 + " " +i;
            ObjectCreate(strGo[i], OBJ_TREND, 0, Time[i], Entry, length, Entry);
            ObjectSet(strGo[i], OBJPROP_COLOR, entrycolor);
            ObjectSet (strGo[i], OBJPROP_RAY, 0);
            
            strWeekHighText[i]="Entry " + WO2 +" "+i;
            ObjectCreate(strWeekHighText[i], OBJ_TEXT, 0, Time[i-(linelen/3)]  , Entry);//Time[i-(linelen/5)]
            ObjectSetText(strWeekHighText[i], Direction  , 9, "Times New Roman", entrycolor);
            
            strLimit[i]="Limit - " +i;
            ObjectCreate(strLimit[i], OBJ_TREND, 0, Time[i], Target, length, Target);
            ObjectSet(strLimit[i], OBJPROP_COLOR, targetcolor);
            ObjectSet (strLimit[i], OBJPROP_RAY, 0);
            ObjectSet(strLimit[i], OBJPROP_STYLE, STYLE_DASH);
            
            strSL[i]="Stop Loss - " + SL + i;
            ObjectCreate(strSL[i], OBJ_TREND, 0, Time[i], SL, length, SL);
            ObjectSet(strSL[i], OBJPROP_COLOR, stoplosscolor);
            ObjectSet (strSL[i], OBJPROP_RAY, 0);
            ObjectSet (strSL[i], OBJPROP_STYLE, STYLE_DASH);
         }
         //}
            
      
      
      intWeek++;
      //WeeklyH[intWeek]=WH;
      WH=High[i];
      WL=Low[i];
      strMondayNotBeen="no";
   
   }
   
   
   
   
   if ((High[i])>WH) {
      WH=High[i];
   
   }
   
   if (Low[i]<WL) {
      WL=Low[i];
   
   }
   


     
CurrentClose=Close[i];      

//----

i--;
}
   return(0);
  }
//+------------------------------------------------------------------+