OrderTicket(), OrderOpenPrice(), etc. are only valid if you have a currently selected order.
However, in your code, your OrderSelect() inside a loop, is very far away from when you eventually use OrderModify(), and any other functions you call that may themselves call trade functions will move the focus or selection away.
Instead, save the ticket number, and then reselect just before modifying (or using other trade functions for the process), or alternatively restructure your entire code logic as it is very stringy and convoluted.
EDIT: It may also happen when by the time you try to modify an order, it is already closed.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
I've been trying to make a trailing stop EA for a while, and this is the first time I've nail it, sort of. when I do the back test I get the error 4108 (Invalid Ticket), when the EA tries to modify an order. I think it shouldn't happen since I use the OrderTicket() function to get it, has anyone any idea what's going on?