Error number 4108: unknown ticket 54 for OrderClose function

 

Hello freinds,

I need your help, please.

I wrote an EA which closes a trade with a different function. Every time I have the specific parameters for closing the trades.

Sometimes, the EA closes 2 diffrent pairs. Sometimes, it closes 3 different pairs, and sometimes it closes 4 different pairs.

I do a backtest to it, and the first time it closes 4 pairs correctly, they are closed nicely, but the next time I

got the "SUBJECT" error above: "Error number 4108: unknown ticket 54 for OrderClose function".

The strange thing is that at the shown chart (of the backtest view) I see the screen line which mean that the trade is closed.

I realy do not know why, although I'm an experienced coder.

Best wishes,

Y

 
There is an error in your code . . . which we cannot see.
 
crossy:

I do a backtest to it, and the first time it closes 4 pairs correctly, they are closed nicely, but the next time I

got the "SUBJECT" error above: "Error number 4108: unknown ticket 54 for OrderClose function".

The strange thing is that at the shown chart (of the backtest view) I see the screen line which mean that the trade is closed.

Looks like you are trying to close an already closed ticket. When you select by ticket it will always select, even if the trade is closed (regardless of using MODE_TRADES).

https://docs.mql4.com/trading/orderselect

If you keep a list of tickets then make sure you kill the ticket variable value (make it -1) when the order is closed.

 
dabbler:

Looks like you are trying to close an already closed ticket. When you select by ticket it will always select, even if the trade is closed (regardless of using MODE_TRADES).

https://docs.mql4.com/trading/orderselect

If you keep a list of tickets then make sure you kill the ticket variable value (make it -1) when the order is closed.


Thanks, RaptorUK and dabbler,

I think that you cann't find the correct reason, till you will see the relevant code lines. You have to know that the EA closes the trade, although it says that it can't find the correct ticket...

I attach the code lines.

The ticket which has a problem is TICKET4.

I thank you all for your help.

Y.

 

In your for loop when you loop through the orders by position if you are closing or deleting orders you MUST count down . . . I don't think this is causing your problem but will certainly mean that you miss some orders.

In function CLOSE_Single_P . . .

for ( int k=0; k < OrdersTotal(); k++ )
      {
         OrderSelect(k, SELECT_BY_POS, MODE_TRADES);
         if( OrderSymbol() == SYMB && OrderType() == Code && OrderMagicNumber() == MAG && OrderLots() == LOTT ) 
         {
            OrderClose(TICKET,LOTT,PRC,Slippage,Yellow);  
            sig = 1;
            break;
         }
      }

. . . you pass the function the TICKET number you are going to close . . . and your OrderClose() is inside a loop, if you have already closed TICKET and there is another Order that matches . . .

 if( OrderSymbol() == SYMB && OrderType() == Code && OrderMagicNumber() == MAG && OrderLots() == LOTT ) 

. . . then you will try to close order with ticket number TICKET again.

Why not simply select the order TICKET, check that the OrderSelecet() has worked and if it has then close the order ? why do you need the loop at all ?

 
  1. Why the loop at all? Select by ticket and see if OrderCloseTime() is non zero.
  2. Or are you missing a line
    int TICKET = OrderTicket();
  3. Standard coding practices - all caps normally reserved for #define's.
 
crossy:

Hello freinds,

I need your help, please.

I wrote an EA which closes a trade with a different function. Every time I have the specific parameters for closing the trades.

Sometimes, the EA closes 2 diffrent pairs. Sometimes, it closes 3 different pairs, and sometimes it closes 4 different pairs.

I do a backtest to it, and the first time it closes 4 pairs correctly, they are closed nicely, but the next time I

got the "SUBJECT" error above: "Error number 4108: unknown ticket 54 for OrderClose function".

The strange thing is that at the shown chart (of the backtest view) I see the screen line which mean that the trade is closed.

I realy do not know why, although I'm an experienced coder.

Best wishes,

Y

This could happen if you are passing a ticket number of an order that was already closed automatically because it reached stoploss or takeprofit level, or perhaps your order is triggering more than 1 of your closing parameters so the EA is trying to close it more than once for different reasons.
 
RaptorUK:

In your for loop when you loop through the orders by position if you are closing or deleting orders you MUST count down . . . I don't think this is causing your problem but will certainly mean that you miss some orders.

In function CLOSE_Single_P . . .

. . . you pass the function the TICKET number you are going to close . . . and your OrderClose() is inside a loop, if you have already closed TICKET and there is another Order that matches . . .

. . . then you will try to close order with ticket number TICKET again.

Why not simply select the order TICKET, check that the OrderSelecet() has worked and if it has then close the order ? why do you need the loop at all ?


Thank you very much, freind.

You were write.

I add:

if( OrderSymbol() == SYMB && OrderType() == Code && OrderMagicNumber() == MAG && OrderLots() == LOTT && OrderTicket() = TICKET )

and it was solved.

Best wishes

 
Again, if you already have the ticket number, what is the purpose of the orderSelect loop. Just select the order.
Reason: