Your code above is correct for closing only the order that matches the condition, but it runs only once per bar. You might want to do that in a trade function, for closing the trade as soon as the condition is met.

For closing a particular order, you can retrieve the TRADE* pointer from the enter command:


TRADE* my_order = enterLong();
...
exitTrade(my_order,0);