Error 4200 when closing order

 

Hi,

I0m trying to code an EA which close all orders when a profit is reached on the whole account. The problem is with some orders (not all) the Journal says: ALert: order ... failed to close. Error 4200. And order is not closed, and at every tick the same alert.

This error is for "object already exixts": what could be the meaning for closing an order?

Thank you very much

 
Show the code for closing the order and the error report
 

Yes.

Do you mean the error shown in Journal? Is ALert: order ... failed to close. Error 4200.


The aim is calulating number of orders, pending or opened, and close all orders in some conditions; I'm trying several combinaitons, so this is a sample code:


   int order_total_buy=0;
   int order_total_sell=0;
   int total_mercato_buy=0;
   int total_mercato_sell=0;
   int totali_mercato=0;
  
   for(int cnt=0;cnt<OrdersTotal();cnt++)
   {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if ((OrderType()==OP_BUY) && OrderSymbol()==Symbol())
      {total_mercato_buy++;}
     
      if ((OrderType()==OP_SELL) && OrderSymbol()==Symbol())
      {total_mercato_sell++;}
     
      if ((OrderType()==OP_BUYSTOP) && OrderSymbol()==Symbol())
      {order_total_buy++;}
     
      if ((OrderType()==OP_SELLSTOP) && OrderSymbol()==Symbol())
      {order_total_sell++;}
     
   }



     
      totali_mercato=total_mercato_buy+total_mercato_sell;
      if (  (OrdersTotal()==1 && total_mercato_buy==1) || (OrdersTotal()==1 && total_mercato_sell==1)         ||((order_total_buy>order_total_sell)&&totali_mercato==0) || ((order_total_sell>order_total_buy)  && totali_mercato==0)           || (order_total_buy==0 && order_total_sell>0) || (order_total_sell==0 && order_total_buy>0))
      {


           int total = OrdersTotal();
           for(int i=total-1;i>=0;i--)
           {
             OrderSelect(i, SELECT_BY_POS);
             int type   = OrderType();
        
             bool result = false;
            
             switch(type)
             {
               //Close pending orders
               case OP_BUYLIMIT  :
               case OP_BUYSTOP   :
               case OP_SELLLIMIT :
               case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );

             }
            
             if(result == false)
             {
               Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
               Sleep(3000);
             } 
           }
           Print ("Chiudo tutti gli ordini");


      }
 
There was no error in your run, so the GetLastError() reported some pending result.
 
Ovo:
There was no error in your run, so the GetLastError() reported some pending result.

Sorry, due to my bad english I don't understand well...can you reformulate "reported some pending result"?.

thank you very very much

 
pipcraftsman:

Sorry, due to my bad english I don't understand well...can you reformulate "reported some pending result"?.

thank you very very much

I meant it was not yet reset.

 

GetLastError

Returns the contents of the system variable _LastError.

int  GetLastError();

Returns the value of the last error that occurred during the execution of an mql4 program.

Note

After the function call, the contents of _LastError are reset.

 

So,

I reset Journal, and I start with my code: I post the whole code, and then I post the journal report after some days. I am in backtest! It seems it can't close some orders and I don't understand why.

//+------------------------------------------------------------------+
//|                                                         expe.mq4 |
//|                                                               Io |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Io"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
extern double lot = 0.01;
extern double profit;
extern double distance=10; //distanza

extern int ncicli = 2;
int ii;
double spread = NormalizeDouble(Ask-Bid, Digits);           
double tp = (1* profit*Point*10)-spread;
double d =  (1* distance*Point*10);
double distanza=d;
double lotti = lot;

int init()
{

return(0);

}


int start()



   {
  
   /*parte di chiusura ordini aperti o pendenti*/
   
   int order_total_buy=0;
   int order_total_sell=0;
   int total_mercato_buy=0;
   int total_mercato_sell=0;
   int totali_mercato=0;
   
   for(int cnt=0;cnt<OrdersTotal();cnt++)
   {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if ((OrderType()==OP_BUY) && OrderSymbol()==Symbol()) 
      {total_mercato_buy++;}
      
      if ((OrderType()==OP_SELL) && OrderSymbol()==Symbol()) 
      {total_mercato_sell++;}
      
      if ((OrderType()==OP_BUYSTOP) && OrderSymbol()==Symbol()) 
      {order_total_buy++;}
      
      if ((OrderType()==OP_SELLSTOP) && OrderSymbol()==Symbol()) 
      {order_total_sell++;}
      
   }
            ObjectCreate("ObjName", OBJ_LABEL, 0, 0, 0);
            ObjectSetText("ObjName",total_mercato_buy,17, "Verdana", Red);
            ObjectSet("ObjName", OBJPROP_CORNER, 1);
            ObjectSet("ObjName", OBJPROP_XDISTANCE, 20);
            ObjectSet("ObjName", OBJPROP_YDISTANCE, 20);
            
            ObjectCreate("time", OBJ_LABEL, 0, 0, 0);
            ObjectSetText("time",OrdersTotal(),17, "Verdana", Red);
            ObjectSet("time", OBJPROP_CORNER, 1);
            ObjectSet("time", OBJPROP_XDISTANCE, 20);
            ObjectSet("time", OBJPROP_YDISTANCE, 60);


      
      totali_mercato=total_mercato_buy+total_mercato_sell;
      if (  (OrdersTotal()==1 && total_mercato_buy==1) || (OrdersTotal()==1 && total_mercato_sell==1)         ||   ((order_total_buy>order_total_sell)&&totali_mercato==0) || ((order_total_sell>order_total_buy)  && totali_mercato==0) || (order_total_buy==0 && order_total_sell>0) || (order_total_sell==0 && order_total_buy>0))
      {


           int total = OrdersTotal();
           for(int i=total-1;i>=0;i--)
           {
             OrderSelect(i, SELECT_BY_POS);
             int type   = OrderType();
         
             bool result = false;
             
             switch(type)
             {
               //Close pending orders
               case OP_BUYLIMIT  :
               case OP_BUYSTOP   :
               case OP_SELLLIMIT :
               case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
             }
             
             if(result == false)
             {
               Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
               Sleep(3000);
             }  
           }
           Print ("Chiudo tutti gli ordini");


      }


   
   
   /*parte di paertura nuovi ordini*/

   if (OrdersTotal()==0)//quando non c'è più nessun ordine aperto riparti

      {
         

          for(int ciclo=1;ciclo<=ncicli;ciclo++)
          {
        
       

         OrderSend(Symbol(), OP_BUYSTOP, lotti, NormalizeDouble(Ask+distanza,Digits), 0,  0,NormalizeDouble(Ask+distanza+tp,Digits), "eva", 0, 0, Green);
         OrderSend(Symbol(), OP_SELLSTOP, lotti, NormalizeDouble(Bid-distanza,Digits), 0,  0,NormalizeDouble(Bid-distanza-tp,Digits), "eva", 0, 0, Green);

        if (ciclo==ncicli)
        {
        distanza=d;
        lotti = lot;
        }
        else
        {
        distanza=distanza+d;
        lotti = lotti+lot;
        }
        
        

          } // chiude ciclo ordini
      }
   
   
   Comment("Balance: ",AccountBalance(),", Account Equity: ",AccountEquity(),", Account Profit: ",AccountProfit(),
           "\nMy Account Profit Target: ","\nMinuti: ",TimeCurrent());
   //Comment (order_total_buy);
   return (0);
   }
