Close 2 Trades

 

Hi guys,


i have to trades open with the same Symbol, Comment, Trading direction and i'm trying to close both but it only works once. The second trade is still open.

Is there anything wrong?

int closeTrade(string TD)
  {
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; // falls Zeile leer
      if(OrderSymbol()==Symbol() && OrderComment()=="Hull") // Symbol
        {                                  
         if(OrderType()==OP_BUY && TD=="B") {OrderClose(OrderTicket(),OrderLots(),Bid,3,Red); Alert(Symbol()," Trade close long"); }  //Offene Position Long
         if(OrderType()==OP_SELL && TD=="S"){OrderClose(OrderTicket(),OrderLots(),Ask,3,Red); Alert(Symbol()," Trade close short"); } //Offene Position Short
        }
     }
  }
 
FamWue:

Hi guys,


i have to trades open with the same Symbol, Comment, Trading direction and i'm trying to close both but it only works once. The second trade is still open.

Is there anything wrong?

You must count down in your for loop  NOT up:  Loops and Closing or Deleting Orders
 
FamWue: i have to trades open with the same Symbol, Comment, Trading direction and i'm trying to close both but it only works once.
  1. Count down.
  2. Brokers can change comments, including complete replacement.
  3. No need for your TD
    Your TD
    int closeTrade(string TD){
       :
       if(OrderType()==OP_BUY && TD=="B") {OrderClose(OrderTicket(),OrderLots(),Bid,3,Red)..
       if(OrderType()==OP_SELL && TD=="S"){OrderClose(OrderTicket(),OrderLots(),Ask,3,Red)..
    Simplify
    #define OP_ALL -1
    int closeTrade(int op=OP_ALL){
       :
       if(op==OP_ALL || OrderType()==op) {
          OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Red)..
    

  4. No need for Bid/Ask on close, use OrderClosePrice. (see above)
  5. EAs must adjust for 4/5 digit brokers, TP, SL, AND slippage
    //++++ These are adjusted for 5 digit brokers.
    int      pips2points;                  // slippage  3 pips  3=points 30=points
    double   pips2dbl;                     // Stoploss 15 pips  0.0015   0.00150
    int      Digits.pips;                  // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int      init(){                                            OptInitialization();
        if(Digits % 2 == 1){   // DE30=1/JPY=3/EURUSD=5 forum.mql4.com/43064#515262
                pips2dbl = Point*10;    pips2points = 10;    Digits.pips = 1;
       } else { pips2dbl = Point;       pips2points =  1;    Digits.pips = 0;     }
       // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    //---- These are adjusted for 5 digit brokers.
       //{On ECN brokers you must open first and THEN set stops
       // int      ticket = OrderSend(..., 0,0,...)
       // if(ticket < 0)
       //    Alert("OrderSend failed: ", GetLastError());
       // else  if(!OrderSelect(ticket, SELECT_BY_TICKET))
       //    Alert("OrderSelect failed: ", GetLastError());
       // else  if(!OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0))
       //    Alert("OrderModify failed: ", GetLastError());
       //}
    

Reason: