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


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

//---- indicator settings 
#property indicator_chart_window
#property indicator_buffers 0


//---- indicator parameters  
 


extern int win            = 0 ;
extern int CORNER         = 1 ;
extern int price.x.offset = -300 ;//250
extern int price.y.offset = 100 ;//-160

extern int   Chart_Period    = 0;
extern int   myBars          = 100 ;
extern int   myRange         = 100 ;

extern string NoteType = "RANGE, HIOP, OPLO" ;
extern string myType = "RANGE" ;

extern color Up_color = Lime;
extern color Eq_color = Yellow;
extern color Dn_color = Red;

extern color aboveColor = LightSteelBlue ;
extern color belowColor  = LightSteelBlue ;

color ClOpColor ;

string tLabel01, tLabel02, tLabel03 ;

double xMax = - 1,  xMin = 9999999999999, xDiff, xClose, xHigh, xLow, xRunUp, xRunDn, xOpen ;
double High0, Low0, Open0,  Close0, Open1,High1, Low1, Close1   ;  

double above, below, pctabove, pctbelow ; 

//+------------------------------------------------------------------+
color fColor( double i, double j)
{
color rColor ;

    if ( i > j ) rColor = Up_color; 
    else {
    if ( i < j )  rColor = Dn_color; 
    else rColor = Eq_color; }
return ( rColor );
}
 
 
//+------------------------------------------------------------------+ 

int init() 
  { 

  if(Chart_Period == 0) { Chart_Period = Period() ; }

tLabel01 = "AB1029" + Chart_Period + myBars + Symbol() + myType ;
tLabel02 = "AB1030" + Chart_Period + myBars + Symbol() + myType ;
tLabel03 = "AB1031" + Chart_Period + myBars + Symbol() + myType ;

ObjectsDeleteAll();
   return(0); 
  } 

int deinit() 
{ 

ObjectDelete( tLabel01 ) ;
ObjectDelete( tLabel02 ) ;
ObjectDelete( tLabel03 ) ;
     
}  
 
//+------------------------------------------------------------------+ 
int start() 
  { 
  
int limit; 


above = 0 ;
below = 0 ;

   for(int i=1; i<=myBars; i++) 
   { 
      if(myType == "RANGE") 
      { 
         xHigh     = iHigh( NULL, Chart_Period, i ) ;
         xLow      = iLow( NULL, Chart_Period, i ) ; 
         xDiff     = ( xHigh - xLow ) / Point ;
      } 
      else
      if(myType == "HIOP") 
      { 
         xHigh     = iHigh( NULL, Chart_Period, i ) ;
         xOpen     = iOpen( NULL, Chart_Period, i ) ; 
         xDiff     = ( xHigh - xOpen  ) / Point ;
      } 
      else      
      if(myType == "OPLO") 
      { 
         xLow      = iLow( NULL, Chart_Period, i ) ;
         xOpen     = iOpen( NULL, Chart_Period, i ) ; 
         xDiff     = ( xOpen - xLow  ) / Point ;
      }       
      
     if( xDiff > myRange ) {above = above  + 1  ; }
     if( xDiff < myRange ) {below = below  + 1  ; }
   } 


pctabove = ( above / myBars ) * 100 ;
pctbelow = ( below / myBars ) * 100 ;

string tpctabove = DoubleToStr(pctabove, 2 ) + "%" ;
string tpctbelow = DoubleToStr(pctbelow, 2 ) + "%" ;

string tPeriod = " "+TimeFrameToString( Chart_Period ) ;

ClOpColor  = SteelBlue ;
 

string tabove =  DoubleToStr(above, 0 ) + "/" + myBars + " Above " + tpctabove ;
string tbelow =  DoubleToStr(below, 0 ) + "/" + myBars + " Below " + tpctbelow ;
                     
   ObjectCreate(tLabel01, OBJ_LABEL, win, 0, 0); 
   ObjectSetText(tLabel01,tabove, 15, "Arial Bold", aboveColor );
   ObjectSet(tLabel01, OBJPROP_CORNER, CORNER);
   ObjectSet(tLabel01, OBJPROP_XDISTANCE, 520+price.x.offset);
   ObjectSet(tLabel01, OBJPROP_YDISTANCE, 37+price.y.offset);
   
  
   
   ObjectCreate(tLabel02, OBJ_LABEL, win, 0, 0); 
   ObjectSetText(tLabel02,tbelow, 15, "Arial Bold", belowColor );
   ObjectSet(tLabel02, OBJPROP_CORNER, CORNER);
   ObjectSet(tLabel02, OBJPROP_XDISTANCE, 520+price.x.offset);
   ObjectSet(tLabel02, OBJPROP_YDISTANCE, 100+price.y.offset); 
                                        

   string sLabel03 = myType+ " " + DoubleToStr(myRange, 0 ) + tPeriod ; 
   
   ObjectCreate(tLabel03, OBJ_LABEL, win, 0, 0);
   ObjectSetText(tLabel03, sLabel03 , 25, "Arial Bold", ClOpColor );
   ObjectSet(tLabel03, OBJPROP_CORNER, CORNER);
   ObjectSet(tLabel03, OBJPROP_XDISTANCE, 520+price.x.offset); // 765
   ObjectSet(tLabel03, OBJPROP_YDISTANCE, 60+price.y.offset); 
          

   WindowRedraw();    
 
//} 

   return(0); 
} 

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

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