Download MetaTrader 5
To add comments, please log in or register
Subscribe to signals right in MetaTrader. It is easy!
J Webster
929
J Webster 2009.06.25 19:25 

I have 2 pending orders 1 BUY, 1 SELL.

If 1 of these trades is triggered and hits its take profit, I want to cancel the other order.

If it instead, hits its stop loss, I want to keep the other order.

//////////////////////////////////////////////////////////
//IT IS NOW after 0700GMT but before 2100GMT, monitor order count
//////////////////////////////////////////////////////////
if (Hour()>=7 && Hour() <21) 
{
      int totalOrdrs = OrdersTotal();
      if (totalOrdrs > 0) 
      {
         for(i=totalOrdrs-1;i>=0;i--) //if no orders then it won't run through this again
         {
            OrderSelect(i, SELECT_BY_POS);
            type   = OrderType();
            result = false;
            if(type=OP_BUY || type==OP_SELL) // ie one of the orders has been triggered, now we delete the other one.
            {
                  for(i=totalOrdrs-1;i>=0;i--) //if no orders then it won't run through this again
                  {
                     OrderSelect(i, SELECT_BY_POS);
                     type   = OrderType();
                     result = false;
                     if(type>1)result = OrderDelete( OrderTicket() );
                     if(result == false)
                     {
                        Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
                        Sleep(3000);
                     }
                  }
              
            }
          }
      }
}

}
23510
505
23510 2009.06.25 21:03  
Use Orderprofit () to check the profitability of the last order then you can use it as a criteria for an OrderDelete()
J Webster
929
J Webster 2009.06.25 21:40  
23510:
Use Orderprofit () to check the profitability of the last order then you can use it as a criteria for an OrderDelete()

If the 2 orders are OP_BUYSTOP and OP_SELLSTOP.

When they are triggered, do they change to OP_BUY and OP_SELL or do they remain as stops?

If it changes to OP_BUY, then I can simply delete any remaining OP_SELLSTOP orders and vice versa.


I am trying the following but getting order failed to close errors, possibly because it is trying to delete open orders?


//////////////////////////////////////////////////////////
//IT IS NOW after 0700GMT but before 2100GMT, monitor order count
//////////////////////////////////////////////////////////
if (Hour()>=7 && Hour() <21) 
{
      total = OrdersTotal();
      for(i=total-1;i>=0;i--) //if no orders then it won't run through this again
      {
         OrderSelect(i, SELECT_BY_POS);
         type   = OrderType();
         result = false;
         if (type==OP_BUY) //one of the orders must have been triggered so delete the remaining pending order
         {
               int totaldel = OrdersTotal();
               for(i=total-1;i>=0;i--) //if no orders then it won't run through this again
               {
                  OrderSelect(i, SELECT_BY_POS);
                  int typedel   = OrderType();
                  bool resultdel = false;
                  if(type>1)resultdel = OrderDelete( OrderTicket() );
                  if(resultdel == false)
                  {
                     Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
                     Sleep(3000);
                  }  
               }
         }
         
         if (type==OP_SELL) //one of the orders must have been triggered so delete the remaining pending order
         {
               totaldel = OrdersTotal();
               for(i=total-1;i>=0;i--) //if no orders then it won't run through this again
               {
                  OrderSelect(i, SELECT_BY_POS);
                  typedel   = OrderType();
                  resultdel = false;
                  if(type>1)resultdel = OrderDelete( OrderTicket() );
                  if(resultdel == false)
                  {
                     Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
                     Sleep(3000);
                  }  
               }
         }

      }
}
23510
505
23510 2009.06.25 21:48  
SanMiguel:

If the 2 orders are OP_BUYSTOP and OP_SELLSTOP.

When they are triggered, do they change to OP_BUY and OP_SELL or do they remain as stops?

If it changes to OP_BUY, then I can simply delete any remaining OP_SELLSTOP orders and vice versa.


a pending order becomes a market order if it is triggered so yes...they become OP_BUY and OP_SELL

J Webster
929
J Webster 2009.06.26 14:40  
23510:

a pending order becomes a market order if it is triggered so yes...they become OP_BUY and OP_SELL

This doesn't delete the existing pending order. Can't see why not:


//////////////////////////////////////////////////////////
//IT IS NOW after 0700GMT but before 2100GMT, monitor order count
//////////////////////////////////////////////////////////
if (Hour()>=7 && Hour() <21) 
{
      total = OrdersTotal();
      for(i=total-1;i>=0;i--) //if no orders then it won't run through this again
      {
         OrderSelect(i, SELECT_BY_POS);
         type   = OrderType();
         result = false;
         if (type==OP_BUY && actionedflag == 0) //the buy order has been triggered so close the pending sell
         {
            int totaltodel = OrdersTotal();
            for(int itodel=totaltodel-1;i>=0;i--) //if no orders then it won't run through this again
            {
               OrderSelect(itodel, SELECT_BY_POS);
               int typetodel   = OrderType();
               bool resulttodel = false;
               if(typetodel>1)resulttodel = OrderDelete( OrderTicket() );
            }
            actionedflag = 1;
         }
         
         if (type==OP_SELL && actionedflag == 0) //the buy order has been triggered so close the pending sell
         {
            totaltodel = OrdersTotal();
            for(itodel=totaltodel-1;i>=0;i--) //if no orders then it won't run through this again
            {
               OrderSelect(itodel, SELECT_BY_POS);
               typetodel   = OrderType();
               resulttodel = false;
               if(typetodel>1)resulttodel = OrderDelete( OrderTicket() );
            }
            actionedflag = 1;
         }
         
      }
}
Panjianom Adi Pratomo
229
Panjianom Adi Pratomo 2016.02.25 21:47  
Interesting thread, I would like to know too... anyone suggest?
my suggest: https://forum.mql4.com/38013
whroeder1
13600
whroeder1 2016.02.26 14:50  
  1. Check your return codes and find out why. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  2. Add print statements before your if statements (include variable values) and find out why.
/
To add comments, please log in or register