2016.02.09 14:16:59.614 2015.12.02 20:18  expe EURUSD,M1: Chiudo tutti gli ordini
2016.02.09 14:16:59.614 2015.12.02 20:18  expe EURUSD,M1: Alert: Order 131 failed to close. Error:4200
2016.02.09 14:16:59.614 2015.12.02 20:18  expe EURUSD,M1: Chiudo tutti gli ordini
2016.02.09 14:16:59.614 2015.12.02 20:18  expe EURUSD,M1: Alert: Order 131 failed to close. Error:4200
2016.02.09 14:16:59.614 2015.12.02 20:17  expe EURUSD,M1: Chiudo tutti gli ordini
2016.02.09 14:16:59.614 2015.12.02 20:17  expe EURUSD,M1: Alert: Order 131 failed to close. Error:4200
2016.02.09 14:16:59.614 2015.12.02 20:17  expe EURUSD,M1: Chiudo tutti gli ordini
2016.02.09 14:16:59.614 2015.12.02 20:17  expe EURUSD,M1: Alert: Order 131 failed to close. Error:4200
2016.02.09 14:16:59.614 2015.12.02 20:17  expe EURUSD,M1: Chiudo tutti gli ordini
2016.02.09 14:16:59.614 2015.12.02 20:17  expe EURUSD,M1: Alert: Order 131 failed to close. Error:4200
2016.02.09 14:16:59.614 2015.12.02 20:17  expe EURUSD,M1: Chiudo tutti gli ordini
2016.02.09 14:16:59.614 2015.12.02 20:17  expe EURUSD,M1: Alert: Order 131 failed to close. Error:4200
2016.02.09 14:16:59.614 2015.12.02 20:17  expe EURUSD,M1: Chiudo tutti gli ordini
2016.02.09 14:16:59.614 2015.12.02 20:17  expe EURUSD,M1: Alert: Order 131 failed to close. Error:4200
2016.02.09 14:16:59.614 2015.12.02 20:17  expe EURUSD,M1: Chiudo tutti gli ordini
[...]
2016.02.09 14:16:59.609 2015.12.02 20:17  expe EURUSD,M1: Chiudo tutti gli ordini
2016.02.09 14:16:59.609 2015.12.02 20:17  expe EURUSD,M1: Alert: Order 131 failed to close. Error:4200
2016.02.09 14:16:59.609 2015.12.02 20:17  expe EURUSD,M1: Chiudo tutti gli ordini
2016.02.09 14:16:59.609 2015.12.02 20:17  expe EURUSD,M1: Alert: Order 131 failed to close. Error:4200
2016.02.09 14:16:59.609 2015.12.02 20:17  expe EURUSD,M1: Chiudo tutti gli ordini
2016.02.09 14:16:59.609 2015.12.02 20:17  expe EURUSD,M1: Alert: Order 131 failed to close. Error:4200
2016.02.09 14:16:59.609 2015.12.02 20:17  expe EURUSD,M1: Chiudo tutti gli ordini
2016.02.09 14:16:59.609 2015.12.02 20:17  expe EURUSD,M1: Alert: Order 131 failed to close. Error:4200
2016.02.09 14:16:46.994 2015.12.01 00:01  expe EURUSD,M1: open #7 buy stop 0.04 EURUSD at 1.05841 tp: 1.05879 ok
2016.02.09 14:16:46.994 2015.12.01 00:01  expe EURUSD,M1: open #6 sell stop 0.03 EURUSD at 1.05489 tp: 1.05451 ok
2016.02.09 14:16:46.994 2015.12.01 00:01  expe EURUSD,M1: open #5 buy stop 0.03 EURUSD at 1.05791 tp: 1.05829 ok
2016.02.09 14:16:46.994 2015.12.01 00:01  expe EURUSD,M1: open #4 sell stop 0.02 EURUSD at 1.05539 tp: 1.05501 ok
2016.02.09 14:16:46.994 2015.12.01 00:01  expe EURUSD,M1: open #3 buy stop 0.02 EURUSD at 1.05741 tp: 1.05779 ok
2016.02.09 14:16:46.994 2015.12.01 00:01  expe EURUSD,M1: open #2 sell stop 0.01 EURUSD at 1.05589 tp: 1.05551 ok
2016.02.09 14:16:46.994 2015.12.01 00:01  expe EURUSD,M1: open #1 buy stop 0.01 EURUSD at 1.05691 tp: 1.05729 ok
2016.02.09 14:16:46.988 2015.12.01 00:00  expe inputs: lot=0.01; profit=4; distance=5; ncicli=5; 
2016.02.09 14:16:46.704 TestGenerator: spread set to 2
2016.02.09 14:16:46.661 Expert expe EURUSD,M1: loaded successfully


Thank you very much!
 
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. What happens if you have no pending orders? OrderDelete is not called, result is still false but you alert.
    What happens if you have multiple pending orders? You can only alert on the last attempt.
    bool result = false;
    switch(type) 
      :
      result = OrderDelete( OrderTicket() );
    }
    if(result == false){
       Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
    Do not look at GLE unless you have an error. Move your if/Alert next to the OrderDelete.
    What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
     
    switch(type) 
      :
      bool result = OrderDelete( OrderTicket() );
      if(!result){
         Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );}
    You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.

  3. Check your return codes (OrderSelect) and not filtering by magic number makes EA incompatible with all others (including itself on other TFs,) and manual trading Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum
 
WHRoeder:

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. What happens if you have no pending orders? OrderDelete is not called, result is still false but you alert.
    What happens if you have multiple pending orders? You can only alert on the last attempt.
    Do not look at GLE unless you have an error. Move your if/Alert next to the OrderDelete.
    What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
     You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.

  3. Check your return codes (OrderSelect) and not filtering by magic number makes EA incompatible with all others (including itself on other TFs,) and manual trading Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum

You're very kind,

I'll study your answer and I'll update post .

Thank you.

 

The error you print out comes from this statement:

ObjectCreate("time", OBJ_LABEL, 0, 0, 0); 

 
             bool result = false;
             
             switch(type)
             {
               //Close pending orders
               case OP_BUYLIMIT  :
               case OP_BUYSTOP   :
               case OP_SELLLIMIT :
               case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
             }
             
             if(result == false)
             {
               Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
               Sleep(3000);
             }  

You initiate result as false,

If the order type you are checking is  not a limit or stop, the value of result is not changed and the alert is printed with the error from ObjectCreate.

Change to

bool result = true;

 .

Reason: