Programmatically execute Multiple OrderCloseBy

 

Hello,

I saw that there is an OrderCloseBy method.

I also saw that when using the UI of the MT4 (build 224) and creating a new order manually there is another type of order "Multiple Close By" (This option only exists if you have opposite trades opened, for example EURUSD buy and EURUSD sell).

How can I execute a Multiple Order Close By programmatically like I can do manually?

Thanks.

 
MosheElisha:

How can I execute a Multiple Order Close By programmatically like I can do manually?

I've also asked about this in the past, without success. I can't see an alternative to a messy loop which pairs repeatedly pairs off individual orders using OrderCloseBy, and which handles the fact that OrderCloseBy creates a new order for the difference in lot size if the sizes of its two parameters aren't equal. This works, but is undesirably slow and messy.


Also need to bear in mind that OrderCloseBy only works on some brokers.

 

Hey,

Thanks for the reply.

I think that you are right but it is weird that it can be done manually but there isn't a program API.

 
bool multiple_closeby(const string symbol=NULL, const int magic=NULL)
{
   for(int i=OrdersTotal()-1; i>0; --i){
      if(OrderSelect(i, SELECT_BY_POS)
         && OrderType() < 2 
         && (symbol == NULL || OrderSymbol() == symbol)
         && (magic == NULL  || OrderMagicNumber() == magic)
      ){
         int    first_ticket = OrderTicket();
         string first_symbol = OrderSymbol();
         int    first_type   = OrderType();
         for(int j=i-1; j>=0; --j){
            if(OrderSelect(j, SELECT_BY_POS)
               && OrderType() < 2
               && (magic == NULL || OrderMagicNumber() == magic)
               && OrderSymbol() == first_symbol  
               && OrderType()   != first_type
            ){
               if(OrderCloseBy(first_ticket, OrderTicket())){
                  return multiple_closeby(symbol, magic);
               }else{
                  return false;
               }
            }
         }
      }
   }
   return true;
}
Reason: