//+------------------------------------------------------------------+
//|                                                     Range_v2.mq4 |
//|                                  Copyright © 2006, Forex-TSD.com |
//|                         Written by IgorAD,igorad2003@yahoo.co.uk |   
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |                                      
//+------------------------------------------------------------------+

//| MODIFIED_VERSION_ Range_v2                                       |
//| MODIFIED BY AVERY T. HORTON, JR. AKA THERUMPLEDONE@GMAIL.COM     |
//| I am NOT the ORIGINAL author 
//  and I am not claiming authorship of this indicator. 
//  All I did was modify it. I hope you find my modifications useful.|
//|                                                                  |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2006, Forex-TSD.com "
#property link      "http://www.forex-tsd.com/"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 DodgerBlue
#property indicator_color2 Tomato
#property indicator_color3 Lime
#property indicator_color4 Orange

#property indicator_width1 0
#property indicator_width2 0
#property indicator_width3 2
#property indicator_width4 2
#property indicator_style1 2
#property indicator_style2 2



extern bool   Sound.Alert    = false ;
extern bool   Show.PriceBox  = true ;
extern bool   Show.MidBand   = true ;
extern int       myBoxWidth  = 3;


extern int TimeFrame =1440;
extern int Shift     =   0;

double UpBuffer[];
double DnBuffer[];
double OpenBuffer[];
double CloseBuffer[];


string symbol, tChartPeriod,  tShortName ;  
int    digits, period  ; 

bool Trigger ;

int OldBars = -1 ;

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

int init()
{


   period       = Period() ;     
   tChartPeriod =  TimeFrameToString(period) ;
   symbol       =  Symbol() ;
   digits       =  Digits ;   

   tShortName = "rv2"+ symbol + tChartPeriod  ;

   string short_name;
   //IndicatorBuffers(4);
   
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,UpBuffer);
   SetIndexLabel(0,"High");
   SetIndexDrawBegin(0,0);
   
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,DnBuffer);
   SetIndexLabel(1,"Low");
   SetIndexDrawBegin(1,0);
   
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,OpenBuffer);
   SetIndexLabel(2,"Open");
   SetIndexDrawBegin(2,0);
   
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,CloseBuffer);
   SetIndexLabel(3,"Close");
   SetIndexDrawBegin(3,0);
   
   SetIndexShift(0,Shift*TimeFrame/Period());
   SetIndexShift(1,Shift*TimeFrame/Period());
   SetIndexShift(2,Shift*TimeFrame/Period());
   SetIndexShift(3,Shift*TimeFrame/Period());
   
   short_name="Range_v2("+TimeFrame+")";
   IndicatorShortName(short_name);
   
   
   switch(TimeFrame) 
   {
   case 1    : TimeFrame=PERIOD_M1;  break; 
   case 5    : TimeFrame=PERIOD_M5;  break;
   case 15   : TimeFrame=PERIOD_M15; break;
   case 30   : TimeFrame=PERIOD_M30; break;
   case 60   : TimeFrame=PERIOD_H1;  break;
   case 240  : TimeFrame=PERIOD_H4;  break;
   case 1440 : TimeFrame=PERIOD_D1;  break;
   case 7200 : TimeFrame=PERIOD_W1;  break;
   case 28800: TimeFrame=PERIOD_MN1; break;
   default  : TimeFrame=Period();   break;
   }
return(0);
}

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

int deinit()
{
   ObjectDelete(tShortName+"U"); 
   ObjectDelete(tShortName+"L"); 
   ObjectDelete(tShortName+"O"); 
   ObjectDelete(tShortName+"C");          
   return(0);
}

