I want to ask you if it is possible to close all open orders and to delete all pending orders within a unique call to a method.
Without the need to iterate over Orders MODE_TRADES.
Thanks!
//+------------------------------------------------------------------+ //| close-all-orders.mq4 | //| Copyright © 2005, Matias Romeo. | //| Custom Metatrader Systems. | //+------------------------------------------------------------------+ #property copyright "Copyright © 2005, Matias Romeo." #property link "mailto:matiasDOTromeoATgmail.com" int start() { int total = OrdersTotal(); for(int i=total-1;i>=0;i--) { OrderSelect(i, SELECT_BY_POS); int type = OrderType(); bool result = false; switch(type) { //Close opened long positions case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE ); break; //Close opened short positions case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE ); break; //Close pending orders case OP_BUYLIMIT : case OP_BUYSTOP : case OP_SELLLIMIT : case OP_SELLSTOP : result = OrderDelete( OrderTicket() ); } if(result == false) { if (GetLastError()>0) Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() ); Sleep(3000); } } return(0); }
I havent explain myself properlly. I am trying to avoid to iterate over the trades in order to close/delete them.
I think MQL4 doesnt offer a method like deleteAllTrades();
Thank you!
... something that produces similar faster results as:
... something that produces similar faster results as:
This is the sequence to close all orders in the fastest possible way.
- Manager EA opens worker EAs on separate charts that can receive commands from the manager for asynchronous order ops.
- Manager EA calculates net positions and sends commands to worker EA to async hedge each net position (if more than one order exists) or close position (if only one order exists).
- Manager send commands to delete pending orders (async)
- Manager does a multiple closeby in order to reconcile zeroed out (temporarily hedged) positions.
This is the sequence to close all orders in the fastest possible way.
- Manager EA opens worker EAs on separate charts that can receive commands from the manager for asynchronous order ops.
- Manager EA calculates net positions and sends commands to worker EA to async hedge each net position (if more than one order exists) or close position (if only one order exists).
- Manager send commands to delete pending orders (async)
- Manager does a multiple closeby in order to reconcile zeroed out (temporarily hedged) positions.
//+------------------------------------------------------------------+ //| close-all-orders.mq4 | //| Copyright © 2005, Matias Romeo. | //| Custom Metatrader Systems. | //+------------------------------------------------------------------+ #property copyright "Copyright © 2005, Matias Romeo." #property link "mailto:matiasDOTromeoATgmail.com" int start() { int total = OrdersTotal(); for(int i=total-1;i>=0;i--) { OrderSelect(i, SELECT_BY_POS); int type = OrderType(); bool result = false; switch(type) { //Close opened long positions case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE ); break; //Close opened short positions case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE ); break; //Close pending orders case OP_BUYLIMIT : case OP_BUYSTOP : case OP_SELLLIMIT : case OP_SELLSTOP : result = OrderDelete( OrderTicket() ); } if(result == false) { if (GetLastError()>0) Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() ); Sleep(3000); } } return(0); }
How do you tell the terminal to make sure that you close open orders first, then work on the pending orders?
case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE ); break; //Close opened short positions case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE );
MT4: You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type to get the close price.
MT5: you can use PositionGetDouble(POSITION_PRICE_CURRENT)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I want to ask you if it is possible to close all open orders and to delete all pending orders within a unique call to a method.
Without the need to iterate over Orders MODE_TRADES.
Thanks!