Download MetaTrader 5

unkowne ticket number for orderclose function

To add comments, please log in or register
What is the best place to find a programmer? Freelance service!
baharin
78
baharin 2013.11.19 01:46 

Hi..

I got an error message "unknown ticket XXXXXX for orderclose function", could some body help me ?...Due to that the expert advisor cannot close the order...

Please help me and my code as below:

...
...
...
int            MagicNumber = 34567;
int            TicketNumber;
...
...
...
void OpenBuy() {   
   if (MinProfit > 0) {
      TicketNumber = OrderSend(Symbol(),OP_BUY,lot,Ask,1,0,Ask+MinProfit*Point,"harimau",MagicNumber,0,Blue);      
   } else {
      TicketNumber = OrderSend(Symbol(),OP_BUY,lot,Ask,1,0,0,"harimau",MagicNumber,0,Blue);      
   }
}

void CloseSell() {   
   TicketNumber = OrderClose(OrderTicket(), OrderLots(), Bid, 3, Red);
}

void OpenSell() {
   if (MinProfit > 0) {
      TicketNumber = OrderSend(Symbol(),OP_SELL,lot,Bid,1,0,Bid-MinProfit*Point,"harimau",MagicNumber,0,Red);       
   } else {
      TicketNumber = OrderSend(Symbol(),OP_SELL,lot,Bid,1,0,0,"harimau",MagicNumber,0,Red);       
   }
}

void CloseBuy() { 
   TicketNumber = OrderClose(OrderTicket(), OrderLots(), Ask, 3, Blue);         
}

int GetMagicNumber (int MagicNumber, string symbol, int timeFrame) {
   int isymbol = 0;
   if (symbol == "EURUSD")       isymbol = 1;
   else if (symbol == "GBPUSD")  isymbol = 2;
   else if (symbol == "USDJPY")  isymbol = 3;
   else if (symbol == "USDCHF")  isymbol = 4;
   else if (symbol == "AUDUSD")  isymbol = 5;
   else if (symbol == "USDCAD")  isymbol = 6;
   else if (symbol == "EURGBP")  isymbol = 7;
   else if (symbol == "EURJPY")  isymbol = 8;
   else if (symbol == "EURCHF")  isymbol = 9;
   else if (symbol == "EURAUD")  isymbol = 10;
   else if (symbol == "EURCAD")  isymbol = 11;
   else if (symbol == "GBPUSD")  isymbol = 12;
   else if (symbol == "GBPJPY")  isymbol = 13;
   else if (symbol == "GBPCHF")  isymbol = 14;
   else if (symbol == "GBPAUD")  isymbol = 15;
   else if (symbol == "GBPCAD")  isymbol = 16;
   else                          isymbol = 17;
   
   return (MagicNumber + isymbol);
   //return (StrToInteger(StringConcatenate(MagicNumber, isymbol, timeFrame)));
}

int init() {
       MagicNumber = GetMagicNumber( MagicNumber, Symbol(), Period() );      
       return (0);
}

int deinit() {
   return(0);
}

int start() {
...
...
...
   if (Test > 5) {     
      if (OrderSelect(TicketNumber, SELECT_BY_TICKET) == true) {
         if (OrderType() == OP_SELL) {
            CloseSell();
            OpenBuy();
         }         
      } else {         
         OpenBuy();
      }      
   } else if (Test < -5) {
      if (OrderSelect(TicketNumber, SELECT_BY_TICKET) == true) {
         if (OrderType() == OP_BUY) {
             CloseBuy();
             OpenSell();
          }
      } else {
         OpenSell();
      }
   }
   
   return(0);
}
 
Ubzen
5394
Ubzen 2013.11.19 02:07  

Means the ticket# isn't valid .. or is not longer valid.

Try using some debugging techniques. Print() Comment() or Alert().

Ubzen
5394
Ubzen 2013.11.19 02:16  

You're trying to OrderClose without OrderSelect .. Not Recommended.

void CloseSell() {   
   TicketNumber = OrderClose(OrderTicket(), OrderLots(), Bid, 3, Red);
}
You have a TicketNumber yet you used OrderTicket() within the OrderClose().
Ubzen
5394
Ubzen 2013.11.19 02:20  

You must remember that the OrderTicket() values derived from the Last_Save_State of the OrderSelect().

OrderClose() does not return OrderTicket(). It returns a Bool should the OrderClose Succeed.

Ubzen
5394
Ubzen 2013.11.19 02:31  

When you OrderSelect by SELECT_BY_TICKET is selects the order from MODE_HISTORY or MODE_TRADES.

It is then necessary to check the OrderCloseTime() to determine if the order is already closed.

Any attempt on closing orders which are already closed would generate the error message.

To add comments, please log in or register