if (OrderType() == OP_BUY && OrderCloseTime()!=0)should be
if (OrderType() == OP_BUY && OrderCloseTime()==0)
Please edit your post and use the code button (Alt+S) when pasting code.
EDIT your original post, please do not just post the code correctly in a new post.
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.
-
if (OrderType() == OP_BUY && OrderCloseTime()==0)
You are reading the open orders pool. There will never be an entry with OCT non-zero. Unnecessary test.
-
You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type to get the close price.
if (OrderType() == OP_BUY && OrderCloseTime()!=0)
should be
if (OrderType() == OP_BUY && OrderCloseTime()==0)
Please edit your post and use the code button (Alt+S) when pasting code.
EDIT your original post, please do not just post the code correctly in a new post.
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.
OrderCloseTime() == 0 you test that for an opened trade? of course is 0... it is still open...
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi Gents, I ran out of ideas to debug this as I couldn't close the last order (always closed most of them except the last active trade). This isn't happening often. It only happens very late at night. When I was monitoring it, it wouldn't happen. so strange. Would you please help me double check? I feel I missed some knowledge.
The error code is:
void CloseOrDeleteAllOrders(){ RefreshRates(); // Start a loop to scan all the orders. // The loop starts from the last order, proceeding backwards; Otherwise it would skip some orders. for (int i = (OrdersTotal() - 1); i >= 0; i--){ // If the order cannot be selected, throw and log an error. if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false){ LogError("Unable to select the order. %d" + GetLastError()); ResetLastError(); continue; } if(OrderSymbol() != MarketSymbol) continue; if(OrderMagicNumber() != BotOrderMagicNumber) continue; // Create the required variables. // Result variable - to check if the operation is successful or not. bool res = false; // Update the exchange rates before closing the orders. // Bid and Ask prices for the instrument of the order. double BidPrice = MarketInfo(MarketSymbol, MODE_BID); double AskPrice = MarketInfo(MarketSymbol, MODE_ASK); // Closing the order using the correct price depending on the type of order. if (OrderType() == OP_BUY && OrderCloseTime()!=0){ res = OrderClose(OrderTicket(), OrderLots(), BidPrice, 0); } else if (OrderType() == OP_SELL && OrderCloseTime()!=0){ res = OrderClose(OrderTicket(), OrderLots(), AskPrice, 0); } else if(OrderType() == OP_SELLSTOP || OrderType() == OP_BUYSTOP) { res = OrderDelete(OrderTicket()); } // If there was an error, log it. if (res == false){ string errorMsg="Unable to close the order"; int theErrorCode = GetLastError(); if(OrderSelect(OrderTicket(), SELECT_BY_TICKET, MODE_HISTORY)){ errorMsg += " as it is closed by market"; } LogWarning(StringFormat("%s. Ticket=%d. %d", errorMsg, OrderTicket(), theErrorCode)); } } }