Download MetaTrader 5
To add comments, please log in or register
Join MQL5 Cloud Network. Start earning money!
Syarif Nur Arief
1168
Syarif Nur Arief 2015.02.08 03:05 

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
1401
SYED NAUFAL GADDAFI 2015.02.08 03:21  
What is the problem actually?
Syarif Nur Arief
1168
Syarif Nur Arief 2015.02.08 04:07  
i get error 4108 and get unknown ticket too, but in tester it successfully closing several order
SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2015.02.08 05:03  
What are you trying to achieve actually?
Syarif Nur Arief
1168
Syarif Nur Arief 2015.02.08 05:22  

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
1401
SYED NAUFAL GADDAFI 2015.02.08 05:27  

Have you look into this ?

https://sites.google.com/site/marketformula/mql4-trading-functions/mql4-orderclose-ordercloseby-orderdelete-ordermodify-ordersend
Syarif Nur Arief
1168
Syarif Nur Arief 2015.02.08 05:33  
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
8007
Keith Watford 2015.02.08 05:43  

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
1401
SYED NAUFAL GADDAFI 2015.02.08 05:45  
Actually what he wants is very simple. Get both opened trades of buy & sell with same lot, close both of it.
Syarif Nur Arief
1168
Syarif Nur Arief 2015.02.09 02:37  
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