Help with some coding

 

I am trying to code an idea, but I got stuck with some things. How do I find the highest value X bars back and the lowest value X bars back? Thank you

Reason: