Order Close Error 138, any ideas?

To add comments, please log in or register
Gary Heard
357
Gary Heard  

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");
         return false;
      }
      if(OrderMagicNumber()==Magic)
      {
         return false;
      }
   }
   return true;

}
lippmaje
605
lippmaje  

Please format your code using the </> button in the editor menu. Use the styler to indent your code properly. (MetaEditor - Tools - Styler)

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.

Gary Heard
357
Gary Heard  
lippmaje:

Please format your code using the </> button in the editor menu. Use the styler to indent your code properly. (MetaEditor - Tools - Styler)

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.

msingaphantsi
7
msingaphantsi  
Gary Heard:

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.

lippmaje
605
lippmaje  
Gary Heard:

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.

https://www.mql5.com/en/articles/24#editor - Features of the message editor. See the </> button?
MQL5.community - User Memo
MQL5.community - User Memo
  • www.mql5.com
You can now not only read articles and download MQL5 programs, but you can also join discussions on the forum, leave comments on articles and source codes, rate MQL5 programs and share your own developments in the Code Base, and even publish articles for a decent fee (see Become an Author at MQL5.com!). MQL5.com services are constantly...
Gary Heard
357
Gary Heard  

Thank you.  I see the other user was refering to the message editor.  Sorry,  here is my code.


int start()
  {

   double   adx1           = iADX(NULL,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_MAIN,0);
   double   adx2           = iADX(NULL,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_PLUSDI,0);
   double   adx3           = iADX(NULL,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_MINUSDI,0);
   double   adx1_1         = iADX(NULL,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_MAIN,1);
   double   adx2_1         = iADX(NULL,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_PLUSDI,1);
   double   adx3_1         = iADX(NULL,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_MINUSDI,1);
   double   adx1_2         = iADX(NULL,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_MAIN,2);
   double   adx2_2         = iADX(NULL,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_PLUSDI,2);
   double   adx3_2         = iADX(NULL,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_MINUSDI,2);
   double   macd           = iMACD(NULL,PERIOD_CURRENT,12,26,9,PRICE_CLOSE,MODE_MAIN,1);

   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");
         return false;
        }
      if(OrderMagicNumber()==Magic)
        {
         return false;
        }
     }
   return true;

  }
MQL5.community - User Memo
MQL5.community - User Memo
  • www.mql5.com
You can now not only read articles and download MQL5 programs, but you can also join discussions on the forum, leave comments on articles and source codes, rate MQL5 programs and share your own developments in the Code Base, and even publish articles for a decent fee (see Become an Author at MQL5.com!). MQL5.com services are constantly...
Seng Joo Thio
187
Seng Joo Thio  
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.
Gary Heard
357
Gary Heard  
Seng Joo Thio:
Error 138 is ERR_REQUOTE, which means the price 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 Seng,  I was afraid you would say that because I don't know how :) 

on to something new to learn I guess.


Thanks again.

lippmaje
605
lippmaje  

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?

Gary Heard
357
Gary Heard  
lippmaje:

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

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

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

To add comments, please log in or register