What's wrong with this ordercloseby function code ??

 

Can someone help me to correct this code

 

void closeby1()
{  
   for (int total = OrdersTotal() - 1; total>= 0; total--) 
   {
      OrderSelect(total, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()) 
      {  
         if (OrderSymbol() == Symbol() && (OrderComment() == "buy1_comment" || OrderComment() == "buy2_comment")) 
         {
            OrderID = OrderTicket();
         }
         if (OrderSymbol() == Symbol() && (OrderComment() == "sell1_comment" || OrderComment() == "sell2_comment")) 
         {
            OrderIDopposite = OrderTicket();
         }
         OrderCloseBy(OrderID,OrderIDopposite,Black);
      }
   }
}
 
What is the problem actually?
 
i get error 4108 and get unknown ticket too, but in tester it successfully closing several order
 
What are you trying to achieve actually?
 

well, i just need to close all hedge order buy and sell with the same lot size,

the problem is a lot of order on my system, so when i use close all order function (not closeby function) it is bad on live forward test, mean all order is not closing all order on the only one order close price

 

Have you look into this ?

https://sites.google.com/site/marketformula/mql4-trading-functions/mql4-orderclose-ordercloseby-orderdelete-ordermodify-ordersend
 
i was read that article doing some try and error, and i was sent you a private message , please read that, thanks
 

It is not clear exactly what you are trying to do

To be honest, I have never used OrderCloseBy in my coding.

Note, in your code,  you try to close the trades after every orderselect with the symbol, whether you have found the relevant trades or not . So you will get errors.

This is not tested, but may be of use. You should still add checking for errors.

void closeby1()
  {
   int OrderID=0,OrderIDopposite=0;
   for(int total=OrdersTotal()-1; total>=0; total--)
     {
      if(OrderSelect(total,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol())
           {
            if(OrderComment()=="buy1_comment" || OrderComment()=="buy2_comment")
              {
               OrderID=OrderTicket();
              }
            if(OrderComment()=="sell1_comment" || OrderComment()=="sell2_comment")
              {
               OrderIDopposite=OrderTicket();
              }
            if(OrderID>0 && OrderIDopposite>0)
              {
               if(OrderCloseBy(OrderID,OrderIDopposite,Black))
                 {
                  OrderID=0;
                  OrderIDopposite=0;
                 }
              }
           }
        }
     }
  }
 
Actually what he wants is very simple. Get both opened trades of buy & sell with same lot, close both of it.
 
GumRai:

It is not clear exactly what you are trying to do

To be honest, I have never used OrderCloseBy in my coding.

Note, in your code,  you try to close the trades after every orderselect with the symbol, whether you have found the relevant trades or not . So you will get errors.

This is not tested, but may be of use. You should still add checking for errors.

 

ok thanks for your help, i will try
Reason: