//+------------------------------------------------------------------+
//|                                              Bars identifier.mq4 |
//|                               Copyright © 2015, Gehtsoft USA LLC | 
//|                                            http://fxcodebase.com |
//|                         Donate / Support:  http://goo.gl/cEP5h5  | 
//+------------------------------------------------------------------+
//|                                      Developed by : Mario Jemic  |                    
//|                                          mario.jemic@gmail.com   |
//|------------------------------------------------------------------|
//|                                     Paypal: http://goo.gl/cEP5h5 |
//|                     BitCoin: 1MfUHS3h86MBTeonJzWdszdzF2iuKESCKU  |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2015, Gehtsoft USA LLC"
#property link      "http://fxcodebase.com"

#property indicator_chart_window

    
	extern color PinBarUpColor = Lime;
	extern color PinBarDownColor = Red;
	
	extern color ShavedBarUpColor = Aqua;
	extern color ShavedBarDownColor = Fuchsia;
	
	extern color InsideBarColor = Yellow;
	extern color OutsideBarColor = Orange; 

    extern bool   ShowPinBars=true;
	extern bool   ShowShavedBars=true;
	extern bool   ShowInsideBars=true;
	extern bool   ShowOutsideBars=true;

	extern double   PercentageInputForPinBars=66;
	extern int  PinBarsLookBackPeriod=6;
	extern double  PercentageInputForShavedBars=5;

	extern bool   TurnRegularBarstoNeutral=true;
	 

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorShortName("BARS_IDENTIFIER");
   int windowIndex=WindowFind("BARS_IDENTIFIER");
   if(windowIndex<0)
     {
      // if the subwindow number is -1, there is an error
      Print("Can\'t find window");
      return(0);
     }
//---- indicator buffers mapping  
   
//---- initialization done   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
    int windowIndex=WindowFind("BARS_IDENTIFIER");
    ObjectsDeleteAll(windowIndex);     
	
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
  
 
	  if(Bars<=3) return(0);
	 int ExtCountedBars=IndicatorCounted();
	 if (ExtCountedBars<0) return(-1);
	 int pos;
	 int limit=Bars-2;
	 if(ExtCountedBars>2) limit=Bars-ExtCountedBars-1;
	 pos=limit;
	 
	 
	double PinBars=PercentageInputForPinBars*0.01;	 
	double ShavedBars=PercentageInputForShavedBars*0.01;
   
   while(pos>=0) 
     {
  
      double Range = High[pos] - Low[pos];
	  bool PinBarUp = false;
	  bool PinBarDown = false;
	  
	  bool ShavedBarUp = false;
	  bool ShavedBarDown = false;
	  
	   bool InsideBar = false; 	  
	   bool OutsideBar = false; 
	  
	  double Min, Max;
	  
	   Max=iHigh(Symbol(),Period(),iHighest(Symbol(),Period(),MODE_HIGH,PinBarsLookBackPeriod,pos));
       Min=iLow(Symbol(),Period(),iLowest(Symbol(),Period(),MODE_LOW,PinBarsLookBackPeriod,pos));
	  
	  if (ShowPinBars)
	  {
			if ((Open[pos] > High[pos] - (Range * PinBars)) && ( Close[pos] > High[pos] - (Range * PinBars)) && (Low[pos] <= Min)) 
			{
			PinBarUp=true;
			}
			
			if  ((Open[pos] < High[pos] - (Range *  PinBars)) &&  (Close[pos] < High[pos]-(Range * PinBars)) && (High[pos] >= Max))
			{
			PinBarDown=true;
			}
			
	  }
	  
	    if (ShowShavedBars)
	  {
              if (Close[pos] >= (High[pos] - (Range * ShavedBars)))  
			  {
			  ShavedBarUp=true;
			  }
			  if (Close[pos] <= (Low[pos] + (Range * ShavedBars))) 
			  {
			  ShavedBarDown=true;
			  }			
	  }
	  
	  
	 
	if (ShowInsideBars ) 
	{
	 if ( High[pos] <= High[pos+1] && Low[pos] >= Low[pos+1] )
	 {
	 InsideBar= true;
	 }
	}

 
	if (ShowOutsideBars ) 
	{
	  if (High[pos] > High[pos+1] && Low[pos] < Low[pos+1]  )
	  {
	  OutsideBar= true;
	  }
	  
	}
	
	  

    
		 if (PinBarUp)
		 {
		 objText(TimeToStr(Time[pos]), "PB-U", Time[pos], Low[pos], 0, PinBarUpColor,  "Arial", 7);
		 }
         else if (PinBarDown) 
		 {
         objText(TimeToStr(Time[pos]), "PB-D", Time[pos], High[pos], 0, PinBarDownColor,  "Arial", 7);
		 }
		 
		 
		 if (ShavedBarUp)
		 {
		  objText(TimeToStr(Time[pos]), "SB-U", Time[pos], Low[pos], 0, ShavedBarUpColor,  "Arial", 7);
		 }
         else if (ShavedBarDown) 
		 {
         objText(TimeToStr(Time[pos]), "SB-D", Time[pos], High[pos], 0, ShavedBarDownColor,  "Arial", 7);
		 }
		 
		 
		  if (InsideBar)
		 {
		 objText(TimeToStr(Time[pos]), "IB", Time[pos], Low[pos], 0, InsideBarColor,  "Arial", 7);
		 }
        
		 
		 
		 if (OutsideBar)
		 {
		  objText(TimeToStr(Time[pos]), "OB", Time[pos], Low[pos], 0, OutsideBarColor,  "Arial", 7);
		 }
       
		 
		 pos--;
		 
	 }
   return(0);
  }
  
 
void objText(string name, string tex, datetime time, double price, int window=0, color tex_color=White, string tex_font="Arial", int tex_size=7)
  {
    int windowIndex=WindowFind("BARS_IDENTIFIER");
   if(ObjectFind(name+windowIndex)==-1)
   {
      ObjectCreate(name+windowIndex, OBJ_TEXT, window, time, price);
   }
   ObjectSet(name+windowIndex, OBJPROP_TIME1, time);
   ObjectSet(name+windowIndex, OBJPROP_PRICE1, price);
   ObjectSetText(name+windowIndex, tex, tex_size, tex_font, tex_color);
  }