Invalid Ticket

 
Can someone help me to find out what is wrong?

I'm writing an EA to put 2 pending order some pips (a lot more than broker's stop level) above and below the opening candle. I'm using the code below to delete the remaining pending order if one of them is triggered. But it doesn't work if the pending order was stop order, it's returning error 4108 (invalid ticket), but working fine if the pending order was limit order.

   total=OrdersTotal();
   for(int ct=0;ct<total;ct++) 
    {
      OrderSelect(ct,SELECT_BY_POS,MODE_TRADES);
      if((OrderType()<=OP_SELL)==true)
       {
          OrderDelete(OrderTicket());
          return(0);
       }
    }
What's wrong with these code?

Thank you
 
You must close open orders and delete pending orders from OrdersTotal()-1 to 0 position on the list. See:

 total=OrdersTotal();
   for(int ct=total-1;ct>0;ct--) 
    {
      OrderSelect(ct,SELECT_BY_POS,MODE_TRADES);
      if((OrderType()<=OP_SELL)==true)
       {
          OrderDelete(OrderTicket());
          return(0);
       }
    }
 
Thanks for the reply Rosh, but somehow it doesn't solve the problem. The sellstop order still didn't get deleted.
Now I'm using magic number, it may not necessary but it's working.

   total=OrdersTotal();
   for(int mg=0;mg<total;mg++)
    {
      OrderSelect(mg,SELECT_BY_POS,MODE_TRADES);
      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
       {
          mag=OrderMagicNumber();                 
       }
    }
   
   for(int ct=0;ct<total;ct++)
    {
      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      if(OrderMagicNumber()==mag)
       {
         if(OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT)
          {
            OrderDelete(OrderTicket());
          }
       }
    }
 

Do You see main difference between your code and code by Rosh?

for(int ct=total-1; ct>=0; ct--)
It is backward loop!
 
I'm aware of it, but it simply didn't work. Maybe I wasn't make myself clear, so he didn't understand my problem.
Thx
 
OrderType <= OP_SELL ???
 
richplank:
OrderType <= OP_SELL ???

Yep, on every new candle this EA will put a buystop and sellstop at a certain point from open[0] with the same magic number. When one of them is triggered, the other one will be deleted.
My idea is if there's an active buy/sell order, the EA will read it's magic number and delete the buystop/sellstop order with the same magic number.
 
devilian1899:
I'm aware of it, but it simply didn't work. Maybe I wasn't make myself clear, so he didn't understand my problem.
Thx


If you ignore Rosh and Stringo advice, and you are deleting more than one pending order during the loop, it will fail to close all the pending orders "sometimes"

 
There's only one pending order left in case one of them is triggered, if none of the 2 pending orders get triggered, both will expired 5 seconds before the candle is closed, if the expiration is failed, there's another code to close all of pending order before placing a couple pending orders on a new candle, but that's another matter.
I was just asking how to successfuly deleting the remaining pending order if one of the pending orders is triggered. I've tried Rosh's code, but somehow it didn't work, I don't know what's wrong. Maybe I should post all of my code to be examined but I prefer not to do that. However it's already solved, thanks for your attention and suggestion :)
 

I have the same OrderDelete issue: 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?

//---- 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);
}