Download MetaTrader 5

Code Problem

To add comments, please log in or register
Harami57
50
Harami57  

Hello All,

With these two order open orders:

int BuyTicketOrder = OrderSend(Symbol(),OP_BUYSTOP,LotSize,Buy_Price,3,Buy_Price-(StopLoss*pips),Buy_Price+(TakeProfit*pips),NULL,MagicNumber,0,Green);

int BuyTicketOrder_TP = OrderSend(Symbol(),OP_BUYSTOP,0.02,Buy_Price,3,Buy_Price-(TP_SL*pips),Buy_Price+(TP_pips*pips),NULL,MagicNumber_TP,0,Green);


When this code is executed, only the first order "BuyTicketOrder" is deleted, not "BuyTicketOrder_TP"


if(OpenOrdersThisPair(Symbol())>=1)          
      {
             for(int b=OrdersTotal()-1; b >= 0; b--)
                {
                if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
                    if(OrderMagicNumber()== MagicNumber||MagicNumber_TP)
                       if(OrderSymbol()==Symbol())
                          if(OrderType()==OP_BUYSTOP)
                            {     
                             DeleteOrder();
                             Print("GBPUSD BUYSTOP DELETED");
                             }

                 }

        }

Any Ideas? Thx    

SYED NAUFAL GADDAFI
1399
SYED NAUFAL GADDAFI  

Can you please edit your post and use SRC (besides video icon) when posting codes? Can't really see what is going on.


Though you may need to double check the for loop logic. Is it supposed to be like that?

Harami57
50
Harami57  
int BuyTicketOrder = OrderSend(Symbol(),OP_BUYSTOP,LotSize,Buy_Price,3,Buy_Price-(StopLoss*pips),Buy_Price+(TakeProfit*pips),NULL,MagicNumber,0,Green);
int BuyTicketOrder_TP = OrderSend(Symbol(),OP_BUYSTOP,0.02,Buy_Price,3,Buy_Price-(TP_SL*pips),Buy_Price+(TP_pips*pips),NULL,MagicNumber_TP,0,Green);
                        




if(OpenOrdersThisPair(Symbol())>=1)         
               {
             for(int b=OrdersTotal()-1; b >= 0; b--)
                    {
                    if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
                       if(OrderMagicNumber()== MagicNumber||MagicNumber_TP)
                           if(OrderSymbol()==Symbol())
                             if(OrderType()==OP_BUYSTOP)
                             {      
                             DeleteOrder();
                             Print("GBPUSD BUYSTOP DELETED");
                             }
                    }  
                } 


Sorry about that - hope that's better? Yes the for loop is supposed to be like that, what is the logic problem?  Thx

whroeder1
15719
whroeder1  
Harami57: Sorry about that - hope that's better? Yes the for loop is supposed to be like that, what is the logic problem?  Thx
  1. You should have edited your original post.
  2. if(OrderMagicNumber()== MagicNumber||MagicNumber_TP)
    if(                false           ||      2       )
    if(                false           ||    true      )
    if(                              false             ) // MagicNumber_TP not selected.
    Bool and int are interchangeable. (8 < 7 < 6 is true)
    for(int b=OrdersTotal()-1; b >= 0; b--)
       if(OrderSelect(b,SELECT_BY_POS)
       && (OrderMagicNumber()== MagicNumber||OrderMagicNumber()==MagicNumber_TP)
       && OrderSymbol()==Symbol()
       && OrderType()==OP_BUYSTOP
       ){      
          DeleteOrder();
          Print("GBPUSD BUYSTOP DELETED");
       }
    
Harami57
50
Harami57  
WHRoeder:
  1. You should have edited your original post.
  2. Bool and int are interchangeable. (8 < 7 < 6 is true)
Ok but, if I remove that line (as both orders will be OP_BUYSTOP anyway), it still only deletes one trade, the MagicNumber one....
whroeder1
15719
whroeder1  
Harami57: still only deletes one trade, the MagicNumber one....
Posted code doesn't delete any trades. Post your code.
Keith Watford
Moderator
10637
Keith Watford  

WHRoeder:

 

if(OrderMagicNumber()== MagicNumber||MagicNumber_TP)
if(                false           ||      2       )
if(                false           ||    true      )
if(                              false             ) // MagicNumber_TP not selected.

 Doesn't

 if(false || true) equate to true? 

I would think that

if(OrderMagicNumber()== MagicNumber||MagicNumber_TP)

 would always be true unless MagicNumber_TP equals 0

Keith Watford
Moderator
10637
Keith Watford  
                             if(OrderType()==OP_BUYSTOP)
                             {      
                             DeleteOrder();
                             Print("GBPUSD BUYSTOP DELETED");
                             }

 Do you check the magic number again in the function DeleteOrder() ? If so are you checking for both?

Do you check and report if the OrderDelete fails? 

Harami57
50
Harami57  

GumRai - yes you're right, thank you. I forgot the DeleteOrder() function was user-defined, not part of the MQL4 library.

Here is the DeleteOrder() function. How can I modify to get rid of both BuyStops?

   void DeleteOrder(){
    
      
      for(int i=OrdersTotal()-1; i>=0; i--)
      {
         if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
            if(OrderMagicNumber()== MagicNumber &&
               OrderSymbol()==Symbol()&&
                  OrderType()>OP_SELL)
                     if(!OrderDelete(OrderTicket(),CLR_NONE))//if Order Close unsuccessful
                        Print("Order Close failed, order number:", OrderTicket(), "Error: ", GetLastError());
      }
   } 
Keith Watford
Moderator
10637
Keith Watford  
Harami57:

GumRai - yes you're right, thank you. I forgot the DeleteOrder() function was user-defined, not part of the MQL4 library.

Here is the DeleteOrder() function. How can I modify to get rid of both BuyStops?

First of all, you do as WHRoeder pointed out

(OrderMagicNumber()== MagicNumber||OrderMagicNumber()==MagicNumber_TP)

 Then

DeleteOrder(); //Change this to
DeleteOrder(OrderTicket());

//And then in your function

void DeleteOrder(int ticket)
  {
   if(OrderSelect(ticket,SELECT_BY_TICKET))
      if(!OrderDelete(OrderTicket(),CLR_NONE))//if Order Close unsuccessful
         Print("Order Delete failed, order number:",ticket,"Error: ",GetLastError());
  }

 Not compiled or tested

Harami57
50
Harami57  
Thanks GumRai and WHRoeder - I'm doing a code rewrite at the weekend will test it out then...;-)
12
To add comments, please log in or register