For loop not working

 

This seems like a small problem and maybe it is but I've been struggling with it for a while and have finally given it.

My ea opens 2 orders on buy and sell signals. First OrderBuy1 with sl and tp, the OrderBuy2 with only sl.

OrderBuy 1 can close with tp and sl but OrderBuy 2 is supposed to be closed by an opposite signal which closes all current open orders and opens 2 new orders in the opposite direction, OrderSell1, OrderSell2.

My problem is that the loop is only running once. OrderBuy1 is closed by sell signal but OrderBuy2 is running till it hits sl. Please let me know if you can spot the issue. Ive provided all the necessary codes and the image of the journal.

Buy Orders

            //Open buys
            bool refreshData = RefreshRates();
            if(refreshData == false){
               Print("RefreshRates did not work");
            }
            int orderBuy1 = OrderSend (Symbol(), OP_BUY, 0.01, Close[0], 100, slBull , 0, "Buy Order", 5, 0, clrGreen);
            Print("orderBuy1 return: "+DoubleToStr(orderBuy1));
            if(orderBuy1 == -1){
               Print("There was an error on order. Error was :"+ DoubleToStr(GetLastError()) + " Type: Sell sl : "+ DoubleToStr(slBull)+ " Close[0] : "+DoubleToStr(Close[0]) + " Spread: "+ DoubleToStr(spread*Point())+ " Mode_STOPLEVEL "+ DoubleToStr(minStopPoint)+ " Lot "+DoubleToStr(lot)+ " atr"+ DoubleToStr(atr));
               Print("Risk "+ DoubleToStr(risk)+ " tpSlGap "+ DoubleToStr(tpSlGap)+ " perPip "+DoubleToStr(perPip));
            }
            int orderBuy2 = OrderSend (Symbol(), OP_BUY, 0.01, Close[0], 100, slBull , tpBull, "Buy Order", 5, 0, clrGreen);
            Print("orderBuy1 return: "+DoubleToStr(orderBuy2));
            if(orderBuy2 == -1){
               Print("There was an error on order. Error was :"+ DoubleToStr(GetLastError()) + " Type: Sell sl : "+ DoubleToStr(slBull)+ " Close[0] : "+DoubleToStr(Close[0]) + " Spread: "+ DoubleToStr(spread*Point())+ " Mode_STOPLEVEL "+ DoubleToStr(minStopPoint)+ " Lot "+DoubleToStr(lot)+ " atr"+ DoubleToStr(atr));
               Print("Risk "+ DoubleToStr(risk)+ " tpSlGap "+ DoubleToStr(tpSlGap)+ " perPip "+DoubleToStr(perPip));
            }

Closing loop

            //Close open Buys
            for( int i = 0 ; i < OrdersTotal() ; i++ ) {
               Print("Total orders: "+DoubleToStr(OrdersTotal()));
                  
                  if(OrderSelect( i, SELECT_BY_POS, MODE_TRADES ))
                  {
                     //If the pair of the order (OrderSymbol() is equal to the pair where the EA is running (Symbol()) then return true
                     if( OrderSymbol() == Symbol() && OrderType() == OP_BUY ){
                        Print("Selected orders: "+DoubleToStr(OrderTicket()));
                        bool refreshData = RefreshRates();
                        if(refreshData == false){
                           Print("RefreshRates did not work on sell");
                        }
                        bool closeSuccess = OrderClose(OrderTicket(), OrderLots(), Close[0], 100,clrPink );
                        if(closeSuccess == false){
                           //MessageBox("Failed to close order number "+ DoubleToString(OrderTicket()));
                           Print("Failed to close order number "+ DoubleToString(OrderTicket()));
                        }else{
                           buys =0;
                           Print("closed buy");
                           Print("New Buys: "+ DoubleToStr(buys));
                          
                        }
                     }
                  }
            }
Files:
Capture.PNG  8 kb
 
In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
  1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol,) you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
              Loops and Closing or Deleting Orders - MQL4 programming forum
    For In First Out (FIFO rules-US brokers,) and you (potentially) process multiple orders per symbol, you must find the earliest order, close it, and on a successful operation, reprocess all remaining positions.
              CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16

  2. and check OrderSelect in case earlier positions were deleted.
              What are Function return values ? How do I use them ? - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask) or (be direction independent and use) OrderClosePrice().
 

Thanks a bunch buddy. Just reversing the loop order fixed it. You are a life saver. I'm only using it on one chart, an it is a non-us broker. There are only 2 orders max at any one time.

Might try to add more charts and orders once I get more skilled and build more advanced ea. 

Reason: