I am trying to delete pending orders but the pending orders won't delete.
The EA is eg: When the Buy criteria to "Open" is meet place a Buy at Market and place a pending order. When the Buy criteria to "close" is meet then close the open Buys and pending order should be closed.
Any assistance would be welcomed. File attached. Comments in the file .Thanks
Don't you get this Alert ?
if (OrderType()>1) { Alert("Pending order detected.EA Doesn't work."); return;
Pending orders aren't Closed (OrderClose()) they are deleted (OrderDelete())
If you want other people to read the code you are posting it will help to sort your indenting and { } braces and to add comments to make it easier to follow.
//Close Pending Orders //Close Pending Orders int y; int allorders; allorders =OrdersTotal(); for(y=allorders-1; y>=0; y--) { //1 if (!OrderSelect(y,SELECT_BY_POS,MODE_TRADES)) continue; int Tip1 =OrderType(); if(OrderType() ==4 && Tip1==0 && Cls_B==true && //Added the same as "Closing orders" so when the Long open orders close, OrderMagicNumber() == magicnoa && // BUY STOP pending orders will also close. OrderSymbol() == Symb) { // **"BUT Doesn't close the pending orders"*** penddela= OrderDelete(Ticket); return(1); } if(OrderType()==5 && Tip1==1 && Cls_S==true && //Added the same as Closing Orders" so when Short Open trades close, // Sell Stop Pending orders will also close. OrderMagicNumber()== magicnoa && //**"BUT Doesn't close the pending orders"*** OrderSymbol() == Symb) { penddelb=OrderDelete(Ticket); return(1); } } //1 //----------------------------------------------------------------------
Print out value
'Ticket' and value 'OrderTicket()'
Print out value
'Ticket' and value 'OrderTicket()'
Thanks, devries, the value of the ticket is fine the issue is as follows:
if(OrderType() ==4 && " Tip1==0 && Cls_B==true " (BuyStop) , if(OrderType()==5 && "Tip1==1 && Cls_S==true " (SellStop) ,(From the //Closing Pending Orders ) Block of code. The part of the code in yellow , I was trying to close the "pending orders" using the same code in " Closing Orders" ,this way I can have pending orders(deleted) and Market orders (closed) when criteria is meet to close trade.
The only problem is that the "Close Pending Ordes" Block of code , doesn't seem to respond when the criteria is meet to close the trades.
But the same code in Yellow closes trades without any issue in "Closing Orders" when I didn't have the "Close Pending orders" Block of code. So I know the code is correct and works but it just won't work to "close the pending orders."
Any suggestions to why it doesn't and how to over come it would be great. Thanks.
// Closing Orders //Closing Orders.Enters at market. // Below is the Pending order close, block of code. while(true) { if (Tip==0 && Cls_B==true) //Tried to use this to close the pending orders(As Per Below) { Alert("Attempt to close buy",Ticket,". Waiting for response.."); RefreshRates(); Ans=OrderClose(Ticket,Lot,Bid,2); if (Ans==true) { Alert ("Closed order Buy ",Ticket); break; } if (Fun_Error(GetLastError())==1) continue; return; } if (Tip==1 && Cls_S==true) //Tried to use this to close the pending orders(As Per Below) { Alert("Attmept to close Sell ",Ticket,". Waitiing for response.."); RefreshRates(); Ans=OrderClose(Ticket,Lot,Ask,2); if (Ans==true) { Alert ("Closed order Sell ",Ticket); break; } if (Fun_Error(GetLastError())==1) continue; return; } break; }
//Tried to use this to close the pending orders(As Per Below) { Alert("Attempt to close buy",Ticket,". Waiting for response.."); RefreshRates(); Ans=OrderClose(Ticket,Lot,Bid,2);
Asked and answered. You can NOT close pending orders.
Print ticket, lot, OrderTicket(), OrderLot() and OrderType() before each attempt, as requested, and you will find out why.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- 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 am trying to delete pending orders but the pending orders won't delete.
The EA is eg: When the Buy criteria to "Open" is meet place a Buy at Market and place a pending order. When the Buy criteria to "close" is meet then close the open Buys and pending order should be closed.
Short Trades same process as the Buy orders.
Any assistance would be welcomed. File attached. Comments in the file .Thanks