The error 4108 could kill me!!!

 

Hi everybody,

Please note that I have a great work coded in MT4. But I have this puzzle of the error 4108 as you can see below

4108 

This error I have searched everywhere in this forum and others' forums but I found no one have totally got the final answer and was happy!

all people says to don't select orders by using ++ in the for loop as below but use --

i++

   for(int i=0; i<=OrdersTotal(); i++)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(..
                ..
                ..
                ..
                        ..
                        ..
                        ..
   }

i--

   for(int i=OrdersTotal()-1; i>=0; i--)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(..
...
...
...
        }

..

but I am using the second "i--" in looping

and still I have 4108 error!

someone advised me to try the following condition after the for loop:

      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;

so, I tried and as below and still the 4108 error coming!!

   for(int i=OrdersTotal()-1; i>=0; i--)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(..
...
..
.
   }


So, what is the final solution?!

nlte that the ea is trading perfectly without any problem except these error messages in the Jornals tab!

 

try counting up.

for(int order = 0; order <= OrdersTotal() - 1; order++)
{
   bool select = OrderSelect(order,SELECT_BY_POS);
   if(select)
   {
        // your code
   }
}
 
Mohammad Soubra:

Hi everybody,

Please note that I have a great work coded in MT4. But I have this puzzle of the error 4108 as you can see below

 

This error I have searched everywhere in this forum and others' forums but I found no one have totally got the final answer and was happy!

all people says to don't select orders by using ++ in the for loop as below but use --

i++

i--

..

but I am using the second "i--" in looping

and still I have 4108 error!

someone advised me to try the following condition after the for loop:

so, I tried and as below and still the 4108 error coming!!


So, what is the final solution?!

nlte that the ea is trading perfectly without any problem except these error messages in the logs tab!

I think the order is already closed or you are trying to delete an opened order.(active order)
You have to close opened orders not delete.

This can be fixed by checking for OrderCloseTime() and Order type()
 

You can not close a pending order with OrderClose() function.

Use OrderDelete() in stead.

 
Lakshan Perera:
I think the order is already closed or you are trying to delete an opened order.(active order)
You have to close opened orders not delete.

This can be fixed by checking for OrderCloseTime() and Order type()
Marco vd Heijden:

You can not close a pending order with OrderClose() function.

Use OrderDelete() in stead.

yes I know!

Please see the below used code for delete the pending orders or close opened orders

//+------------------------------------------------------------------+
//| CLOSE ALL OPENED POSITIONS
//+------------------------------------------------------------------+
void CloseAllOpened(int type)
{

   for(int i=OrdersTotal()-1; i>=0; i--)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
      if(OrderSelect(i,SELECT_BY_POS))
      {
         bool result;
   
         if( OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber )
         {
            switch(type)
            {
               //Close opened Buy positions
               case OP_BUY       : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage*3,clrNONE);
               break;
      
               //Close opened Sell positions
               case OP_SELL      : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage*3,clrNONE);
               break;
               
               //Close opened Buy-Stop positions
               case OP_BUYSTOP   : result = OrderDelete(OrderTicket());
               break;
   
               //Close opened Sell-Stop positions
               case OP_SELLSTOP  : result = OrderDelete(OrderTicket());
               break;
               
               //Close opened Buy-Limit positions
               case OP_BUYLIMIT  : result = OrderDelete(OrderTicket());
               break;
   
               //Close opened Sell-Limit positions
               case OP_SELLLIMIT : result = OrderDelete(OrderTicket());
            }
         }
      }
   }

}

also I am checking the count of the orders before trying to delete them 

so, if they are greater than 0 then it will continue the code commands..

 
Mohammad Soubra:

yes I know!

Please see the below used code for delete the pending orders or close opened orders

Yes but this doesn't check whether the order is already deleted or closed
Use OrderCloseTime(), 
if this is 0 it means the order is still opened, then only use OrderClose() for opened orders and OrderDelete () for pending orders
 

Try something like this

//+------------------------------------------------------------------+
//| CLOSE ALL OPENED POSITIONS
//+------------------------------------------------------------------+
void CloseAllOpened(int type)
{
   if(OrdersTotal()==0) return;
   
   for(int i=OrdersTotal()-1; i>=0; i--)
   {
      if(!OrderSelect(i,SELECT_BY_POS)) continue;
      if(OrderCloseTime() != 0) continue;
      if(OrderSymbol() != Symbol()) continue;
      if(OrderType() != type) continue;	//Added later
      if(OrderMagicNumber() != MagicNumber) continue;
      
      
      bool result;
      switch(type)
      {
         //Close opened Buy positions
         case OP_BUY       : result = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage*3,clrNONE);
         break;

         //Close opened Sell positions
         case OP_SELL      : result = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage*3,clrNONE);
         break;
         
         //Close opened Buy-Stop positions
         case OP_BUYSTOP   : result = OrderDelete(OrderTicket());
         break;

         //Close opened Sell-Stop positions
         case OP_SELLSTOP  : result = OrderDelete(OrderTicket());
         break;
         
         //Close opened Buy-Limit positions
         case OP_BUYLIMIT  : result = OrderDelete(OrderTicket());
         break;

         //Close opened Sell-Limit positions
         case OP_SELLLIMIT : result = OrderDelete(OrderTicket());
      }
         
      
   }

}
 
Lakshan Perera:

Try something like this

still giving error 4108

:(

 
Mohammad Soubra:

yes I know!

Please see the below used code for delete the pending orders or close opened orders

also I am checking the count of the orders before trying to delete them 

so, if they are greater than 0 then it will continue the code commands..

Counting up Or counting down has nothing to do with your problem your problem comes from the fact that you do not consider ordertype before calling switch. So in the end EA tries to close pending order with rules for closing active orders and vice versa

Your final code should look like this

void CloseAllOpened(int type)
{

   for(int i=OrdersTotal()-1; i>=0; i--)
   {
      if(OrderSelect(i,SELECT_BY_POS))
      { if(OrderType() != type || OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber)
         continue;      
         bool result;
            switch(type)
            {
               //Close opened Buy positions
               case OP_BUY       : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage*3,clrNONE);
               break;
      
               //Close opened Sell positions
               case OP_SELL      : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage*3,clrNONE);
               break;
               
               //Close opened Buy-Stop positions
               case OP_BUYSTOP   : result = OrderDelete(OrderTicket());
               break;
   
               //Close opened Sell-Stop positions
               case OP_SELLSTOP  : result = OrderDelete(OrderTicket());
               break;
               
               //Close opened Buy-Limit positions
               case OP_BUYLIMIT  : result = OrderDelete(OrderTicket());
               break;
   
               //Close opened Sell-Limit positions
               case OP_SELLLIMIT : result = OrderDelete(OrderTicket());
            }
         
      }
   }

}
 
Chidera Anakpe:

Counting up Or counting down has nothing to do with your problem your problem comes from the fact that you do not consider ordertype before calling switch. So in the end EA tries to close pending order with rules for closing active orders and vice versa

Your final code should look like this

Yes that is true, I also just realized it, added to my code above.

 
mmmmmm
I think this is the correct code 
Thank you Lakshan, Perera and all
I am not near the laptop current moment

I will check when i am back again

Reason: