Please help with loop

 

I am trying to count the total lots for all open Buy trades but the EA keeps on going through the loop and does not stop adding the lots together.

So in the first round it calculates the total lots correct but then keeps on adding that amount of lots over and over.

I want to stop it once it has gone through the loop one time.

Here is the code:

   Count = 0;
   
          for(Count = OrdersTotal()-1; Count >= 0; Count--)
           {
             if(OrderSelect(Count, SELECT_BY_POS)
            // && OrderMagicNumber() == MagicNumber
             && OrderSymbol() == Symbol()
             && OrderCloseTime() == 0
             && OrderType() == OP_BUY)  
             {
             BuyTotal++;
             if(BuyTotal >= OrdersTotal())break;
             BuyLots = BuyLots + OrderLots();
             }
           }
 
  1. You need to initialize BuyTotal and BuyLots before entering the loop.
  2. The if(…) break is just wrong; delete it.
  3. OrderCloseTime will always be zero; delete it.
 
William Roeder:
  1. You need to initialize BuyTotal and BuyLots before entering the loop.
  2. The if(…) break is just wrong; delete it.
  3. OrderCloseTime will always be zero; delete it.

Thank you very much William. I will do so.

 
Ernest Klokow:

Thank you very much William. I will do so.

Initializing the BuyLots variable did the job!

(I deleted the BuyTotal also since it was not required for the "if" statement.

Thanks again!