| / | Forum |
|
joetrader
2010.02.17 01:59
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) Any ideas?
|
|
Step on New Rails: Custom Indicators in MQL5 In this article we will consider the indicators, their structure, drawing, types and their programming details, as compared to MQL4. I hope that this article will be useful both for beginners and experienced developers, maybe some of them will find something new. |
2030 |
gordon
2010.02.17 04:42
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:
|
|
joetrader
2010.02.17 05:41
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. |
|
joetrader
2010.02.17 05:49
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. |
2030 |
gordon
2010.02.17 11:54
joetrader:
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: http://forum.mql4.com/25216 |
|
amdegia
2010.05.07 03:59
joetrader:
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 I then exit using this: if ( Exitstage2 )
|
|
Roger
2010.05.07 05:42
Read carefully about OrderSelect(). |