//+------------------------------------------------------------------+
//|                            _tp BrianIndex (SinglePair) V2.02.mq4 |
//|                                                                  |
//|  V2.00                                                           |
//|     1) Add 90% & 30% lines                                       |
//|                                                                  |
//|  V2.01                                                           |
//|     1) Show multiple history week value                          |
//|                                                                  |
//|  V2.02                                                           |
//|     1) Fix ObjectCreate error                                    |
//+------------------------------------------------------------------+

// ***********************************************                     modified by CODEMUNCHER@GMAIL.COM


#property indicator_chart_window
#property indicator_buffers      7
#property indicator_color1       Blue        
#property indicator_color2       Orange       
#property indicator_color3       Orange        
#property indicator_color4       Lime        
#property indicator_color5       Lime        
#property indicator_color6       Magenta        
#property indicator_color7       Magenta   


#property indicator_style1 STYLE_SOLID
#property indicator_style2 STYLE_DOT
#property indicator_style3 STYLE_DOT
#property indicator_style4 STYLE_DOT
#property indicator_style5 STYLE_DOT
#property indicator_style6 STYLE_DASH
#property indicator_style7 STYLE_DASH

#property indicator_level1       0
#property indicator_levelcolor   DarkGray


extern bool   Sound.Alert    = true ;
extern bool ShowLable        = true;
extern int    BoxSize        = 2;

extern double Level1Factor = 2.3;
extern double Level2Factor = 0.85;
extern int HistoryWeek      = 104;
extern int FontSize         = 16;

string TimeFrame_value = "TimeFrame(0,M5,M15,M30,H1,D1)";
string TimeFrame = "0";
string MA_Type_value = "SMA, EMA, LWMA";
string MA_Type = "SMA";

int timeFrame = 0;
double pair1Close;
int totalWeek = 0;
int lastDay = 0;
int maType;

double cmBufBILLLevel15[];
double cmBufBIHHLevel15[];

double cmBufBI[];
double cmBufBIHHLevel1[];
double cmBufBIHHLevel2[];
double cmBufBILLLevel2[];
double cmBufBILLLevel1[];


double BufBI[];
double BufBIHHLevel1[];
double BufBIHHLevel2[];
double BufBILLLevel2[];
double BufBILLLevel1[];

int MaxWeek = 0;
double WeekCloseArray[];
double WeekHHpipsArray[];
double WeekLLpipsArray[];
int History2, History3, History4, History5;

datetime LastWeekDateTime = 0;
int LastWeekBarShift;


//+------------------------------------------------------------------+

string sShortName = "_tp BI (SP) V2.02";

int objectcnt=1;


string symbol, tChartPeriod,  tShortName ;  
int    digits, period  ; 

 string note2 = "Default Font Color";
 color  cmFontColor              = DimGray;
 string note3                  = "Font Size";
 int cmFontSize                  = 8;
 string note4                  = "Font Type";
 string cmFontType               = "Verdana"; // Tahoma Courier

bool Trigger ;

int OldBars = -1 ;



//+------------------------------------------------------------------+

int init() {


   period       = Period() ;     
   tChartPeriod =  TimeFrameToString(period) ;
   symbol       =  Symbol() ;
   digits       =  Digits ;  

//   IndicatorBuffers(5);

   IndicatorShortName(sShortName);


   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexStyle(4,DRAW_LINE);
   SetIndexStyle(5,DRAW_LINE);
   SetIndexStyle(6,DRAW_LINE); 
 
   
   SetIndexLabel(0,"BI");
   SetIndexLabel(1,"HH1");
   SetIndexLabel(2,"HH22");
   SetIndexLabel(3,"LL2");
   SetIndexLabel(4,"LL1");
   SetIndexLabel(5,"HH1.5");
   SetIndexLabel(6,"LL1.5");
   
   SetIndexBuffer(0,cmBufBI);
   SetIndexBuffer(1,cmBufBIHHLevel1);
   SetIndexBuffer(2,cmBufBIHHLevel2);
   SetIndexBuffer(3,cmBufBILLLevel2);
   SetIndexBuffer(4,cmBufBILLLevel1);
   SetIndexBuffer(5,cmBufBIHHLevel15);
   SetIndexBuffer(6,cmBufBILLLevel15);   
   
 
   if (MA_Type == "SMA") {

      maType = MODE_SMA;

   } else if (MA_Type == "EMA") {

      maType = MODE_EMA;

   } else if (MA_Type == "LWMA") {

      maType = MODE_LWMA;

   } else {

      maType = MODE_SMA;

   }


   if (TimeFrame == "M5") {

      timeFrame = PERIOD_M5;

   } else if (TimeFrame == "M15") {

      timeFrame = PERIOD_M15;

   } else if (TimeFrame == "M30") {

      timeFrame = PERIOD_M30;

   } else if (TimeFrame == "H1") {

      timeFrame = PERIOD_H1;

   } else if (TimeFrame == "D1") {

      timeFrame = PERIOD_D1;

   } else {

      timeFrame = 0;

   }

//   HistoryWeek = MathMin(HistoryWeek+1, iBars(NULL,PERIOD_W1)) - 1;
   ArrayResize(WeekCloseArray, HistoryWeek);
   ArrayResize(WeekHHpipsArray, HistoryWeek);
   ArrayResize(WeekLLpipsArray, HistoryWeek);

   return(0);

}

int deinit() {

   ObjectsDeleteAll(0, OBJ_VLINE);
   ObjectDelete(tShortName+"01"); 
   ObjectDelete(tShortName+"02"); 
   ObjectDelete(tShortName+"03"); 
   ObjectDelete(tShortName+"04");   
   ObjectDelete(tShortName+"05"); 
   ObjectDelete(tShortName+"06");   
   ObjectDelete(tShortName+"07"); 

   ObjectDelete("cmtagtpbi");    
   
   return(0);
}

