Problem Closing Single Trade

 

Hi all, I am having trouble with my EA closing single trade positions. If I have more than 1 trade open the EA works just fine and closes trades when a close signal is generated. However, if there is only 1 trade open, the EA does not close the trade no matter how many times the close signal is generated. Help will be greatly appreciated. Here is the related portion of the EA:

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

               
               int Buys = 0;
               int x = OrdersTotal();
               for (x = OrdersTotal() - 1; x < MaxOrders; x++)
                  if (OrderSelect(x, SELECT_BY_POS, MODE_TRADES)
                  && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol())
                  {
                  Buys ++;
                  }   
            
                     if (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)                                          |
            //+------------------------------------------------------------------+

               int Sells = 0;
               int y = OrdersTotal();
               for (y = OrdersTotal() - 1; y < MaxOrders; y++)
                  if (OrderSelect(y, SELECT_BY_POS, MODE_TRADES)
                  && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol())
                  {
                  Sells ++;
                  }   
                  
                  if (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;
                  }
               }
            }
         }
      }
   }
 
for (x = OrdersTotal() - 1; x < MaxOrders; x++)

instead try:

for(i=OrdersTotal()-1; i>=0; i--){
 
ubzen:

instead try:


Thanks for your reply but I actually tried that already...............when that is the code the EA doesn't close ANY trades at all
 
J_Cutts:

Thanks for your reply but I actually tried that already...............when that is the code the EA doesn't close ANY trades at all

If this

for(i=OrdersTotal()-1; i>=0; i--){

doesn't work

this

for (x = OrdersTotal() - 1; x < MaxOrders; x++)

isn't gonna work for sure never ever

 
J_Cutts:

Thanks for your reply but I actually tried that already...............when that is the code the EA doesn't close ANY trades at all
Well you're selecting by "SELECT_BY_POS" x, y, i or whatever you're using for position needs to become 0 for the lone order to be selected. I'll recommend you use line base Print( Statements ) and GetLastError(). Something else might be wrong.
 
 for (y = ..
 if (OrderSelect(y, ..
                  Sells ++;                 
:
            if (Order == SIGNAL_CLOSESELL && ..
               OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);

You've completed the for loop, nothing is selected, so why do you suppose the OrderClose Fails there?

Always test return codes, so you find out WHY.

if (!OrderClose(...))
    Alert("OrderClose failed: ", GetLastError());
 
WHRoeder:

You've completed the for loop, nothing is selected, so why do you suppose the OrderClose Fails there?

Always test return codes, so you find out WHY.


the problem isn't in closing orders because when there are multiple trades open, the EA closes trades just fine...........the problem I am having is that if there is only a SINGLE TRADE open in the terminal, it does not close that trade when it should and I can't figure out why.................
Reason: