for(int i=0;i<OrdersTotal();i++)
Count down, not up when closing trades
if(OrderType() == OP_BUY)
{
OrderClose(OrderTicket(), OrderLots(), Bid, 10, Blue);
}
else
if(OrderType() == OP_SELL)
{
OrderClose(OrderTicket(), OrderLots(), Ask, 10, Blue);
}
No need to check for Buy or Sell
OrderClosePrice() can be used for both.
- EAs/Indicators/Scripts must exit withing three (3) seconds of being shut down. You can not close orders, reliability, in OnDeinit.
- In the presence of multiple orders (one EA multiple charts, multiple EAs, manual
trading)
- You must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
- and check OrderSelect. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
- You must RefreshRates after sleep and between multiple server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead.
- Do you really want to close order when all you did was change timeframe or recompile the program?
- EAs/Indicators/Scripts must exit withing three (3) seconds of being shut down. You can not close orders, reliability, in OnDeinit.
Just to be precise, the documentation says :
The OnDeinit() function run is restricted to 2.5 seconds
- docs.mql4.com
for(int i=0;i<OrdersTotal();i++)
Count down, not up when closing trades
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.
for(int order = 0; order <= OrdersTotal()-1; order++)
Hi all,
I've tried all the alternatives you wrote. But it still failt.
I attach my mq4 here and you could have try. Please do followings:
1. Open several orders for example 2 buys and 2 sells.
2. Drag the EA on a opened chart for exampel EURUSD.
3. Then remove the EA fro mthe chart. You can see though the message box tells all the orders are closed, one order is still opened.
Quite strange.
And I notice as compile the AE I see warning like:
"return value of 'OrderClose' should be checked DeleteOrders.mq4 43 16"
Hi,
The saying "The OnDeinit() function run is restricted to 2.5 seconds" seems interesting. I am the first time hearing that. But I've tried even when I just open one order, it can't be closed.
Hi,
The saying "The OnDeinit() function run is restricted to 2.5 seconds" seems interesting. I am the first time hearing that. But I've tried even when I just open one order, it can't be closed.
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).
"return value of 'OrderClose' should be checked DeleteOrders.mq4 43 16"
OrderClose returns a bool. If it returns false, print the error.
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.
for(int order = 0; order <= OrdersTotal()-1; order++)
WhRoeder has provided the link that explains why you must NOT count up.
<= OrdersTotal()-1
is exactly the same as
< OrdersTotal()
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I first open 3 orders manually. Then I start the EA which does nothing but close all the opened orders if I delte it from the chart.
My code look as follow:
But stragely no matter how many orders I open manually, there are n-1 orders can be closed. For example, if I opened 3 orders, only two of them can be closed.
Why?