# Count RSI oversold between Donchian high phases

I am trying to count the number of times the rsi>70 betweent he phase of high moving above donchian 20 and low moving below donchian 20.
I programed it in a loop but i am guessing there is a more optimal way.
Does anyone know how to optimize this loop?

``````SetBarsRequired( sbrAll ) ;
donh = HHV( H, 20 );
donl = llV( l, 20 );
rsiv = RSI( 14 );
twentyHighLong = Ref( HHV( H, 20 ), -1 );
twentyHighShort = Ref( llv( l, 20 ), -1 );
Plot( twentyHighLong, "20 High", colorWhite, styleLine );
Plot( twentyHighShort, "20 low", colorWhite, styleLine );
rsih = rsiv >= 70; // rsi is above 70
rsil = rsiv <= 30; // rsi <30

//find if the last time bet the time the price moved between donh and donl, there was a rsi>70 .
// ie as soon as i hit donh, look for rsi 70
// as soon as i hit donl, look for rsi 30
MakingHigh = H > Ref( donh, -1 );
MakingLow = L < Ref( donl, -1 );
BullishRsi = SumSince( Bullish, rsih );

for( i = 0;   i < BarCount - 1; i++ )
{
if( i > 15 )
{
if( Bullish[i] == False )
{
BullishRsi[i] = 0;
}
else
{
BullishRsi[i] = BullishRsi[i - 1] + rsih[i];

}
}

}

PlotShapes( Bullish*shapeUpArrow, colorGold );
PlotShapes( Bearish*shapedownArrow, colorGreen );
PlotShapes( rsih * ( shapeDigit1 + shapePositionAbove ), colorGold );
PlotShapes( rsil * ( shapeDigit1 + shapePositionAbove ), colorGreen );

Filter = True;

``````

You do not need looping.

Use Cross function (or ExRem) to convert from state to impulse.
Below I just changed to Cross instead of ">" and "<" to show difference.
And for BullishRSI and BearishRsi have added ".... + ValueWhen(...)"

e.g.

``````// code of before above....

MakingHigh = H > Ref( donh, -1 );
MakingLow = L < Ref( donl, -1 );