#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006 void CloseDeleteAll() { for (int i = OrdersTotal() - 1; i >= 0; i--) if (OrderSelect(i, SELECT_BY_POS)) if (OrderType() <= OP_SELL) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 100); else OrderDelete(OrderTicket()); } void OnStart() { CloseDeleteAll(); }
Hello coders,
I can't close running position and pending order. Please check the above code.
Thank's in advance.
Unless you have a specific reason to be messing around with the low-level stuff, it's best interact with the stdlib classes.
#include <Trade\Trade.mqh> void OnStart() { CTrade trade; trade.SetAsyncMode(true); CPositionInfo pos; COrderInfo ord; for(int i=PositionsTotal()-1; i>=0; --i) if(!pos.SelectByIndex(i) || !trade.PositionClose(pos.Ticket())) Print("Could not close position! Error: ",_LastError); for(int i=OrdersTotal()-1; i>=0; --i) if(!ord.SelectByIndex(i) || !trade.OrderDelete(ord.Ticket())) Print("Could not delete order! Error: ",_LastError); }
MT4 work well. I need MT5 code.
MT4 work well. I need MT5 code.
Try this code in the MT5.
Unless you have a specific reason to be messing around with the low-level stuff, it's best interact with the stdlib classes.
Thank you so much Nicholi Shen. it's working great.
Try this code in the MT5.
Thank you so much Fxsaber. I appreciate your idea. My code already in mql5. I will test it when I use mql4 code in mql5.
Thank you so much Nicholi Shen. it's working great.
I have a question for you which is if the client meta trader 5 has been deleted the header file 'Trade.mqh' then is my '.EX5' file will still work.
I have a question for you which is if the client meta trader 5 has been deleted the header file 'Trade.mqh' then is my '.EX5' file will still work.
Yes. Once it's "compiled" you no longer need any source files.
- 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 coders,
I can't close running position and pending order. Please check the above code.
Thank's in advance.