Download MetaTrader 5
To add comments, please log in or register
Your broker does not support mobile MetaTrader 5? Ask him to buy them!
moonsboy
889
moonsboy 2014.12.04 09:10 

Dears,

 

Please refer to the script below.

When 'total' is over 1, all open orders must be closed with the script almost at the same time.  

However, when I tested it in the strategy tester, you can see the 2 orders below were closed with so long time gap in the screenshot/printing below.

Besides, how can 'total' and 'slipppage' change as they are within the "for()" cycle?

Please note that I didn't remove any word or sentence from the script.  

What's the root cause? How to fix it? Thanks a lot.  

 

 for(int i=0;i<total;i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue;
      if(OrderSymbol()!=Symbol()) continue;
      if(OrderTicket()<10) Print("totalOrders: ",totalOrders,"  total: ",total,"  ticket: ",OrderTicket(),"  Slip: ",slippage);
      if(OrderType()==OP_BUY)
         if(!OrderClose(OrderTicket(),OrderLots(),Bid,slippage)) Print("CloseBuy error-",GetLastError());
      if(OrderType()==OP_SELL)
         if(!OrderClose(OrderTicket(),OrderLots(),Ask,slippage)) Print("CloseSell error-",GetLastError());
     }

 

2014.12.04 17:03:45.236 2013.11.14 12:00  BB_Trend XAUUSD,H4: close #3 sell 0.19 XAUUSD at 1273.930 at price 1290.070
2014.12.04 17:03:45.236 2013.11.14 12:00  BB_Trend XAUUSD,H4: totalOrders: 1  total: 1  ticket: 3  Slip: 8
2014.12.04 17:03:45.095 2013.11.13 16:50  BB_Trend XAUUSD,H4: close #2 sell 0.12 XAUUSD at 1292.123 at price 1280.998
2014.12.04 17:03:45.095 2013.11.13 16:50  BB_Trend XAUUSD,H4: totalOrders: 2  total: 2  ticket: 2  Slip: 6
Carl Schreiber
5930
Carl Schreiber 2014.12.04 09:22  

That's why you should count down you loop:

int i=total;
while (i>0) { i--;
 ...
}
moonsboy
889
moonsboy 2014.12.04 09:36  
gooly:

That's why you should count down you loop:


Dear Gooly, it did work with your "while()". Why? 

Why can't use "for()"? Sorry to say "I can't find any difference between your 'while()' and my 'for()'".

However, the testing approved you are correct! So I'm confused why? Could you please help tell? 

Keith Watford
Moderator
7873
Keith Watford 2014.12.04 10:08  

It is because you are selecting by position.

When you count up in the loop and there are 2 orders open, you select the order at position 0 first. When you close that order, there is only 1 open order at position 1. This order is now moved to position 0 to fill the gap.

The next orderselect is for position 1 and that no longer exists as the order position has moved, so no order is selected.

Count down and no such problem 

whroeder1
13593
whroeder1 2014.12.04 14:01  
gooly: That's why you should count down you loop:
You must count down when closing/deleting in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
moonsboy
889
moonsboy 2014.12.05 02:37  
Thank you all for help!
/
To add comments, please log in or register