Close SELL order and Open BUY order at the same time

 

Greetings,


i'm trying to write a code that ( at a indicator signal ) closes any possible, lets say, BUY orders and opens a SELL order in its place; also vice-versa. I've tried a few different ways, seemed to work when backtesting, but when looking at it on LIVE, the orders do get closed, but the new ones do not get opened. Does someone have a solution for it? What i have so far is the following:


int CloseOrderOpenOrder(int closeOperation, int openOperation){
   bool closedOrderId = false;
   bool closeOrderExsist = OrderExsists(closeOperation);
   
   if(closeOrderExsist){
      while(true){
   
         closedOrderId = CloseOrders(closeOperation);
         // Print("Attempting to close an order.");
            
         if(closedOrderId == true){
            // Success. Break out of loop
            break;
         }
         
         if (Fun_Error(GetLastError())==1){
            // Print("Failed. Retrying. Order ID :" + closedOrderId);
            continue;
         }
         
         // Print("Order closed. Opening order.");
         OpenOrder(openOperation);
         
         break;
      }
   } else {
      OpenOrder(openOperation);
   }
   
   return true;
}

int OpenOrder(int type){
   // ... open order for a specific direction
}

int CloseOrders(int type){   
   // ... close open orders for a specific direction
}
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
  • www.mql5.com
For equity securities, the Depth of Market window is available, where you can see the current Buy and Sell orders. Desired direction of a trade operation, required amount and requested price are specified for each order. To obtain information...
Reason: