How to close open trades on Expert Removal?

 

Hi,

I have been trying to close any open trades from the DeInit function but it is returning that trading is already stopped. I  also tried to override ExpertRemove function but it says it is a system function. Anyone know how I can do this?

Thanks

cs

 

**EDIT ** This is for MT5

 
cs0amc:

Hi,

I have been trying to close any open trades from the DeInit function but it is returning that trading is already stopped. I  also tried to override ExpertRemove function but it says it is a system function. Anyone know how I can do this?

Thanks

cs

 

**EDIT ** This is for MT5

What's the reason of the removal ?
 
Alain Verleyen:
What's the reason of th
Alain Verleyen:
What's the reason of the removal ?
Simply to ensure no trades are left open when an expert advisor is removed from a chart. This is helpful for live testing and also if dealing with multiple EA's, it removes the chance of any trades being left open.
 
cs0amc:

Hi,

I have been trying to close any open trades from the DeInit function but it is returning that trading is already stopped. I  also tried to override ExpertRemove function but it says it is a system function. Anyone know how I can do this?

Thanks

cs

 

**EDIT ** This is for MT5

You have to use OrderSend() or OrderSendAsync(), not CTrade.
Reason: