Help calculating Bar range (Inside bars)

 

example

With my code below I can identify Bar 6 on the image above. How do I calculate that Bar 5 4 3 2 1 is still inside bar 7 ?


for(int i=0; i<limit; i++)
     {
      
      dCurrent=High[i];
      PrevH=High[i+1];
      CurrH=High[i];
      PrevL=Low[i+1];
      CurrL=Low[i];
      
       
      if(PrevH < CurrH )
        {
         
        //do staff
        }
        
      
      if(PrevH < CurrH && CurrL > PrevL)
      {
     
     //Do staff
      }
 

Just for  reference :

1.Recognize first InsideBar,get the BarIndex;

2.Call function N=NBreakInside(BarIndex).


int  NBreakInside(int MyTF,int MyShift) // surely  MyShift>=1 

{

double PH=iHigh(NULL,MyTF,MyShift);

double PL=iLow (NULL,MyTF,MyShift);


    for(int i=MyShift-1;i>=0;i--)

    {

        if(iHigh(NULL,MyTF,i)>PH || iLow(NULL,MyTF,i)<PL)

        {

        return(MyShift-i);

        }

    }

return(0);

}