//+------------------------------------------------------------------+
//|                                            ay-BarsOnTheRange.mq4 |
//|                      Copyright © 2010, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Green

#define PREFIX "ay-RBar_"


//---- input parameters
extern int       BarsToLook     = 4;
extern int       Range          = 50;
extern int       NumBarsToScan  = 100;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];


double fix_point;    
double fix_digits; 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
  
   fix_point = Point;
   fix_digits = Digits;
   if (Point == 0.001 || Point == 0.00001 ){ fix_point=Point*10; fix_digits= Digits -1; }
  
//---- indicators
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,159);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexEmptyValue(0,0.0);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,159);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexEmptyValue(1,0.0);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   int objs = ObjectsTotal();
   string name;
   for(int cnt=ObjectsTotal()-1;cnt>=0;cnt--)
   {
      name=ObjectName(cnt);
      if (StringSubstr(name,0,StringLen(PREFIX)) == PREFIX) ObjectDelete(name); 
   }   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {

   int limit, i, j, k;
   int counted_bars=IndicatorCounted();
   //---- check for possible errors
   if(counted_bars<0) return(-1);
   //---- the last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
   limit = MathMin(NumBarsToScan, limit);   
   for (i=limit; i>=0; i--)
   {
      
      
      double hr = High[iHighest(NULL, 0, MODE_HIGH, BarsToLook, i)];
      double lr = Low [iLowest (NULL, 0, MODE_LOW,  BarsToLook, i)];
      double rangeinpip = (hr-lr)/fix_point;
      //Comment("rangeinpip " + rangeinpip + " hr " + hr + " lr " + lr);
      if (rangeinpip <= Range)
      {
         ExtMapBuffer1[i] = hr;
         ExtMapBuffer2[i] = lr;
         DrawTl(PREFIX+Time[i]+"hr",Time[i], hr, Time[i+BarsToLook-1], hr, Green);
         DrawTl(PREFIX+Time[i]+"lr",Time[i], lr, Time[i+BarsToLook-1], lr, Red);
      }
      else
      {
         ExtMapBuffer1[i] = 0.0;
         ExtMapBuffer2[i] = 0.0;            
      }
   }
   return(0);
  }
  
void DrawTl(string tlName, datetime t1, double v1, datetime t2, double v2, 
            color tlColor, int style = STYLE_SOLID, int width = 1, string desc="")
{
   if(ObjectFind(tlName) != 0)
   {
      ObjectCreate(
            tlName
            , OBJ_TREND
            , 0
            , t1
            , v1
            , t2
            , v2
            );      
   }else
   {
      ObjectMove(tlName, 0, t1, v1);   
      ObjectMove(tlName, 1, t2, v2);
   }
   ObjectSet(tlName, OBJPROP_COLOR, tlColor);
   ObjectSet(tlName, OBJPROP_RAY, false);
   ObjectSet(tlName, OBJPROP_STYLE, style);
   ObjectSet(tlName, OBJPROP_WIDTH, width);
   ObjectSetText(tlName, desc);
}  
//+------------------------------------------------------------------+