HELP!!!! EA Doesn't Close Any Trades!!!! What's Wrong With My Code????

Kendall Sands
31
Kendall Sands  

Help coders, my EA has no trouble opening trades but doesn't close any. I suspect the trouble is in my OrderSelect loop but i can't figure out what's wrong. HELP!!!!

//+------------------------------------------------------------------+
            //| Signal Begin(Exit Buy)                                           |
            //+------------------------------------------------------------------+

               for (int x = OrdersTotal() - 1; x >= 0; x--)
               if (OrderSelect(x, SELECT_BY_POS, MODE_TRADES))
               {
                   if (OrderType() == OP_BUY 
                       && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol())
                       bool CloseBuyOrder = true;
                       }
                       
                       
                       if ((CloseBuyOrder == true) && (CloseBuy1_1 < CloseBuy1_2 && CloseBuy2_1 >= CloseBuy2_2 && CloseBuy3_1 > CloseBuy3_2)) Order = SIGNAL_CLOSEBUY;          


            //+------------------------------------------------------------------+
            //| Signal End(Exit Buy)                                             |
            //+------------------------------------------------------------------+

            if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
               OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);
               if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");
               if (!EachTickMode) BarCount = Bars;
               IsTrade = False;
               continue;
            }
            //Trailing stop
            if(UseTrailingStop && TrailingStop > 0) {                 
               if(Bid - OrderOpenPrice() > Point * TrailingStop) {
                  if(OrderStopLoss() < Bid - Point * TrailingStop) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);
                     if (!EachTickMode) BarCount = Bars;
                     continue;
                  }
               }
            }
         } else {
            //Close

            //+------------------------------------------------------------------+
            //| Signal Begin(Exit Sell)                                          |
            //+------------------------------------------------------------------+

               for (int y = OrdersTotal(); y >= 0; y--)
               if (OrderSelect(y, SELECT_BY_POS, MODE_TRADES))
               {
                  if (OrderType() == OP_SELL
                  && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol())
                  bool CloseSellOrder = true;
                     
                     
                     if ((CloseSellOrder == true) && (CloseSell1_1 > CloseSell1_2 && CloseSell2_1 <= CloseSell2_2 && CloseSell3_1 < CloseSell3_2)) Order = SIGNAL_CLOSESELL;
                     }      


            //+------------------------------------------------------------------+
            //| Signal End(Exit Sell)                                            |
            //+------------------------------------------------------------------+

            if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
               OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);
               if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");
               if (!EachTickMode) BarCount = Bars;
               IsTrade = False;
               continue;
            }
            //Trailing stop
            if(UseTrailingStop && TrailingStop > 0) {                 
               if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {
                  if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);
                     if (!EachTickMode) BarCount = Bars;
                     continue;
                  }
               }
            }
         }
      }
   }
Simon Gniadkowski
17392
Simon Gniadkowski  
J_Cutts:

Help coders, my EA has no trouble opening trades but doesn't close any. I suspect the trouble is in my OrderSelect loop but i can't figure out what's wrong. HELP!!!!

I suspect your suspicion is wrong.

Add some Print statements to help you see what is going on . . . we can't figure this out for you as you don't give enough info, for example: what are these ? CloseBuy1_1 < CloseBuy1_2 && CloseBuy2_1 >= CloseBuy2_2 && CloseBuy3_1 > CloseBuy3_2 and these (EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount . . . they are all involved in the decision taking involved in getting to your OrderClose . . .

By the way . . Bars is unreliable, you shouldn't use it.

William Roeder
25057
William Roeder  
  1. Always test return codes
    if (!OrderClose(...))
      Alert("OrderClose failed: ", GetLastError());
    If you don't get the message, then you're not even reaching the close.
  2.  for (int x = OrdersTotal() - 1; x >= 0; x--)
                   if (OrderSelect(x, SELECT_BY_POS, MODE_TRADES))
                   {
                       if (OrderType() == OP_BUY 
                           && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol())
                           bool CloseBuyOrder = true;
                           }
    After the first iteration, what will be the value of CloseBuyOrder when it encounters other orders (non-buy, non-MN, or non-pair?) Answer, what ever the first value was!
        for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if (
            OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
        &&  OrderType()         == OP_BUY 
        &&  OrderMagicNumber()  == Magic.Number                 // my magic number
        &&  OrderSymbol()       == chart.symbol                 // and my pair.
        ){
          // no CloseBuyOrder needed. Just close right here.

Kendall Sands
31
Kendall Sands  
WHRoeder:
  1. Always test return codes If you don't get the message, then you're not even reaching the close.
  2. After the first iteration, what will be the value of CloseBuyOrder when it encounters other orders (non-buy, non-MN, or non-pair?) Answer, what ever the first value was!


Thanks for the reply WHRoeder, with respect to point 2 in your reply, where is do I place the closing criterion (CloseBuy1_1.......)????

William Roeder
25057
William Roeder  
J_Cutts:
where is do I place the closing criterion (CloseBuy1_1.......)????
bool closeBuys = ...,
     closeSells= ...;
if (closeBuys)  CloseAllOrders(OP_BUY);
if (closeSells) CloseAllOrders(OP_SELL);
//or if (closeBuys || closeSells) CloseAllOrders();
:
void CloseAllOrders(int op = -1){
    for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
    &&  OrderMagicNumber() == Magic.Number                  // my magic number
    &&  OrderSymbol()      == chart.symbol                  // and my pair,
    &&  (op < 0 || op == OrderType())                       // and wanted type.
    ){  // Don't combine with &&'s Compiler bug.
        CloseOrder();
}   }
void CloseOrder(){
    if (!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(),
                                        Slippage.Pips*pips2points, op.color)
    ) Alert("OrderClose failed: ", GetLastError());
}