Alain Verleyen: If your goal is to close all orders use a script. When you decide to close, launch the script. Done.
What I want is, when I remove an EA, all the orders must be closed. This is somewhat like when you remove an indicator, all the objects must be removed or cleared, without running another script.
If you insist on using Bid and Ask instead of OrderClosePrice() you will need to call RefreshRates() before every attempt to close an order. Bid and Ask can get out of date when closing a number of trades.
You have been told that it is not a good idea to try to close trades in OnDeinit.
Why not use OnChartEvent with a keydown to disable the EA,close all trades and then ExpertRemove() to remove the EA once all trades are closed? If doing this, it makes sense to use MessageBox so that you can confirm in case of an accidental keystroke.
You've already been told that you must count down, (#3.2) but you ignored it.
You've already been told "EAs/Indicators/Scripts must exit withing 2.5 seconds of being shut down. You can not close orders, reliability, in OnDeinit," (#3.1) but you ignored it.
Just because you change timeframes, or EA parameters, shouldn't mean you kill open orders, but it's your money lost. You've been told it's a bad idea, (#8) but you ignored it.
Key event, GUI event, or even a EA parameter change could be used to trigger a close all and then remove EA, but not inOnDeinit.
In the presence of multiple orders (one EA multiple charts, multiple EAs, manual
trading,) because while you are waiting for the current operation
(closing, deleting, modifying) to complete, any number of other operations on
other orders could have concurrently happened and changed the position indexing:
For non-FIFO (US brokers,)(or the EA only opens one order
per symbol,) you can simply count down in a position loop, and you don't
miss orders. Get in the habit of always counting down.
For FIFO (US brokers,)and you (potentially) process
multiple orders, you must count up and on a successful
operation, reprocess all positions (set index to -1 before continuing.)
and if you (potentially) process multiple orders, must call
RefreshRates() after server calls if you want to use the Predefined
Variables(Bid/Ask) or OrderClosePrice() instead, on the next
order/server call.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Closing order(s) in OnDeinit() is a bad idea. Change your mind and find an other way.
About your persisting problem, you just have to follow advice you get above (Keith and WHRoeder).
And what is your way?
And what is your way?
I've change a little bit of as follow:
As I remove the EA out of the chart I can see now the error messages as follow (screenshot):
If your goal is to close all orders use a script. When you decide to close, launch the script. Done.
What I want is, when I remove an EA, all the orders must be closed. This is somewhat like when you remove an indicator, all the objects must be removed or cleared, without running another script.
If you insist on using Bid and Ask instead of OrderClosePrice() you will need to call RefreshRates() before every attempt to close an order. Bid and Ask can get out of date when closing a number of trades.
You have been told that it is not a good idea to try to close trades in OnDeinit.
Why not use OnChartEvent with a keydown to disable the EA,close all trades and then ExpertRemove() to remove the EA once all trades are closed? If doing this, it makes sense to use MessageBox so that you can confirm in case of an accidental keystroke.
Mohamad Zulhairi Baba: I believe it's still possible counting up for closing order.
It's just that, the original code missing -1 in array.
it comply with FIFO requirement, when closing all order at once.
Thanks to all of you. I will consider your opinions and will have a try.