Help: Why The EA does not close a trade - page 7

 

Don't you think that 20000 retrys is a bit excessive. If it fails to close 5 times in a row then it's time to quit.

It would be nice to print the error message if the close fails.

 
I'm not feeding the Troll any more . . .
 

Hi there crossy,

You right, I only see partial of your code and you probably have different plans in your mind, and surely reading 4000 lines of code doesn't help to get clear head - me too.

You said that your EA can not close a trade. So we have to find out what causing it and deal with it. This is the way I see it:

:D

#include <stdlib.mqh>   
   ...
for(int pos = OrdersTotal(); pos >= 0 ; pos--) 
   {
   if ( OrderSelect(pos, SELECT_BY_POS, MODE_TRADES) 
        && OrderMagicNumber() == My_Magic_Number
        && OrderSymbol() == Symbol())
      {
       if (OrderType() = OP_BUY) // we close long
         {
         for (int try_to_close = 1; try_to_close <= 100; try_to_close ++)
            {
            bool success = OrderClose (OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, Yellow);
            if (success = false)
               {
               int Error = GetLastError();
               Print ("Unable to close ticket ",OrderTicket()," with error ",ErrorDescription(Error)); // from #include <stdlib.mqh>
               if (TRY_CLOSE_AGAIN_OR_NOT(Error) == false ) break; // 
               }
            }
         }
         
       if (OrderType() = OP_SELL) // we close short
         {
         // do the same with sell
         
         }
      }   
   }
//+------------------------------------------------------------------+
bool TRY_CLOSE_AGAIN_OR_NOT(int err) // True
  {
  switch (err)
     {
      case 1:   return (false); //error_string="no error";                                                 
      case 2:   return (false); //error_string="common error";                                          
      case 3:   return (false); //error_string="invalid trade parameters";                                
      case 4:   Sleep (500); return (true); //error_string="trade server is busy";                               
      case 5:   return (false); //error_string="old version of the client terminal";                  
      case 6:   Sleep (500); return (true); //error_string="no connection with trade server";                 
      case 7:   return (false); //error_string="not enough rights";                                
      case 8:   Sleep (500); return (true); //error_string="too frequent requests";                                
      case 9:   return (false); //error_string="malfunctional trade operation (never returned error)"; 
      case 64:  return (false); //error_string="account disabled";                        
      case 65:  return (false); //error_string="invalid account";                   
      case 128: Sleep (500); return (true); //error_string="trade timeout";                                    
      case 129: return (false); //error_string="invalid price";                               
      case 130: return (false); //error_string="invalid stops";                          
      case 131: return (false); //error_string="invalid trade volume";                 
      case 132: return (false); //error_string="market is closed";                      
      case 133: return (false); //error_string="trade is disabled";                       
      case 134: return (false); //error_string="not enough money";                   
      case 135: return (false); //error_string="price changed";                         
      case 136: return (false); //error_string="off quotes";                              
      case 137: Sleep (500); return (true); //error_string="broker is busy (never returned error)";    
      case 138: return (false); ////error_string="requote";                                 
      case 139: return (false); //error_string="order is locked";                           
      case 140: return (false); //error_string="long positions only allowed";                 
      case 141: return (false); //error_string="too many requests";                               
      //case 145: return (true); //error_string="modification denied because order too close to market";  
      case 146: Sleep (500); return (true); //error_string="trade context is busy";                              
      //case 147: return (true); error_string="expirations are denied by broker";                    
      //case 148: return (true); error_string="amount of open and pending orders has reached the limit"; 
      //case 149: return (true); error_string="hedging is prohibited";                                   
      case 150: return (false); //error_string="prohibited by FIFO rules";  
      default : return (false);         
     }
     
  return(false); // can not manage the err we get out
  }
 

So, You have a problem, that NOT ALL orders are closed. This code, You've pasted - WILL NEVER close ALL orders. So if You want some help - paste ALL the code witch's working with order CLOSING. There's no need to paste all You code (I understand You want it to keep secret), but You'll never get help without pasting part of the code, witch's involved in the problem (in this case - there's NO code for SHORT order closing).

 
onewithzachy:

Hi there crossy,

You right, I only see partial of your code and you probably have different plans in your mind, and surely reading 4000 lines of code doesn't help to get clear head - me too.

You said that your EA can not close a trade. So we have to find out what causing it and deal with it. This is the way I see it:

:D


Thanks onewithzachy for this idea. It is useful anywhere as it is here.

Please let it work for another few hours, and I promise to update you.

Thanks Raptor, too, for his nice contribution.

Y.

 

Hello,

As I promised, after 6 hours the the EA works, everything works properly, thanks to your

good thinking.

So, meanwhile let's go ahead and I hope that there will not be any truboles.

Y.

 
crossy:

Hello,

As I promised, after 6 hours the the EA works, everything works properly, thanks to your

good thinking.

So, meanwhile let's go ahead and I hope that there will not be any truboles.

Y.

Great !,Please update us again in a weeks

:D