Order Close Function Issues

 

I have written the function below to close my orders. I want to close only the orders for this symbol and magic number, and only either buy or sell. I've placed the order close within a loop that checks if orders are still open on this currency pair and continues to attempt the close until successful. I don't want it to fail and then stop trying. Does this look correct or am I doing anything wrong here?

The price part in the order close function seems to be giving me problems. In strategy tester the OrderClose() will only work if I use OrderClosePrice() for the closing price, otherwise I get error 138. But on a live account if I use OrderClosePrice() I get error 138, and it only works if I use the MODE_BID and MODE_ASK as shown below. Can anyone advise what is going on and what is the correct way to do it?

Thanks! 


 

void CloseOrders(string pair, int MN, int Type)
{
int TotalNumberOfOrders = OrdersTotal();

   while(OpenOrdersThisPair(Symbol(),MagicNumber)>0)
   {
      for(int i=TotalNumberOfOrders; i>=0;i--)
      {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         if( OrderMagicNumber() == MN      
               && OrderSymbol() == pair        
               && ( OrderType() == Type ) )    
         {
            if(OrderType()==OP_BUY)
            {
               if ( ! OrderClose( OrderTicket(), OrderLots(), MODE_BID, Slippage ) )            
                  Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );  
            }
            
            if(OrderType()==OP_SELL)
            {
               if ( ! OrderClose( OrderTicket(), OrderLots(), MODE_ASK, Slippage ) )            
                  Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );
            }
         }
                    
      if(OpenOrdersThisPair(Symbol(),MagicNumber)<=0) break;
      }
   }
}


int OpenOrdersThisPair(string pair, int MN)
{
  int total=0;
   for(int i=OrdersTotal()-1; i >= 0; i--)
          {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()== pair)
         if(OrderMagicNumber()==MN)
         total++;
          }
          return (total);
}
 
               if ( ! OrderClose( OrderTicket(), OrderLots(), MarketInfo(Symbol(),MODE_BID), Slippage ) )            
                  Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );  
            }
            
            if(OrderType()==OP_SELL)
            {
               if ( ! OrderClose( OrderTicket(), OrderLots(), MarketInfo(Symbol(),MODE_ASK), Slippage ) )            
                  Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );
            }
MODE_BID

and

MODE_ASK

For use with MarketInfo()

MarketInfo - Market Info - MQL4 Reference
MarketInfo - Market Info - MQL4 Reference
  • docs.mql4.com
MarketInfo - Market Info - MQL4 Reference
 
Marco vd Heijden:
               if ( ! OrderClose( OrderTicket(), OrderLots(), MarketInfo(Symbol(),MODE_BID), Slippage ) )            
                  Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );  
            }
            
            if(OrderType()==OP_SELL)
            {
               if ( ! OrderClose( OrderTicket(), OrderLots(), MarketInfo(Symbol(),MODE_ASK), Slippage ) )            
                  Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );
            }
MODE_BID

and

MODE_ASK

For use with MarketInfo()

Excellent, thanks!

Can't believe I missed that! 

 
There is no need to test the type, just close it.
In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading)
Reason: