//+------------------------------------------------------------------+ 
//|   cm_Kantor_Dashboard                                            | 
//|                                                                  | 
//|   Copyright © 2008, Avery T. Horton, Jr. aka TheRumpledOne       |
//|                                                                  |
//|   PO BOX 43575, TUCSON, AZ 85733                                 |
//|                                                                  |
//|   GIFTS AND DONATIONS ACCEPTED                                   | 
//|                                                                  |
//|   therumpldone@gmail.com                                         |  
//+------------------------------------------------------------------+ 


#property  copyright "Copyright © 2008, Avery T. Horton, Jr. aka TRO" 
#property  link      "http://www.therumpldone.com/" 

#property indicator_separate_window
#property indicator_buffers 0
#property indicator_minimum 0
#property indicator_maximum 1

extern int price.x.offset= 0;//250
extern int price.y.offset= 10;//-160

extern string        mySymbol = "" ;

extern int           myPeriod = 0;

extern string myHeading = "cmdb" ;

extern color myColorHeading   =  Gray ; 
extern color myColorValues    =  Yellow ; 

extern string BInote1 = "Brian Index Variables" ;
extern double Level1Factor = 2.3;
extern double Level2Factor = 0.85;
extern int HistoryWeek      = 104;

//+---------- rule of 9 code ------------------------------------------------+

extern string TimeFrameR9_value = "TimeFrame(0,M5,M15,M30,H1,D1)";
extern string TimeFrameR9 = "0";
extern int SumPeriod = 30;
extern string Sum_Mode = "0=Close-Open, 1=+1/-1";
extern int SumMode = 1;
extern string Day_Calculation_Type = "0=Brian,1=Logical Trader,2=Amibroker";
extern int Calculation_Type = 0;

double P, PRU, PRD, R, D;

double LastValue[];
double HSum[];
double LSum[];
double SumValue[];

int LastBar = -1;

double HCount[];
double LCount[];

double HigherTimeFrameValue[];
int timeFrameR9 = 0;


//+---------- HH/LL code ------------------------------------------------+

extern int HHLLPeriod = 4;


//+------------------------------------------------------------------+
double pdayHigh;
double pdayLow;
double cdayHigh;
double cdayLow;
double pdayClose ;
double pdayOpen;
double cdayClose;
double cdayOpen;
double pdayRange;
double cdayRange;
double cdayMid;

double value1;
double value2;  
  
double point, close, open, diff ;
int xPoint ;

string   labelNames, tChartPeriod;
string   shortName;

int      corner;
int      totalLabels;
int      window;    

color ColorHeading  ; 
color ColorValues ; 
color ColorPrice ; 

color colorArrowcMid  ; 
string ArrowcMid ;



string ArrowN   = "ñ"; // north
string ArrowNE  = "ö";  // northeast
string ArrowE   = "ð";  // east
string ArrowSE  = "ø";  // southeast
string ArrowS   = "ò";  // south


//+------------------------------------------------------------------+

double PriceSMAX ; 

string   pAlertSMAX, pSMAX_Dir, SMAX_Dir;
string   tAlertSMA05, tAlertSMA07, tAlertSMA22, SMAX_String, tAlert0507, tAlert0722 ; 

color ColorValuesSMA05, ColorValuesSMA07, ColorValuesSMA22 ;
 
bool   ShTriggerSMAX; 
bool   LgTriggerSMAX;
bool   OktoAlertSMAX = false;

string ArrowSMA05, ArrowSMA07, ArrowSMA22, Arrow0507, Arrow0722 ;
color colorArrowSMA05, colorArrowSMA07, colorArrowSMA22, diff05_07color, diff07_22color ;
   
color colorArrowSMAX  ; 
 





//+------------------------------------------------------------------+
double     D1Range[4] ; 
double     AvgD1Range ;
double PriceD1RG ; 

string   pAlertD1RG, pD1RG_Dir, D1RG_Dir;
string   tAlertD0RG, tAlertD1RG,tAlertAvgRG, D1RG_String; 

color ColorValuesD0RG , ColorValuesD1RG, ColorValuesAvgRG;
 
bool   ShTriggerD1RG; 
bool   LgTriggerD1RG;
bool   OktoAlertD1RG = false;

string ArrowD0RG , ArrowD1RG , ArrowAvgRG  ;  
   
color colorArrowD1RG  ; 



//+------------------------------------------------------------------+

double PriceHHLL ; 

string   pAlertHHLL, pHHLL_Dir, HHLL_Dir;
string   tAlertD1HH,tAlertD1LL,tAlertHH,tAlertLL, HHLL_String; 

color ColorValuesD1HH,ColorValuesD1LL,ColorValuesHH,ColorValuesLL ;
 
bool   ShTriggerHHLL; 
bool   LgTriggerHHLL;
bool   OktoAlertHHLL = false;

string ArrowD1HH, ArrowD1LL,  ArrowHH, ArrowLL ;  
   
color colorArrowHHLL  ; 


//+------------------------------------------------------------------+

double PriceRULE9 ; 

string   pAlertRULE9, pRULE9_Dir, RULE9_Dir;
string   tAlertRULE9, RULE9_String; 

color ColorValuesRULE9 ;
 
bool   ShTriggerRULE9; 
bool   LgTriggerRULE9;
bool   OktoAlertRULE9 = false;

string ArrowRULE9  ;  
   
color colorArrowRULE9  ; 


//+------------------------------------------------------------------+

