OrderDelete, Again...

 

I have an OrderDelete issue, and I've read through all the related threads and made adjustments, but it still doesn't work properly: I have set a buystop and a sellstop and when one is triggered, I want the other one to be deleted. My code will delete the buystop if the sellstop is triggered, but the it will never delete the sellstop if the buystop is triggered. In other words, in NEVER deletes the sellstop. So, what have I missed?

Thanks, Kevin

//---- input parameters
extern int       ChannelSize=20;
extern double    StopLoss=120;      
extern double    ProfitTarget=240;
extern double    LotSize=0.01;
extern double    LotSizeIncrease=0;  //for later use...
extern int       Magic=2008;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
      Comment("ChannelBreakOut");   
      return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
      Comment("");
      return(0);
}

int start()
  
{     
PendingOrders(Magic);         //COUNTS PENDING ORDERS
DeletePendingOrders(Magic);   //DELETES REMAINING PENDING ORDER AFTER BUYSTOP/SELLSTOP TRIGGERED
int Total=OrdersTotal();      //COUNTS ALL ORDERS, OPEN AND PENDING 

      if(Total==0)            //IF NO ORDERS, THEN CREATE CHANNEL WITH BUYSTOP AND SELL STOP
      {
         OrderSend(Symbol(),OP_BUYSTOP,LotSize,Ask+(Point*ChannelSize/2),0,Ask+(Point*ChannelSize/2)-StopLoss*Point,
         Ask+(Point*ChannelSize/2)+ProfitTarget*Point,"",Magic,0,Green);
         
         OrderSend(Symbol(),OP_SELLSTOP,LotSize,Ask-(Point*ChannelSize/2),0,Ask-(Point*ChannelSize/2)+StopLoss*Point,
         Ask-(Point*ChannelSize/2)-ProfitTarget*Point,"",Magic,0,Green);
      }
   return(0);
}


int PendingOrders(int Magic)              //COUNTS PENDING ORDERS
{
  int c=0;
  int total  = OrdersTotal();

  for (int i=total-1; i >=0; i--)
  {
    OrderSelect(i,SELECT_BY_POS,MODE_TRADES);     
    if (OrderMagicNumber()==Magic && OrderSymbol()==Symbol() && (OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP))
    {
      c++;
    }
  }
  return(c);
}  

int DeletePendingOrders(int Magic)        //DEFINITION OF HOW TO DELETE THE PENDING ORDERS
{      
    if(PendingOrders(Magic)==1)
    {
    OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
    if (OrderMagicNumber()==Magic && OrderSymbol()==Symbol() && (OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP))
    {
      OrderDelete(OrderTicket());
    }
  }
  return(0);
}
 

consider ur use of magic

and how it MUST be unique for each order in trade pool...

4got... great that u use SRC btn to display code ;)

 
//IF YOU WANT TO DELETE ALL PENDING ORDERS OF A CERTAIN MAGIC NUMBER, CONSIDER A LOOP AS BELOW

int DeletePendingOrders(int Magic)        //DEFINITION OF HOW TO DELETE THE PENDING ORDERS
{      
  for(int i=0; i<OrdersTotal(); i++)
  {
    //if(PendingOrders(Magic)==1)
    
    if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         continue;    
    if (OrderMagicNumber()==Magic && OrderSymbol()==Symbol() && (OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP))
    {
      OrderDelete(OrderTicket());
    }
  
 }
  return(0);
}
 

agree abstract_mind, is so... but i mention cuz [at least for my multi sym EA] wanna be total unique ident code in each order (so i just think is wats normal - course is not... i appreciate that :)

simple nuff msk out sequentially numbered field in magic leaving the EA magic field to use and apply like in your code but targeting eg, 1 order out of 10 or more is needing more info in magic.

not fan of using symbol name, cuz may have 1,2 or? orders on a symbol and comment field changes when do part close cuz broker makes new order with remaining lots and completely unique magic is only datum of closed order that is copied across to new order (with new ticket# of course) ie, gotta find this ticket# in tradePool via that orig order [100%unique] magic#

that only reason i drone on - is horse for course i guess ;)

oh well... just fwiw stuff thrown into cook pot

;)

 

Thanks for the response! I cut and pasted your solution into the EA. Leaving it as is without the pending order counter, it deletes BOTH pending orders before either can trigger. Then, of course, the start function places two more pending orders and it loops through again, ad infinitum.

If I leave your code AND include the pending order counter again, it works as before. Deletes the buystops, but not the sellstops.

Also, I don't understand how to make the changes you both suggest about the magic number.

I'd be grateful for another solution. Thanks again.

Kevin

int DeletePendingOrders(int Magic)        //DEFINITION OF HOW TO DELETE THE PENDING ORDERS
{      
    if(PendingOrders(Magic)==1)
    
    for(int i=0; i<OrdersTotal(); i++)
  {
        
    if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         continue;
    
    OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
    if (OrderMagicNumber()==Magic && OrderSymbol()==Symbol() && (OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP))
    {
      OrderDelete(OrderTicket());
    }
  }
  return(0);
}
 

Original code above, you have 0 instead of i :

int DeletePendingOrders(int Magic)        //DEFINITION OF HOW TO DELETE THE PENDING ORDERS
{      
    if(PendingOrders(Magic)==1)
    {
    OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
You also may need to go "find" the order, not "assume" it is in position 0 of the trade pool.
 
Phy, thank you, replacing the 0 with i made the difference. Now it deletes both buystop and sellstop orders appropriately. This is my first EA and sometimes it's difficult to understand exactly what each part of a function does. Can you tell me why changing 0 to i causes it to appropriately delete both instead of just the buystops? I'm enjoying learning, but it can be awfully frustrating sometimes! In the beginning it took me three hours to figure out that I had written "If" instead of "if"!! Thanks again. kevin
 

0 is always 0.

i changes the "index" into the list of orders according to the current value of i.

Reason: