Deleting Pending Order (Just Once)

 

Hello guys, 

What is the code to delete the last ticket pending order, but it is important to do this, just one time!

For example:

EA opened 02 orders:

1) Sell Limit

2) Buy Limit

If one of thoses orders had a deal (Market hit the price) and become an "open trade", imediately EA will delete the other (pending) order.
But after a while (some minutes for example), if I would like to include another pending other, EA will not delete it anymore. 


I'm using this code, but its a continuos loop so it is not working, because it is keeping deleting my orders.


   int total_p=0;
   ulong ticket;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      ticket=OrderGetTicket(i);
      if(ticket>0)
        {
         if(OrderGetString(ORDER_SYMBOL)==_Symbol)
           {
            if(OrderGetInteger(ORDER_TYPE)==ORDER_TYPE_BUY_STOP ||
               OrderGetInteger(ORDER_TYPE)==ORDER_TYPE_BUY_LIMIT||
               OrderGetInteger(ORDER_TYPE)==ORDER_TYPE_SELL_STOP||
               OrderGetInteger(ORDER_TYPE)==ORDER_TYPE_SELL_LIMIT)
              {
               total_p++;
               if(exist)
                  if(Trade.OrderDelete(ticket))
                    continue;
 

in that case you can catch ticket_1 and ticket_2 upon opening these two initial orders, and if either one of them becomes an OP_BUY or OP_SELL market order, delete the remaining one.

Then you can open as much as you like without your EA deleting them, in fact you can do this anytime so even before they become market orders, because it will filter for ticket number.

 
Marco vd Heijden:

in that case you can catch ticket_1 and ticket_2 upon opening these two initial orders, and if either one of them becomes an OP_BUY or OP_SELL market order, delete the remaining one.

Then you can open as much as you like without your EA deleting them, in fact you can do this anytime so even before they become market orders, because it will filter for ticket number.


Thank you for your answer!!

Is there any other way, like using  "break" or something that EA will execute one time and stop?

Or, how could I extract the number of the ticket of an specific order?

I'm using this kind of parameter


      double price=high+(HLPriceDistance+OrderPrice);
      double sl=stoploss==0?0:price+stoploss;
      double tp=TakeProfit==0?0:price-TakeProfit;

      Trade.SellLimit(Lot,price,_Symbol,sl,tp,0);

So, if I would like to know the numer of the ticket of that "Trade.SellLimit ()" Which parameter should I have to use?


Again, thank you for your help!

 

Well the tickets are the return values of the OrderSend() function but this part of your code isnt visible.

If you mean that you will place the orders manually then you would have to catch the first two orders and save their tickets to their respected variables.

Of course you can also set a flag once two orders have been opened, this would then prevent the code block from running again.

bool flag;
---------------------


if(OrdersTotal()==2)
 {
  flag=1;
 }

if(flag==0)
 {
  // still not first two orders...
  // do something...
 }

if(flag==1)
 {
  // first two orders opened...
  // do something else...
 }

Then the code in the first block (flag==0) will not be processed until you reset the value of the flag to zero.

But there are of course better ways to deal with this issue and if you proceed you will find them.

 
Marco vd Heijden:

Well the tickets are the return values of the OrderSend() function but this part of your code isnt visible.

If you mean that you will place the orders manually then you would have to catch the first two orders and save their tickets to their respected variables.

Of course you can also set a flag once two orders have been opened, this would then prevent the code block from running again.


Thank you very much again!!

I just include a line and I think it should works

                    if(OrdersTotal()<2)  

So the lines will be:


   int total_p=0;
   ulong ticket;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      ticket=OrderGetTicket(i);
      if(ticket>0)
        {
         if(OrderGetString(ORDER_SYMBOL)==_Symbol)
           {
            if(OrderGetInteger(ORDER_TYPE)==ORDER_TYPE_BUY_STOP ||
               OrderGetInteger(ORDER_TYPE)==ORDER_TYPE_BUY_LIMIT||
               OrderGetInteger(ORDER_TYPE)==ORDER_TYPE_SELL_STOP||
               OrderGetInteger(ORDER_TYPE)==ORDER_TYPE_SELL_LIMIT)
              {
               total_p++;
               if(exist)
                  if(OrdersTotal()<2)       
                    if(Trade.OrderDelete(ticket))
                       continue;
Reason: