-
if(OrderType()!=OP_BUY && OrderType()!=OP_SELL) //if there's no open order buy and sell
You don't know that yet; you are still in the loop. Loop through all the orders and count the open ones. Then, once you know, loop again and delete all the pending ones.
-
There is no need to create pending orders in code.
- The pending has the slight advantage, A) you are closer to the top of the queue (filled quicker), B) there's no round trip network delay (filled quicker.)
Don't worry about it unless you're scalping M1 or trading news.
-
Humans can't watch the screen 24/7, so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and just open an order.
- The pending has the slight advantage, A) you are closer to the top of the queue (filled quicker), B) there's no round trip network delay (filled quicker.)
- You don't know that yet; you are still in the loop. Loop through all the orders and count the open ones. Then, once you know, loop again and delete all the pending ones.I tried your suggestion and separating the code into 2 function. But the problem is still exist. Did I do it right?
bool CheckOpenOrders() { for(int i=OrdersTotal()-1; i >=0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { if(OrderType()==OP_BUY && OrderType()==OP_SELL) return (true); } } return false; } //+------------------------------------------------------------------+ void DeletePendOrder() { if(!CheckOpenOrders()) { for(int a = OrdersTotal()-1; a >=0; a--) { if(OrderSelect(a,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber) { if(OrderType()==OP_SELLSTOP) { bool res = OrderDelete(OrderTicket()); if(!res) Print("Delete pending order failed for ticket # ",OrderTicket()," with error: ",GetLastError()); } if(OrderType()==OP_BUYSTOP) { bool res = OrderDelete(OrderTicket()); if(!res) Print("Delete pending order failed for ticket # ",OrderTicket()," with error: ",GetLastError()); } } } } }
- Humans can't watch the screen 24/7, so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and just open an order.
Thanks for the knowledge, I will try your suggestion after this problem resolve.
Yes, my mistake. I've change it into this and the problem still exist.
bool CheckNoOpenOrders() { for(int i=OrdersTotal()-1; i >=0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { if(OrderType()!=OP_BUY && OrderType()!=OP_SELL) return (true); } } return false; }
I have another EA that open 2 pending order. When price moves to one direction and hit the pending order the other one would be deleted. The code almost exact same with the first code that I post and it works but I don't know why it doesn't works on here.
William has already given you the solution
-
You don't know that yet; you are still in the loop. Loop through all the orders and count the open ones. Then, once you know, loop again and delete all the pending ones.
if(OrderType()!=OP_BUY && OrderType()!=OP_SELL) return (true);
This will exit the loop as soon as it finds a pending order.
So your function should not be called
bool CheckNoOpenOrders()
It should be
bool CheckIfPendingOrderExists()
Thanks for the answer William it already fix the problem, there's a minor issue with the calculation but after fix it everything works perfectly.
Hi
How you fix the error
I too have same problem and unable to find the solution
Pls if you post the working code means it will greatfull
here's the final code
bool AnyBuyOrders() { for(int i=OrdersTotal()-1; i >=0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { if(OrderType()==OP_BUY || OrderType()==OP_SELL) return true; } } return false; }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi my EA open 1 order and several order stop. I tried to delete the order stop if the open order is closed or get stopped out.
Using the code above and since my EA works on every tick, the EA will close the order stop right after EA opens it. Basically the first tick will open it and the second tick is close it. My guess is this line of code. Is this the right way to tell that if there's no open order buy or sell close all pending order?