Download MetaTrader 5

OrderDelete() Function in Loop Will Not Work. Please Help!!!!

To add comments, please log in or register
Lynn
49
Lynn  
Ever since the new MT4 build was released, I've had issues with OrderDelete(). I have a loop that manages my open trades, but this is the only function not working. Can some please help? The code is below. Thanks.
  double OpenPoint = iOpen(Symbol(), PERIOD_W1, 0);
 
  int total, i, ticket, OrdOpen;
  total = OrdersTotal();                          
  for(i=0; i<total; i++)
  { 
   if(OrderSelect(i, SELECT_BY_POS)==true)
   {
      if(OrderSymbol() == Symbol() && (OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP || OrderType() == OP_BUY || OrderType() == OP_SELL)) OrdOpen++;                                                                            
      if(OrderSymbol() == Symbol() && OrderType() == OP_BUYSTOP && Ask < OpenPoint) bool res=OrderDelete(ticket); //THIS DOES NOT WORK
      if(OrderSymbol() == Symbol() && OrderType() == OP_SELLSTOP && Bid > OpenPoint)  res=OrderDelete(ticket);    //THIS DOES NOT WORK                                               
      if(OrderSymbol() == Symbol() && OrderType() == OP_BUY && Bid > OrderOpenPrice() + 100/Multiple) res=OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + 100/Multiple, 0, 0, Red);                                        
      if(OrderSymbol() == Symbol() && OrderType() == OP_SELL && Ask < OrderOpenPrice() – 100/Multiple) res=OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() - 100/Multiple, 0, 0, Red);                            
      if(OrderSymbol() == Symbol() && OrderType() == OP_BUY && Bid > OrderOpenPrice() + 5/Multiple && Bid < OrderOpenPrice() + 100/Multiple) res=OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), 0, 0, Red); 
      if(OrderSymbol() == Symbol() && OrderType() == OP_SELL && Ask < OrderOpenPrice() - 5/Multiple && Ask > OrderOpenPrice() - 100/Multiple) res=OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), 0, 0, Red);
   }
  }  
   if(OrdOpen > 0) return(0);
Keith Watford
Moderator
10569
Keith Watford  

Your code is twice as wide as my screen and to be honest, I can't be bothered to scroll back and forth in order to read it.

If you make your code easier to read, on different lines and with indentation, you will probably get more people willing to help you.

Lynn
49
Lynn  
Sorry GumRai. Hopefully this helps. Thanks.
double OpenPoint = iOpen(Symbol(), PERIOD_W1, 0); 
int total, i, ticket, OrdOpen;
total = OrdersTotal();                          
for(i=0; i<total; i++)
{ 
 if(OrderSelect(i, SELECT_BY_POS)==true)
 {
  if(OrderSymbol() == Symbol() && OrderType() == OP_BUYSTOP && Ask < OpenPoint) bool res=OrderDelete(ticket); 
  if(OrderSymbol() == Symbol() && OrderType() == OP_SELLSTOP && Bid > OpenPoint)  res=OrderDelete(ticket);                                                 
 }
}  
 return(0);
GreenMoney
379
GreenMoney  

You haven't made the variable "ticket" equal to a valid ticket number.

You need to either make the variable "ticket" equal to OrderTicket() inside the OrderSelect() IF statement or just use OrderTicket() inside the OrderDelete().

if(OrderSymbol() == Symbol() && OrderType() == OP_BUYSTOP && Ask < OpenPoint) bool res=OrderDelete(OrderTicket());
if(OrderSymbol() == Symbol() && OrderType() == OP_SELLSTOP && Bid > OpenPoint)  res=OrderDelete(OrderTicket());

You should also consider counting down in your FOR loop. See Loops and Closing or Deleting Orders.

Lynn
49
Lynn  
Thirteen:

You haven't made the variable "ticket" equal to a valid ticket number.

You need to either make the variable "ticket" equal to OrderTicket() inside the OrderSelect() IF statement or just use OrderTicket() inside the OrderDelete().

.


Thanks, Thirteen! I'll give it a whirl.
qjol
3243
qjol  
OrderDelete(OrderTicket());
Lynn
49
Lynn  
7455:

Thanks, Thirteen! I'll give it a whirl.

Thirteen, it works like a charm. Thanks again!
whroeder1
15596
whroeder1  
for(i=0; i<total; i++)
Count DOWN. Loops and Closing or Deleting Orders - MQL4 forum
To add comments, please log in or register