double PriceMA211 ; 

string   pAlertMA211, pMA211_Dir, MA211_Dir;
string   tAlertMA211, MA211_String; 

color ColorValuesMA211 ;
 
bool   ShTriggerMA211; 
bool   LgTriggerMA211;
bool   OktoAlertMA211 = false;

string ArrowMA211  ;  
   
color colorArrowMA211  ; 




//+------------------------------------------------------------------+

double pair1Close;
int totalWeek = 0;
int lastDay = 0;
 
int MaxWeek = 0;
double WeekCloseArray[];
double WeekHHpipsArray[];
double WeekLLpipsArray[];
int History2, History3, History4, History5;

datetime LastWeekDateTime = 0;

double cmBufBILLLevel15;
double cmBufBIHHLevel15;

double cmBufBI;
double cmBufBIHHLevel1;
double cmBufBIHHLevel2;
double cmBufBILLLevel2;
double cmBufBILLLevel1;


double BufBI;
double BufBIHHLevel1;
double BufBIHHLevel2;
double BufBILLLevel2;
double BufBILLLevel1;

double PriceBIDX ; 

string   pAlertBIDX, pBIDX_Dir, BIDX_Dir;
string   tAlertBIDX, BIDX_String; 

color ColorValuesBIDX ;
 
bool   ShTriggerBIDX; 
bool   LgTriggerBIDX;
bool   OktoAlertBIDX = false;

string ArrowBIDX  ;  
   
color colorArrowBIDX  ; 

//+------------------------------------------------------------------+

double PriceD1WL ; 

string   pAlertD1WL, pD1WL_Dir, D1WL_Dir;
string   tAlertD1WL, D1WL_String; 

color ColorValuesD1WL ;
 
bool   ShTriggerD1WL; 
bool   LgTriggerD1WL;
bool   OktoAlertD1WL = false;

string ArrowD1WL  ;  
   
color colorArrowD1WL  ; 


//+------------------------------------------------------------------+

double PriceH1WL ; 

string   pAlertH1WL, pH1WL_Dir, H1WL_Dir;
string   tAlertH1WL, H1WL_String; 

color ColorValuesH1WL ;
 
bool   ShTriggerH1WL; 
bool   LgTriggerH1WL;
bool   OktoAlertH1WL = false;

string ArrowH1WL  ;  
   
color colorArrowH1WL  ; 

//+------------------------------------------------------------------+

int pcounted_bars, counted_bars, bars ;

//


//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
string TimeFrameToString(int tf)
{
   string tfs;
   switch(tf) {
      case PERIOD_MN1: tfs="MN1"; break;
      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;
   }
   return(tfs);
}

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   corner     = 0;
   shortName  = MakeUniqueName(myHeading,"");
   labelNames = shortName;
      IndicatorShortName(shortName);
 
 
 if( mySymbol == "" ) { mySymbol = Symbol() ; }
 
 if( myPeriod == 0 ) { myPeriod = Period() ; } 

   tChartPeriod =  TimeFrameToString(myPeriod) ; 
 
//----


//+---------- rule of 9 code ------------------------------------------------+


   for(int idx=0; idx < SumPeriod; idx++) {

      SumValue[idx] = 0.0;
      HCount[idx] = 0.0;
      LCount[idx] = 0.0;
      HSum[idx] = 0.0;
      LSum[idx] = 0.0;
      LastValue[idx] = 0.0;

   }

   if (TimeFrameR9 == "M5") {

      timeFrameR9 = PERIOD_M5;

   } else if (TimeFrameR9 == "M15") {

      timeFrameR9 = PERIOD_M15;

   } else if (TimeFrameR9 == "M30") {

      timeFrameR9 = PERIOD_M30;

   } else if (TimeFrameR9 == "H1") {

      timeFrameR9 = PERIOD_H1;

   } else if (TimeFrameR9 == "H4") {

      timeFrameR9 = PERIOD_H4;

   } else if (TimeFrameR9 == "D1") {

      timeFrameR9 = PERIOD_D1;

   } else {

      timeFrameR9 = 0;

   }

   LastBar = iTime(NULL, 0, 0);

//+---------- rule of 9 code ------------------------------------------------+

   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   while (totalLabels>0) { ObjectDelete(StringConcatenate(labelNames,totalLabels)); totalLabels--;}
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{

bool sFirst = true ;

window      = WindowFind(shortName);
   
//if ( sFirst ) {
   sFirst = false ;
//   ObjectsDeleteAll( window, 21);
//   ObjectsDeleteAll( window, 22);
//   ObjectsDeleteAll( window, 23);
   ObjectsDeleteAll( window );   

//}
//----


ColorHeading      = myColorHeading ; 
ColorValues       = myColorValues ; 
 

   pdayHigh       = iHigh(mySymbol,PERIOD_D1,1);
   pdayLow        = iLow(mySymbol,PERIOD_D1,1);
   cdayHigh       = iHigh(mySymbol,PERIOD_D1,0);
   cdayLow        = iLow(mySymbol,PERIOD_D1,0);
   pdayRange      = pdayHigh - pdayLow;
   cdayRange      = cdayHigh - cdayLow;
   pdayClose      = iClose(mySymbol,PERIOD_D1,1);
   pdayOpen       = iOpen(mySymbol,PERIOD_D1,1);
   cdayClose      = iClose(mySymbol,PERIOD_D1,0);
   cdayOpen       = iOpen(mySymbol,PERIOD_D1,0);
 

 

   close       = iClose(mySymbol,myPeriod,0);
   open        = iOpen(mySymbol,myPeriod,0);     
   diff        = close - open ;     

int hSize = 9 ;

point =MarketInfo(mySymbol,MODE_POINT);  

if ( point == 0.01 )  {xPoint = 2 ; } else { xPoint = 4 ;  } 
   
//------------- 

counted_bars  = IndicatorCounted(); 
bars          = Bars - counted_bars ;
 

DoD1WL ();  
DoH1WL ();  
DoBIDX ();  
DoMA211 ();  
DoRULE9 ();  
DoHHLL ();  
DoD1RG (); 
DoSMAX ();  

pcounted_bars = counted_bars ;

//-------------    

DoHead() ;
 

if(cdayClose > cdayOpen) { ColorPrice = Lime ; } else {
if(cdayClose < cdayOpen) { ColorPrice = Red ; } else {ColorPrice = Yellow ; } }

setObject(next(),mySymbol,10+price.x.offset, 5+price.y.offset,ColorHeading ,"Arial bold",hSize);

setObject(next(), DoubleToStr(close,xPoint),75+price.x.offset, 5+price.y.offset, ColorPrice ,"Arial bold",hSize);               


setObject(next(),tAlertD1WL ,200+price.x.offset, 5+price.y.offset, ColorValuesD1WL ,"Arial bold",hSize);               
setObject(next(),ArrowD1WL  ,230+price.x.offset, 5+price.y.offset, ColorValuesD1WL ,"Wingdings",hSize);     

setObject(next(),tAlertH1WL ,250+price.x.offset, 5+price.y.offset, ColorValuesH1WL ,"Arial bold",hSize);               
setObject(next(),ArrowH1WL  ,280+price.x.offset, 5+price.y.offset, ColorValuesH1WL ,"Wingdings",hSize);               


setObject(next(),tAlertBIDX ,300+price.x.offset, 5+price.y.offset, ColorValuesBIDX ,"Arial bold",hSize);               
setObject(next(),ArrowBIDX  ,330+price.x.offset, 5+price.y.offset, ColorValuesBIDX ,"Wingdings",hSize); 

setObject(next(),tAlertMA211 ,350+price.x.offset, 5+price.y.offset, ColorValuesMA211 ,"Arial bold",hSize);               
setObject(next(),ArrowMA211  ,380+price.x.offset, 5+price.y.offset, ColorValuesMA211 ,"Wingdings",hSize);     
        

setObject(next(),tAlertRULE9 ,400+price.x.offset, 5+price.y.offset, ColorValuesRULE9 ,"Arial bold",hSize);               
setObject(next(),ArrowRULE9  ,430+price.x.offset, 5+price.y.offset, ColorValuesRULE9 ,"Wingdings",hSize);     
    

setObject(next(),tAlertHH ,450+price.x.offset, 5+price.y.offset, ColorValuesHH ,"Arial bold",hSize);               
setObject(next(),ArrowHH  ,480+price.x.offset, 5+price.y.offset, ColorValuesHH ,"Wingdings",hSize); 

setObject(next(),tAlertLL ,500+price.x.offset, 5+price.y.offset, ColorValuesLL ,"Arial bold",hSize);               
setObject(next(),ArrowLL  ,530+price.x.offset, 5+price.y.offset, ColorValuesLL ,"Wingdings",hSize); 

setObject(next(),tAlertD1HH ,550+price.x.offset, 5+price.y.offset, ColorValuesD1HH ,"Arial bold",hSize);               
setObject(next(),ArrowD1HH  ,580+price.x.offset, 5+price.y.offset, ColorValuesD1HH ,"Wingdings",hSize); 

setObject(next(),tAlertD1LL ,600+price.x.offset, 5+price.y.offset, ColorValuesD1LL ,"Arial bold",hSize);               
setObject(next(),ArrowD1LL  ,630+price.x.offset, 5+price.y.offset, ColorValuesD1LL ,"Wingdings",hSize); 


setObject(next(),tAlertD0RG ,650+price.x.offset, 5+price.y.offset, ColorValuesD0RG ,"Arial bold",hSize);  
setObject(next(),tAlertD1RG ,675+price.x.offset, 5+price.y.offset, ColorValuesD1RG ,"Arial bold",hSize); 
setObject(next(),tAlertAvgRG ,700+price.x.offset, 5+price.y.offset, ColorValuesAvgRG ,"Arial bold",hSize); 
             
//setObject(next(),ArrowD1RG  ,680+price.x.offset, 5+price.y.offset, ColorValuesD1RG ,"Wingdings",hSize);  


setObject(next(),ArrowSMA05  ,750+price.x.offset, 5+price.y.offset, colorArrowSMA05 ,"Wingdings",hSize);    
setObject(next(),ArrowSMA07  ,775+price.x.offset, 5+price.y.offset, colorArrowSMA05 ,"Wingdings",hSize);    
setObject(next(),ArrowSMA22  ,800+price.x.offset, 5+price.y.offset, colorArrowSMA05 ,"Wingdings",hSize);    


setObject(next(),tAlert0507 ,850+price.x.offset, 5+price.y.offset, diff05_07color ,"Arial bold",hSize); 
setObject(next(),Arrow0507  ,880+price.x.offset, 5+price.y.offset, diff05_07color ,"Wingdings",hSize); 


setObject(next(),tAlert0722 ,900+price.x.offset, 5+price.y.offset, diff07_22color ,"Arial bold",hSize); 
setObject(next(),Arrow0722  ,930+price.x.offset, 5+price.y.offset, diff07_22color ,"Wingdings",hSize); 

/*

 string note2 = "Default Font Color";
 color  cmFontColor              = DimGray;
 string note3                  = "Font Size";
 int cmFontSize                  = 8;
 string note4                  = "Font Type";
 string cmFontType               = "Verdana"; // Tahoma Courier
string RAN953 =  ">>>> CODEMUNCHER@GMAIL.COM <<<<"  ;   
string tObjName03 = "cmtagtpbi"  ; 
    
   ObjectCreate(tObjName03, OBJ_LABEL, window, 0, 0);//HiLow LABEL
   ObjectSetText(tObjName03, RAN953 , cmFontSize ,  cmFontType,  cmFontColor );
   ObjectSet(tObjName03, OBJPROP_CORNER, 0);
   ObjectSet(tObjName03, OBJPROP_XDISTANCE,  5 );
   ObjectSet(tObjName03, OBJPROP_YDISTANCE, 10); 
*/
   WindowRedraw();     
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

string next() { totalLabels++; return(totalLabels); }  

string MakeUniqueName(string first, string rest)
{
   string result = first+(MathRand()%1001)+rest;

   while (WindowFind(result)> 0)
          result = first+(MathRand()%1001)+rest;
   return(result);
}

//+------------------------------------------------------------------+
void setObject(string name,string text,int x,int y,color theColor, string font = "Arial",int size=10,int angle=0)
{
   string labelName = StringConcatenate(labelNames,name);

      if (ObjectFind(labelName) == -1)
          {
             ObjectCreate(labelName,OBJ_LABEL,window,0,0);
             ObjectSet(labelName,OBJPROP_CORNER,corner);
             if (angle != 0)
                  ObjectSet(labelName,OBJPROP_ANGLE,angle);
          }               
       ObjectSet(labelName,OBJPROP_XDISTANCE,x);
       ObjectSet(labelName,OBJPROP_YDISTANCE,y);
       ObjectSetText(labelName,text,size,font,theColor);
}

//+------------------------------------------------------------------+
void DoHead ()
{
int hSize = 9 ;

setObject(next(),"Price",75+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);               
 
setObject(next(),"D1WL",200+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);   
             
setObject(next(),"H1WL",250+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);               

setObject(next(),"BIDX",300+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);  
 
setObject(next(),"MA211",350+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);  
  
setObject(next(),"RULE9",400+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);    

setObject(next(),tChartPeriod+"-HH",450+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);    

setObject(next(),tChartPeriod+"-LL",500+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize); 
 
setObject(next(), "D1-HH",550+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);    

setObject(next(), "D1-LL",600+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize); 

setObject(next(),"D(0) D(1) Avg(5)",650+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);    

setObject(next(),"(05)  (07)  (22)",740+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);         

setObject(next(),"(5)x(7)",850+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);   

setObject(next(),"(7)x(22)",900+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize); 


}

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
// SMAX strategy
//+------------------------------------------------------------------+


void DoSMAX()
{

   close             = cdayClose ;
   pSMAX_Dir         = SMAX_Dir ;
//   pAlertSMAX        = tAlertSMAX ;
//   tAlertSMAX        = "WAIT";
//   PriceSMAX         = 0 ;
//   ColorValuesSMAX   = myColorValues ; 

//   ArrowSMAX      = ArrowE ; 
//   colorArrowSMAX = Yellow ; 


SMAX_String = "" ;


SMAX_String = pSMAX_Dir + SMAX_Dir;
 

//+---------- SMA 5 X 7 X 22 CODE ------------------------------------------------+ 


double sma05 = iMA(NULL, PERIOD_D1, 5, 0, 0, PRICE_CLOSE,0);
double sma07 = iMA(NULL, PERIOD_D1, 7, 0, 0, PRICE_CLOSE,0);
double sma22 = iMA(NULL, PERIOD_D1, 22, 0, 0, PRICE_CLOSE,0);

double sma05p = iMA(NULL, PERIOD_D1, 5, 0, 0, PRICE_CLOSE,1);
double sma07p = iMA(NULL, PERIOD_D1, 7, 0, 0, PRICE_CLOSE,1);
double sma22p = iMA(NULL, PERIOD_D1, 22, 0, 0, PRICE_CLOSE,1);

double diff05_07 = ( sma05 - sma07 ) / Point;
double diff07_22 = ( sma07 - sma22 ) / Point; 



if( sma05 >= sma05p) { colorArrowSMA05 = Lime ; ArrowSMA05 = ArrowN ; } else { colorArrowSMA05 = Red ;  ArrowSMA05 = ArrowS ;  }  
if( sma07 >= sma07p) { colorArrowSMA07 = Lime ; ArrowSMA07 = ArrowN ; } else { colorArrowSMA07 = Red ;  ArrowSMA07 = ArrowS ;  }  
if( sma22 >= sma22p) { colorArrowSMA22 = Lime ; ArrowSMA22 = ArrowN ; } else { colorArrowSMA22 = Red ;  ArrowSMA22 = ArrowS ;  }  


diff05_07color = Yellow ;
diff07_22color = Yellow ;


tAlert0507 =  DoubleToStr(diff05_07, 0) ; 
tAlert0722 =  DoubleToStr(diff07_22, 0) ; 

if(diff05_07 >= 0 ) { diff05_07color = Lime ;   Arrow0507 = ArrowN ; } else { diff05_07color = Red ;  Arrow0507 = ArrowS ;  }   
if(diff07_22 >= 0 ) { diff07_22color = Lime ;   Arrow0722 = ArrowN ; } else { diff07_22color = Red ;  Arrow0722 = ArrowS ;  }   


 

} // END SMAX


//+------------------------------------------------------------------+



//+------------------------------------------------------------------+
// D1RG strategy
//+------------------------------------------------------------------+


void DoD1RG()
{

   close             = cdayClose ;
   pD1RG_Dir         = D1RG_Dir ;
//   pAlertD1RG        = tAlertD1RG ;
   tAlertD1RG        = "WAIT";
//   PriceD1RG         = 0 ;
   ColorValuesD1RG   = myColorValues ; 

//   ArrowD1RG      = ArrowE ; 
//   colorArrowD1RG = Yellow ; 


D1RG_String = "" ;


D1RG_String = pD1RG_Dir + D1RG_Dir;

    
//+---------- D1 RANGE CODE ------------------------------------------------+     
 
AvgD1Range = 0 ; 
     
for( int i=0; i<5; i++)
{

   D1Range[i] = (iHigh(NULL,PERIOD_D1,i) - iLow(NULL,PERIOD_D1,i))  / Point;
   AvgD1Range = AvgD1Range + D1Range[i]  ;

}    

AvgD1Range = NormalizeDouble(AvgD1Range / 5 , Digits ) ;
 
   
tAlertD0RG   = DoubleToStr(D1Range[0], 0)   ;
tAlertD1RG   = DoubleToStr(D1Range[1], 0)   ;
tAlertAvgRG  = DoubleToStr(AvgD1Range, 0)   ;

if(D1Range[0] >= D1Range[1] ) { ColorValuesD0RG  = Lime ; ArrowD0RG = ArrowN ;  } 
else  { ColorValuesD0RG = Red   ; ArrowD0RG = ArrowS ; }   

if(D1Range[1] >= AvgD1Range ) { ColorValuesD1RG  = Lime ; ArrowD1RG = ArrowN ;  } 
else  { ColorValuesD1RG = Red   ; ArrowD1RG = ArrowS ; }   
   
ColorValuesAvgRG = Orange ;


} // END D1RG


//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
// HHLL strategy
//+------------------------------------------------------------------+


void DoHHLL()
{

   close             = cdayClose ;
   pHHLL_Dir         = HHLL_Dir ;
//   pAlertHHLL        = tAlertHHLL ;
//   tAlertHHLL        = "WAIT";
//   PriceHHLL         = 0 ;
   ColorValuesHH   = myColorValues ; 
   ColorValuesLL   = myColorValues ; 
//   ArrowHHLL      = ArrowE ; 
//   colorArrowHHLL = Yellow ; 


HHLL_String = "" ;


HHLL_String = pHHLL_Dir + HHLL_Dir;
 


//+---------- HH/LL code ------------------------------------------------+           
           
           
           
           
   int iHH = iHighest(NULL,myPeriod,MODE_HIGH,HHLLPeriod,0); // index to high
   int iLL = iLowest(NULL,myPeriod,MODE_LOW,HHLLPeriod,0); // index to low  

   double HH = iHigh(NULL,myPeriod,iHH);
   double LL = iLow(NULL,myPeriod,iLL); 


   
   double HHpips = (close - HH) / Point ;
   double LLpips = (close - LL) / Point  ;   
   

tAlertLL = DoubleToStr(LLpips, 0)  ;
tAlertHH = DoubleToStr(HHpips, 0)  ;

if(LLpips >= 0  ) { ColorValuesLL  = Lime ; ArrowLL = ArrowN ;  } 
else { ColorValuesLL = Red   ; ArrowLL = ArrowS ; }     
 

if(HHpips >= 0  ) { ColorValuesHH  = Lime ; ArrowHH = ArrowN ;  } 
else { ColorValuesHH = Red   ; ArrowHH = ArrowS ; }    

 
   double D1HH = iHigh(NULL,1440,0);
   double D1LL = iLow(NULL,1440,0); 

   double D1HHpips = (close - D1HH) / Point ;
   double D1LLpips = (close - D1LL) / Point  ;   
      


tAlertD1LL = DoubleToStr(D1LLpips, 0)  ;
tAlertD1HH = DoubleToStr(D1HHpips, 0)  ;

if(D1LLpips >= 0  ) { ColorValuesD1LL  = Lime ; ArrowD1LL = ArrowN ;  } 
else { ColorValuesD1LL = Red   ; ArrowD1LL = ArrowS ; }     
 

if(D1HHpips >= 0  ) { ColorValuesD1HH  = Lime ; ArrowD1HH = ArrowN ;  } 
else { ColorValuesD1HH = Red   ; ArrowD1HH = ArrowS ; }    



//+---------- HH/LL code ------------------------------------------------+
     

   
 
   


} // END HHLL


//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
// RULE9 strategy
//+------------------------------------------------------------------+


void DoRULE9()
{

   close             = cdayClose ;
   pRULE9_Dir         = RULE9_Dir ;
   pAlertRULE9        = tAlertRULE9 ;
   tAlertRULE9        = "WAIT";
//   PriceRULE9         = 0 ;
   ColorValuesRULE9   = myColorValues ; 

   ArrowRULE9      = ArrowE ; 
//   colorArrowRULE9 = Yellow ; 


RULE9_String = "" ;


RULE9_String = pRULE9_Dir + RULE9_Dir;
 

//+---------- rule of 9 code ------------------------------------------------+

ArrayResize( LastValue,Bars) ; 
ArrayResize( SumValue,Bars) ; 
ArrayResize( HCount , Bars ) ;
ArrayResize( LCount , Bars ) ;
ArrayResize( HSum , Bars ) ;
ArrayResize( LSum , Bars ) ;

  counted_bars = IndicatorCounted();
  int i = Bars - counted_bars - 1;
   double sum = 0;

   if (i < SumPeriod) {
   
      i = SumPeriod;
   
   }
   
   while (i >= 0) {

      if (iTime(NULL, timeFrameR9, i) != LastBar) {

         LastBar = iTime(NULL, timeFrameR9, i);
         sum = 0;

         if (Calculation_Type == 0 || (Calculation_Type != 1 && Calculation_Type != 2)) {
         
            D = (iOpen(NULL, timeFrameR9, i) - iClose(NULL, timeFrameR9, i+1));
         
         } else if (Calculation_Type == 1) {
         
            D = (iClose(NULL, timeFrameR9, i+1) - iOpen(NULL, timeFrameR9, i+1));
            
         } else if (Calculation_Type == 2) {
         
            D = (iClose(NULL, timeFrameR9, i) - iOpen(NULL, timeFrameR9, i+1));

         }
         
/*         // Experiment of the logical trader book ACD signal
         P = (iHigh(NULL, PERIOD_D1, i+1) + iLow(NULL, PERIOD_D1, i+1) + iClose(NULL, PERIOD_D1, i+1)) / 3;
         R = (iHigh(NULL, PERIOD_D1, i+1) + iLow(NULL, PERIOD_D1, i+1)) / 2;
         PRU = P+(MathAbs(P-R));
         PRD = P-(MathAbs(P-R));

         if (iOpen(NULL, PERIOD_D1, i+1) <= PRU && iClose(NULL, PERIOD_D1, i+1) > PRU) {
      
            LastValueUp[i] = 2;
      
         } else if (iOpen(NULL, PERIOD_D1, i+1) >= PRD && iClose(NULL, PERIOD_D1, i+1) < PRD) {
      
            LastValueUp[i] = -2;
      
         } else {
      
            LastValueUp[i] = 0;
      
         }
*/

         if (SumMode == 0) {

            LastValue[i] = D;

         } else {

            if (D > 0) {
  
               LastValue[i] = 1;

            } else {

               LastValue[i] = -1;

            }
               
         
         }
 
 
 
 
         
         int period = 0;
         
         if (SumPeriod > ArraySize(LastValue) ) {

            period = ArraySize(LastValue);

         } else {

            period = SumPeriod;

         }






         for(int idx=0; idx<period; idx++) {
      
            sum += LastValue[i+idx];

         }

         SumValue[i] = sum;




         HSum[i] = SumValue[ArrayMaximum(SumValue, SumPeriod*8, 0)];
         LSum[i] = SumValue[ArrayMinimum(SumValue, SumPeriod*8, 0)];



         if (SumValue[i] >= 8) {
         
            HCount[i] = HCount[i+1] + 1;
             
         } else {
         
            HCount[i] = 0.0;

         }
         if (SumValue[i] <= -8) {

            LCount[i] = LCount[i+1] + 1;

         } else {

            LCount[i] = 0.0;

         }
         
         if (LCount[i] <= -9 || HCount[i] >= 9) {
         
            Alert("Potential trend change on ", TimeToStr(Time[i], TIME_DATE));
         
         }
         
      }

      i--;

   }
 

   
//+---------- rule of 9 code ------------------------------------------------+


tAlertRULE9 = DoubleToStr(SumValue[0],0) ;
   

if(SumValue[0] >= 8 ) { ColorValuesRULE9  = Lime ; ArrowRULE9 = ArrowN ;  } 
else { if(SumValue[0] <= -8 ) { ColorValuesRULE9 = Red   ; ArrowRULE9 = ArrowS ; }   }   
   


} // END RULE9


//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
// MA211 strategy
//+------------------------------------------------------------------+


void DoMA211()
{

   close             = cdayClose ;
   pMA211_Dir         = MA211_Dir ;
   pAlertMA211        = tAlertMA211 ;
   tAlertMA211        = "WAIT";
//   PriceMA211         = 0 ;
   ColorValuesMA211   = myColorValues ; 

//   ArrowMA211      = ArrowE ; 
//   colorArrowMA211 = Yellow ; 


MA211_String = "" ;


MA211_String = pMA211_Dir + MA211_Dir;
 
double ma211 = iMA(NULL, PERIOD_D1, 211, 0, 1, PRICE_CLOSE,0);

   diff       = (close - ma211) / Point ;
   
   tAlertMA211 = DoubleToStr(diff,0) ;
   
   if(diff > 0 ) {ColorValuesMA211  = Lime ; ArrowMA211 = ArrowN ; }
   else          {ColorValuesMA211 = Red   ; ArrowMA211 = ArrowS ; }   


//Comment( "diff=", DoubleToStr(diff,4), " close= ", close ) ;

} // END MA211


//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
// BIDX strategy
//+------------------------------------------------------------------+


void DoBIDX()
{
   close             = cdayClose ;
   pBIDX_Dir         = BIDX_Dir ;
   pAlertBIDX        = tAlertBIDX ;
   tAlertBIDX        = "WAIT";
//   PriceBIDX         = 0 ;
   ColorValuesBIDX   = myColorValues ; 

//   ArrowBIDX      = ArrowE ; 
//   colorArrowBIDX = Yellow ; 


BIDX_String = "" ;

   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);
    }
 
 
int timeFrame = Period() ;

   while (i >= 0) 
   {
      int timeFrameIndex = iBarShift(0, timeFrame, Time[i], true);

      if ((TimeDayOfWeek(iTime(NULL, timeFrame, timeFrameIndex)) == 1 && TimeDayOfWeek(iTime(NULL, timeFrame, timeFrameIndex+1)) == 5) ||
         (TimeDayOfWeek(iTime(NULL, timeFrame, timeFrameIndex)) == 0 && TimeDayOfWeek(iTime(NULL, timeFrame, timeFrameIndex+1)) == 5)){

         pair1Close = iClose(NULL, timeFrame, timeFrameIndex+1);  // closes of last week
      }
      

      if (pair1Close > 0 && TimeDayOfWeek(iTime(NULL, timeFrame, 0)) >= 1) 
      {
          
          if (lastDay != TimeDayOfWeek(iTime(NULL, timeFrame, 0))) 
          {
            lastDay = TimeDayOfWeek(iTime(NULL, timeFrame, 0));
            totalWeek = 0;

          }

          BufBI = (iClose(Symbol(), timeFrame, i) - pair1Close)/MarketInfo(Symbol(),MODE_POINT);
          BufBIHHLevel1 = TotalHHtoLastFriClose * Level1Factor;
          BufBIHHLevel2 = TotalHHtoLastFriClose * Level2Factor;
          BufBILLLevel2 = TotalLLtoLastFriClose * Level2Factor;
          BufBILLLevel1 = TotalLLtoLastFriClose * Level1Factor;
      } 

      i--;

   } 
 


      cmBufBI         = close -BufBI*Point ;
      cmBufBIHHLevel1 = BufBIHHLevel1*Point + cmBufBI ;
      cmBufBIHHLevel2 = BufBIHHLevel2*Point + cmBufBI ;
      cmBufBILLLevel2 = BufBILLLevel2*Point + cmBufBI ;
      cmBufBILLLevel1 = BufBILLLevel1*Point + cmBufBI ;

      cmBufBIHHLevel15 = ( cmBufBIHHLevel2 - cmBufBIHHLevel1 ) * 0.50 + cmBufBIHHLevel1 ;
      cmBufBILLLevel15 = ( cmBufBILLLevel1 - cmBufBILLLevel2 ) * 0.50 + cmBufBILLLevel2 ;
      

 
      cmBufBI         = NormalizeDouble(cmBufBI , Digits) ;
      cmBufBIHHLevel1 = NormalizeDouble(cmBufBIHHLevel1 , Digits) ;
      cmBufBIHHLevel2 = NormalizeDouble(cmBufBIHHLevel2 , Digits) ;
      cmBufBILLLevel2 = NormalizeDouble(cmBufBILLLevel2 , Digits) ;
      cmBufBILLLevel1 = NormalizeDouble(cmBufBILLLevel1 , Digits) ;

      cmBufBIHHLevel15 = NormalizeDouble(cmBufBIHHLevel15 , Digits) ;
      cmBufBILLLevel15 = NormalizeDouble(cmBufBILLLevel15 , Digits) ;

color BIcolor ;

if( BufBI >= 0 ) { BIcolor = Lime ; } else { BIcolor = Red ; }      
 
string  RAN931 = "Brian Index= "   + DoubleToStr( BufBI , 0) ;   
 

   diff       = (close - cmBufBI) / Point ;
   
   tAlertBIDX = DoubleToStr(diff,0) ;
   
   if(diff > 0 ) {ColorValuesBIDX  = Lime ; ArrowBIDX = ArrowN ; }
   else          {ColorValuesBIDX = Red   ; ArrowBIDX = ArrowS ; }   

//Comment( "diff=", DoubleToStr(diff,4), " close= ", DoubleToStr(close,4), " pair1Close= ", DoubleToStr(pair1Close,4), " cmBufBI= ", cmBufBI ) ; 



} // END BIDX


//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
// D1WL strategy
//+------------------------------------------------------------------+


void DoD1WL()
{

   close             = cdayClose ;
   pD1WL_Dir         = D1WL_Dir ;
   pAlertD1WL        = tAlertD1WL ;
   tAlertD1WL        = "WAIT";
//   PriceD1WL         = 0 ;
   ColorValuesD1WL   = myColorValues ; 

//   ArrowD1WL      = ArrowE ; 
//   colorArrowD1WL = Yellow ; 


D1WL_String = "" ;


D1WL_String = pD1WL_Dir + D1WL_Dir;
 

   double Avg1_D1    = iMA(NULL, PERIOD_D1, 5,  0, MODE_SMA, PRICE_CLOSE, 0);
   double Avg2_D1    = iMA(NULL, PERIOD_D1, 7,  0, MODE_SMA, PRICE_CLOSE, 0);
   double Avg3_D1    = iMA(NULL, PERIOD_D1, 22, 0, MODE_SMA, PRICE_CLOSE, 0);
   double Avg4_D1    = iMA(NULL, PERIOD_D1, 34, 0, MODE_SMA, PRICE_CLOSE, 0);
   double dWLHourly  = 0.0;
    
   if(Symbol() == "GBPUSD")
   {
     dWLHourly   = NormalizeDouble((Avg1_D1 + Avg2_D1 + Avg3_D1 + Avg4_D1)/4 , Digits) ;
   }
   else
   {
     dWLHourly   = NormalizeDouble((Avg1_D1 + Avg2_D1 + Avg3_D1)/3 , Digits);
   }

   diff       = (close - dWLHourly) / Point ;
   
   tAlertD1WL = DoubleToStr(diff,0) ;
   
   if(diff > 0 ) {ColorValuesD1WL  = Lime ; ArrowD1WL = ArrowN ; }
   else          {ColorValuesD1WL = Red   ; ArrowD1WL = ArrowS ; }   



} // END D1WL


//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
// H1WL strategy
//+------------------------------------------------------------------+


void DoH1WL()
{

   close             = cdayClose ;
   pH1WL_Dir         = H1WL_Dir ;
   pAlertH1WL        = tAlertH1WL ;
   tAlertH1WL        = "WAIT";
//   PriceH1WL         = 0 ;
   ColorValuesH1WL   = myColorValues ; 

//   ArrowH1WL      = ArrowE ; 
//   colorArrowH1WL = Yellow ; 


H1WL_String = "" ;


H1WL_String = pH1WL_Dir + H1WL_Dir;
 

   double Avg1_H1    = iMA(NULL, PERIOD_H1, 5,  0, MODE_SMA, PRICE_CLOSE, 0);
   double Avg2_H1    = iMA(NULL, PERIOD_H1, 7,  0, MODE_SMA, PRICE_CLOSE, 0);
   double Avg3_H1    = iMA(NULL, PERIOD_H1, 22, 0, MODE_SMA, PRICE_CLOSE, 0);
   double Avg4_H1    = iMA(NULL, PERIOD_H1, 34, 0, MODE_SMA, PRICE_CLOSE, 0);
   double dWLHourly  = 0.0;
    
   if(Symbol() == "GBPUSD")
   {
     dWLHourly   = NormalizeDouble((Avg1_H1 + Avg2_H1 + Avg3_H1 + Avg4_H1)/4 , Digits) ;
   }
   else
   {
     dWLHourly   = NormalizeDouble((Avg1_H1 + Avg2_H1 + Avg3_H1)/3 , Digits);
   }

   diff       = (close - dWLHourly) / Point ;
   
   tAlertH1WL = DoubleToStr(diff,0) ;
   
   if(diff > 0 ) {ColorValuesH1WL  = Lime ; ArrowH1WL = ArrowN ; }
   else          {ColorValuesH1WL = Red   ; ArrowH1WL = ArrowS ; }   


//Comment( "diff=", DoubleToStr(diff,4), " close= ", close ) ;

} // END H1WL


//+------------------------------------------------------------------+



//+------------------------------------------------------------------+
// code needed to add strategy
//+------------------------------------------------------------------+

/* 


//+------------------------------------------------------------------+

double PriceSMAX ; 

string   pAlertSMAX, pSMAX_Dir, SMAX_Dir;
string   tAlertSMA05, tAlertSMA07, tAlertSMA22, SMAX_String; 

color ColorValuesSMA05, ColorValuesSMA07, ColorValuesSMA22 ;
 
bool   ShTriggerSMAX; 
bool   LgTriggerSMAX;
bool   OktoAlertSMAX = false;

string ArrowSMA05, ArrowSMA07, ArrowSMA22 ;
color colorArrowSMA05, colorArrowSMA07, colorArrowSMA22 ;
   
color colorArrowSMAX  ; 




//+------------------------------------------------------------------+
DoSMAX ();  



setObject(next(),tAlertSMAX ,200+price.x.offset, 5+price.y.offset, ColorValuesSMAX ,"Arial bold",hSize);               
setObject(next(),ArrowSMAX  ,220+price.x.offset, 5+price.y.offset, ColorValuesSMAX ,"Wingdings",hSize);     



setObject(next(),"SMAX",200+price.x.offset, -10+price.y.offset, ColorHeading ,"Arial bold",hSize);    



//+------------------------------------------------------------------+
// SMAX strategy
//+------------------------------------------------------------------+


void DoSMAX()
{

   close             = cdayClose ;
   pSMAX_Dir         = SMAX_Dir ;
   pAlertSMAX        = tAlertSMAX ;
   tAlertSMAX        = "WAIT";
//   PriceSMAX         = 0 ;
   ColorValuesSMAX   = myColorValues ; 

//   ArrowSMAX      = ArrowE ; 
//   colorArrowSMAX = Yellow ; 


SMAX_String = "" ;


SMAX_String = pSMAX_Dir + SMAX_Dir;
 

     

if(D1Range[0] >= D1Range[1] ) { ColorValuesD0RG  = Lime ; ArrowD0RG = ArrowN ;  } 
else  { ColorValuesD0RG = Red   ; ArrowD0RG = ArrowS ; }   

if(D1Range[1] >= AvgD1Range ) { ColorValuesSMAX  = Lime ; ArrowSMAX = ArrowN ;  } 
else  { ColorValuesSMAX = Red   ; ArrowSMAX = ArrowS ; }   
   
 


} // END SMAX


//+------------------------------------------------------------------+

 

*/