MT4 Error 4108 (ERR_INVALID_TICKET)

ezraluandre

Trader
Jun 17, 2020
18
0
17
29
Hi everyone, I encountered with error 4108 which is invalid ticket. I looked on the log and it clearly trying to close order that has already closed like image below. I've looked into my code but still failed to find which part that make this error occurred. Anyone knows why it tries to close a closed order?

Capture.PNG

MQL4:
if(NewBar() && CheckIfOpenOrdersByMagicNumber(MagicNumber))
     {
      double lots          = OrderLots();
      RefreshRates();
      //double trailingStop = Stoploss_Multiplier*ATR();
      if(OrderSelect(ticket,SELECT_BY_TICKET))
        {
         //+--------------------------------------------------------for BUY order--------------------------------------------------------------+
         if(OrderSymbol() == Symbol() && OrderType()==OP_BUY)
           {
            //+--------------------------------------------------------Closing Order--------------------------------------------------------------+
            if(CheckClosePosition(1)==1)
              {
               bool resCO = OrderClose(ticket,lots,Bid,slippage);
               Print("Buy order #",OrderTicket(), " closed");
               if(!resCO)
                 {
                  Print("Error in Closing Order. Error code=",GetLastError());
                  return;
                 }
               else
                  Print("Closing Order successfully for ticket: #", OrderTicket());
              }
 
            //+---------------------------------------------------trailing stop loss Plan---------------------------------------------------------+
            double trailingStop = Bid-Stoploss_Multiplier*ATR(1);
            if(OrderStopLoss()<trailingStop)
              {
               bool resSL = OrderModify(ticket,OrderOpenPrice(),trailingStop,OrderTakeProfit(),0);
               if(!resSL)
                 {
                  Print("Error in Order Modify. Error code=",GetLastError());
                  return;
                 }
               else
                  Print("Order modify successfully for ticket: #", OrderTicket());
              }
            else
               return;
 
           }
 
         //+----------------------------------------------------------for SELL order-------------------------------------------------------------+
         if(OrderSymbol() == Symbol() && OrderType()==OP_SELL)
           {
            //+--------------------------------------------------------Closing Order-------------------------------------------------------------+
            if(CheckClosePosition(1)==2)
              {
               bool resCO = OrderClose(ticket,lots,Ask,slippage);
               Print("Sell order #",OrderTicket(), " closed");
               if(!resCO)
                 {
                  Print("Error in Closing Order. Error code=",GetLastError());
                  return;
                 }
               else
                  Print("Closing Order successfully for ticket: #", OrderTicket());
              }
 
            //+-----------------------------------------------------trailing stop loss Plan---------------------------------------------------------+
            double trailingStop = Ask+Stoploss_Multiplier*ATR(1);
            if(OrderStopLoss()>trailingStop)
              {
               bool resSL = OrderModify(ticket,OrderOpenPrice(),trailingStop,OrderTakeProfit(),0);
               if(!resSL)
                 {
                  Print("Error in OrderModify. Error code=",GetLastError());
                  return;
                 }
               else
                  Print("Order modify successfully for ticket: #", OrderTicket());
              }
            else
               return;
 
           }
        }
     }
 
Last edited by a moderator:

ezraluandre

Trader
Jun 17, 2020
18
0
17
29
MQL4:
bool CheckIfOpenOrdersByMagicNumber(int magicNumber)
  {
   for(int i = OrdersTotal()-1; i >= 0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS)==true)
        {
         if(OrderMagicNumber() == magicNumber)
           {
            return true;
           }
        }
     }
   return false;
  }

it checks whether there's open trade with specific magic number.
 
Last edited by a moderator:

ezraluandre

Trader
Jun 17, 2020
18
0
17
29
ticket is a global variable and it sets value when OrderSend() take place.

MQL4:
if(newBar && !CheckIfOpenOrdersByMagicNumber(MagicNumber))
     {
      RefreshRates();
      //+----------------------------BUY-----------------------------+
      if(CheckOpenPosition(1)==1)
        {
         double stoploss      = Bid-(Stoploss_Multiplier*ATR());
         //double takeprofit    = Bid+(Takeprofit_Multiplier*ATR());
         //double takeprofit    = (MathAbs(supertrend2()-Open[0])*Takeprofit_Multiplier)+Open[0];
 
         double lotSize       = NormalizeDouble(OptimalLotSize(Risk_Per_Trade,Ask,stoploss)/lotType,2);
         if(lotSize < MarketInfo(Symbol(),MODE_MINLOT))
            lotSize = MarketInfo(Symbol(),MODE_MINLOT);
         //Print(lotSize);
 
         /***********************************************************************/
         /*                 check free margin before sending order              **
         ** https://www.earnforex.com/blog/ordersend-error-134-no-enough-money/ **
         **                                                                     */
         if(((AccountStopoutMode() == 1)                                                     &&
             (AccountFreeMarginCheck(Symbol(), OP_BUY, lotSize) > AccountStopoutLevel()))    ||
            ((AccountStopoutMode() == 0)                                                    &&
             ((AccountEquity() / (AccountEquity() - AccountFreeMarginCheck(Symbol(), OP_BUY, lotSize)) * 100) > AccountStopoutLevel())))
            /***********************************************************************/
 
            ticket              = OrderSend(Symbol(),OP_BUY,lotSize,Ask,slippage,stoploss,0,"Strong BUY",MagicNumber);
 
        }
 
Last edited by a moderator: