Download MetaTrader 5
To add comments, please log in or register
Interessiert am Partnerprogramm MQL5.community? Veröffentliche Links und verdiene Geld damit!
m
105
m 2015.03.13 15:42 

Hello,

I tried close orders for multicurrency strategy (all open orders). I'm not sure can I use twice OrderClose() in the same function.  I tried used one, but didn't work either. Has anyone any suggestions, different point of view? Thank you.

for (int z=OrdersTotal()-1;z>=0;z--)
 {
        
       if (OrderSelect(z,SELECT_BY_TICKET)==true)
    //  if (OrderSymbol()!=Symbol())continue;
        if (MagicNumber==OrderMagicNumber())
      
    while (true)
    {    
     if (OrderType()==OP_BUY)
     {
      if (X>Y )
      {      
       bool ans=OrderClose(OrderTicket(),OrderLots(),MarketInfo("A/C",MODE_BID),1);
        ans=OrderClose(OrderTicket(),OrderLots(),MarketInfo("C/B",MODE_BID),1);                  
      }
      if (X<Y)
      {
       ans=OrderClose(OrderTicket(),OrderLots(),MarketInfo("A/B",MODE_BID),1);
      }
       
      }
      
      if (OrderType()==OP_SELL)
      {
      if (X<Y)
       {       
        ans=OrderClose(OrderTicket(),OrderLots(),MarketInfo("A/C",MODE_ASK),1); 
        ans=OrderClose(OrderTicket(),OrderLots(),MarketInfo("C/B",MODE_ASK),1);         
       }
       if (X>Y)
       {
        ans=OrderClose(OrderTicket(),OrderLots(),MarketInfo("A/B",MODE_ASK),1);
      
       }
       
       break;
      }
   }   
      
   }    
whroeder1
13596
whroeder1 2015.03.13 17:39  
ans=OrderClose(OrderTicket(),OrderLots(),MarketInfo("A/C",MODE_ASK),1); 
  1. OrderClose - MQL4 Documentation takes 5 arguments, you pass 4
  2. Check your return codes (OrderClose) What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. You have no idea what symbol the order is, but you hard code "A/C" and "C/B" just use OrderSymbol()
  4. No need for MarketInfo, just use OrderClosePrice().
  5. for (int z=OrdersTotal()-1;z>=0;z--)
     {        
           if (OrderSelect(z,SELECT_BY_TICKET)==true)
    z is a position number, not a ticket number.
m
105
m 2015.03.13 19:26  

Thank you for answer.

5. the index z means position or ticket number (MagicNumber or ...I don't know what author thought "unique number") according: https://book.mql4.com/trading/orderclose   maybe should be OrderMagicNumber() instead OrdersTotal(), I will try.

4. I read somewhere on the forum in the multi currency should be MarketInfo, because the code can change price from different crosses. It is the reason I used it.

3. I have idea. In this specific case letters means currencies.

2. I read what RaptorUK wrote twice. If I understand you suggest OrderSelect() can't work correctly because it not return true? Only false because problem is with position or number in this case. I will try with if(!OrderSelect())

1. I don't need 5 arguments according:  https://docs.mql4.com/trading/orderclose

I think I will leave this code for 2-3 days :)

whroeder1
13596
whroeder1 2015.03.14 18:15  

kot_filemon: 5. the index z means position or ticket number 

4. I read somewhere on the forum in the multi currency should be MarketInfo, because the code can change price from different crosses. It is the reason I used it.

  1. Either it's a position number [0 .. OrdersTotal - 1] or it's a ticket (a 8-12 digit number returned by the broker.) Can't be both. (Only in the tester are ticket numbers 1,2,3... And if you close ticket 2 then you have ticket's 1 and 3 at positions 0 and 1.
  2. You can't use Bid and Ask because it is multi-currency. But OrderClosePrice() returns the actual value, independent of direction. Try it.
/
To add comments, please log in or register