title: What is the reason for the error 'invalid ticket for Order close function' in mql4?
body: <p>In my mql4 Expert Advisor, I've written this function to close all the buy trades at once. When I test it via the <em>Strategy Tester</em>, it closes some buy trades, but for a few other <em>buy</em> trades it returns the: <code>OrderClose error 4051:invalid ticket for OrderClose function</code> error.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>void Close_All_Buy_Trdes(){
     for (int i=0;i&lt;=OrdersTotal();i++){
        OrderSelect(i,SELECT_BY_POS);
        if(OrderType()==OP_BUY {
             OrderClose(OrderTicket(),OrderLots(),Bid,3);
        }
     }
  }
  </code></pre>
  
  <p><strong>What is the reason for this happening?</strong></p>