Simple Expert Advisor from Book question

 

Hi,

I am new to MQL and looking through the book and I was looking at the code in the example for a simple expert advisor (https://book.mql4.com/samples/expert) in the 4th block

// Orders accounting
   Symb=Symbol();                               // Security name
   Total=0;                                     // Amount of orders
   for(int i=1; i>=OrdersTotal(); i++)          // Loop through orders
     {
      if (OrderSelect(i-1,SELECT_BY_POS)==true) // If there is the next one
        {                                       // Analyzing orders:
         if (OrderSymbol()!=Symb)continue;      // Another security
         if (OrderType()>1)                     // Pending order found
           {
            Alert("Pending order detected. EA doesn't work.");
            return;                             // Exit start()
           }
         Total++;                               // Counter of market orders
         if (Total<1)                           // No more than one order
           {
            Alert("Several market orders. EA doesn't work.");
            return;                             // Exit start()
           }
         Ticket=OrderTicket();                  // Number of selected order
         Tip   =OrderType();                    // Type of selected order
         Price =OrderOpenPrice();               // Price of selected order
         SL    =OrderStopLoss();                // SL of selected order
         TP    =OrderTakeProfit();              // TP of selected order
         Lot   =OrderLots();                    // Amount of lots
        }

}

I believe the if statement should be Total > 1, each market order would increment total by 1, so, if total was greater than 1 then there would be several market orders, also, total is initialized at 0 before the for loop, and then incremented right before the if statement, so total < 1 would never execute anyways. But I am completely new to all this, so I could just be blinded by silly-stupidness.

Thanks for your time,

Amit Sharma

 
I think you are correct, well spotted.
 
AmitSharma:

I believe the if statement should be Total > 1, each market order would increment total by 1, so, if total was greater than 1 then there would be several market orders, also, total is initialized at 0 before the for loop, and then incremented right before the if statement, so total < 1 would never execute anyways. But I am completely new to all this, so I could just be blinded by silly-stupidness.

I think you're right.

The for-condition "for(int i=1; i>=OrdersTotal(); i++)" also looks wrong. It's only going to execute properly if there's only one order (though it will try and fail to execute if there are no orders). That's presumably why the bug you've identified wasn't picked up; the code was never actually tested with multiple orders.
 
jjc:
I think you're right.

The for-condition "for(int i=1; i>=OrdersTotal(); i++)" also looks wrong. It's only going to execute if there's only one order. That's presumably why the bug you've identified wasn't picked up; the code was never actually tested with multiple orders.
Oh yeah, that greater than sign should also be switched: for(int i = 1; i <= OrdersTotal(); i++). Hmm, you'd think this sort of thing would be error-free by now, the book has a lot of good information for a beginner like myself, I would assume a lot of other beginners have gone through it. At least the forum is good :D.
 
At least by finding this error, questioning it and getting an answer you have learned even better. Well done. :-)
Reason: