//+------------------------------------------------------------------+ 
//|   TRO_BAR_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 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_bufferOp[];

 
 
      
      
//+------------------------------------------------------------------+

int init()
  {
   period       =  Period() ; 
   tChartPeriod =  TimeFrameToString(period) ;
   symbol       =  Symbol() ;
   digits       =  Digits ;
   point        =  Point ;
   ShortName    =  "BarCount"+symbol+period ;
   win          =  0 ;
   
   ArrayResize(ind_bufferOp,myBars) ;
   
   return(0);
  }
  
//+------------------------------------------------------------------+
int deinit()
  {
 
ObjectsDeleteAll();
 
   return(0);
  }
  
//+------------------------------------------------------------------+
int start()
{

n        = 10;
 
 

j       = 0 ;
lbl[j]  = ShortName+j;
lbl2[j] = "Bar Count " + symbol;
DoShow(j, Orange);

DoWork( PERIOD_M1 ) ;

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "M1       " + ind_bufferOp[0];
DoColor( ind_bufferOp[0], 0 ) ; 
DoShow(j, pColor); 

DoWork( PERIOD_M5 ) ;

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "M5       " + ind_bufferOp[0];
DoColor( ind_bufferOp[0], 0 ) ; 
DoShow(j, pColor); 

DoWork( PERIOD_M15 ) ;

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "M15      " + ind_bufferOp[0];
DoColor( ind_bufferOp[0], 0 ) ; 
DoShow(j, pColor); 

DoWork( PERIOD_M30 ) ;

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "M30      " + ind_bufferOp[0];
DoColor( ind_bufferOp[0], 0 ) ; 
DoShow(j, pColor); 

DoWork( PERIOD_H1 ) ;

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "H1       " + ind_bufferOp[0];
DoColor( ind_bufferOp[0], 0 ) ; 
DoShow(j, pColor); 

DoWork( PERIOD_H4 ) ;

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "H4       " + ind_bufferOp[0];
DoColor( ind_bufferOp[0], 0 ) ; 
DoShow(j, pColor); 

DoWork( PERIOD_D1 ) ;

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "D1       " + ind_bufferOp[0];
DoColor( ind_bufferOp[0], 0 ) ; 
DoShow(j, pColor); 


DoWork( PERIOD_W1 ) ;

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "W1       " + ind_bufferOp[0];
DoColor( ind_bufferOp[0], 0 ) ; 
DoShow(j, pColor); 


DoWork( PERIOD_MN1 ) ;

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "MN       " + ind_bufferOp[0];
DoColor( ind_bufferOp[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,dsColor);
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_bufferOp[i] = 0 ;             
   } 

     
   for( i=myBars; i >= 0; i--)
   {
   
      Open0  = iOpen(NULL,wPeriod,i) ;
      Open1  = iOpen(NULL,wPeriod,i+1) ;
      Close0  = iClose(NULL,wPeriod,i) ;
      Close1  = iClose(NULL,wPeriod,i+1) ;

   
      if(Close0 >= Open0) { if(Close1 <  Open1) {ind_bufferOp[i] = 1;  } else { ind_bufferOp[i] =  ind_bufferOp[i+1] + 1 ; } }
      if(Close0 <  Open0) { if(Close1 >= Open1) {ind_bufferOp[i] = -1; } else { ind_bufferOp[i] =  ind_bufferOp[i+1] - 1 ; } }
     
   } 




}

//+------------------------------------------------------------------+
/*

Comment(

"ind_bufferOp[0]" , ind_bufferOp[0] , "\n" ,   
"Open0 " , DoubleToStr(Open0,Digits) , "\n" ,  
"Close0 " , DoubleToStr(Close0,Digits) , "\n" ,   
"Open1 " , DoubleToStr(Open1,Digits) , "\n" ,  
"Close1 " , DoubleToStr(Close1,Digits) , "\n" ,   

"") ; 
*/