When exactly do u get this error?
Partial close changes the ticket number (of the remaining open position), so if u attempt next time to close the SAME ticket, that would cause 4108 (since the ticket was already closed). If u r using this code in a loop, then make sure it loops down properly, otherwise it won't do what it's supposed to (but I can't see how that would cause this error).
And one more thing:
Thanks for the tips.
I get the error while backtesting when executing this line:
OrderClose(OrderTicket(),NormalizeDouble((OrderLots()/2),2),Bid,3,Violet);
Here's the context:
void CheckForClose() { int cnt; for(cnt=0;cnt<OrdersTotal();cnt++) { if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==false) { Print("Error="+GetLastError()); return; } if(OrderMagicNumber()!=MagicNumber || OrderSymbol()!=Symbol()) continue; if(OrderType()==OP_BUY) { if((Bid-OrderOpenPrice())>=(OrderOpenPrice()-BuyStop)&&(CloseHalfFlag==false))//Close half if Bid-Open>Open-stop { OrderClose(OrderTicket(),NormalizeDouble((OrderLots()/2),2),Bid,3,Violet); CloseHalfFlag=true; } if(OrderStopLoss()<(Bid-(OrderOpenPrice()-BuyStop)))//Trailing stop { OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(OrderOpenPrice()-BuyStop),OrderTakeProfit(),0,Green); return(0); } } }
While preparing this, I tested it while commenting out the trailing stop, and the error went away. Would OrderModify change the ticket number? When the trailing stop executes OrderModify, the ticket number stays the same in the "Results" page of the tester. But it does change when the OrderClose executes as you said. And yet, it seems to be the OrderModify that leads to the unknown ticket error.
I figured it out. The error is caused because OrderClose changes the ticket number and and the subsequent OrderModify calls the wrong ticket number. Since OrderClose changes the ticket number and the OrderModify command immediately follows it, it's calling the wrong ticket number.
Thanks Gordon for the good questions and ideas.
I've noticed in your posts that you're very detail oriented. I need to improve in that area.
I figured it out. The error is caused because OrderClose changes the ticket number and and the subsequent OrderModify calls the wrong ticket number. Since OrderClose changes the ticket number and the OrderModify command immediately follows it, it's calling the wrong ticket number.
Exactly.
Please note that u still need to have your loop counter decrementing, or it won't work properly...
for( cnt=OrdersTotal()-1;cnt>=0;cnt-- )
More info here: https://www.mql5.com/en/forum/119840
I figured it out. The error is caused because OrderClose changes the ticket number and and the subsequent OrderModify calls the wrong ticket number. Since OrderClose changes the ticket number and the OrderModify command immediately follows it, it's calling the wrong ticket number.
Thanks Gordon for the good questions and ideas.
I've noticed in your posts that you're very detail oriented. I need to improve in that area.
Dear Joetrader
can you please help me. Im having a similar issue woth my code. What did you do to prevent
: unknown ticket 1 for OrderClose function
and error
Error: 4108 invalid ticket
Heres my bit of code.. can you please help:
Can you give me the bit of code you used to get rid of this error
Condition 1
RefreshRates();
if ( fastma1 < slowma1 && fastma2 > slowma2 && fastma1 < midma1 || fastma1 < midma1 && fastma2 > midma2 && fastma1 < slowma1)
{
Exitstage2 = false; // Force initialise
Exitstage1 = false; // Force initialise
tp = NormalizeDouble(Ask - InitTP * Point, Digits);
sl = NormalizeDouble(Ask + InitSL * Point, Digits);
OrderSend(Symbol(), OP_SELL, Lot, NormalizeDouble(Bid, Digits), Slippage, sl, tp, comment1, iMagic, CLR_NONE, HotPink);
err = GetLastError();
Print("Error: ", err, " ", ErrorDescription(err));
}
I then exit using this:
if ( Exitstage2 )
{
if (OrderType() == OP_BUY && iClose (Symbol(), 0,1) < iBands(NULL,0,slow_pds,1,0,PRICE_CLOSE,MODE_UPPER,1))
{
Exitstage1 = false; // Force initialise
Exitstage2 = false; // Initiallise
err = GetLastError();
Print("Error: ", err, " ", ErrorDescription(err));
if (Lot != 0 /*&& err == 0*/ ) // if Lot is not equal to zero
{
if (OrderLots() < Lot)
{
CurrLot = OrderLots();
}
else
{
CurrLot = Lot;
}
RefreshRates();
OrderClose(OrderTicket(), CurrLot, NormalizeDouble(Ask, Digits), Slippage, Black);
err = GetLastError();
Print("Error: ", err, " ", ErrorDescription(err));
}
}
if (OrderType() == OP_SELL && iClose (Symbol(), 0,1) > iBands(NULL,0,slow_pds,1,0,PRICE_CLOSE,MODE_LOWER,1))
{
Exitstage1 = false; // Force initialise
Exitstage2 = false; // Initiallise
err = GetLastError();
Print("Error: ", err, " ", ErrorDescription(err));
if (Lot != 0 /*&& err == 0*/) // if Lot is not equal to zero
{
if (OrderLots() < Lot)
{
CurrLot = OrderLots();
}
else
{
CurrLot = Lot;
}
RefreshRates();
OrderClose(OrderTicket(), CurrLot, NormalizeDouble(Ask, Digits), Slippage, Black);
err = GetLastError();
Print("Error: ", err, " ", ErrorDescription(err));
}
}
} // End of exitStage2 */
I also use trailing stop and take some part of postion out as profit levels are reached.
Read carefully about OrderSelect().

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I'm trying to close half of the trade once it reached 1x the difference between orderopen level and the stoploss. My EA closes half the position correctly, but I get this error:
If the ticket number is 2
unknown ticket 2 for OrderClose function
OrderClose error 4108
I know 4108 is "ERR_INVALID_TICKET", so it seems to be something with the ticket.
Here's my code:
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==false)
{
Print("Error="+GetLastError());
return;
}
if(OrderMagicNumber()!=MagicNumber || OrderSymbol()!=Symbol()) continue;
if(OrderType()==OP_BUY)
{
if((Bid-OrderOpenPrice())>=(OrderOpenPrice()-BuyStop)&&(CloseHalfFlag==false))
{
OrderClose(OrderTicket(),NormalizeDouble((OrderLots()/2),2),Bid,3,Violet);
CloseHalfFlag=true;
}
}
Any ideas?