Getting errors on this TS code

 

Hi, here is a test EA for checking my (soon to be) drop-in TrailingStop routine. The TS seems to work but I get flooded with "OrderModify error 4051" and "invalid ticket for OrderModify function" errors.

Just run the EA on 1M EURUSD 2009.09.01 to 2009.09.02 in visual mode. Anyone see what is wrong ?



Files:
 
DayTrader:

Hi, here is a test EA for checking my (soon to be) drop-in TrailingStop routine. The TS seems to work but I get flooded with "OrderModify error 4051" and "invalid ticket for OrderModify function" errors.

Just run the EA on 1M EURUSD 2009.09.01 to 2009.09.02 in visual mode. Anyone see what is wrong ?

It could simply be because you're looping from 0 to OrdersTotal(), rather than OrdersTotal() minus 1 ("cnt <= OrdersTotal()"). If there are ten orders, numbered #0 through #9 by position, the code will finish by trying to select order #10. OrderTicket() should then be returning zero - hence the "invalid ticket" error messages.


BTW, there are people who'd say that you should loop from OrdersTotal()-1 down to zero, rather than looping up. You don't actually need to do it with this code as it stands, but the code will potentially break if you start closing or cancelling orders rather than just modifying them.

 

There are never more than one open order at any given time, and obviously there may also be 0 open orders.

Have tried looping both ways, and also with a +1 offset but nothing seems to help.

If I don't loop, by setting OrderSelect(1, SELECT_BY_POS, MODE_TRADES); then the errors dissapears.... so it looks like

it has something to do with the orderselection.

I'll try that "OrdersTotal()-1" tonight and see what happends.

 
DayTrader:

There are never more than one open order at any given time, and obviously there may also be 0 open orders.

If there are zero open orders, then your current code will try to do OrderSelect(0, SELECT_BY_POS), and then attempt to modify the non-existent order. If there's one open order, then it will do OrderSelect(0, SELECT_BY_POS) followed by OrderSelect(1, SELECT_BY_POS), with the latter leading to an invalid ticket number. You simply need to alter your "for" condition so that it does "cnt < OrdersTotal()" rather than "cnt <= OrdersTotal()". There may be other problems in the code, but this is the one which leapt out at me. And looping up rather than down leaves you open to problems in future if (a) you have multiple orders, and (b) the loop closes or cancels orders rather than just modifying them.


In addition, your code isn't currently catering for your EA running on multiple charts at once, or running in conjunction with other EAs. In other words, no attempt by the EA only to process its own trades based on symbol and magic number.

 
jjc:

If there are zero open orders, then your current code will try to do OrderSelect(0, SELECT_BY_POS), and then attempt to modify the non-existent order. If there's one open order, then it will do OrderSelect(0, SELECT_BY_POS) followed by OrderSelect(1, SELECT_BY_POS), with the latter leading to an invalid ticket number. You simply need to alter your "for" condition so that it does "cnt < OrdersTotal()" rather than "cnt <= OrdersTotal()". There may be other problems in the code, but this is the one which leapt out at me. And looping up rather than down leaves you open to problems in future if (a) you have multiple orders, and (b) the loop closes or cancels orders rather than just modifying them.


In addition, your code isn't currently catering for your EA running on multiple charts at once, or running in conjunction with other EAs. In other words, no attempt by the EA only to process its own trades based on symbol and magic number.

Thanks for your input, the code is now running without errors and I can continue the development. It is supposed to end up as a universal simple drop-in 'module' that handles all TS related tasks. When finished I may post it here for review and comments.

My gut feeling is that it may be safer and more profitable to break even as soon as possible and do TS. Obviously many potentially good trades may lost due to small pullbacks etc, but overall the results may be better as losses may be much lower.

Reason: