Experts: 1 Click To Close All Open Positions

 

1 Click To Close All Open Positions:

Click "Close All" button to close all open positions at once.

Author: grandaevus

 

Does it close the positions in one particular chart or also close all positions of any other charts, please?

Thank you.

 
seems it will close all opened orders, it doesn't care the chart which you have applied EA!
 
Suleymann:

Does it close the positions in one particular chart or also close all positions of any other charts, please?

Thank you.

here is for particular close all ( on chart only)
change the section like below

void CloseAllOpenPositions(int intMaxSlippage)
  {
   bool checkOrderClose=false;
   int index=OrdersTotal()-1;

   while(index>=0 && OrderSelect(index,SELECT_BY_POS,MODE_TRADES)==true)
     {
      if(OrderType()==OP_BUY || OrderType()==OP_SELL)
        {
        if(OrderSymbol()==Symbol()){
         checkOrderClose=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),intMaxSlippage,CLR_NONE);

         if(checkOrderClose==false) continue;
          }
        }
      index--;
     }
  }
 

This ea closes all open positions (manual trades and/or trades opened by an expert advisor) regardless of the pair. It has the same function as  "Close All" button.

if(OrderType()==OP_BUY || OrderType()==OP_SELL)
        {
         checkOrderClose=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),intMaxSlippage,CLR_NONE);

         if(checkOrderClose==false) continue;
        }


To make it work only on current attached chart, change the if statement as written below

if((OrderType()==OP_BUY || OrderType()==OP_SELL && OrderSymbol() == Symbol())


        


The code sent by Latifah does the same trick but it`s better to use less if sentences since less if sentences means better execution speeds.

Today I`ve sent another code to be released. This new expert advisor works on only @ attached chart and you can choose to close only manual orders or all orders.

My advice is, if you`re a manual trader and you`re trading in only one pair, please use this expert advisor else use the one v02.

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


I don't want close all order, just want to close  3 or 4 tickets last trade if profit of them > target( ex: target = 6 usd)

same the picture, can you help me,( martingle )

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

I strongly disagree.  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. 

 
grandaevus:

I strongly disagree.  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. 

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.

 
Alain Verleyen:

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.

This is exactly what I do. Talking about facts.

The fact is this code can end in an infinite loop if and only if broker's trading server is down. This is the fact. 

So I don't like repeating my self writing the same answers but could you give me a logical reason why somebody have to worry about an infinite loop where trading server is down which means it does't accept orders.

It doesn't accept orders means you can't open a trade and/or close a trade manually.

If you can't do such a thing manually how do you expect an expert advisor to do this task?

 

Well, you can say add some control code to check error messages , etc... or change the code and remove "continue" .

Why not, but each error control sentence means extra "if sentence" and I'm quite sure I've said this before but  please let me say it again extra "if sentences" means extra load and extra load means slower execution speed.

If you open just one or two trades at a time, it's really unnecessary to use this expert advisor  since you can manually close your orders in maximum 10 seconds.

What if you have 10 or more orders and you want to close them immediately. Well this expert is the right tool for you. 

Reason: