why sometimes my EA fails to close a position?

 

Dear All:

I believe this has been asked and answered before. I am new to this forum. Sorry for the "old" question.

I have written an EA. Sometimes, it failes to close the open position. My code related to closing are:

OrderSelect(0, SELECT_BY_POS);//one and only one open position, so the index is 0

bool isCloseSucs = false;

int type = OrderType();

switch(type)
{
//Close opened long positions
case OP_BUY : isCloseSucs = OrderClose( OrderTicket(), OrderLots(), Bid, slipPips, Red );
break;

//Close opened short positions
case OP_SELL : isCloseSucs = OrderClose( OrderTicket(), OrderLots(), Ask, slipPips, Red );

}

if(isCloseSucs == false)
{//failed in closing position
Alert("Order ", OrderTicket(), " failed to close. Error:", GetLastError() );
}

Most times, the open position was closed successfully. But sometime, I got a message like:

Order xxx failed to close. Error: 1 (or Error: 128)

I have put a while loop outside the code above. If there is still a position open, it will continuely try closing it. So, eventually, the open position is closed. However, I am just curious why it was not closed on the first try.

Thank you very much.

 
ERR_TRADE_TIMEOUT 128 Timeout for the trade has been reached. Before retry (at least, in 1-minute time), it is necessary to make sure that trading operation has not really succeeded (a new position has not been opened, or the existing order has not been modified or deleted, or the existing position has not been closed)
Try searching for this order and then retry the close after 1-minute. If you need more information on how to do that then read the mql-book above.
 
  1. Probably a intermittent internet connection or overloaded server. need to wait, refresh rates, and retry; or just return from start, wait for the next tick (connection is up and server is good,) orderSelect and retry. You might want to run on a VPS and if you still get them, the problem is the broker.

  2. OrderSelect(0, SELECT_BY_POS);//one and only one open position, so the index is 0
    Only one position open by THIS ea. Other positions could be open by this or other EA's on other charts or manual trading. Always use magic numbers. Always test return codes.
        for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol()                 // and my pair.
        ){
           if (!OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), slipPips*pips2point, Red ))
              Alert("OrderClose failed: ",GetLastError());
        }
    You can use OrderClosePrice for either buy or sell. The slippage units in OrderSend/OrderClose is NOT pips, it's points. EA's must adjust TP, SL, AND slippage for 4/5 digit brokers.
    //++++ These are adjusted for 5 digit brokers.
    int     pips2points;    // slippage  3 pips    3=points    30=points
    double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    //---- These are adjusted for 5 digit brokers.
        /* On ECN brokers you must open first and THEN set stops
        int ticket = OrderSend(...)
        if (ticket < 0)
           Alert("OrderSend failed: ", GetLastError());
        else if (!OrderSelect(ticket, SELECT_BY_POS))
           Alert("OrderSelect failed: ", GetLastError());
        else if (!OrderModify(OrderTicket()...)
           Alert("OrderModify failed: ", GetLastError());
         */