Download MetaTrader 5
To add comments, please log in or register
All activity of the MQL5.community members is displayed on a single page!
brettosm8
241
brettosm8 2015.12.09 06:10 

Hi there,

I have this code:

void OnStart()
  {
//---
   for(int i=OrdersTotal()-1;i>=0; i--)
     {
      Print("OrdersTotal: ",OrdersTotal());

      if(OrderSelect(i,SELECT_BY_POS)==false) Print("Unable to close order. Error: ",GetLastError());

      Print("Ticket: ",OrderTicket());
      Print("Lots ",OrderLots());
      Print("Type: ",OrderType());

      if(OrderType()==0)//BUY
        {
         if(OrderClose(OrderTicket(),OrderLots(),Bid,4,Red)==false) Print("Unable to close order. Error: ",GetLastError());
        }
      else if(OrderType()==1)//SELL
        {
         if(OrderClose(OrderTicket(),OrderLots(),Ask,4,Red)==false) Print("Unable to close order. Error: ",GetLastError());
        }
      else if(OrderType()==4)//BUY pending order
        {
         if(OrderDelete(OrderTicket()==false)) Print("Unable to delete pending order. Error: ",GetLastError());
        }
      else if(OrderType()==5)//SELL pending order
        {
         if(OrderDelete(OrderTicket()==false)) Print("Unable to delete pending order. Error: ",GetLastError());
        }
     }//End for
  }//End OnStart

There are several pending orders on the chart.

When I run the script I get the below the pending orders don't delete.

 

Thanks in advance.

Ovo Cz
2957
Ovo Cz 2015.12.09 06:20  
A bad practice is comparing boolean to boolean to get boolean. Now you can see it is also dangerous, when you misplace the parenthesis.
whroeder1
13596
whroeder1 2015.12.09 15:46  
if(OrderDelete(OrderTicket()==false))
if(OrderDelete(   123456    ==false))
if(OrderDelete(   123456    ==  0  ))
if(OrderDelete(           false    ))
if(OrderDelete(             0      ))  // there is no order number zero

This is why I say: You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
/
To add comments, please log in or register