unknown ticket for OrderModify function

 

 ive make a code to change to takeprofit ,, and it work normal for open position etc,, but in the journall it write unknown ticket for OrderModify function,, and the takeprofit and te stop loss in this situation change complitelly ,, i don't know why

void change(int number,double tp)
{
           if(OrderSelect(oredrs[number],SELECT_BY_TICKET,MODE_TRADES)==true)
            { 
                  if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==OP_BUY && OrderTakeProfit()!= tp)
                   { 
                     int ticket1=OrderModify(OrderTicket(),Ask,OrderStopLoss(),tp,0,clrBlack);
                   }
             else if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==OP_SELL &&OrderTakeProfit() != tp)
                   { 
                    int ticket2= OrderModify(OrderTicket(),Bid,OrderStopLoss(),tp,0,clrBlack);
                    
                    if(ticket2 == 1)
                        {
                            Print("Ordermodify successful");
                        }

                    if(ticket2 == 0)
                        {
                            Print("Ordermodify UN-successful");
                        }
                   }
            }   
}
void change_all()
 {
   int count=2;
  for(int i=0;i<Max_Orders;i++)
    {
      if(OrderSelect(oredrs[i],SELECT_BY_TICKET)==true)
       {
        if(i==0 && OrderType()==OP_BUY)
        {
          change(i,tp);
        }else if(i==0 && OrderType()==OP_SELL)
        {
         change(i,ntp);
        }
        else if(OrderType()==OP_BUY)
        {
         change(i,open+(count*harmonic));
         count++;
        }
        else if(OrderType()==OP_SELL)
        {
         change(i,open-(count*harmonic));
         count++;
        }
        }
    }
 }
 
rezk allah said fellah:

 ive make a code to change to takeprofit ,, and it work normal for open position etc,, but in the journall it write unknown ticket for OrderModify function,, and the takeprofit and te stop loss in this situation change complitelly ,, i don't know why

You are probably trying to modify a trade that has already closed.

 
Keith Watford:

You are probably trying to modify a trade that has already closed.

let's assume that what have you said is right ,, how to avoid it ?

 
rezk allah said fellah:

let's assume that what have you said is right ,, how to avoid it ?

Check whether the trade has been closed before attempting to modify it.

 
Keith Watford:

Check whether the trade has been closed before attempting to modify it.

yes they are closed as you see


 

Hello rezk allah said fellah,

 I got your email..

you are sending the for loop index number ... 0, to the change function instead of the actual ticket number.. in order to use SELECT_BY_TICKET you need to use the OrderTicket() instead of the index of the for loop....

If you are going to use the index number then you should use SELECT_BY_POS..

Pip Pip... Jimdandy

 
Keith Watford:

Check whether the trade has been closed before attempting to modify it.

It looks like the close happened in the same moment when the OrderModify was issued. You cannot protect your code from this race condition, that is, you can never make sure that an OrderModify is issued against a valid order.

The only way to handle this situation is to check after OrderModify failed, if the order is still present, and then use the error code to sort out what happened.


bool adjustTP(int number,double tp)
 {
    bool isOk=true;
    if(OrderSelect(orders[number],SELECT_BY_TICKET,MODE_TRADES) && OrderSymbol()==_Symbol && OrderMagicNumber()==Magic)
     {
        if(OrderType()==OP_BUY && OrderTakeProfit()!=tp)
         { 
            isOk=OrderModify(orders[number],OrderOpenPrice(),OrderStopLoss(),tp,0,clrBlack);
         }
        else if(OrderType()==OP_SELL && OrderTakeProfit()!=tp)
         {
            isOk=OrderModify(orders[number],OrderOpenPrice(),OrderStopLoss(),tp,0,clrBlack);
         }
        if(isOk)
         {
            Print("Ordermodify successful");
         }
        else if(OrderSelect(orders[number],SELECT_BY_TICKET,MODE_TRADES))
         {
            Print("Ordermodify UN-successful, error: ",GetLastError());
         }
        else
         {
            Print("Ordermodify UN-successful, trade is already closed");
            isOk=true;
         }
     }
    return isOk; // true: all ok, false: an error occurred, treat it
 }
 
rezk allah said fellah: let's assume that what have you said is right ,, how to avoid it ?

How did you generate your array of tickets? EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.

 
James Hodges:

Hello rezk allah said fellah,

 I got your email..

you are sending the for loop index number ... 0, to the change function instead of the actual ticket number.. in order to use SELECT_BY_TICKET you need to use the OrderTicket() instead of the index of the for loop....

If you are going to use the index number then you should use SELECT_BY_POS..

Pip Pip... Jimdandy

From what I can gather from the code, he is storing the ticket numbers in an array.

He is sending the element index of the array to the function.

 
lippmaje:
bool change(int number,double tp)
{
           bool isOk=true;
           if(OrderSelect(oredrs[number%4],SELECT_BY_TICKET,MODE_TRADES) && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
            { 
                  if(OrderType()==OP_BUY && OrderTakeProfit()!= tp)
                   { 
                      isOk=OrderModify(oredrs[number%4],OrderOpenPrice(),OrderStopLoss(),tp,0,clrBlack);
                   }
             else if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==OP_SELL &&OrderTakeProfit() != tp)
                   { 
                     isOk= OrderModify(oredrs[number%4],OrderOpenPrice(),OrderStopLoss(),tp,0,clrBlack);
                   }
                   }
    if(isOk)
     {
        Print("Ordermodify successful");
     }
    else if(OrderSelect(oredrs[number%4],SELECT_BY_TICKET,MODE_TRADES))
     {
        Print("Ordermodify UN-successful, error: ",GetLastError());
     }
    else
     {
        Print("Ordermodify UN-successful, trade is already closed");
        isOk=true;
     }
    return isOk; // true: all ok, false: an error occurred
 }
//----------------------------------------------------------------------+
 void change_all()
 {
   int count=2;
  for(int i=0;i<Max_Orders;i++)
    {
      if(OrderSelect(oredrs[i],SELECT_BY_TICKET)==true)
       {
        if(i==0 && OrderType()==OP_BUY)
        {
          change(i,tp);
        }
        else if(OrderType()==OP_BUY)
        {
         change(i,open+(count*harmonic));
         count++;
        }
        else if(i==0 && OrderType()==OP_SELL)
        {
         change(i,ntp);
        }
        else if(OrderType()==OP_SELL)
        {
         change(i,open-(count*harmonic));
         count++;
        }
        }
    }
 }

It looks like the close happened in the same moment when the OrderModify was issued. You cannot protect your code from this race condition, that is, you can never make sure that an OrderModify is issued against a valid order.

The only way to handle this situation is to check after OrderModify failed, if the order is still present, and then use the error code to sort out what happened.


 Ordermodify UN-successful, error: 1 ,,, this issue start the moment the 5 order appear

 
Keith Watford:

From what I can gather from the code, he is storing the ticket numbers in an array.

He is sending the element index of the array to the function.

yes it is
Reason: