Order Delete function - help.

 
Ok, first of all, I have read the entire MQL4 manual... (so before I get badgered this time for being ignorant, I have done my homework...)

if (direction==0) //--Buy--//
   {
      
      {
      double bsl=buy_stop_price;
      double btp=buy_takeprofit_price;
      LotSize = (RiskedAmount/(pips_to_bsl/pips))/10;
      if(OpenOrdersThisPair(Symbol())==0) 
      OrderSend(Symbol(),OP_BUYSTOP,LotSize,buyPrice,3,bsl,btp,NULL,MagicNumber,0,Green);
      //if (BuyTicket>0)OrderModify(BuyTicket,OrderOpenPrice(),bsl,btp,0,CLR_NONE);
      if (Close[1]<s12) // s12 is a 60 moving average.
      for(int s =0; s< OrdersTotal(); s++)

      {
      if (OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
      if (OrderSymbol()== Symbol())    
      if (OrderMagicNumber()== MagicNumber)
      if (OrderType()>0)
      OrderDelete(OrderTicket(),CLR_NONE);
      }
      }
    
   }
            
if (direction==1)//--Sell--//
   {
      {
      double ssl=sell_stop_price;
      double stp=sell_takeprofit_price;
      LotSize = (RiskedAmount/(pips_to_ssl/pips))/10;    
      if(OpenOrdersThisPair(Symbol())==0)  
      OrderSend(Symbol(),OP_SELLSTOP,LotSize,sellPrice,3,ssl,stp,NULL,MagicNumber,0,Red); 
      //if (SellTicket>1)OrderModify(BuyTicket,OrderOpenPrice(),ssl,stp,0,CLR_NONE);
      if (Close[1]>s12) 
      for(int i =0; i< OrdersTotal(); i++)

      {
      if (OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
      if (OrderSymbol()== Symbol())    
      if (OrderMagicNumber()== MagicNumber)
      if (OrderType()>0)
      OrderDelete(OrderTicket(),CLR_NONE);
      }
      }
   }
Orders stops and take profits are working perfectly in accordance to my rules... what I cannot get working is the OrderDelete function based upon the if statement? Any suggestions?
 
DomGilberto:
Orders stops and take profits are working perfectly in accordance to my rules... what I cannot get working is the OrderDelete function based upon the if statement? Any suggestions?

First, please read What are Function return values ? How do I use them ?.  What error code is generated when you say the OrderDelete() function isn't working?

Second, I suggest that you read Loops and Closing or Deleting Orders.  Counting down (rather than up) in your FOR loop may eliminate your problem.

Third, in your Buy section (direction==0), you are using the variable 's' in your FOR loop and OrderSelect() as an index to the order pool.  However, in your Sell section (direction==1), you are using the variable 'i' in the FOR loop but the variable 's' in your OrderSelect().  This may be the source of your problem with deleting sell orders.

 
DomGilberto:
Ok, first of all, I have read the entire MQL4 manual... (so before I get badgered this time for being ignorant, I have done my homework...)

Orders stops and take profits are working perfectly in accordance to my rules... what I cannot get working is the OrderDelete function based upon the if statement? Any suggestions?

What does this mean in plain English ?

if (OrderType() > 0)

 Make your code readable and make it mean what you want it to mean,  for example,  you cannot delete an OP_BUY,  you can OrderClose() it but not OrderDelete() it . . .

 
DomGilberto: I have read the entire MQL4 manual... 
  1. Not well enough.
  2. Fourth, you can only delete pending orders
  3. if (OrderType()>0) // Not a OP_BUY: an open Sell, a pending limit or stops.
    Don't hard code constants - use the enumerations
 

My mistake! I have no idea why it was set to "0" - It was actually set to >2 (as I am using stop orders

 This still does not change the fact that the orders are not being deleted...

 

The only way I can get this OrderDelete to work is this way (see code).

 

The problem with this code is it will actually constantly be looking to delete on the "if" statement being true... which apparently is true all the time... I am only wanting to delete pending orders from the time it is placed, and to IF whether or not price closes beneath the 60 ema (in a case of a buy order) and above for a sell order...

WHRoeder, just because I have read the manual in under a week doesn't mean I am proficient in MQL4... I'm still learning! 

if(Close[1]>CurrentBigFish6) // CurrentBigFish6 is a 60 EMA... 
   {
   DeleteOrder(0); // 0 = buy order and 1 is a sell order. This is further up in the code next to where the MA crossovers are written.
   }
//--------------------------------------------------------------------------------------------------//
//-------------------------------------------------------------------------------------------------//
void DeleteOrder (int Pendingorder)
{   
   
if (Pendingorder==0)

      for(int i =0; i< OrdersTotal(); i++)

      {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      if (OrderSymbol()== Symbol())    
      if (OrderMagicNumber()== MagicNumber)
      if (OrderType()>2)
      OrderDelete(OrderTicket(),CLR_NONE);
      }

if (Pendingorder==1)   

      for(i =0; i< OrdersTotal(); i++)

      {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      if (OrderSymbol()== Symbol())    
      if (OrderMagicNumber()== MagicNumber)
      if (OrderType()>2)
      OrderDelete(OrderTicket(),CLR_NONE);
      }

}
 
DomGilberto:

My mistake! I have no idea why it was set to "0" - It was actually set to >2 (as I am using stop orders) 

 This still does not change the fact that the orders are not being deleted...

 

The only way I can get this OrderDelete to work is this way (see code).

 

The problem with this code is it will actually constantly be looking to delete on the "if" statement being true... which apparently is true all the time... I am only wanting to delete pending orders from the time it is placed, and to IF whether or not price closes beneath the 60 ema (in a case of a buy order) and above for a sell order...

WHRoeder, just because I have read the manual in under a week doesn't mean I am proficient in MQL4... I'm still learning! 

What does . . . 

if (OrderType()>2)

 . . . mean ?  

 

Read this:   Loops and Closing or Deleting Orders

 
Yea, I have no idea why I have that in there... I was just under the impression I could get OrderType to work, by identifying what order it was in conjunction with the If Statement being true = DeleteOrder...

Thanks for that link! It's incredibly informative and well presented! Thank you!
 
DomGilberto:

My mistake! I have no idea why it was set to "0" - It was actually set to >2 (as I am using stop orders) 

So, if you have it set to >2, you are going to also use OP_SELLLIMITs in addition to stop orders?  See Trade Contants/Enumerations.

In your code (displayed below):

void DeleteOrder (int Pendingorder)
{   
   
if (Pendingorder==0)

      for(int i =0; i< OrdersTotal(); i++)

      {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      if (OrderSymbol()== Symbol())    
      if (OrderMagicNumber()== MagicNumber)
      if (OrderType()>2)
      OrderDelete(OrderTicket(),CLR_NONE);
      }

if (Pendingorder==1)   

      for(i =0; i< OrdersTotal(); i++)

      {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      if (OrderSymbol()== Symbol())    
      if (OrderMagicNumber()== MagicNumber)
      if (OrderType()>2)
      OrderDelete(OrderTicket(),CLR_NONE);
      }

}

...what is the difference between Pendingorder==0 and Pendingorder==1?  Looks like the same code to me, so why discriminate based on whether Pendingorder is 0 or 1 if the function executes the same code?  Aren't just duplicating code?  Also, why use void instead of having your function notify the calling function if the OrderDelete() actually succeeded or failed by returning a bool?  As I mentioned in my first post, you might want to read What are Function return values ? How do I use them ?

 

Just replace

if (OrderType()>2)

to 

if (OrderType()>=2)

 P.S. When you try to delete/close orders you to countdown orders.

 
Roger:

Just replace

if (OrderType()>2) 

to 

if (OrderType()>=2) 

Or you can code it this way:

if (OrderType() >= OP_BUYLIMIT && OrderType() <= OP_SELLSTOP)

The above uses enumeration and allows for only pending orders.  In addition, it takes care of some possible undocumented OrderType values (see here). 

 

Hey thanks for everyone's posts and helpful comments! Appreciate it.

Just to give an update, I've used this code below. It is now deleting the pending orders, but seems to be having a knock on effect on how my orders are placed with regards to my moving stop loss (which is calculated on a moving average relative to the entry price in pips / lots risked relative to percentage risk profile of closed equity.)

Either way, I'm sure ill be able to figure that out.... hopefully :)

 

 

(so all that happens is, that the IF statements calls this void if True and runs through the parameters below... Code is thanks to RaptorUK highlighting a very important part of how deleting orders works.) 

void DeleteOrder (int Pendingorder)
{   
   
if (Pendingorder==0)

int PositionIndex;    //  <-- this variable is the index used for the loop

int TotalNumberOfOrders;   //  <-- this variable will hold the number of orders currently in the Trade pool

TotalNumberOfOrders = OrdersTotal();    // <-- we store the number of Orders in the variable

for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --)  //  <-- for loop to loop through all Orders . .   COUNT DOWN TO ZERO !
   {
   if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;   // <-- if the OrderSelect fails advance the loop to the next PositionIndex
   
   if( OrderMagicNumber() == MagicNumber       // <-- does the Order's Magic Number match our EA's magic number ? 
      && OrderSymbol() == Symbol()         // <-- does the Order's Symbol match the Symbol our EA is working on ? 
      && ( OrderType() == OP_BUYSTOP           // <-- is the Order a Buy Order ? 
      ||   OrderType() == OP_SELLSTOP ) )      // <-- or is it a Sell Order ?
   
      if ( ! OrderDelete( OrderTicket(),CLR_NONE) )               // <-- try to close the order
         Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() );  // <-- if the Order Close failed print some helpful information 
      
   } //  end of For loop

}