What is "Order Close Error 138"

Would appreciate the help!

   if(IsTrade()==false)

    {

      if(adx3_1<adx2_1 && adx3>=adx2)

      {

         int closebuyticket = OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrChartreuse);

         Print("Buy Order Closed");

      }   

      else;

      if((adx2<adx3 && adx2_2<adx2_1 && adx2_1<adx2)||(adx1>adx3 && adx1>adx2 && adx3_1>adx2_1 && adx3<adx2))

      {

          int closesellticket = OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,clrChartreuse);

          Print("Sell Order Closed");

      }

    }      

      else;

    if(IsTrade())

    {

      if(adx1>adx3 && adx1>adx2 && adx3_1>adx2_1 && adx3<adx2)

      {

         int buyticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,Ask - StopLoss * Point * P,Ask + TakeProfit * Point * P,TradeComment,Magic,0,clrBlue);

         Print("Buy Order Complete");

      }   

      else;

      if(adx1<adx3 && adx1<adx2 && adx3_1<adx2_1 && adx3>adx2)

      {

          int sellticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,Bid + StopLoss * Point * P,Bid - TakeProfit * Point * P,TradeComment,Magic,0,clrRed);

          Print("Sell Order Complete");

      }
      return(0);
   }
   return(0);   
}

//+------------------------------------------------------------------+

bool IsTrade()
{

   int limit = OrdersTotal();

   for(int i=0;i<limit;i++)

   {

      if(!OrderSelect(i,SELECT_BY_POS))

      {

         Print("could not [select order](https://www.mql5.com/en/docs/trading/orderselect)");

         return false;

      }
      if(OrderMagicNumber()==Magic)

      {
         return false;
      }
   }
   return true;
}

You have put a semicolon behind your else commands, that literally means ‘else do nothing’. So it seems the whole if-else constructs need rebalancing.

Thanks,

Ive removed the ; after the else. About the buttons, I’m not sure what you are referring to. looked for any such button and could not find it.

Error 138 is ERR_REQUOTE, which means the price (specifically, the values of Ask and Bid) has changed when you call OrderClose(). Hence a call to RefreshRates() within a loop that repeatedly tests for the return value of OrderClose() will solve the problem. Exit the loop only when OrderClose() returns a true.

Thanks afinneyq, I was afraid you would say that because I don’t know how :slight_smile:

on to something new to learn I guess.

Thanks again.

Why should it be required to refresh quotes when the program jumps right away from OnTick into the order close logic?

I think this error could also happen when you try to close a sell order at the Bid or a buy order at the Ask.

So I suspect the order close logic in some cases tries to close a Buy when a Sell is opened, or vice versa.

Say, when this condition comes true

if(adx3_1<adx2_1 && adx3>=adx2)

why are you sure that the actual open order is a Buy?

yes you are right. I had bid in the close sell order.