Delete Pending Orders when a market order is closed

 

How can I delete pending orders when any of the buy or sell order is closed.

I would like to code to delete pending orders after closing market orders?

Is there any suggestion?

 

If you close your market orders with a function in your EA you simply need to use OrderDelete for pendings in that function.

If you want to delete pendings after a market order hit SL/TP it's different, you can to check if there are no market orders but pendings (in case of your EA open only a single order per Symbol/Magic No.)

 
Mehmet Cak:

How can I delete pending orders when any of the buy or sell order is closed.

I would like to code to delete pending orders after closing market orders?

Is there any suggestion?

  1. MT5: OnTradeTransaction() event. MT4: Count the open orders. If the count goes down, delete your pending orders.
  2. MT5: OnTradeTransaction() event. MT4: Count the open orders. If the count is zero, delete all pending orders.
  3. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol,) you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum
      For In First Out (FIFO rules-US brokers,) and you (potentially) process multiple orders per symbol, you must find the earliest order, close it, and on a successful operation, reprocess all positions.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: #1 № 11 ACCOUNT_FIFO_CLOSE

    2. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask) or (be direction independent and use) OrderClosePrice().

 
There is no way to delete pending orders when market is closed
 
Mehmet Cak:

How can I delete pending orders when any of the buy or sell order is closed.

1. Detect "when any of the buy or sell order is closed" in the OnTradeTransaction() event.

2. Then, "delete pending orders."

 
Fabio Cavalloni:

If you close your market orders with a function in your EA you simply need to use OrderDelete for pendings in that function.

If you want to delete pendings after a market order hit SL/TP it's different, you can to check if there are no market orders but pendings (in case of your EA open only a single order per Symbol/Magic No.)

Hi Fabio, thank you for your help.

If you want to delete pendings after a market order hit SL/TP it's different,

That is excatly what i want. EA opens multiple orders. I want to delete pending orders when one of the market order is closed.

EA use Magic Number. Lets say there are 5 market orders open.. When one is closed with TP or SL, There are 4 orders. When order 5 is closed 

i want to delete awaiting pending orders. How can i do that??

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
  • www.mql5.com
Trading is done by sending orders to open positions using the OrderSend() function, as well as to place, modify or delete pending orders. Each trade order refers to the type of the requested operation. Trading operations are described in the ENUM_TRADE_REQUEST_ACTIONS enumeration...
 

Sorry, was a missunderstanding.

if you know, how many pending are done, and you habe no open position then you can count your open pending and delete it

Here a function to count open positions


int CountPositionsExpert(const string symbol, const int MagicNumber) //Gewinn der Gesamtposition
  {

   MqlTradeRequest request;
   MqlTradeResult  result;

   int positionen=0;
//--- in allen offenen Positionen suchen

   int i = PositionsTotal();
   while(i-->0)
     {
      //--- Parameter der Order
      ulong  position_ticket=PositionGetTicket(i);// das Ticket der Position
      PositionSelectByTicket(position_ticket);
      string position_symbol=PositionGetString(POSITION_SYMBOL); // Symbol
      ulong  magic=PositionGetInteger(POSITION_MAGIC); // MagicNumber der Position
      double position_profit=PositionGetDouble(POSITION_PROFIT);

      //--- wenn die MagicNumber übereinstimmt, sind Stop Loss und Take Profit nicht gesetzt
      if(position_symbol==symbol && MagicNumber==magic)
        {
         positionen++;
        }
     }
   return(positionen);
  }

You can do the same on open orders

 be aware between the difference about orders, deals and positions

 
He
amando:

Sorry, was a missunderstanding.

if you know, how many pending are done, and you habe no open position then you can count your open pending and delete it

Here a function to count open positions


You can do the same on open orders

 be aware between the difference about orders, deals and positions

Helllo amando, first of all thank you very much for your consideration. It is no problem how many pending orders awaiting. My EA opens limit and stop orders. When they become actual market buy or sell orders, it continues to open limit and stop orders. I would like stop it to open limit and stop orders when one of the active market buy or sell order is closed either buy SL or TP. So there is no need to count pending limit or stop orders. I want to close all pending orders when any of the open market order is closed.I could not find a way how to do it. That is my problem. I just want a piece of code that will delete all pending orders when any of the market order is closed. I am a medical doctor not a programmer, coding is my hobby only. It tried everyting but i could not find a away. 

 
amando:
There is no way to delete pending orders when market is closed
Although it's off-topic, strictly speaking it may happen it's possible to delete pending orders when the market is closed. Not in Forex, but on some exchange and with some brokers.
 
Alain Verleyen:
Although it's off-topic, strictly speaking it may happen it's possible to delete pending orders when the market is closed. Not in Forex, but on some exchange and with some brokers.

I will try to figure it out. Can you tell me the broker you work and what are some exchanges, i will be apprecitad if you can provide me ERROR CODES.. I mean WHY my work does not his work in a correct manner way. 

U can contact me from private. Thank for your feedback.

 
Alain Verleyen:
Although it's off-topic, strictly speaking it may happen it's possible to delete pending orders when the market is closed. Not in Forex, but on some exchange and with some brokers.

There is no way delete pending orders when the market is closed but i can add some codes to delete pendings as soon as market open. Help me to make it better. Thanks again

Reason: