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!

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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);
}