//+------------------------------------------------------------------+ 
//|   TRO_HL_COUNT                                                   | 
//|                                                                  | 
//|   Copyright © 2008, Avery T. Horton, Jr. aka TheRumpledOne       |
//|                                                                  |
//|   PO BOX 43575, TUCSON, AZ 85733                                 |
//|                                                                  |
//|   GIFTS AND DONATIONS ACCEPTED                                   | 
//|   All my indicators should be considered donationware. That is   |
//|   you are free to use them for your personal use, and are        |
//|   under no obligation to pay for them. However, if you do find   |
//|   this or any of my other indicators help you with your trading  |
//|   then any Gift or Donation as a show of appreciation is         |
//|   gratefully accepted.                                           |
//|                                                                  |
//|   Gifts or Donations also keep me motivated in producing more    |
//|   great free indicators. :-)                                     |
//|                                                                  |
//|   PayPal - THERUMPLEDONE@GMAIL.COM                               |  
//+------------------------------------------------------------------+ 
 

#property indicator_chart_window

extern int myChartX   = 600 ;
extern int myChartY   = 600 ;
extern int myCorner   = 0 ;
extern string myFont  = "Courier" ;
extern int myFontSize = 12 ;
extern color colorHead  = Orange ; 

extern int myBars        = 10 ;


double Hi, Li, C;

double prev_high, prev_low, prev_close, D, p, value ;
int h  ;
string lbl[15], lbl2[15];
int n, j, i, shift, stop, nHH, nLL  ;

color pColor = Blue ;

string symbol, tChartPeriod, ShortName ;  

int    digits, period, win ; 
double    H1L0, L1H0 ;

double point,  open, high, low, close, mid, range, pClose, pOpen, pHigh, pLow, pMid, pRange, cRange  ;  
double xMax = - 1,  xMin = 9999999999999, xDiff, xClose, xHigh, xLow, xRunUp, xRunDn, xPain ;
double High0, Low0, Open0,  Close0, Open1,High1, Low1, Close1   ;  
 
 
int     ind_bufferHH[];
int     ind_bufferLL[];
 
 
      
      
//+------------------------------------------------------------------+

int init()
  {
   period       =  Period() ; 
   tChartPeriod =  TimeFrameToString(period) ;
   symbol       =  Symbol() ;
   digits       =  Digits ;
   point        =  Point ;
   ShortName    =  "HLCount"+symbol+period ;
   win          =  0 ;
   
   ArrayResize(ind_bufferHH,myBars) ;
   ArrayResize(ind_bufferLL,myBars) ; 
     
   return(0);
  }
  
//+------------------------------------------------------------------+
int deinit()
  {
 
ObjectsDeleteAll();
 
   return(0);
  }
  
//+------------------------------------------------------------------+
int start()
{

n        = 10;
 
 

j       = 0 ;
lbl[j]  = ShortName+j;
lbl2[j] = "HL Count " + symbol;
DoShow(j, Orange);

DoWork( PERIOD_M1 ) ;

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "M1       " ;
DoColor( ind_bufferHH[0], 0 ) ; 
DoShow(j, pColor); 

DoWork( PERIOD_M5 ) ;

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "M5       " ;
DoColor( ind_bufferHH[0], 0 ) ; 
DoShow(j, pColor); 

DoWork( PERIOD_M15 ) ;

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "M15      " ;
DoColor( ind_bufferHH[0], 0 ) ; 
DoShow(j, pColor); 

DoWork( PERIOD_M30 ) ;

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "M30      " ;
DoColor( ind_bufferHH[0], 0 ) ; 
DoShow(j, pColor); 

DoWork( PERIOD_H1 ) ;

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "H1       " ;
DoColor( ind_bufferHH[0], 0 ) ; 
DoShow(j, pColor); 

DoWork( PERIOD_H4 ) ;

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "H4       " ;
DoColor( ind_bufferHH[0], 0 ) ; 
DoShow(j, pColor); 

DoWork( PERIOD_D1 ) ;

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "D1       " ;
DoColor( ind_bufferHH[0], 0 ) ; 
DoShow(j, pColor); 


DoWork( PERIOD_W1 ) ;

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "W1       "  ;
DoColor( ind_bufferHH[0], 0 ) ; 
DoShow(j, pColor); 


DoWork( PERIOD_MN1 ) ;

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "MN       " ;
DoColor( ind_bufferHH[0], 0 ) ; 
DoShow(j, pColor); 
//---------


   return(0);
  }

//+------------------------------------------------------------------+
  
void DoShow( int u, color dsColor )
{
ObjectCreate(lbl[u],23,0,Time[0],PRICE_CLOSE);
ObjectSet(lbl[u], OBJPROP_CORNER, myCorner );
ObjectSet(lbl[u],OBJPROP_XDISTANCE, myChartX);
ObjectSet(lbl[u],OBJPROP_YDISTANCE,n+myChartY);
ObjectSetText(lbl[u],lbl2[u],myFontSize,myFont,colorHead);


if(u > 0)
{

string glabel = "HH" + lbl[u] ;
ObjectCreate(glabel,23,0,Time[0],PRICE_CLOSE);
ObjectSet(glabel, OBJPROP_CORNER, myCorner );
ObjectSet(glabel,OBJPROP_XDISTANCE, 100+myChartX);
ObjectSet(glabel,OBJPROP_YDISTANCE,n+myChartY);
ObjectSetText(glabel,""+ind_bufferHH[0],myFontSize,myFont,Lime);

string rlabel = "LL" + lbl[u] ;
ObjectCreate(rlabel,23,0,Time[0],PRICE_CLOSE);
ObjectSet(rlabel, OBJPROP_CORNER, myCorner );
ObjectSet(rlabel,OBJPROP_XDISTANCE, 130+myChartX);
ObjectSet(rlabel,OBJPROP_YDISTANCE,n+myChartY);
ObjectSetText(rlabel,""+ind_bufferLL[0],myFontSize,myFont,Red);

} 

n = n+20;

   return(0);
  }

//+------------------------------------------------------------------+
  
void DoColor( double c1, double c2 )
{

if(c1 > c2 ) { pColor = Lime ; } else {
if(c1 < c2 ) { pColor = Red ; } else { pColor = Yellow ; } } 

}  
  
//+------------------------------------------------------------------+
void drawLabel(string name,double lvl,color Color)
{
    if(ObjectFind(name) != 0)
    {
        ObjectCreate(name, OBJ_TEXT, 0, Time[10], lvl);
        ObjectSetText(name, name, myFontSize, myFont , EMPTY);
        ObjectSet(name, OBJPROP_COLOR, Color);
    }
    else
    {
        ObjectMove(name, 0, Time[10], lvl);
    }
}

//+------------------------------------------------------------------+
void drawLine(double lvl,string name, color Col,int type)
{
         if(ObjectFind(name) != 0)
         {
            ObjectCreate(name, OBJ_HLINE, 0, Time[0], lvl,Time[0],lvl);
            
            if(type == 1)
            ObjectSet(name, OBJPROP_STYLE, STYLE_SOLID);
            else
            ObjectSet(name, OBJPROP_STYLE, STYLE_DOT);
            
            ObjectSet(name, OBJPROP_COLOR, Col);
            ObjectSet(name,OBJPROP_WIDTH,1);
            
         }
         else
         {
            ObjectDelete(name);
            ObjectCreate(name, OBJ_HLINE, 0, Time[0], lvl,Time[0],lvl);
            
            if(type == 1)
            ObjectSet(name, OBJPROP_STYLE, STYLE_SOLID);
            else
            ObjectSet(name, OBJPROP_STYLE, STYLE_DOT);
            
            ObjectSet(name, OBJPROP_COLOR, Col);        
            ObjectSet(name,OBJPROP_WIDTH,1);
          
         }
}

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

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);
}

//+------------------------------------------------------------------+
void DoWork( int wPeriod )
{
   for(int i=myBars; i >= 0; i--) // initialize buffers to 0
   {  
      ind_bufferHH[i] = 0 ; 
      ind_bufferLL[i] = 0 ;                      
   } 

     
   for( i=myBars; i >= 0; i--)
   {
   
      High0   = iHigh(NULL,wPeriod,i) ;
      High1   = iHigh(NULL,wPeriod,i+1) ;
      Low0  = iLow(NULL,wPeriod,i) ;
      Low1  = iLow(NULL,wPeriod,i+1) ;

   
      if(High0 <= High1) {ind_bufferHH[i] = 0;  } else { ind_bufferHH[i] =  ind_bufferHH[i+1] + 1 ; } 
      if(Low0  >= Low1)  {ind_bufferLL[i] = 0;  } else { ind_bufferLL[i] =  ind_bufferLL[i+1] + 1 ; }       
   } 




}

//+------------------------------------------------------------------+
/*

Comment(

"ind_bufferHH[0]" , ind_bufferHH[0] , "\n" ,   
"Open0 " , DoubleToStr(Open0,Digits) , "\n" ,  
"Close0 " , DoubleToStr(Close0,Digits) , "\n" ,   
"Open1 " , DoubleToStr(Open1,Digits) , "\n" ,  
"Close1 " , DoubleToStr(Close1,Digits) , "\n" ,   

"") ; 
*/