invalid Ticket and invalid Price error, and it dose not make sense

 

hello

i have developed an EA that use to work well. but since almost a week ago when it wants to close orders, it gives me invalid Ticket and invalid Price error.

the strange part is that right after the OrderClose a put an Alert to see if the Ticket or the Price is Wrong. but it is right. Alert shows that Ticket and the price is Right !!!

i even put the Second OrderClose after the Alert and RefreshRates so if it dosent close the first time, it closes in the next try. but it gives me the same error and still Alert shows that every value is right!!!

also i use 5 different EAs in the same time on 5 different currency Pairs. but all Variables have different names including the ticket and lot. i am choosing the order by Ticket, not by index so this can not be the problem.

this is the part of my code that has problem: can anyone help me? 

int CloseBuyNowSmallLot()

  {

//Alert("start of CloseBuyNowFifteen() TimeCurrent()= ",TimeCurrent());

//Alert("Attempt to close Buy ",Ticket," LocalTime= ",TimeLocal());

//RefreshRates();                          // Refresh rates

   TimeNow=TimeCurrent();

   PriceBID=MarketInfo(Symbol(),MODE_BID);

   Ticket = OrderTicket();

   if(OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES)==true)

     {

      Lts = OrderLots();

     }

   bool Ans=false;

   Ans=OrderClose(Ticket,Lts,Bid,3,clrPink);        // Closing Buy

   if(Ans==true)                            // Success :)

     {

      PlaySound("stops.wav");

      Alert("Closed order Buy Profit: ",OrderProfit()," TimeCurrent()= ",TimeCurrent()," Symbol= ",Symbol());

      //Alert("profit of this trade:",OrderProfit());

      ObjectCreate(0,"ClosingBuy_EURUSD",OBJ_ARROW_CHECK,0,TimeNow,PriceBID);

      ObjectFind(0,"ClosingBuy_EURUSD");

      ObjectSet("ClosingBuy_EURUSD",OBJPROP_COLOR,clrPink);

      //CountTrades();

      Ans=false;

      //total=0;

      //Ticket=0;

      if(OrderCloseTime()!=0)        // if order is closed

        {

         OrderCode=0;

        }

      //Alert("OrderCode= ",OrderCode);

      return(0);                                // Exit closing loop

     }

   if(Ans==false)

     {

      Alert(" OrderClose did not work in ",Symbol());

      Fun_Error();

      // in case that order close dose not work this Alert will be shown:

      Alert(" Ticket= ",Ticket," Lts= ",Lts," PriceBID= ",PriceBID);

      RefreshRates();

      Ans=OrderClose(Ticket,Lts,Bid,3,clrPink);        // Closing Buy

      if(Ans==true)                            // Success :)

        {

         PlaySound("stops.wav");

         Alert("Closed order Buy Profit: ",OrderProfit()," TimeCurrent()= ",TimeCurrent()," Symbol= ",Symbol());

         //Alert("profit of this trade:",OrderProfit());

         ObjectCreate(0,"ClosingBuy_EURUSD",OBJ_ARROW_CHECK,0,TimeNow,PriceBID);

         ObjectFind(0,"ClosingBuy_EURUSD");

         ObjectSet("ClosingBuy_EURUSD",OBJPROP_COLOR,clrPink);

         //CountTrades();

         Ans=false;

         //total=0;

         //Ticket=0;

         if(OrderCloseTime()!=0)        // if order is closed

           {

            OrderCode=0;

           }

         //Alert("OrderCode= ",OrderCode);

         return(0);                                // Exit closing loop

        }

      if(Ans==false)

        {

         Alert(" Second OrderClose did not work in ",Symbol());

         Fun_Error();

         // in case that order close dose not work this Alert will be shown:

         Alert(" Ticket= ",Ticket," Lts= ",Lts," PriceBID= ",PriceBID);

         return(0);                               // Exit Function

        }

     }

   return(0);

  }


Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Trade Server Return Codes
Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Trade Server Return Codes
  • www.mql5.com
Trade Server Return Codes - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
   Ticket = OrderTicket();       //You MUST select an order before you can use OrderTicket()

   if(OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES)==true)

     {

      Lts = OrderLots();

     }

When selecting by ticket, MODE_TRADES is ignored, so you could be selecting an order that has already been closed (hence invalid ticket)

if(OrderCloseTime()!=0)        // if order is closed

You must select the order first.


How do you know that it is a buy order?

 

hello , thank you kindly for your answer. but this cant be the problem. why in Alert it returns the correct ticket number?

and also in my EA Ticket is the global variable of TicketNumber() , and since it is open Ticket works in all the EA. and it becomes zero after the order is closed. so it cant choose an old order ticket.

i have searched for answer for days. it cant be that simple. if you have another idea why it dose not work please tell me.

also for the coders who think i have wright Bid for all close orders, i should say that this is just for closing Buy orders. so it cant be that as well.

 
kaith watford i apologize for not seeing the last line that you wrote. i know this is buy order becaus before this function their is a navigation function that guides the Buy and Sell orders to its StopTrail function and if the indicators gives the exit signal, control will be guided to this function.
 
kriss.ro #:


hello , thank you kindly for your answer. but this cant be the problem. why in Alert it returns the correct ticket number?

We can only see your code. We do not know from that whether it is the correct ticket or not.

and also in my EA Ticket is the global variable of TicketNumber() , and since it is open Ticket works in all the EA. 

What is  TicketNumber() ?

In your code Ticket may be a global scope variable, but you possibly assign it a new value.

Ticket = OrderTicket();

As I have already said, you must select an order first. We don't know what the last selected order may be.

and it becomes zero after the order is closed. so it cant choose an old order ticket.

Does it? I don't see that in your code.

if(OrderCloseTime()!=0)        // if order is closed
            OrderCode=0;

You assign zero to a variable named OrderCode, not to Ticket.  (As I have already said, you must select the order first.)

i have searched for answer for days. it cant be that simple. if you have another idea why it dose not work please tell me.

Address the issues that I have mentioned first.

 

ok, i have tested your solution and it took me a while to make sure that it works.

i chose the orderTicket() first and then i put the the OrdrTicket() itself in the OrderClose(). i added a RefreshRates() at the begining of the function.

i also changed the Slippage from 3 to 5. and between 2 times of trying to close an order, i have put a 3 second Sleep() before the RefreshRates().

when i posted this problem, i had 2 issues. errors: invalid Ticket and invalid price.

the results of these changes: it works. your solution fixed the invalid Ticket error and RefreshRates() fixed the invalid Price error.

thank you for your help. i owe you one.

Reason: