pending order magic number/comment

 

Hi,


I have an EA which is sending 3 buy orders with different magic number/ comment, and what i want is the EA will not send out a new order  if  the pending orders already had the orders

but when i use the code below, I wanna check if the existing orders have the comment of my orders so it wont send it again,  it only returns the magic num/comment of the last order i sent, and i cant extract the magic number of first order and second order even i see there are three orders. Please help! thanks!


int PendingOrder(string comment)

{

int a; int t = OrdersTotal();

for (int i=0; i < OrdersTotal(); i++)

{

ulong ticket = OrderGetTicket(i);

string t = OrderGetString(ORDER_COMMENT);

if(t==comment)

a=1; else a=0;

}

return(a);

}

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Requests to execute trade operations are formalized as orders. Each order has a variety of properties for reading. Information on them can be obtained using functions Position identifier that is set to an order as soon as it is executed. Each executed order results in a deal that opens or modifies an already existing position. The...
 

Some suggestions:

  • Use speaking names to express what you are going to do. PendingOrder could mean anything.
  • When you test for a yes/no condition, bool is the proper variable type to use. Instead of 1 or 0 use true/false.
  • When you need to loop through orders or positions always go from highest to lowest index.

The bug in your function is that you continue looping even if you found the comment, and this resets the flag on the next iteration.

bool IsPendingOrder(string comment)
  {
   bool found=false;

   for (int i=OrdersTotal()-1; i>=0; i--)
     {
      ulong ticket=OrderGetTicket(i);
      if(ticket==0) continue;

      string s=OrderGetString(ORDER_COMMENT);
      if(s==comment)
        {
         found=true;
         break;
        }
     }
   return found;
  }
 
it works, thank you so much!
Reason: