MQL4 >#600: OrderClose() not working but sometimes with the SAME code without a special notice

 

Hello again,

that tiny thing is playing games with me... I have reduced the code to the mimium in a special EA and I can reproduce the failure with that.

I stack a ticke# in the code and when the system is going to trade the oposite direction, I tried to Close the order with the stacked number. Over night the code is ordering but not closing anything. (Demo-Account) Two orders ha been closed. And this morning, I can`t close the orders with any kind of code but by hand.

So here it is:

   bool what;
   int ret;
   int ticket = 63242872;

   double price=NormalizeDouble(Ask, 4);
   what = OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES);
   if (what) Print("Found the Order without trouble, now say goodbye");
   double lot = OrderLots();
   what = false;
   
   if (IsTradeAllowed())
   {
      Print("SCRIPT - Order# " + (string)ticket + " will close at: " + (string)price);

      ret = OrderClose(ticket, lot, price, 2, clrBrown);
      // ret = OrderDelete(ticket);
      Print((string)ret);
      if (ret < 0) Print("!!!!!!!! -> Failure on CLOSE: " + (string)GetLastError());
   }
   else {Print("Idiot - Trading is not allowed yet");};
   
   what = OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES);
   if (what) Print("But... ! - Order is still alive, Jippieh :-(");

The system gave me this answers:

0    09:03:28.796    Killdas EURUSD,M5: Found the Order without trouble, now say goodbye
0    09:03:28.796    Killdas EURUSD,M5: SCRIPT - Order# 63177979 will close at: 1.1243
0    09:03:28.796    Killdas EURUSD,M5: 0
0    09:03:28.796    Killdas EURUSD,M5: But... ! - Order is still alive, Jippieh :-(

In the code it has to work over night, it gave the same notices of success. But mostly it did nothing with the running orders. (I restarted and that...)

So please, where do I have to look now ?

 
   what = OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES);
   if (what) Print("Found the Order without trouble, now say goodbye"); <<< where is the error processing?
      <<<<<<<If you select by ticket you must test for the order has already close. mode_trades is ignored  
   double lot = OrderLots();
   what = false;
   
   if (IsTradeAllowed())
   {
      Print("SCRIPT - Order# " + (string)ticket + " will close at: " + (string)price); << What is the value of price?

      ret = OrderClose(ticket, lot, price, 2, clrBrown);                               << Where is the error processing?
 
 
int ticket = 63242872;
//
//
 Print("SCRIPT - Order# " + (string)ticket + " will close at: " + (string)price);

//This prints  09:03:28.796    Killdas EURUSD,M5: SCRIPT - Order# 63177979 will close at: 1.1243 ????

Obviously not the same code as produces the log

 

OrderClose returns a bool

change 

int ret;
//
ret = OrderClose(ticket, lot, price, 2, clrBrown);
//
if (ret < 0) Print("!!!!!!!! -> Failure on CLOSE: " + (string)GetLastError());

 to

bool ret;
//
ret = OrderClose(ticket, lot, price, 2, clrBrown);
//
if (!ret) Print("!!!!!!!! -> Failure on CLOSE: " + (string)GetLastError());
 

Change

   what = OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES);
   if (what) Print("But... ! - Order is still alive, Jippieh :-(");

 to

   if(OrderSelect(ticket,SELECT_BY_TICKET) && OrderCloseTime()==0)
      Print("But... ! - Order is still alive, Jippieh :-(");
 

@all: thank you very much for your answers !

@WHRoeder:

1.redrow - right, but I`ve always got a true on that. (checked debugging)

2.redrow - right again. I've not checked if the order is already historic. But - please correct me if I`m wrong - the worst thing happen is, I try to terminate a order which is historic. This orders ARE active - and fight for their lifes...

@GumRai:

ticket# :-) - you are right but it is only a copy and paste problem. I've hundrets of log-rows. All the same.

I've done your suggested changings like that:

   bool ret;
   int ticket = 63177049;

   double price = NormalizeDouble(Ask, 4);   // Type is SHORT = 1
   
   ret = OrderSelect(ticket,SELECT_BY_TICKET);
   if (ret) Print("Found the Order without trouble, now say goodbye"); else {Print("Order not selectable by ID: " + (string)GetLastError());};
   double lot = OrderLots();
   int ty = OrderType();
   datetime ct = OrderCloseTime();
   Print("The Order is selected with: (Lot)" + (string)lot + " - (Type)" + (string)ty + "(CloseTime)" + ct);
   
   if (IsTradeAllowed())
   {
      Print("SCRIPT - Order# " + (string)ticket + " will close at: " + (string)price);

      ret = OrderClose(ticket, lot, price, 40, clrBrown);
      // ret = OrderDelete(ticket);
      if (!ret) Print("!!!!!!!! -> Failure on CLOSE: " + (string)GetLastError());
   }
   else {Print("Idiot - Trading is not allowed yet");};
   
   if(OrderSelect(ticket,SELECT_BY_TICKET) && OrderCloseTime()==0)
      Print("But... ! - Order is still alive, Jippieh :-(");



At first I got the Error 129 (wrong price) But the price was valide. (?) 

Then I changed to slippage 40. And this works !!


Is it possible, that MT4 is working with other quotes than I get from FXCM. The Chart shows much greater spread than the broker now uses for trading ???

And another miracle: With the same price I tried to terminate a order, I was able to book a order in the other direction ???



But, this is a solution ! Thanks a lot
 

You may now consider putting your code in a more logical order. As it is, if the OrderSelect fails, it will still attempt to close a trade

Suggestion...

   //bool ret;
   int ticket = 63177049;

   //double price = NormalizeDouble(Ask, 4);   // Type is SHORT = 1
   
   if(OrderSelect(ticket,SELECT_BY_TICKET))
      {
      if (IsTradeAllowed())
         {
         if(OrderCloseTime()!=0)
         Print("Found the Order without trouble, now say goodbye");   
         double lot = OrderLots();
         int ty = OrderType();
         datetime ct = OrderCloseTime();
         double price = OrderClosePrice();
         Print("The Order is selected with: (Lot)" + (string)lot + " - (Type)" + (string)ty + "(CloseTime)" + ct);
         Print("SCRIPT - Order# " + (string)ticket + " will close at: " + (string)price);
         if(!OrderClose(ticket, lot, price, 40, clrBrown))
            Print("!!!!!!!! -> Failure on CLOSE: " + (string)GetLastError());
         if(OrderSelect(ticket,SELECT_BY_TICKET) && OrderCloseTime()==0)
            Print("But... ! - Order is still alive, Jippieh :-(");
         }
      else 
         Print("Idiot - Trading is not allowed yet");
      } 
   else
      Print("Order not selectable by ID: " + (string)GetLastError());
      
   
 
GumRai:

You may now consider putting your code in a more logical order. As it is, if the OrderSelect fails, it will still attempt to close a trade

Suggestion...

 

Hello and thank you for your Suggestion. Of course, your code looks beautiful.

My sloppy attempt should only show me where the problems when placing the order are. And I`ve had a lot more in the last days.

At the end I found a code in a grid-trader. That guy spend 1200 Lines of code only for sending, closing and changing orders in a reliable way ! (counted loops and so on)

Sad, but it`s not as easy as I thought.


Thanks again !

Reason: