Close all position function

 
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 ##

 
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".

 
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. 

 
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...." ...
 
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?
 
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.
 
Comments that do not relate to this topic, have been moved to "How to close a position with mql5 ?".
Reason: