- I want check that exist order pending for GBPJPY
- See the orders open
- place just one pending order
How can I check if there exists a pending order (in my case, a Buy Stop order) for a specific symbol?
You can start by reading the documentation about how to work with orders : https://www.mql5.com/en/articles/211
- 2011.02.01
- MetaQuotes Software Corp.
- www.mql5.com
How can I check if there exists a pending order (in my case, a Buy Stop order) for a specific symbol?
If you're talking about mql4, the following two functions are fair enough to give you what you're looking for.
I use them - mostly for Buy and Sell - heavily in my codes.
Something happen makes code is not attached ! ...
... ... ... if(!ExistPositionBuyStop() && ... && ...) OpenBuyStop(); if(!ExistPositionSellStop() && ... && ...) OpenSellStop(); ... ... ... bool ExistPositionBuyStop() { for (int i=0; i<OrdersTotal(); i++) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderSymbol()==Symbol() && OrderType()==OP_BUYSTOP && OrderMagicNumber()==MagicNo) { return(true); } } } return(false); } bool ExistPositionSellStop() { for (int i=0; i<OrdersTotal(); i++) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderSymbol()==Symbol() && OrderType()==OP_SELLSTOP && OrderMagicNumber()==MagicNo) { return(true); } } } return(false); }
If you're talking about mql4, the following two functions are fair enough to give you what you're looking for.
I use them - mostly for Buy and Sell - heavily in my codes.
You can start by reading the documentation about how to work with orders : https://www.mql5.com/en/articles/211
Thanks! I read it.
Based on it I, (question) do you think it's reasonable to use
if (cPosition == 0){ bool openedOrder = false; ulong ticket = 0; for (int i=0; i < OrdersTotal(); i++){ ticket = OrderGetTicket(); if (OrderGetString(ORDER_COMMENT) == "myKeyOrder" && ticket != 0){ openedOrder = true; break; } } if (conditionsApply){ posPr = hRates[1].high; // Just example posSl = hRates[1].low; // Just example posTp = 5 * hRates[1].high; // Just example if (!openedOrder) trade.BuyStop(cLots,posPr,_Symbol,posSl,posTp,ORDER_TIME_GTC,0,"myKeyOrder"); else trade.OrderModify(ticket, posPr,posSl,posTp,ORDER_TIME_GTC,0) return; } else{ if (openedOrder) trade.OrderDelete(ticket); } }
?
If you're talking about mql4, the following two functions are fair enough to give you what you're looking for.
I use them - mostly for Buy and Sell - heavily in my codes.
Thank you Osama! But I am using mql5.
Working code for MQL5:
I use multiple EA and some of them trade on same _Symbol.
bool hasPendingOrder() { for (int i=0; i < OrdersTotal(); i++) { ulong ticket = OrderGetTicket(i); if(OrderSelect(ticket)) { if (OrderGetInteger(ORDER_MAGIC) == Expert_MagicNumber && OrderGetString(ORDER_SYMBOL) == _Symbol && ticket != 0){ return true; break; } } } return false; }
for (int i=0; i < OrdersTotal(); i++) { ulong ticket = OrderGetTicket(i);
MT4: You can not use any Trade Functions until you first select an order.
MT5: first select a position via CPositionInfo, directly, or by 'MT4Orders' library (2016)
bool hasPendingOrder() { for (int i=0; i < OrdersTotal(); i++) { ulong ticket = OrderGetTicket(i); if(OrderSelect(ticket)) { if (OrderGetInteger(ORDER_MAGIC) == Expert_MagicNumber && OrderGetString(ORDER_SYMBOL) == _Symbol && ticket != 0){ return true; break; } } } return false; }
Thanks!
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use