int start() {
   if( Bars != OldBars ) { Trigger = True ; }

   ArrayResize(BufBI, Bars ) ;
   ArrayResize(BufBIHHLevel1, Bars ) ;   
   ArrayResize(BufBIHHLevel2, Bars ) ;   
   ArrayResize(BufBILLLevel1, Bars ) ;
   ArrayResize(BufBILLLevel2, Bars ) ;
   ArrayResize(cmBufBI, Bars ) ;
   ArrayResize(cmBufBIHHLevel1, Bars ) ;   
   ArrayResize(cmBufBIHHLevel2, Bars ) ;   
   ArrayResize(cmBufBILLLevel1, Bars ) ;
   ArrayResize(cmBufBILLLevel2, Bars ) ;   
   ArrayResize(cmBufBIHHLevel15, Bars ) ;   
   ArrayResize(cmBufBILLLevel15, Bars ) ;   
      
   int i, j, counted_bars = IndicatorCounted();
   int AvailableWeek = 0;
   
   i = Bars - counted_bars - 1;
   
   
   double TotalHHtoLastFriClose = 0.0; 
   double TotalLLtoLastFriClose = 0.0;

   if (LastWeekDateTime != iTime(NULL,PERIOD_W1,1) )
   {
      for (j = HistoryWeek; j>0; j--)
      {
         if (iClose(NULL,PERIOD_W1,j+1) > 0)
         {
            TotalHHtoLastFriClose = TotalHHtoLastFriClose  + ( iHigh(NULL,PERIOD_W1,j) - iClose(NULL,PERIOD_W1,j+1) );
            TotalLLtoLastFriClose = TotalLLtoLastFriClose  + ( iLow(NULL,PERIOD_W1,j) - iClose(NULL,PERIOD_W1,j+1) );
            AvailableWeek++;
         }
      }
      TotalHHtoLastFriClose = TotalHHtoLastFriClose / AvailableWeek / MarketInfo(Symbol(),MODE_POINT);
      TotalLLtoLastFriClose = TotalLLtoLastFriClose / AvailableWeek / MarketInfo(Symbol(),MODE_POINT);

      LastWeekDateTime = iTime(NULL,PERIOD_D1,1);
    }

   while (i >= 0) 
   {

      LastWeekBarShift = iBarShift(NULL, PERIOD_W1, Time[i]) + 1;

      pair1Close = iClose(NULL, PERIOD_W1, LastWeekBarShift);  // closes of last week
 
      if (pair1Close > 0) 
      {
          
          if (lastDay != TimeDayOfWeek(iTime(NULL, timeFrame, i))) 
          {
            lastDay = TimeDayOfWeek(iTime(NULL, timeFrame, i));
            totalWeek = 0;

          }

          BufBI[i] = (iClose(Symbol(), timeFrame, i) - pair1Close) / MarketInfo(Symbol(),MODE_POINT);
          BufBIHHLevel1[i] = TotalHHtoLastFriClose * Level1Factor;
          BufBIHHLevel2[i] = TotalHHtoLastFriClose * Level2Factor;
          BufBILLLevel2[i] = TotalLLtoLastFriClose * Level2Factor;
          BufBILLLevel1[i] = TotalLLtoLastFriClose * Level1Factor;
      } 

      i--;

   }

double buffdiff;


   int limit = Bars - counted_bars - 1;
   for( i=0; i<=limit; i++)
   {
      cmBufBI[i]         = Close[0] -BufBI[0]*Point ;
      cmBufBIHHLevel1[i] = BufBIHHLevel1[0]*Point + cmBufBI[0] ;
      cmBufBIHHLevel2[i] = BufBIHHLevel2[0]*Point + cmBufBI[0] ;
      cmBufBILLLevel2[i] = BufBILLLevel2[0]*Point + cmBufBI[0] ;
      cmBufBILLLevel1[i] = BufBILLLevel1[0]*Point + cmBufBI[0] ;

      cmBufBIHHLevel15[i] = ( cmBufBIHHLevel2[i] - cmBufBIHHLevel1[i] ) * 0.50 + cmBufBIHHLevel1[i] ;
      cmBufBILLLevel15[i] = ( cmBufBILLLevel1[i] - cmBufBILLLevel2[i] ) * 0.50 + cmBufBILLLevel2[i] ;
      
   }
 
      cmBufBI[0]         = NormalizeDouble(cmBufBI[0] , Digits) ;
      cmBufBIHHLevel1[0] = NormalizeDouble(cmBufBIHHLevel1[0] , Digits) ;
      cmBufBIHHLevel2[0] = NormalizeDouble(cmBufBIHHLevel2[0] , Digits) ;
      cmBufBILLLevel2[0] = NormalizeDouble(cmBufBILLLevel2[0] , Digits) ;
      cmBufBILLLevel1[0] = NormalizeDouble(cmBufBILLLevel1[0] , Digits) ;

      cmBufBIHHLevel15[0] = NormalizeDouble(cmBufBIHHLevel15[0] , Digits) ;
      cmBufBILLLevel15[0] = NormalizeDouble(cmBufBILLLevel15[0] , Digits) ;
  

   if(ShowLable==true)
   {


       if (ObjectFind(tShortName+"01") != 0)
      {
          ObjectCreate(tShortName+"01",OBJ_ARROW,0,Time[0],cmBufBI[0]);
          ObjectSet(tShortName+"01",OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tShortName+"01",OBJPROP_COLOR,indicator_color1);  
          ObjectSet(tShortName+"01",OBJPROP_WIDTH,BoxSize);  
      } 
      else
      {
         ObjectMove(tShortName+"01",0,Time[0],cmBufBI[0]);
         ObjectSet(tShortName+"01",OBJPROP_COLOR,indicator_color1);  
      }

       if (ObjectFind(tShortName+"02") != 0)
      {
          ObjectCreate(tShortName+"02",OBJ_ARROW,0,Time[0],cmBufBIHHLevel1[0]);
          ObjectSet(tShortName+"02",OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tShortName+"02",OBJPROP_COLOR,indicator_color2);  
          ObjectSet(tShortName+"02",OBJPROP_WIDTH,BoxSize);  
      } 
      else
      {
         ObjectMove(tShortName+"02",0,Time[0],cmBufBIHHLevel1[0]);
         ObjectSet(tShortName+"02",OBJPROP_COLOR,indicator_color2);  
      }

  

       if (ObjectFind(tShortName+"03") != 0)
      {
          ObjectCreate(tShortName+"03",OBJ_ARROW,0,Time[0],cmBufBIHHLevel2[0]);
          ObjectSet(tShortName+"03",OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tShortName+"03",OBJPROP_COLOR,indicator_color3);  
          ObjectSet(tShortName+"03",OBJPROP_WIDTH,BoxSize);  
      } 
      else
      {
         ObjectMove(tShortName+"03",0,Time[0],cmBufBIHHLevel2[0]);
         ObjectSet(tShortName+"03",OBJPROP_COLOR,indicator_color3);  
      } 
      
       if (ObjectFind(tShortName+"04") != 0)
      {
          ObjectCreate(tShortName+"04",OBJ_ARROW,0,Time[0],cmBufBILLLevel2[0]);
          ObjectSet(tShortName+"04",OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tShortName+"04",OBJPROP_COLOR,indicator_color4);  
          ObjectSet(tShortName+"04",OBJPROP_WIDTH,BoxSize);  
      } 
      else
      {
         ObjectMove(tShortName+"04",0,Time[0],cmBufBILLLevel2[0]);
         ObjectSet(tShortName+"04",OBJPROP_COLOR,indicator_color4);  
      }      
   

    

       if (ObjectFind(tShortName+"05") != 0)
      {
          ObjectCreate(tShortName+"05",OBJ_ARROW,0,Time[0],cmBufBILLLevel1[0]);
          ObjectSet(tShortName+"05",OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tShortName+"05",OBJPROP_COLOR,indicator_color5);  
          ObjectSet(tShortName+"05",OBJPROP_WIDTH,BoxSize);  
      } 
      else
      {
         ObjectMove(tShortName+"05",0,Time[0],cmBufBILLLevel1[0]);
         ObjectSet(tShortName+"05",OBJPROP_COLOR,indicator_color5);  
      } 
      

       if (ObjectFind(tShortName+"06") != 0)
      {
          ObjectCreate(tShortName+"06",OBJ_ARROW,0,Time[0],cmBufBIHHLevel15[0]);
          ObjectSet(tShortName+"06",OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tShortName+"06",OBJPROP_COLOR,indicator_color6);  
          ObjectSet(tShortName+"06",OBJPROP_WIDTH,BoxSize);  
      } 
      else
      {
         ObjectMove(tShortName+"06",0,Time[0],cmBufBIHHLevel15[0]);
         ObjectSet(tShortName+"06",OBJPROP_COLOR,indicator_color6);  
      } 

       if (ObjectFind(tShortName+"07") != 0)
      {
          ObjectCreate(tShortName+"07",OBJ_ARROW,0,Time[0],cmBufBILLLevel15[0]);
          ObjectSet(tShortName+"07",OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tShortName+"07",OBJPROP_COLOR,indicator_color7);  
          ObjectSet(tShortName+"07",OBJPROP_WIDTH,BoxSize);  
      } 
      else
      {
         ObjectMove(tShortName+"07",0,Time[0],cmBufBILLLevel15[0]);
         ObjectSet(tShortName+"07",OBJPROP_COLOR,indicator_color7);  
      }      
         
    
   }

string RAN953 =  ">>>> CODEMUNCHER@GMAIL.COM <<<<"  ;   
string tObjName03 = "cmtagtpbi"  ; 
    
   ObjectCreate(tObjName03, OBJ_LABEL, 0, 0, 0);//HiLow LABEL
   ObjectSetText(tObjName03, RAN953 , cmFontSize ,  cmFontType,  cmFontColor );
   ObjectSet(tObjName03, OBJPROP_CORNER, 3);
   ObjectSet(tObjName03, OBJPROP_XDISTANCE,  5 );
   ObjectSet(tObjName03, OBJPROP_YDISTANCE, 10); 

      cmBufBI[0]         = NormalizeDouble(cmBufBI[0] , Digits) ;
      cmBufBIHHLevel1[0] = NormalizeDouble(cmBufBIHHLevel1[0] , Digits) ;
      cmBufBIHHLevel2[0] = NormalizeDouble(cmBufBIHHLevel2[0] , Digits) ;
      cmBufBILLLevel2[0] = NormalizeDouble(cmBufBILLLevel2[0] , Digits) ;
      cmBufBILLLevel1[0] = NormalizeDouble(cmBufBILLLevel1[0] , Digits) ;

      cmBufBIHHLevel15[0] = NormalizeDouble(cmBufBIHHLevel15[0] , Digits) ;
      cmBufBILLLevel15[0] = NormalizeDouble(cmBufBILLLevel15[0] , Digits) ;

      if ( Trigger &&  Sound.Alert ) 
      {
        if( Close[0] == cmBufBI[0] ) { Trigger = False ; Alert(symbol,"  ", tChartPeriod, " Price touched Brian Index "+ DoubleToStr(cmBufBI[0] ,digits)); }
        if( Close[0] == cmBufBIHHLevel1[0] ) { Trigger = False ; Alert(symbol,"  ", tChartPeriod, " Price touch HH L1 "+ DoubleToStr(cmBufBIHHLevel1[0] ,digits)); }
        if( Close[0] == cmBufBIHHLevel15[0] ) { Trigger = False ; Alert(symbol,"  ", tChartPeriod, " Price touch HH L1.5 "+ DoubleToStr(cmBufBIHHLevel15[0] ,digits)); }
        if( Close[0] == cmBufBIHHLevel2[0] ) { Trigger = False ; Alert(symbol,"  ", tChartPeriod, " Price touch HH L2 "+ DoubleToStr(cmBufBIHHLevel2[0] ,digits)); }
        if( Close[0] == cmBufBILLLevel1[0] ) { Trigger = False ; Alert(symbol,"  ", tChartPeriod, " Price touch LL L1 "+ DoubleToStr(cmBufBILLLevel1[0] ,digits)); }
        if( Close[0] == cmBufBILLLevel15[0] ) { Trigger = False ; Alert(symbol,"  ", tChartPeriod, " Price touch LL L1.5 "+ DoubleToStr(cmBufBILLLevel15[0] ,digits)); }
        if( Close[0] == cmBufBILLLevel2[0] ) { Trigger = False ; Alert(symbol,"  ", tChartPeriod, " Price touch LL L2 "+ DoubleToStr(cmBufBILLLevel2[0] ,digits)); }
      }


   OldBars = Bars ;  

   return(0);
}

