Download MetaTrader 5

What's wrong with this ordercloseby function code ??

To add comments, please log in or register
Syarif Nur Arief
1560
Syarif Nur Arief  

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);
      }
   }
}
SYED NAUFAL GADDAFI
1399
SYED NAUFAL GADDAFI  
What is the problem actually?
Syarif Nur Arief
1560
Syarif Nur Arief  
i get error 4108 and get unknown ticket too, but in tester it successfully closing several order
SYED NAUFAL GADDAFI
1399
SYED NAUFAL GADDAFI  
What are you trying to achieve actually?
Syarif Nur Arief
1560
Syarif Nur Arief  

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

SYED NAUFAL GADDAFI
1399
SYED NAUFAL GADDAFI  

Have you look into this ?

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

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;
                 }
              }
           }
        }
     }
  }
SYED NAUFAL GADDAFI
1399
SYED NAUFAL GADDAFI  
Actually what he wants is very simple. Get both opened trades of buy & sell with same lot, close both of it.
Syarif Nur Arief
1560
Syarif Nur Arief  
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
To add comments, please log in or register