I have two bar index say B1 and B2, and I want to find the highest and second-highest value similarly lowest and second-lowest value between two bar index. And search should include the value of high and low of B1 and B2 bar index.
Can someone please help with the loop.
The best way to get help on the forum is to show your work. That way the more experienced members can see what you understand and what you don't. They might suggest a different function, or point out how you have misinterpreted its' usage.
When all you do is post what you want, it sounds like you want some one else to do your work for you. While there are many very helpful forum members, most like to see you have put in an effort, and are happy to tweak your work, to help you get your result.
Another way to get good results is to SEARCH. There are probably several threads in the forum of a similar nature that may help you find the functions you could use, and the techniques necessary. Perhaps like this one: Unusual Exploration results when calculating # of bars since an event
That link probably has a few of the functions you might be able to use for your purposes.