for(int i = 0; i < OrdersTotal(); i ++)
In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading), 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 and order count:
-
For non-FIFO (non-US brokers), (or the EA only opens one order per symbol), you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
Loops and Closing or Deleting Orders - MQL4 programming forum -
For In First Out (FIFO rules — US brokers), and you (potentially) process multiple orders per symbol, you must find the earliest order (count up), close it, and on a successful operation, reprocess all positions.
CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11 -
and check OrderSelect in case later positions were deleted.
What are Function return values ? How do I use them ? - MQL4 programming forum
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles -
and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().
I would recommend using decreasing iterator (from OrdersTotal ()-1) to 0) in a loop – so you won’t miss any orders when previous are closing (so the iterator won’t change.
There’s no need to use this ticket variable and OrderSymbol – keep your code clean and remove redundant elements – it’s better to manage it later.
In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading), 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 and order count:
-
For non-FIFO (non-US brokers), (or the EA only opens one order per symbol), you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
Loops and Closing or Deleting Orders - MQL4 programming forum -
For In First Out (FIFO rules — US brokers), and you (potentially) process multiple orders per symbol, you must find the earliest order (count up), close it, and on a successful operation, reprocess all positions.
CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11 -
and check OrderSelect in case later positions were deleted.
What are Function return values ? How do I use them ? - MQL4 programming forum
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles -
and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().
Very valuable explanation, thank you :-)
//+------------------------------------------------------------------+ //| Đóng tất cả //+------------------------------------------------------------------+ void Close_all() { CTrade m_trade; // Trades Info and Executions library COrderInfo m_order; //Library for Orders information CPositionInfo m_position; // Library for all position features and information //--Đóng Positions for(int i = PositionsTotal() - 1; i >= 0; i--) // loop all Open Positions if(m_position.SelectByIndex(i)) // select a position { m_trade.PositionClose(m_position.Ticket()); // then close it --period Sleep(100); // Relax for 100 ms } //--End Đóng Positions //--Đóng Orders for(int i = OrdersTotal() - 1; i >= 0; i--) // loop all Orders if(m_order.SelectByIndex(i)) // select an order { m_trade.OrderDelete(m_order.Ticket()); // then delete it --period Sleep(100); // Relax for 100 ms } //--End Đóng Orders //--Đóng Positions lần 2 cho chắc for(int i = PositionsTotal() - 1; i >= 0; i--) // loop all Open Positions if(m_position.SelectByIndex(i)) // select a position { m_trade.PositionClose(m_position.Ticket()); // then close it --period Sleep(100); // Relax for 100 ms } //--End Đóng Positions lần 2 cho chắc }// End func Close_all //+------------------------------------------------------------------+
"Here is a function I often use. Please note that you need to add necessary libraries yourself. Just by looking at it, you should know what libraries are needed, right?"

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use