//+------------------------------------------------------------------+

string formatInteger(int value) {

   if (value < 10) {
   
      return("0" + value);
   
   } else {
   
      return("" + value);
   
   }

}


//+------------------------------------------------------------------+


string TimeFrameToString(int tf)
{
   string tfs;
   switch(tf) {
      case PERIOD_M1:  tfs="M1"  ; break;
      case PERIOD_M5:  tfs="M5"  ; break;
      case PERIOD_M15: tfs="M15" ; break;
      case PERIOD_M30: tfs="M30" ; break;
      case PERIOD_H1:  tfs="H1"  ; break;
      case PERIOD_H4:  tfs="H4"  ; break;
      case PERIOD_D1:  tfs="D1"  ; break;
      case PERIOD_W1:  tfs="W1"  ; break;
      case PERIOD_MN1: tfs="MN";
   }
   return(tfs);
}


//+------------------------------------------------------------------+
/*

Comment(
"BufBI[0]= ", BufBI[0] , "\n",
"BufBIHHLevel2[0]= ", BufBIHHLevel2[0] , "\n",
"BufBIHHLevel1[0]= ", BufBIHHLevel1[0] , "\n",
"BufBILLLevel1[0]= ", BufBILLLevel1[0] , "\n",
"BufBILLLevel2[0]= ", BufBILLLevel2[0] , "\n",
"") ;

Comment(
"cmBufBI[i]= ", cmBufBI[i] , "\n",
"cmBufBIHHLevel2[i]= ", cmBufBIHHLevel2[i] , "\n",
"cmBufBIHHLevel1[i]= ", cmBufBIHHLevel1[i] , "\n",
"cmBufBILLLevel1[i]= ", cmBufBILLLevel1[i] , "\n",
"cmBufBILLLevel2[i]= ", cmBufBILLLevel2[i] , "\n",
"") ; 

*/