Download MetaTrader 5
To add comments, please log in or register
Have you developed a trading robot? Publish it in the Market and make profit!
Dominic Gilbert
1632
Dominic Gilbert 2014.09.18 14:18 
//+----------------------------------------------------------------------+
//| Delete Order function - If price closes above or below applicable MA |
//+----------------------------------------------------------------------+
void DeleteOrder()
  {

   for(int i=OrdersTotal()-1; i>=0; i--) 
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;   
        if( OrderMagicNumber() == MagicNumber1 || OrderMagicNumber()== MagicNumber2 ||
            OrderMagicNumber()== MagicNumber3 || OrderMagicNumber()== MagicNumber4 )
        if(OrderSymbol() == Symbol()) 
         if(OrderType() > OP_SELL)
          {  
          if(!OrderDelete(OrderTicket(),Magenta)){ // <-- try to close the order
            Print("Order Delete failed, order number: ",OrderTicket()," Error: ",GetLastError(), " On: ", OrderSymbol()); 
            }
            else{
            Print("Pending Order was deleted: ", OrderTicket(), " On: ", OrderSymbol());
            }
          }
      } //  end of For loop
  }
Trying to close 4 orders on one given pair. It fails to close due to "Invalid Ticket 4108 error" and also returns the wrong OrderSymbol() for the pair it's working on?
Dominic Gilbert
1632
Dominic Gilbert 2014.09.18 15:47  
UPDATE: do I need a unique magicnumber per OrderSend??? Or should I just be using the same MagicNumber1 per EA and pair?
SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.09.18 16:31  
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;   

?


You have a missing { } too.


You don't need to use too many IF, just simplify into one condition.

if( (OrderMagicNumber() == MagicNumber1 || OrderMagicNumber()== MagicNumber2 ||
            OrderMagicNumber()== MagicNumber3 || OrderMagicNumber()== MagicNumber4 ) &&
        (OrderSymbol() == Symbol()) && (OrderType() > OP_SELL) )

Something like that.

Dominic Gilbert
1632
Dominic Gilbert 2014.09.18 17:48  
//+----------------------------------------------------------------------+
//| Delete Order function - If price closes above or below applicable MA |
//+----------------------------------------------------------------------+
void DeleteOrder()
{

   for(int i=OrdersTotal()-1; i>=0; i--) // OrderTicket() used to be OrdersTotal() 
   {
      if(!OrderSelect(i,SELECT_BY_POS, MODE_TRADES) == true) continue;
      { 
        if( OrderMagicNumber() == MagicNumber1 && OrderSymbol() == Symbol() 
            && OrderType() > OP_SELL)
          {  
          if(!OrderDelete(OrderTicket(),Magenta)){ // <-- try to close the order
            Print("Order Delete failed, order number: ",OrderTicket()," Error: ",GetLastError(), " On: ", OrderSymbol()); 
            }
            else{
            Print("Pending Order was deleted: ", OrderTicket(), " On: ", OrderSymbol());
            }
          }
      }     
    } //  end of For loop
}

  
Are you suggesting like this?

Likewise, do I actually need a unique magic number for each individual order when I have an EA for each pair? As in, do I need MagicNumber1, MagicNumber 2 and so forth when all the trades (4 of them) will be managed all under this specific EA?
Keith Watford
Moderator
7887
Keith Watford 2014.09.18 19:09  
&& OrderType() > OP_SELL

OrderDelete(

 You can't use OrderDelete with an OP_BUY or OP_SELL

You have to use OrderClose 

SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.09.19 00:58  

Nah. He is correct.

OrderType() > OP_SELL

Means any ordertype bigger than op_buy & op_sell.

Keith Watford
Moderator
7887
Keith Watford 2014.09.19 03:26  
deysmacro:

Nah. He is correct.

Means any ordertype bigger than op_buy & op_sell.

Oops, yes

I misread that. 

/
To add comments, please log in or register