# Code Problem

50

Hello All,

With these two order open orders:

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(OrderMagicNumber()== MagicNumber||MagicNumber_TP)
if(OrderSymbol()==Symbol())
{
DeleteOrder();
}

}

}

Any Ideas? Thx

1399

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?

50

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

if(OpenOrdersThisPair(Symbol())>=1)
{
for(int b=OrdersTotal()-1; b >= 0; b--)
{
if(OrderMagicNumber()== MagicNumber||MagicNumber_TP)
if(OrderSymbol()==Symbol())
{
DeleteOrder();
}
}
} ```

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

15719

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()
){
DeleteOrder();
}
```
50

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....
15719

 Harami57: still only deletes one trade, the MagicNumber one.... Posted code doesn't delete any trades. Post your code.
Moderator
10637

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

Moderator
10637

```                             if(OrderType()==OP_BUYSTOP)
{
DeleteOrder();
}
```

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?

50

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(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());
}
} ```
Moderator
10637

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());

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

50

Thanks GumRai and WHRoeder - I'm doing a code rewrite at the weekend will test it out then...;-)
12