//+------------------------------------------------------------------+
int start()
{


//+--------- TRO MODIFICATION ---------------------------------------+   
   if( Bars != OldBars ) { Trigger = True ; }
   
   datetime TimeArray[];
   int i=0,y=0, prevy=0;  
   int counted_bars=IndicatorCounted();
   double LowArray[],HighArray[],OpenArray[],CloseArray[];
   
   
      if (TimeFrame<Period()) 
      {
      SetIndexDrawBegin(0,Bars);  
      SetIndexDrawBegin(1,Bars);
      Comment("Incorrect TimeFrame");
      return(0);
      }

   if ( counted_bars > 0 )  int limit=Bars-counted_bars+TimeFrame/Period();
   if ( counted_bars < 0 )  return(0);
   if ( counted_bars ==0 )  limit=Bars-1; 
     
   ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame);
   ArrayCopySeries(LowArray,MODE_LOW,Symbol(),TimeFrame);  
   ArrayCopySeries(HighArray,MODE_HIGH,Symbol(),TimeFrame);
   ArrayCopySeries(OpenArray,MODE_OPEN,Symbol(),TimeFrame);  
   ArrayCopySeries(CloseArray,MODE_CLOSE,Symbol(),TimeFrame);
     
      for(i=0,y=0;i<limit;i++)
      {
      prevy = y;
      if (Time[i]<TimeArray[y]) y++;
      UpBuffer[i]=HighArray[y];    
      DnBuffer[i]=LowArray[y];
      OpenBuffer[i]=OpenArray[y];    
      
      if(y!=prevy) 
      CloseBuffer[i]=CloseArray[y];
      else CloseBuffer[i]=CloseArray[prevy];
      
      } 
      

      if ( Trigger &&  Sound.Alert ) 
      {
        if( Close[0] > UpBuffer[0] ) { Trigger = False ; Alert(symbol,"  ", tChartPeriod, " Price above High "+ DoubleToStr(UpBuffer[0] ,digits)); }
        if( Close[0] < DnBuffer[0] ) { Trigger = False ; Alert(symbol,"  ", tChartPeriod, " Price below Low " + DoubleToStr(DnBuffer[0] ,digits)); }     
      }


    if(Show.PriceBox)
    {


       if (ObjectFind(tShortName+"U") != 0)
      {
          ObjectCreate(tShortName+"U",OBJ_ARROW,0,Time[0],UpBuffer[0]);
          ObjectSet(tShortName+"U",OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tShortName+"U",OBJPROP_COLOR,indicator_color1);  
          ObjectSet(tShortName+"U",OBJPROP_WIDTH,myBoxWidth);  
      } 
      else
      {
         ObjectMove(tShortName+"U",0,Time[0],UpBuffer[0]);
         ObjectSet(tShortName+"U",OBJPROP_COLOR,indicator_color1);  
      }

       if (ObjectFind(tShortName+"L") != 0)
      {
          ObjectCreate(tShortName+"L",OBJ_ARROW,0,Time[0],DnBuffer[0]);
          ObjectSet(tShortName+"L",OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tShortName+"L",OBJPROP_COLOR,indicator_color2);  
          ObjectSet(tShortName+"L",OBJPROP_WIDTH,myBoxWidth);  
      } 
      else
      {
         ObjectMove(tShortName+"L",0,Time[0],DnBuffer[0]);
         ObjectSet(tShortName+"L",OBJPROP_COLOR,indicator_color2);  
      } 

       if (ObjectFind(tShortName+"O") != 0)
      {
          ObjectCreate(tShortName+"O",OBJ_ARROW,0,Time[0],OpenBuffer[0]);
          ObjectSet(tShortName+"O",OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tShortName+"O",OBJPROP_COLOR,indicator_color3);  
          ObjectSet(tShortName+"O",OBJPROP_WIDTH,myBoxWidth);  
      } 
      else
      {
         ObjectMove(tShortName+"O",0,Time[0],OpenBuffer[0]);
         ObjectSet(tShortName+"O",OBJPROP_COLOR,indicator_color3);  
      }
 
       if (ObjectFind(tShortName+"C") != 0)
      {
          ObjectCreate(tShortName+"C",OBJ_ARROW,0,Time[0],CloseBuffer[0]);
          ObjectSet(tShortName+"C",OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tShortName+"C",OBJPROP_COLOR,indicator_color4);  
          ObjectSet(tShortName+"C",OBJPROP_WIDTH,myBoxWidth);  
      } 
      else
      {
         ObjectMove(tShortName+"C",0,Time[0],CloseBuffer[0]);
         ObjectSet(tShortName+"C",OBJPROP_COLOR,indicator_color4);  
      }

   } // if(Show.PriceBox)

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


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