Help with deleting pending orders.

 

Hi anyone that can help. I have been trying to code this EA for a while and getting there slowly, however I am stuck with deleting previous pending orders before opening the next one.

Below is the code I have just before entering either a buy trade and sellstop trade or the other way round. It works as it should do but now I need to add the funtion call to close the previous pending order the /////////////////////////is were I believe the code should go I have also inserted the function that I have come up with for findng the previous pending order(again I am unsurre if this is correct) any help in the right direction would be appreciated

 {
    if(enableTrade() == true)
         if(OpenOrders(OP_BUY)<MaxAmountOfTrades)
            if((HedgingAllowed) || (!HedgingAllowed && OpenOrders(OP_SELL)==0))
               EnterTrade(OP_BUY);
               
               if(OpenOrders(OP_BUY)>=1 && OpenOrdersB(OP_SELLSTOP)<1)
              
   ////////////////////////////////////////////////////////           
             
            EnterTradeB(OP_SELLSTOP);
         else Print("Hedging did not allow this trade.");
        }
      candletime=Time[0];
     }
  }
---------------------------------------------------------------
int  OrderDelete()
   
{
   int ticket_number=-1;
   for(int i=OrdersHistoryTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
         if(OrderMagicNumber()==magicB)
           {
            ticket_number=OrderTicket();
            break;
           }

     }//for

   return(ticket_number);
  } //function

 
Adam Woods:

Hi anyone that can help. I have been trying to code this EA for a while and getting there slowly, however I am stuck with deleting previous pending orders before opening the next one.

Below is the code I have just before entering either a buy trade and sellstop trade or the other way round. It works as it should do but now I need to add the funtion call to close the previous pending order the /////////////////////////is were I believe the code should go I have also inserted the function that I have come up with for findng the previous pending order(again I am unsurre if this is correct) any help in the right direction would be appreciated


The missing two " { " make your code incomprehensible.

 

Algorithm for deleting pending orders (I use just this method):

A deletion order is issued (flag m_need_delete_all is raised)

               m_need_delete_all=true;

In OnTick it is checked: if the flag m_need_delete_all is cocked, then we begin to delete:

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(m_need_delete_all)
     {
      if(IsPendingOrdersExists())
        {
         double level;
         if(FreezeStopsLevels(level))
           {
            DeleteAllPendingOrders(level);
            return;
           }
         else
            return;
        }
      else
         m_need_delete_all=false;
     }
//---

After removal, we check whether there are pending orders left or not: if they remain, we start deleting again. If there are no more pending orders, reset the flag.

Example taken from code: Step Pending Orders