What is your concrete issue???. Put your attempt and maybe anybody help you. Otherwise, you could consider this section:
This is what I tried;
Always count down when working with orders, mandatory if you want to close.
No need to break, continue.
Completely unneeded. Use OrderClosePrice() instead of Bid/Ask, it will be the right close price.
Here I am not sure what you want to do, but this condition will never be true. An order can't be opened after the current time, unless you found a way to travel in time. (Nota: please gurus don't start to argue about that :-D).