Experts: 1 Click To Close All Open Positions at Current Attached Chart

 

1 Click To Close All Open Positions at Current Attached Chart:

1 Click "Close Button" to close all open positions at current attached chart.

Author: grandaevus

 

Thanks
***** 

 

Forum on trading, automated trading systems and testing trading strategies

Experts: 1 Click To Close All Open Positions

Alain Verleyen, 2016.02.23 20:32

This code is not reliable, in case of error you can end up with an infinite loop and crash your MT4. Very dangerous.

You don't take into account my remark.

To everyone :

Don't use this code on a real account, it can crash your MT4 and you can have very bad surprise.

 
Alain Verleyen:

You don't take into account my remark.

To everyone :

Don't use this code on a real account, it can crash your MT4 and you can have very bad surprise.

To everyone :

You can only end up with an infinite loop if and only if your broker's trading server is down like weekends. 

So when a trading server is down where you can't open a trade and/or close a trade does it really matter if you've ended up with an infinite loop or not?

Just restart your mt4 and everything would be fine. 

 

Forum on trading, automated trading systems and testing trading strategies

Experts: 1 Click To Close All Open Positions

Alain Verleyen, 2016.03.19 11:36

It's not about agree or disagree it's about facts. The fact is : this code can end in an infinite loop.

Murphy's law said "Anything that can go wrong, will go wrong." In my opinion it's not something acceptable about trading real money.

I want to add that there are several situation where this code can end up in a loop (not necessarily infinite).

  1. Trading disabled on client side for some reasons.
  2. High slippage due to high volatility, news.
  3. ... ?
 
Alain Verleyen:

I want to add that there are several situation where this code can end up in a loop (not necessarily infinite).

  1. Trading disabled on client side for some reasons.
  2. High slippage due to high volatility, news.
  3. ... ?

Here's my answers 

1)  When trading disabled on client side you can't run an expert advisor so you don't have to worry

2) High slippage due to high volatility, news. Well, this expert advisor closes your orders with OrderClosePrice() which means no matter what the bid/ask price is,  I can assure you all your orders will be closed.

3) .....? Me too.

 

Forum on trading, automated trading systems and testing trading strategies

Experts: 1 Click To Close All Open Positions

Alain Verleyen, 2016.03.19 17:40

That's fine...if you don't want to listen, don't listen, if you don't want to learn, don't learn. Stuart said it very nicely "In coding, you can't think in terms of "it will never happen".

When using your code for your self, and you don't have problem to restart MT4 to fix an issue, it's ok. When you post code publicly, you have a responsibility, at least it's how I am seeing things.

My conclusion to everyone. Don't use this code on a real account, it could lead to undesirable issues.


 

Forum on trading, automated trading systems and testing trading strategies

Experts: 1 Click To Close All Open Positions

grandaevus, 2016.03.19 18:01

To whom it may concern,

Those who're afraid of an infinite loop, 

Just delete this sentence from the code

if(checkOrderClose==false) continue;

or

add //

// if(checkOrderClose==false) continue;

 

Forum on trading, automated trading systems and testing trading strategies

Experts: 1 Click To Close All Open Positions

grandaevus, 2016.03.22 05:20

Code has been fixed and sent to be published.

This new version doesn't contain the code written below that previous versions had which may cause an infinite loop . 

if(checkOrderClose==false) continue;

but instead brings an error control system to prevent an infinite loop while still closing all trades. 

if(checkOrderClose == false)
         {
         int errorCode = GetLastError();
         
         if (errorCode == 1 || errorCode == 2 || errorCode == 5 || errorCode == 6 || errorCode == 64 || errorCode == 65 || errorCode == 132 || errorCode == 133 || errorCode == 139) break;
         else continue;        
         }

 
hi, do you know how to edit the code make this EA can immediately close all trade instead of close 1 by 1 ?
Reason: