Hi all,
I am recently testing an EA that I developed myself and I found that for some reason, when it closes a position on a pair, it closes all other positions in all other pairs, regardless if the EA is running on that pair.
For example, if the EA is running on EURJPY and one condition is met for closing the trades on the EURJPY pair, the EA closes not only those trades, but all other trades that are opened in any pair, even if it is not loaded in that chart.
I checked if the EA could be sharing the magic number between other EA but its not the case.
Here is the code responsible of closing the trades and removing pending orders.
Any help would be appreciated!
Thank you!
//+------------------------------------------------------------------+ void checkTrades() { for(int i=OrdersTotal()-1;i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) { if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==OP_BUY) { if(SellCheck(1) && closeOn0) // OrderSymbol()==Symbol() && OrderMagicNumber()==Magic { removePending(); double totalLots=OrderLots(); if(OrderClose(OrderTicket(),totalLots,Bid,Slippage,0)) { Print("****Condition 1*****"); } return; } if(SellCheck(1) && closeOn1)// OrderSymbol()==Symbol() && OrderMagicNumber()==Magic { removePending(); double totalLots=OrderLots(); if(OrderClose(OrderTicket(),totalLots,Bid,Slippage,0)) { Print("****Condition 1*****"); } return; } if(stop==1 && iClose(_Symbol,PERIOD_D1,1)<Limit) OrderSymbol()==Symbol() && OrderMagicNumber()==Magic { removePending(); double totalLots=OrderLots(); if(OrderClose(OrderTicket(),totalLots,Bid,Slippage,0)) { Print("*****Condition 2*****"); } return; } } if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==OP_SELL) { if(BuyCheck(1) && closeOn0) OrderSymbol()==Symbol() && OrderMagicNumber()==Magic { removePending(); double totalLots=OrderLots(); if(OrderClose(OrderTicket(),totalLots,Ask,Slippage,0)) { Print("****Condition 0*****"); } return; } if(BuyCheck(1) && closeOn1) OrderSymbol()==Symbol() && OrderMagicNumber()==Magic { removePending(); double totalLots=OrderLots(); if(OrderClose(OrderTicket(),totalLots,Ask,Slippage,0)) { Print("****Condition 1*****"); } return; } if(stop==1 && iClose(_Symbol,PERIOD_D1,1)>Limit) OrderSymbol()==Symbol() && OrderMagicNumber()==Magic { removePending(); double totalLots=OrderLots(); if(OrderClose(OrderTicket(),totalLots,Ask,Slippage,0)) { Print("*****Condition 2*****"); } return; } } } } } void removePending() { for(int ix=OrdersTotal()-1;ix>=0;ix--) { if(OrderSelect(ix,SELECT_BY_POS,MODE_TRADES)) { if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && (OrderType()==OP_BUYLIMIT || OrderType()==OP_SELLLIMIT)) { OrderDelete(OrderTicket(),0); } } } }
Hi, thank you for your reply.
I don't get it. You suggest to change the structure of the function and put the magic number and symbol with the other conditions?
If this is the case, why it makes a difference with the actual function? I mean, if one of the conditions in the "if" statement aren't meeted, then the trades in other pairs shouldn't be touched.
Why this happens?
Hi,
I did this changes, but the results are the same. I don't know what I'm doing wrong.
void checkTrades() { for(int i=OrdersTotal()-1;i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) { if(/*OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && */OrderType()==OP_BUY) { if(SellCheck(1) && closeOn0 && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic) { removePending(); double totalLots=OrderLots(); if(OrderClose(OrderTicket(),totalLots,Bid,Slippage,0)) { Print("****Condition 1*****"); } return; } if(SellCheck(1) && closeOn1)// OrderSymbol()==Symbol() && OrderMagicNumber()==Magic { removePending(); double totalLots=OrderLots(); if(OrderClose(OrderTicket(),totalLots,Bid,Slippage,0)) { Print("****Condition 1*****"); } return; } if(stop==1 && iClose(_Symbol,PERIOD_D1,1)<Limit && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic) { removePending(); double totalLots=OrderLots(); if(OrderClose(OrderTicket(),totalLots,Bid,Slippage,0)) { Print("*****Condition 2*****"); } return; } } if(/*OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && */OrderType()==OP_SELL) { if(BuyCheck(1) && closeOn0 && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic) { removePending(); double totalLots=OrderLots(); if(OrderClose(OrderTicket(),totalLots,Ask,Slippage,0)) { Print("****Condition 0*****"); } return; } if(BuyCheck(1) && closeOn1 && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic) { removePending(); double totalLots=OrderLots(); if(OrderClose(OrderTicket(),totalLots,Ask,Slippage,0)) { Print("****Condition 1*****"); } return; } if(stop==1 && iClose(_Symbol,PERIOD_D1,1)>Limit && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic) { removePending(); double totalLots=OrderLots(); if(OrderClose(OrderTicket(),totalLots,Ask,Slippage,0)) { Print("*****Condition 2*****"); } return; } } } } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void removePending() { for(int i=OrdersTotal()-1;i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) { if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && (OrderType()==OP_BUYLIMIT || OrderType()==OP_SELLLIMIT)) { OrderDelete(OrderTicket(),0); } } } }
Hi,
I did this changes, but the results are the same. I don't know what I'm doing wrong.
Hi, try check this logic..
Print(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic);
Hi,
I did this changes, but the results are the same. I don't know what I'm doing wrong.
You are calling OrderSelect() in nested loops. You have first loop in checkTrades() and then you call second loop in removePending() and that is overwriting order selected by the first loop because only one order can be selected at a time.
You are calling OrderSelect() in nested loops. You have first loop in checkTrades() and then you call second loop in removePending() and that is overwriting order selected by the first loop because only one order can be selected at a time.
Ohh, so if I don't use a function to remove the pending orders and just remove them in the checkTrades loop, that would fix the problem?
Or I should execute removePending() if one of the 3 conditiones are met for closing a trade?Ohh, so if I don't use a function to remove the pending orders and just remove them in the checkTrades loop, that would fix the problem?
Or I should execute removePending() if one of the 3 conditiones are met for closing a trade?Untested quick fix of your code to avoid this and preserve logic
if(checkTrades()) removePending(); //--- bool checkTrades() { for(int i=OrdersTotal()-1;i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) { if(/*OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && */OrderType()==OP_BUY) { if(SellCheck(1) && closeOn0 && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic) { double totalLots=OrderLots(); if(OrderClose(OrderTicket(),totalLots,Bid,Slippage,0)) { Print("****Condition 1*****"); } return (true); } if(SellCheck(1) && closeOn1)// OrderSymbol()==Symbol() && OrderMagicNumber()==Magic { double totalLots=OrderLots(); if(OrderClose(OrderTicket(),totalLots,Bid,Slippage,0)) { Print("****Condition 1*****"); } return (true); } if(stop==1 && iClose(_Symbol,PERIOD_D1,1)<Limit && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic) { double totalLots=OrderLots(); if(OrderClose(OrderTicket(),totalLots,Bid,Slippage,0)) { Print("*****Condition 2*****"); } return (true); } } if(/*OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && */OrderType()==OP_SELL) { if(BuyCheck(1) && closeOn0 && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic) { double totalLots=OrderLots(); if(OrderClose(OrderTicket(),totalLots,Ask,Slippage,0)) { Print("****Condition 0*****"); } return (true); } if(BuyCheck(1) && closeOn1 && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic) { double totalLots=OrderLots(); if(OrderClose(OrderTicket(),totalLots,Ask,Slippage,0)) { Print("****Condition 1*****"); } return (true); } if(stop==1 && iClose(_Symbol,PERIOD_D1,1)>Limit && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic) { double totalLots=OrderLots(); if(OrderClose(OrderTicket(),totalLots,Ask,Slippage,0)) { Print("*****Condition 2*****"); } return (true); } } } } return (false); } //--- void removePending() { for(int i=OrdersTotal()-1;i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) { if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && (OrderType()==OP_BUYLIMIT || OrderType()==OP_SELLLIMIT)) { OrderDelete(OrderTicket(),0); } } } } //+------------------------------------------------------------------+
Untested quick fix of your code to avoid this and preserve logic
Thank you very much. I'll try this out and I'll let you know if this fix the issue. Meanwhile, would you mind explaining me the problem with nested loops?

- 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 all,
I am recently testing an EA that I developed myself and I found that for some reason, when it closes a position on a pair, it closes all other positions in all other pairs, regardless if the EA is running on that pair.
For example, if the EA is running on EURJPY and one condition is met for closing the trades on the EURJPY pair, the EA closes not only those trades, but all other trades that are opened in any pair, even if it is not loaded in that chart.
I checked if the EA could be sharing the magic number between other EA but its not the case.
Here is the code responsible of closing the trades and removing pending orders.
Any help would be appreciated!
Thank you!