try counting up.
for(int order = 0; order <= OrdersTotal() - 1; order++) { bool select = OrderSelect(order,SELECT_BY_POS); if(select) { // your code } }
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!
You can not close a pending order with OrderClose() function.
Use OrderDelete() in stead.
I think the order is already closed or you are trying to delete an opened order.(active order)
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..
yes I know!
Please see the below used code for delete the pending orders or close opened 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()); } } }
Try something like this
still giving error 4108
:(
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()); } } } }
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.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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 Jornals tab!