Download MetaTrader 5

close a group of open orders

To add comments, please log in or register
MrChuckW
401
MrChuckW  
//+-------- if the series meets profit targets... close the series--------------------------------------------------------+

            if(((profit > target_profit)) )                                                       {   
              for(cnt=0;cnt<total;cnt++)     {
                  if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)){ 
                    while(IsTradeContextBusy()) Sleep(100);
                     ordr_symbol = OrderSymbol();
                      order_type = OrderType()  ;
                    order_ticket = OrderTicket();
                        lots_1   = OrderLots()  ;

            if ((ordr_symbol==Symbol())) {
                  while(IsTradeContextBusy()) Sleep(100);
               if(OrderType()==OP_SELL) {ticket =OrderClose(OrderTicket(),lots_1,Ask,150,Red);}
               if(OrderType()==OP_BUY)  {ticket =OrderClose(OrderTicket(),lots_1,Bid,150,Red);}
                                         }
                                              }
                                                                   }
                                                                                                   }

My EA opens a series of orders.  When collective profits hit a certain target, it does this routine.

However, it leaves one order still open.   In the middle of the pack no less.

I put the slipage to an unearthly high number hoping that would help... didn't .

See anything wrong?    Anything else I can do to fore the closing of all orders? 

Keith Watford
Moderator
10570
Keith Watford  

Don't count up when closing orders.

If Sleep() Refreshrates() to get up to date Bid and Ask

for(cnt=0;cnt<total;cnt++)
change to
for(cnt=total-1;cnt>=0;cnt--)   
whroeder1
16274
whroeder1  
  1. You must RefreshRates after Sleep and between multiple server calls (OrderClose).
  2. In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading) you must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
  3. You don't have to test the OrderType, just use OrderClosePrice instead of Bid/Ask.
MrChuckW
401
MrChuckW  
GumRai:

Don't count up when closing orders.

If Sleep() Refreshrates() to get up to date Bid and Ask

Thanks... works great now
To add comments, please log in or register