Download MetaTrader 5

Close all position function

To add comments, please log in or register
Do you keep a blog? Place a link to MQL5.community there and make money!
Daniel George
150
Daniel George 2016.03.30 10:25 
void CloseEverything()
  {
   for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
     {
      xxx=OrderSelect(count,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()==Symbol() && (OrderMagicNumber()==Magic || OrderComment()==Comment))
        {
         if(OrderType()==OP_BUY)
          Printing=OrderClose(OrderTicket(),OrderLots(),Ask,100);  // not working on IFX
         if(OrderType()==OP_SELL)
          Printing=OrderClose(OrderTicket(),OrderLots(),Bid,100);  // not working on IFX
         if(OrderType()==OP_BUYLIMIT || OrderType()==OP_BUYSTOP || OrderType()==OP_SELLLIMIT || OrderType()==OP_SELLSTOP)
          Printing=OrderDelete(OrderTicket());  // working on all tested brokers
         if(Printing==false)
          {
           Printing=GetLastError();
           Print("## Close all trades ",string(Printing)+" = "+ErrorDescription(Printing)," ##");
          }
        }
     }
  }

A question for someone smarter than me. I have a function that is supposed to close all trades, active or pending orders. It works on some brokers I'm trading with, but not on InstaFX. On IFX it only closes the pending orders and I cannot figure it out why. So the opened orders are not closed.

The printed log returns this: ## Close all trades true = no error, trade conditions not changed ##

Alain Verleyen
Moderator
30743
Alain Verleyen 2016.03.30 10:40  
Daniel George:

A question for someone smarter than me. I have a function that is supposed to close all trades, active or pending orders. It works on some brokers I'm trading with, but not on InstaFX. On IFX it only closes the pending orders and I cannot figure it out why. So the opened orders are not closed.

The printed log returns this: ## Close all trades true = no error, trade conditions not changed ##

It only close if Magic or Comment matched, is it the case ?
      if(OrderSymbol()==Symbol() && (OrderMagicNumber()==Magic || OrderComment()==Comment))
A BUY position is closed at BID price, not Ask, reverse for sell.
if(OrderType()==OP_BUY)
          Printing=OrderClose(OrderTicket(),OrderLots(),Ask,100);  // not working on IFX

Instead use OrderClosePrice() it will be Ask/Bid and you don't need to care about it :

if(OrderType()==OP_BUY || OrderType()==OP_SELL)
          Printing=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),100);  

Your "Printing" variable is not set correctly, it's a bool or an integer ? If an order is not close/delete, it should be reported and not return "no error".

Daniel George
150
Daniel George 2016.03.30 17:21  
Alain Verleyen:
It only close if Magic or Comment matched, is it the case ?
A BUY position is closed at BID price, not Ask, reverse for sell.

Instead use OrderClosePrice() it will be Ask/Bid and you don't need to care about it :

Your "Printing" variable is not set correctly, it's a bool or an integer ? If an order is not close/delete, it should be reported and not return "no error".

Correct. I thought to use both Magic and Comment just to make sure the EA will stay on the same chart :) Although I realize only one of them would have been enough, probably.

I've made the modification you suggested and waiting now on an open trade to see what happens.  

The Printing is a bool. I'm not sure what you mean by 'reported', so is it ok, you think? Or can you give me a link to the documentation?

Thanks a lot for your help. 

Alain Verleyen
Moderator
30743
Alain Verleyen 2016.03.30 17:29  
Daniel George:

Correct. I thought to use both Magic and Comment just to make sure the EA will stay on the same chart :) Although I realize only one of them would have been enough, probably.

I've made the modification you suggested and waiting now on an open trade to see what happens.  

The Printing is a bool. I'm not sure what you mean by 'reported', so is it ok, you think? Or can you give me a link to the documentation?

Thanks a lot for your help. 

Sorry wrong English...If an order is not close/delete, it should be logged : "error 1234...." ...
Daniel George
150
Daniel George 2016.03.31 01:46  
Alain Verleyen:
Sorry wrong English...If an order is not close/delete, it should be logged : "error 1234...." ...
Oh, I see. So Printing should be 'int' just so that the EA can identify the error type by its number, right?
Alain Verleyen
Moderator
30743
Alain Verleyen 2016.03.31 12:11  
Daniel George:
Oh, I see. So Printing should be 'int' just so that the EA can identify the error type by its number, right?
Yes. Also you said all orders are not processed, but you have said you receive the message "## Close all trades true = no error, trade conditions not changed ##", that's not normal.
Alain Verleyen
Moderator
30743
Alain Verleyen 2016.04.19 15:11  
Comments that do not relate to this topic, have been moved to "How to close a position with mql5 ?".
To add comments, please log in or register