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?


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: