Immediate close of position

 
Hi all, can anyone help with a situation I want to code. An EA has opened a position and is active, now the EA has a "rule to close the position immediately" I can find no way to achieve this action. Thought of holding position ticket number in a buffer, but how do I get that number and close the position??? Any help or examples would be a big help. Thanks.
Reason: