How to prevent / avoid 138 error reqoute

 

Dear All,

It has been 3-4 weeks since I started coding in mq4. 

I have developed an EA which creates buy/sell orders. I used slippage = 3, but, since I used to get error 138 reqoute  most of the time I came up with below idea for both ordersend and orderclose as attached: 


I would like an expert to comment on my thoughts.


Thank you in advance:


int MyOrderSend(

   string   symbol,              // symbol

   int      cmd,                 // operation

   double   volume,              // volume

   double   price,               // price

   int      slippage,            // slippage

   double   stoploss,            // stop loss

   double   takeprofit,          // take profit

   string   comment=NULL,        // comment

   int      magic=0,             // magic number

   datetime expiration=0,        // pending order expiration

   color    arrow_color=clrNONE  // color

   ){

   int ticket;

   int lastError;

   do {

      ticket = OrderSend(

            symbol,              // symbol

            cmd,                 // operation

            volume,              // volume

            price,               // price

            slippage,            // slippage

            stoploss,            // stop loss

            takeprofit,          // take profit

            comment,             // comment

            magic,               // magic number

            expiration,          // pending order expiration

            arrow_color          // color

      );

      lastError = GetLastError();

      if (lastError > 0) {

         Print("Error MyOrderSend, Code: ", lastError, " Desc: ", 

               ErrorDescription(lastError));

      }

      slippage ++;

      Print("slippage = ", slippage);

   } while (lastError == 138);

   Print("Order placed: ", cmd == OP_BUY?  "Buy" : "Sell");

   return ticket;

}





bool  MyOrderClose(

   int        ticket,      // ticket

   double     lots,        // volume

   double     price,       // close price

   int        slippage,    // slippage

   color      arrow_color  // color

   ){

   bool result;

   do {

      result  = OrderClose(

                  ticket,      // ticket

                  lots,        // volume

                  price,       // close price

                  slippage,    // slippage

                  arrow_color  // color

            );

      slippage ++;

      Print("slippage = ", slippage);

   } while( !result );

   

   return result;

}
Basic Principles - Trading Operations - MetaTrader 5
Basic Principles - Trading Operations - MetaTrader 5
  • www.metatrader5.com
is an instruction given to a broker to buy or sell a financial instrument. There are two main types of orders: Market and Pending. In addition, there are special Take Profit and Stop Loss levels. is the commercial exchange (buying or selling) of a financial security. Buying is executed at the demand price (Ask), and Sell is performed at the...
Files:
 

You need to check the return value of OrderSend() / OrderCLose() functions.

Analyze what they return to see if the call was successful or not, and if not take action on that.

Look at examples in CodeBase.
 
Marco vd Heijden:

You need to check the return value of OrderSend() / OrderCLose() functions.

Analyze what they return to see if the call was successful or not, and if not take action on that.

Look at examples in CodeBase.



Yes that's exactly what I am doing ... basically I increase the slippage by 1 and try one more time until I success...


but what if I want to place the order with current market price any way.... or what if I want to close a position no matter what the current price is....

how could I implement such logic? 

I will check the CodeBase anyway.


Once again thanks for the input.

 

No it's not if your price is off your code will get stuck in an endless do loop and flood the server and it will block and disconnect your terminal connection.

You can see code base for examples. 

 

Hello friend, I am not sure that you realize this, but you haven't filled in the required fields correctly.

  int position=OrderSend(NULL,OP_BUY,0.01,Ask,3,0,0,"Place order",0,0,clrGreen);

if(position<0)
{
Alert("Order Failed);
}
 else  Alert("Order Placed");
}

Good luck

Reason: