Download MetaTrader 5
To add comments, please log in or register
Publish your programs in the Market. Make money with your knowledge!
MrChuckW
398
MrChuckW 2016.09.26 04:37 
//+-------- 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
7897
Keith Watford 2016.09.26 05:15  

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
13602
whroeder1 2016.09.27 12:23  
  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
398
MrChuckW 2016.09.28 02:24  
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