Finding and marking the largest range within the previous n periods

 

Just to clarify I am asking for coding help (to learn) more than a quick fix for this particular issue.

I am modifying an existing indicator that marks any range larger than a minimum value (minRange) over a lookback period.  I want it to mark only the largest range of the lookback period.

So I created two new variables. priorRange and newMaxRange.

On line 36 I check priorRange the same way that GetRange does, but I use [x-1] instead of x, which I think should be checking the previous bar. But actually it makes no difference if I use [x+1].

Then on line 37 I say if GetRange is >= priorRange set a value for newMaxRange that is the same as GetRange. In my mind this newMaxRange should only be updated in the loop whenever a new range value is larger than the prior one. This should ensure that newMaxRange is always the largest range value.

However as you can see if you test the code it marks every range that is greater than the minRange just as in the original code. I'm not sure why. 

What is the correct way to do this?

Code follows

//updated so that it will only show a range if it is greater than a prior range check, so if you set lookback to 
//9 periods for example it will only show the highest range in that lookback.
#property  indicator_chart_window

extern int MinRange        =80;
extern int LookBack        =9;
extern bool ShowRange      =1;               // 0=Show Nothing, 1=Show Pips/Range
//extern bool ShowOpnClo     =0;               // 0=Show Nothing, 1=Show Open to close
extern int  PaintBarWidth  =3;               // How fat paint bar will be ?       
extern bool Popup          =0;               // Enable to show Popup Window
extern color BULL          =DodgerBlue;            // Colour for UP bar
extern color BEAR          =Brown;             // Colour for DOWN bar
extern int UpBarShift      =100;               // Interval upside bar
extern int DnBarShift      =50;               // Interval behind bar
datetime RangeT;
// --- Init & Deinit ----------------------------------------------------------
int init()  {RangeT=Time[1]; return(0); }
int deinit()   
   {
   for(int i=LookBack; i>=0; i--)
      {
           ObjectDelete(""+i);
      ObjectDelete("L"+i);
      ObjectDelete("H"+i);
      } 
return(0); }
// --- Main Function call -----------------------------------------------------
int start() //hmm, it does update itself on every new tick, so it has the potential to alert in real-time?
   {
   double GetRange, GetOC, Range; bool BarUP, Flag, priorRange, newMaxRange; 
   for(int x=1; x<LookBack; x++)
      {
      GetRange =(High[x]-Low[x]); if(Digits<4) GetRange=GetRange*100; else GetRange=GetRange*10000;
      priorRange =(High[x-1]-Low[x-1]); if(Digits<4) priorRange=priorRange*100; else priorRange=priorRange*10000;
      if (GetRange >= priorRange) newMaxRange = GetRange;
      //GetOC    =(Close[x]-Open[x]); if(Digits<4) GetOC=GetOC*100; else GetOC=GetOC*10000; 
      if (Open[x]<Close[x]) BarUP=1; else BarUP=0;
      //Alert("GetRange : ",GetRange,"  MinRange : ",MinRange,"  x : ",x);
      if ( /* GetRange > MinRange && */ GetRange >= newMaxRange)
         {
         // Draw Line on bar / Repaint Bar
         if (BarUP==0)
            {
            ObjectDelete("L"+x);
            ObjectCreate("L"+x, OBJ_TREND, 0, Time[x],High[x],Time[x],Low[x] );
            ObjectSet("L"+x,10,0); ObjectSet("L"+x,8,PaintBarWidth); ObjectSet("L"+x,6,BEAR);
            }
         if (BarUP==1)
            {
            ObjectDelete("H"+x);
            ObjectCreate("H"+x, OBJ_TREND, 0, Time[x],High[x],Time[x],Low[x] );
            ObjectSet("H"+x,10,0); ObjectSet("H"+x,8,PaintBarWidth); ObjectSet("H"+x,6,BULL);
            }
         if (ShowRange /* && !ShowOpnClo*/)          // Draw Range on bars
            {
            if (BarUP==0)
               {
               ObjectDelete(""+x);
               ObjectCreate(""+x, OBJ_TEXT, 0, Time[x],Low[x]-DnBarShift*Point );
               ObjectSetText(""+x, ""+DoubleToStr(GetRange,0), 10, "Arial", BEAR);
               }
            if (BarUP==1)
               {
               ObjectDelete(""+x);
               ObjectCreate(""+x, OBJ_TEXT, 0, Time[x],High[x]+UpBarShift*Point );
               ObjectSetText(""+x, ""+DoubleToStr(GetRange,0), 10, "Arial", BULL);
               }
            }  
         /* if (ShowOpnClo && !ShowRange)          // Draw OpenClose on bars
            {
            if (BarUP==0)
               {
               ObjectDelete(""+x);
               ObjectCreate(""+x, OBJ_TEXT, 0, Time[x],Low[x]-DnBarShift*Point );
               ObjectSetText(""+x, ""+DoubleToStr(MathAbs(GetOC),0), 10, "Arial", BEAR);
               }
            if (BarUP==1)
               {
               ObjectDelete(""+x);
               ObjectCreate(""+x, OBJ_TEXT, 0, Time[x],High[x]+UpBarShift*Point );
               ObjectSetText(""+x, ""+DoubleToStr(GetOC,0), 10, "Arial", BULL);
               }
            }  
         if (ShowOpnClo && ShowRange)          // Draw OpenClose & Range
            {
            if (BarUP==0)
               {
               ObjectDelete(""+x);
               ObjectCreate(""+x, OBJ_TEXT, 0, Time[x],Low[x]-DnBarShift*Point );
               ObjectSetText(""+x, ""+DoubleToStr(MathAbs(GetOC),0)+"/"+DoubleToStr(GetRange,0), 10, "Arial", BEAR);
               }
            if (BarUP==1)
               {
               ObjectDelete(""+x);
               ObjectCreate(""+x, OBJ_TEXT, 0, Time[x],High[x]+UpBarShift*Point );
               ObjectSetText(""+x, ""+DoubleToStr(GetOC,0)+"/"+DoubleToStr(GetRange,0), 10, "Arial", BULL);
               }
            } */ 
         }
      }
   Range=(High[1]-Low[1]); if(Digits<4) Range=Range*100; else Range=Range*10000;
   if (RangeT == Time[1]) {RangeT=Time[0]; Flag = 1;}
   if (Range>MinRange && Flag==1 && Popup==1) 
      {
      if (Open[1]<Close[1]) {Alert(Symbol(),"  BULL bar formed Range is ",Range," Pips"); Flag=0;}
      if (Open[1]>Close[1]) {Alert(Symbol(),"  BEAR bar formed Range is ",Range," Pips"); Flag=0;}
      }
//   Comment("Range : ",Range,"  RangeT : ",RangeT,"  Time[1] : ",Time[1],"  Flag : ",Flag);
   return(0);
   }

// --- End of Main Function ---------------------------------------------------



 

I think my error was that I did not use a different variable for the second quantity. 

Fixed code follows if it will of help to anyone in the future.


int start() //hmm, it does update itself on every new tick, so it has the potential to alert in real-time?
   {
   double GetRange, GetOC, Range; bool BarUP, Flag;
   double max=0,oc;
   int x=1,imax=0,up;
   
   
   if(OnlyLargestRange==true){
   for(int y=1; y<LookBack; y++)
      {
      GetRange =(High[y]-Low[y]); if(Digits<4) GetRange=GetRange*100; else GetRange=GetRange*10000;
      oc    =(Close[y]-Open[y]); if(Digits<4) oc=oc*100; else oc=oc*10000; 
      if (Open[y]<Close[y]) up=1; else up=0;
      
      if(GetRange>max){max=GetRange;x=y;BarUP=up;GetOC=oc;}
      }
      
      //Alert(x);
    
    if (max > MinRange){
    
    GetRange=max;
    
    if (BarUP==0)
            {
            ObjectDelete("L"+x);
            ObjectCreate("L"+x, OBJ_TREND, 0, Time[x],High[x],Time[x],Low[x] );
            ObjectSet("L"+x,10,0); ObjectSet("L"+x,8,PaintBarWidth); ObjectSet("L"+x,6,BEAR);
            } etc.