OrderModify error

 

Hi,

I have the following messages : invalid ticket for OrderModify function and Ordermodify function error 4051 and 4018. Can anybody help me to solve this ?

here is the code :

//Buy

if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

if(!IsTrade) {

//Check free margin

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 0, 0, 0, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);

OrderSelect(Ticket, SELECT_BY_TICKET,MODE_TRADES);

OrderModify(OrderTicket(),OrderOpenPrice(), Bid - (SL*Point),Bid +(TP*Point),0,Red);

if(Ticket > 0) {

if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {

Print("BUY order opened : ", OrderOpenPrice());

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");

} else {

Print("Error opening BUY order : ", GetLastError());

}

}

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

return(0);

}

}

//Sell

if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

if(!IsTrade) {

//Check free margin

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);

OrderSelect(Ticket, SELECT_BY_TICKET,MODE_TRADES);

OrderModify(OrderTicket(),OrderOpenPrice(),Ask +(SL*Point), Ask -(TP*Point),0,Green);

if(Ticket > 0) {

if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {

Print("SELL order opened : ", OrderOpenPrice());

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell");

} else {

Print("Error opening SELL order : ", GetLastError());

}

}

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

return(0);

}

}

if (!EachTickMode) BarCount = Bars;

return(0);

}

 

I'm using that:

OrderSelect(Ticket,SELECT_BY_TICKET);

OrderModify(OrderTicket(),OrderOpenPrice(),.......);

;;

Buy:

OrderSelect(ticket,SELECT_BY_TICKET);

OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-StopLoss*Point,OrderOpenPrice()+TakeProfit*Point,0,Blue)

...

Sell;

OrderSelect(ticket,SELECT_BY_TICKET);

OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+StopLoss*Point,OrderOpenPrice()-TakeProfit*Point,0,Red);

 

Hi,

Thank you for answer. I changed parameters, but it still don't recognize the ticket. In fact when it opens ticket #1, then it closes it and after it opens ticket#2 it displays :"unknown ticket #1"........(I guess it can't recognize it because it's already closed) but how to solve that ?...

 
Tradefx1:
Hi, Thank you for answer. I changed parameters, but it still don't recognize the ticket. In fact when it opens ticket #1, then it closes it and after it opens ticket#2 it displays :"unknown ticket #1"........(I guess it can't recognize it because it's already closed) but how to solve that ?...

Can you post the mql

 

What Happens is that the order ticket 1 closes, and conditions are still valid for ordermodify to execute, but EA can't modify a closed order.

Add the following to your code:

if (your__modify_order_condition && OrdersTotal!=0)

this way he just modifies orders that are still open.

good luck

V.

 

In those cases usually you will get a "unknown ticked nnnnnnn for OrderModify function" error (if you try to modify an order that is closed in the mean time and you are using a ticket number of that closed order)

vsimoes:
What Happens is that the order ticket 1 closes, and conditions are still valid for ordermodify to execute, but EA can't modify a closed order.

Add the following to your code:

if (your__modify_order_condition && OrdersTotal!=0)

this way he just modifies orders that are still open.

good luck

V.
Reason: