[HELP - Pending Order Selection] Code only works on latest

 

Hi all.

I have been working on a function that acts on the pending orders inside a multi symbol EA. When I run the code it works great if I use only 1 pair but when I enable multi symbol it only acts on the latest opened pending Order.

I tried using m_order.select from COrderInfo and directly OrderSelect(ticket) and both show same behaviour - only works on latest.

Please help!

void ReviewOrders(){
   if(OrdersTotal()==0) return;
   for(int orderIndex = 0; orderIndex < OrdersTotal(); orderIndex++)
     {
      ulong ticket=OrderGetTicket(orderIndex);
      if(OrderSelect(ticket)== false) continue;
      if(OrderGetInteger(ORDER_MAGIC)!=EAmagic) continue;
      string par_temp=OrderGetString(ORDER_SYMBOL);
      double ask=SymbolInfoDouble(par_temp,SYMBOL_ASK);
      double bid=SymbolInfoDouble(par_temp,SYMBOL_BID);
      double point=SymbolInfoDouble(par_temp,SYMBOL_POINT);
      double price=bid;
      double spread=MathMax(ask-bid,point);
Documentation on MQL5: Standard Library / Trade Classes / COrderInfo
Documentation on MQL5: Standard Library / Trade Classes / COrderInfo
  • www.mql5.com
COrderInfo - Trade Classes - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Still stuck with this issue. Can someone offer guidance on what can I look into to understand the issue at hand?

 
Nuno Costa #:

Still stuck with this issue. Can someone offer guidance on what can I look into to understand the issue at hand?

Why are you setting price to bid every time?
Should you not select bid or ask depending one whether it is a sell or buy?

Also have you debugged/printed out the ticket numbers to check your loop is selecting each one? You need to step through the logic and understand why it is not working as you expect
 
Nuno Costa: but when I enable multi symbol it only acts on the latest opened pending Order.
      if(OrderSelect(ticket)== false) continue;
      if(OrderGetInteger(ORDER_MAGIC)!=EAmagic) continue;

Where do you filter on symbols?

 
William Roeder #:

Where do you filter on symbols?

Magic - not symbols right?

      if(OrderGetInteger(ORDER_MAGIC)!=EAmagic) continue;
 
R4tna C #: Magic - not symbols right?

Only if you use a unique Magic for each symbol.

Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
          Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
          PositionClose is not working - MQL5 programming forum (2020)
          MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
          Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
          Limit one open buy/sell position at a time - General - MQL5 programming forum (2022)

You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

 
William Roeder #:

Only if you use a unique Magic for each symbol.

Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
          Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
          PositionClose is not working - MQL5 programming forum (2020)
          MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
          Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
          Limit one open buy/sell position at a time - General - MQL5 programming forum (2022)

You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

Well its for the OP to say whether its a unique magic ID per symbol or not

 
R4tna C #: Well its for the OP to say whether its a unique magic ID per symbol or not

So your statement

R4tna C #: Magic - not symbols right?

is wrong, misleading, assuming.

 
William Roeder #:

So your statement

is wrong, misleading, assuming.

I was trying to convey/discuss that there only seems to be a filter for magic, not symbols, but I see how it could be misleading in the context if your post 

 
Why do you write if order select == false continue?

Shouldn't it be if order select == true continue ?
 
  1. Tobias Johannes Zimmer #: Shouldn't it be if order select == true continue ?

    No. If you succeed in selecting the order, you want to process it. If you do not select the order, you want to continue to the next index. I prefer positive logic only:

    uLong ticket;
       for(int orderIndex = 0; orderIndex < OrdersTotal(); orderIndex++) if(
          OrderSelect( ticket=OrderGetTicket(orderIndex) )
       && OrderGetInteger(ORDER_MAGIC)==EAmagic
    // && symbol match, etc.
       ){ // Process
  2.       if(OrderSelect(ticket)== false) continue;

    You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and “if long entry” is an incomplete sentence.