Why "No Order Selected"?

 

Hello!

In the trading pool exist an OP_SELLSTOP and an OP_BUYSTOP order.

Can anybody say me wihy the following code returns me an error 4105, but neverthless it returns True to "Founded" and 1 to "Counted" ?

In orher words, it returns an "No Order Selected" error but returns also the expected result?

Thank you very much!!! My head crashes with this ...

//+------------------------------------------------------------------+
bool FindAndHowMany(int BuySellStop) {    //Search for given: OP_BUYSTOP or OP_SELLSTOP
   bool Result=false;
   int Count=0;
   Founded=0;
   Counted=0;
   for(int i=0;i<=OrdersTotal();i++) {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderType()==BuySellStop) {
         Result=true;
         Count=Count+OrderLots()/(vol/Multiplicador);
      }
   }
   Founded=Result;
   Counted=Count;

   Error=GetLastError();
   if(Error!=0) Print("FindAndHowMany = ",Error," / Founded: ",Founded," / Counted: ",Counted," / Type: ",BuySellStop);
   return(0);
}
//+------------------------------------------------------------------+
 

Ah, excuse me, Don't read this... I know why ... because of the "<=OrdersTotal()".

Excuse me please!!!

Thank you anyway!

 
eliusm:

Ah, excuse me, Don't read this... I know why ... because of the "<=OrdersTotal()".

Excuse me please!!!

Thank you anyway!

Great, you are on "MQL4 learning" way... :)
 
 for(int i=0;i<=OrdersTotal();i++) {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
  1. Position is zero based, so if there are n orders their positions are 0,1,... (n-1). What does your loop cover?
  2. Always test return codes. What are Function return values ? How do I use them ? - MQL4 forum
  3. Always count down. Loops and Closing or Deleting Orders - MQL4 forum
  4. Your loop makes your EA incompatible with every other including itself on other charts. Always test magic number and pair and TF.
Reason: