Download MetaTrader 5

Stop Orders being placed for current Symbol/Magic Number/OrderType

To add comments, please log in or register
Michael Mosley
14
Michael Mosley  
I need some help with making sure orders are not being placed for the same Magic Number && Symbol && Direction for the purpose of having several EA's running and not interfering with each other. I will have an EA running for multiple symbols etc. If true, don't trade again until the current order is closed or if false, allow the trade be placed with an if (CheckForOpenOrders()= false) to trigger ordersend step.
//+------------------------------------------------------------------+
//| Check For Orders Open On This Trade                              |
//+------------------------------------------------------------------+
bool CheckForOpenOrders()
{
 if(OrdersTotal()>0)
 {
  for(i = OrdersTotal()-1; i>=0; i--) //loop
  {
   if ((OrderSelect(i,SELECT_BY_POS, MODE_TRADES))&&(OrderType()==OP_SELL) && (OrderMagicNumber() == MagicNumber)
   && (OrderSymbol()==Symbol())); 
  } 
}
}                						
Any help is appreciated!
honest_knave
Moderator
2326
honest_knave  

Function:

bool CheckForOpenOrders(int order_type, int magic_number)
  {
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS))      continue;
      if(OrderSymbol() != _Symbol)           continue;
      if(OrderMagicNumber() != magic_number) continue;
      if(OrderType() == order_type)          return(true);
     }
   return(false);
  }

Call:

   if(!CheckForOpenOrders(OP_SELL,MagicNumber)) OrderSend(_Symbol,OP_SELL,...
Michael Mosley
14
Michael Mosley  
honest_knave:

Function:

Call:

Thanks so much!! I amended slightly it to suit my code as per below (One for buy and one for sell) and works perfectly.
bool CheckForOpenOrdersLong()
  {
   for(int b=OrdersTotal()-1; b>=0; b--)
     {
      if(!OrderSelect(b,SELECT_BY_POS))      continue;
      if(OrderSymbol() != _Symbol)           continue;
      if(OrderMagicNumber() != MagicNumber) continue;
      if(OrderType() == OP_BUY)          return(true);
     }
   return(false);
  }   
honest_knave
Moderator
2326
honest_knave  
Michael Mosley:
Thanks so much!! I amended slightly it to suit my code as per below (One for buy and one for sell) and works perfectly.


Glad you got it sorted. You can have 2 functions in you prefer, or the function I wrote can actually be used for both (by changing the order type in the call)

i.e.

if(!CheckForOpenOrders(OP_SELL,MagicNumber)) OrderSend(_Symbol,OP_SELL,...
if(!CheckForOpenOrders(OP_BUY,MagicNumber)) OrderSend(_Symbol,OP_BUY,...
To add comments, please log in or register