Alternative option of ExRem?

I am trying to find top and bottom using the following code

TOP = H>Ref(H,-1) AND H>Ref(H,1) AND Ref(H,-1)> Ref(H,-2) AND Ref(H,1)> Ref(H,2);
BOTTOM = L<Ref(L,-1) AND L< Ref(L,1) AND Ref(L,-1)<Ref(L,-2) AND Ref(L,1)<Ref(L,2);

Now I want to mark alternate TOP and BOTTOM, When there is consecutive TOP, I just want to plot the highest one and in case of consecutive BOTTOM , only lowest BOTTOM.

When I try to filter with ExRem it just takes the first TOP / BOTTOM and discard all other consecutive till the next opposite comes.

So what should i use instead ? thank you.

You'll have to use Barssince or ValueWhen.