OrderSelect() results in lingering order selection of different currencies

 

Hello members,

Your help would be greatly appreciated. I am using the OrderSelect() function to cycle through orders for various reasons, however, when I do, the EA begins to affect trades on other currencies and magic numbers.

I believe this is because when the OrderSelect() function exits, it sets on the zero order which can be any symbol / magic number. Here is an example of what I am doing:

bool CountOrders()
{
   int cnt = 0;
   int OrderCount = 0;
   for (cnt=OrdersTotal()-1;cnt>=0;cnt--)
   {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == magic)
      {                  
         OrderCount++;
      }
   }
   if (OrderCount > 0) return(true);
   else return(false);
}

int start()
{   
   if (CountOrders())
   {
      if(OrderType()==OP_BUY && OrderNeedsClosing()) OrderClose(OrderTicket(), OrderLots(), Bid, slip, ArrowColourBuy_Close); 
   }
}


I know what is happening, the last order selected could be anything (any Magic / Symbol) and when I later use OrderTicket() it is using the ticket of the last order.

What I need some advice on is how to avoid this from happening. The above is just an example and I am not worried about fixing that particular code. I just need to know the basics on avoiding this issue. Here is what I had in mind but I would appreciate some feedback on whether this will work or not:

i=0;
for(i=OrdersTotal()-1;i>=0;i--) 
{
   if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
   if (OrderMagicNumber() != magic || OrderSymbol() != Symbol()) continue;
   if (OrderType() == OP_SELL || OrderType() == OP_BUY) 
   {
      // Do something
   }
}


// or should continue here:
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;

// be break; ?
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) break;


Thanks in advance.

Regards,

JonDgls

 

Several ways to fix

- NEVER assume what order is selected when using OrderType() etc. ALWAYS do an OrderSelect() first

- Change CountOrders to 'remember' any valid order as it scans, and then select it before returning.

- Change bool CountOrders to either int ChecktOrdersAndSelectReturnValueByPositionOrMinus1() or int CheckForAnyRelevantOrderAndReturnItsOrderNumberOrMinus1()
if return is -1 then no open/pending orders, otherwise you can select an order

NB do not complain if my functions names are too long. I just like self-documenting code ;-)

 
that is really a hell of a function name ;)
 
In your main loop you're counting down, but in the sub loop you're counting up but not selecting anything.
    for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol() ){              // and my pair.
        return(true);
    }
    return(false);
 

OK .. So what if I assign a variable at the start that references an order count function. Later on when I use that variable is the function run again or does is the value of the variable stored when assigned.


Example:

int CountOrders()
{
   int cnt = 0;
   int OrderCount = 0;
   for (cnt=OrdersTotal()-1;cnt>=0;cnt--)
   {
      if (!OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == magic)
      {                  
         OrderCount++;
      }
   }
   return(OrderCount);
}

int start()
{
   int TotalOrders = CountOrders();

   if (TotalOrders  == 1)
   {
      // When I use the variable TotalOrders  is the function run here or is it run when the variable is assigned?
   }
}


Regards,

JonDgls

 
The function runs when you called it. TotalOrders is a simple variable.
Reason: