How to calculate BarsSince Price touches the Support or Resistance Line?

#1
Hi guys,

I am using KPL swing AFL.

This is the current AFL which calculates Buy Sell.

Code:
res20 = HHV( H, no );
sup20 = LLV( L, no );
avd20 = IIf( C > Ref( res20, -1 ), 1, IIf( C < Ref( sup20, -1 ), -1, 0 ) );
avn20 = ValueWhen( avd20 != 0, avd20, 1 );
SupRes20 = IIf( avn20 == 1, sup20, res20 );

Buy = Cross( C, Ref( res20, -1 ) );
Sell = Cross( Ref( sup20, -1 ), C );

BarsSincebuy = BarsSince( Buy );
BarsSincesell = BarsSince( Sell );
The BarsSinceBuy or Sell above works as intended for the buy or sell signal.

But as we know many times the price touches the SupRes line and then continues in the original direction. I want to calculate the number of bars before when price touched such a support or resistance.

I know that the main logic is

Code:
HasTouchedSup=IIf((L-supres20)==0,1,-1);
HasTouchedRes=IIf((H-supres20)==0,1,-1);
And this works as intended but I am unable to figure how do I calculate the BarsSince it touched the supres line. Really need your help.

Thanks