Does anyone know of any simple code that will close all of my orders based on GMT (time?) Thanks, would be much appreciated!
- Expiration date code?
- MQL4 Learning
- using time to remove orders?
YG
Try something like...
extern int MagicNumber = 1234; extern int CloseHour = 18; start() { if (Hour() > CloseHour) { CloseAllBuyOrders(MagicNumber); CloseAllSellOrders(MagicNumber); }
return (0) }
void CloseAllBuyOrders(int MN) { int i, iTotalOrders; iTotalOrders=OrdersTotal(); for (i=0; i<iTotalOrders; i++) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderMagicNumber()==MN) { if (OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),Bid,5,Violet); if (OrderType()==OP_BUYSTOP) OrderDelete(OrderTicket()); if (OrderType()==OP_BUYLIMIT) OrderDelete(OrderTicket()); } } } } void CloseAllSellOrders(int MN) { int i, iTotalOrders; iTotalOrders=OrdersTotal(); for (i=0; i<iTotalOrders; i++) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderMagicNumber()==MN) { if (OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Ask,5,Violet); if (OrderType()==OP_SELLSTOP) OrderDelete(OrderTicket()); if (OrderType()==OP_SELLLIMIT) OrderDelete(OrderTicket()); } } } }
This has worked - but IIRC, Rosh has said we should go back the other way when deleting orders, i.e. use i--
-BB-
void CloseAllBuyOrders(int MN) { int i, iTotalOrders; iTotalOrders=OrdersTotal()-1; for (i=iTotalOrders; i>=0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderMagicNumber()==MN) { if (OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),Bid,5,Violet); if (OrderType()==OP_BUYSTOP) OrderDelete(OrderTicket()); if (OrderType()==OP_BUYLIMIT) OrderDelete(OrderTicket()); } } } } void CloseAllSellOrders(int MN) { int i, iTotalOrders; iTotalOrders=OrdersTotal()-1; for (i=iTotalOrders; i>=0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderMagicNumber()==MN) { if (OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Ask,5,Violet); if (OrderType()==OP_SELLSTOP) OrderDelete(OrderTicket()); if (OrderType()==OP_SELLLIMIT) OrderDelete(OrderTicket()); } } } }
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register