Delete Pending order after new order

 

Hello Folks.

I'm trying to figure out how to write a code which will delete a pending order after I placed a new one.


Example:

Existing BuyStop. Now I place a new BuyStop.

What I need is a code which can look up all Buy Stops and will delete all, except the new one.

I guess somehow I may be able to use OrderSelect and filter the BuyStops and then delete all, except the newest ticket...?

Can anybody help? My programming skill seems to be too bad to figure it out on my own...

Thanks.
WorstCases

 

the first u placed manually & the second 2?

 

Yes. I just want to be able to place new pending orders manually and want the EA to automatically delete all previous pending orders of the same type with the next tick.

I know I could do it different (manual), but I really like to do it with a coded EA. It's kind of a learning lesson for me.

 
Thank you for your response.

I you don't mind be bothering you again: Can you please clarify some things (I made comments in the code)...?

Basically I don't understand where in your code example it makes sure it's only touching BuyStop Orders and where it says to delete all, but the newest. Also I wonder why you did add the time. I guess in my case I don't need time, because I want to keep the highest ticket # and delete the rest...

OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES);
   {
   int OT = OrderType(); // do I have to change this in BuyStop to make it delete BuyStop orders?
   string Symb = OrderSymbol();
   datetime OOT = OrderOpenTime();  //Not sure if I need this. I'd rather delete ALL, except the newest (highest ticket)
   }
   for (int cnt = OrdersTotal() - 1; cnt >= 0; cnt--)
      {
      OrderSelect (cnt, SELECT_BY_POS,MODE_TRADES);
      
      if (OrderSymbol() == Symb && OrderType() == OT && OrderOpenTime() < OOT)
         {
         OrderDelete(OrderTicket());
         }
 
OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES); //selecting tha last order
   {                                                    //& put the details into variables:
   int OT = OrderType();          //......................1) the order type
   string Symb = OrderSymbol();   //......................2) the order symbol
   datetime OOT = OrderOpenTime();//......................3) the order open time
   //-----
   for (int cnt = OrdersTotal() - 1; cnt >= 0; cnt--)  // & now starting to compare to all orders
      {
      OrderSelect (cnt, SELECT_BY_POS,MODE_TRADES);
      
      if (OrderSymbol() == Symb && OrderType() == OT && OrderOpenTime() < OOT) 
         {
         OrderDelete(OrderTicket());    //deleting if it's the same order type & the same symbol & if it's placed (in time) before the last
         }
      }
   }
//i didn't checked it I hope it works
 

Hello Gjol.


I really appreciate you're taking the time to try to help me find a solution.


Can you please tell me, why you do the orderdelete with time?

Is there no way to do it by the ticket? I mean in the example BuyStop: Delete all BuyStop orders, except the one with the highest ticket (or order number - whatever it is in this case).

Greetings.

WorstCases

 

because no one can guarantee you (Although this is a faraway possibility) that your broker will not replace numbering (& start over) of the OrderTicket() But you can assure yourself that you delete only what you placed earlier and leave what you've placed later that is why I use time + It was your request to delete what you placed earlier and leave what you've placed later

 
OK. This makes very much sense. Thanks for the clarification.
Reason: