void CloseAll() {double bid, ask, price;bool success;
for(int n=0 ;n<=tot;n++){if(!OrderSelect(n,SELECT_BY_POS,MODE_TRADES)) continue;
bid=MarketInfo(OrderSymbol(),MODE_BID);ask=MarketInfo(OrderSymbol(),MODE_ASK);price=(OrderType()==0)?bid:ask;
---------------------------------------------------------------------------------------------------------------------------------------------------------
This simple code should close all active orders, but does not do it. Every time it close some of the orders, but next orders are closed on the next run of the code.
For example, if there are 10 orders, it needs to run this code 3-4 times in order to remove all orders.
Can anyone give any advice? I changed slippage, tried different parameters but no success so far....
loop upside down into the orders
-
Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
Messages Editor
Forum rules and recommendations - General - MQL5 programming forum (2023) -
MT4: You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type to get the close price.
-
Your code assumes all orders are on the current chart symbol. See № 2
-
For non-FIFO (non-US brokers), (or the EA only opens one order per symbol), you can simply count down, in an index loop, and you won't miss orders. Get in the habit of always counting down.
Loops and Closing or Deleting Orders - MQL4 programming forum (2012) -
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 (from zero).
CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16 (2019)
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 (2019)
and check OrderSelect in case other 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 Articlesand 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().
-
This simple code should close all active orders, but does not do it. Every time it close some of the orders, but next orders are closed on the next run of the code.
For example, if there are 10 orders, it needs to run this code 3-4 times in order to remove all orders.
Can anyone give any advice? I changed slippage, tried different parameters but no success so far....
Suggestion - use OrderClosePrice() instead of ask and bid.
void CloseAll()
{bool success; int sel;
for(int n=0 ;n<=tot;n++)
{sel=OrderSelect(n,SELECT_BY_POS,MODE_TRADES);RefreshRates();
Simplified it as you advised also used OrderClosePrice() and RefreshRates() functions, .... but still does not work indicating mistake as follows:
invalid ticket for OrderClose function...
I will try other advices laid down here...
A function to do this, a function to do that... It is not enough to simply think in functions. Your program must have a state. Some of the state is fetched anew with each tick (like market orders, for example), some of the state is saved between ticks. Some functions get the data you need in a form convenient for you, other functions analyze this data in order to trade. First write orders to an array of structures/classes (separate code), then close orders using information from that array (separate code that does not call OrderSelect)
[edit]
Pseudocode (oversimplified):
class COrder { public: double volume; int ticket; int type; //... }; bool closeOrder(const COrder &ord) { // Here you can, for example, make several attempts in a loop (in case of a fail) RefreshRates(); ResetLastError(); if(OrderClose(ord.ticket, ord.volume, ord.type == OP_BUY ? Bid : Ask, SHORT_MAX)) return true; PrintFormat("OrderClose failed. Error %i, ...[additional information]...", GetLastError()); return false; } void closeAll(const COrder &orders[]) { for(int i = ArraySize(orders) - 1; i >= 0; i--) closeOrder(orders[i]); }
i needs to close all orders or all profitable at once... i tried all kind of options no luck...
if you have any simple code which you are sure it works, please share it with me, advicing does not help as you see)))
if you have any simple code
What do you mean by simple code?
which you are sure it works
I'm not sure, the script I attached was just written and I haven't run it even once (the market is closed). Unless I made some stupid typo (as sometimes happens), it should work.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
void CloseAll() {double bid, ask, price;bool success;
for(int n=0 ;n<=tot;n++){if(!OrderSelect(n,SELECT_BY_POS,MODE_TRADES)) continue;
bid=MarketInfo(OrderSymbol(),MODE_BID);ask=MarketInfo(OrderSymbol(),MODE_ASK);price=(OrderType()==0)?bid:ask;
---------------------------------------------------------------------------------------------------------------------------------------------------------
This simple code should close all active orders, but does not do it. Every time it close some of the orders, but next orders are closed on the next run of the code.
For example, if there are 10 orders, it needs to run this code 3-4 times in order to remove all orders.
Can anyone give any advice? I changed slippage, tried different parameters but no success so far....