Unknown ticket 1 for OrderModify function + OrderModify error 4108

 

Hi,

I'm coding an EA that opens two limit orders at the same time but which then closes them using different logic. One of the orders ( the first in the logic ) is being handled fine. The second order is not being modified as per the logic I'm trying to apply to it and I'm getting the errors noted in the journal.

Here's the relevant code. Any suggestions gratefully received as to what I am doing wrong.

 for( int i=OrdersTotal()-1;i>=0;i-- ) 
   {
      Ticket=OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == Symbol() && ( OrderMagicNumber()==MagicNumber ))
      {
     //Changes limit orders
         if ( Volume[0]==1 )
         {
            if (OrderType() == OP_SELLLIMIT ) ModifyPending("SELLLIMIT", Ticket);
            if (OrderType() == OP_BUYLIMIT ) ModifyPending("BUYLIMIT", Ticket);
         }
      // Sets stop loss if there is not one      
         if(OrderType() == OP_BUY && OrderStopLoss() == 0 ) OrderModify(OrderTicket(),0, NormalizeDouble(OrderOpenPrice()-StopPips,4), 0, 0, DarkGreen);
         if(OrderType() == OP_SELL && OrderStopLoss() == 0 )  OrderModify(OrderTicket(), 0, NormalizeDouble(OrderOpenPrice()+StopPips,4), 0, 0, DarkOrange);
       
         if (OrderMagicNumber() == MagicNumber)
         {
            if(OrderType() == OP_BUY) ModifyLive("BUY",Ticket);
            if(OrderType() == OP_SELL) ModifyLive("SELL",Ticket);
         }
      }
      if(OrderSymbol() == Symbol() && ( OrderMagicNumber()==MagicNumber2 ))
      {
         if ( Volume[0]==1 )
         {
            if (OrderType() == OP_SELLLIMIT ) ModifyPending("SELLLIMIT", Ticket);
            if (OrderType() == OP_BUYLIMIT ) ModifyPending("BUYLIMIT", Ticket);
         }
      // Sets stop loss if there is not one      
         if(OrderType() == OP_BUY && OrderStopLoss() == 0 ) OrderModify(OrderTicket(),0, NormalizeDouble(OrderOpenPrice()-StopPips,4), 0, 0, DarkGreen);
         if(OrderType() == OP_SELL && OrderStopLoss() == 0 )  OrderModify(OrderTicket(), 0, NormalizeDouble(OrderOpenPrice()+StopPips,4), 0, 0, DarkOrange);
    
         if (OrderType() == OP_BUY && MA_8_1 < MA_21_1 && MA_8_2 > MA_21_2 ) OrderClose(Ticket, Lots, Bid, Slippage, MediumSeaGreen);
         if (OrderType() == OP_SELL && MA_8_1 > MA_21_1 && MA_8_2 < MA_21_2 )  OrderClose(Ticket, Lots, Ask, Slippage, MediumSeaGreen);
       
       // Carries out the breakeven  
         if(OrderType() == OP_BUY && MA_8_1 < MA_8_2 && OrderStopLoss() != OrderOpenPrice()+ BreakEvenPips ) MoveToBreakEven(Ticket, "BUY");
         if(OrderType() == OP_SELL && MA_8_1 > MA_8_2 && OrderStopLoss() != OrderOpenPrice() - BreakEvenPips ) MoveToBreakEven(Ticket, "SELL");
      }
   
   }
 

OrderSelect() returns a bool, not a ticket number . . so how is your OrderClose() going to work ?

Also, what happens if any of your OrderModify or OrderClose calls fail ? don't you want to know about it ? What are Function return values ? How do I use them ?

Also, perhaps you are closing the Order and then trying to modify it ? after the OrderClose() perhaps you should continue;

 

Of course, thanks. I got confused with OrderSend which returns the ticket number. Replacing "ticket" in this code with OrderTicket() solved the problem.

The EA is at an early stage, at the moment I'm just testing the logic of the trades. Once it's proved profitable on strategy tester I'll make it more robust in terms of checking for order execution failure.

Thanks again.

Best regards Steve

 
Stevetrade:

Of course, thanks. I got confused with OrderSend which returns the ticket number. Replacing "ticket" in this code with OrderTicket() solved the problem.

The EA is at an early stage, at the moment I'm just testing the logic of the trades. Once it's proved profitable on strategy tester I'll make it more robust in terms of checking for order execution failure.

Despite what some may say . . . ignorance is not bliss. It is much better to do it correctly from the start . . . in my opinion.
 
There are many paths up the mountain.
 
Stevetrade:
There are many paths up the mountain.
Yes, you are quite correct . . . but would you go without knowing the weather forecast ? or go blindfolded ?