You should count down in your loop when closing orders.
When you close one order eg. position 1, the order at position 2 now moves to position 1, so the next cycle checks order 2 which was order3 and misses the order that was at position 2
Ok , Let me try.
Hello ,I want to find a way to close several opening positions at the same time, as soon as possible .I have some code below...
but ... In Strategy Tester ,The Orders do not close at the same time. They even close in different bars.
Is there something like asynchronous mode ?There is OrderSendAsync() in mql5 ,but not in mql4.
somebody help me please~
as GumRai says,the for clause will cause serious issue.
potential issue one:the EA don't have the chance to close the closing failed orders again
potential issue two:the EA don't handle the pending orders.
the following is part of my EA,FYI
//+------------------------------------------------------------------+ //| Close all orders | //+------------------------------------------------------------------+ void CloseAll() { int total = OrdersTotal(); bool result = false; int errno; int closednum = 0; while(OrdersTotal() > 0) { if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderSymbol() == Symbol()) { if(OP_BUY == OrderType()) { result = OrderClose(OrderTicket(), OrderLots(), Bid, slippage, CLR_NONE); }else if(OP_SELL == OrderType()) { result = OrderClose(OrderTicket(), OrderLots(), Ask, slippage, CLR_NONE); }else//pending order... { result = OrderDelete(OrderTicket()); } if(true != result) { errno = GetLastError(); Print("Close err:",errno); Sleep(10); }else { closednum++; } } } }
@ Moggy
Your EA is a bit outdated because there are better ways to close orders even more faster. Code it right and it can close 3 orders per second (subject to broker speed as well).
for(int x=OrdersTotal();x>=0;x--) { if(OrderSelect(x,SELECT_BY_POS)==true) { if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,clrNONE)) { Print("Close Order"); } else { Print("Error on closing"); } } }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello ,I want to find a way to close several opening positions at the same time, as soon as possible .I have some code below...
but ... In Strategy Tester ,The Orders do not close at the same time. They even close in different bars.
Is there something like asynchronous mode ?There is OrderSendAsync() in mql5 ,but not in mql4.
somebody help me please~