How to notify the EA upon completing a trade with Stop Loss or Take Profit?

 

I'm writing an Expert Advisor that closes trades on his own, given a stop loss or a take profit using OrderSend.

double tp = price + point * slTpDeviation;
request.tp = tp;
OrderSend(request,result)

I have a boolean variable, that depends on whether or not the agent currently has an active trade or not.

bool undergoingTrade;

When I find a condition that makes me want to close the trade I can set the boolean easily, but when the trade completes automatically, either in a profit or a loss, I am not updating the boolean.

Is there a function, that is called whenever a trade is completed?

Is there a practice programmers use to notify the EA that a trade has been completed?

Thanks in advance.

 
Assuming that you only send one order at a time you can continuously check whether you have open orders which are not yet filled. You can do so by using OrdersTotal(). When you order is executed and gets filled you have no more remaining open orders. This is the signal to you that you order has been executed and filled.