Closing trades using the Expert iD magic number

 

Hello!

Thank you for having a look at this thread! I'm looking to use multiple EAs on the same symbol and the same timeframe but each EA has its own magic number. Could someone give me a short algorithm which will close an open position which was open by a EA which has its own unique magic number?


Thank you!

Bart

 
Bartlomiej Winter:

Hello!

Thank you for having a look at this thread! I'm looking to use multiple EAs on the same symbol and the same timeframe but each EA has its own magic number. Could someone give me a short algorithm which will close an open position which was open by a EA which has its own unique magic number?


Thank you!

Bart

Just check the magic numbers of the already existing order(s) before doing anything to existing orders or opening new orders
 
Mladen Rakic:
Just check the magic numbers o the already existing order(s) before doing anything to existing orders or opening new orders

Thanks for your quick reply! What is the best way to check through the magic number of open order?

 
Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
          Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum
 
whroeder1:
Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
          Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

May I ask how you mean "Using OrdersTotal directly"?

 
ZetaTrader: May I ask how you mean "Using OrdersTotal directly"?
if( OrdersTotal() == 0 ) Buy();
vs
int count=0;
for(int iPos=OrdersTotal(); iPos > 0;) if(
   OrderSelect(--iPos, SELECT_BY_POS)
&& OrderMagicNumber() == Magic.Number
&& OrderSymbol()      == _Symbol)
   ++count;
if(count == 0) Buy();
 
hi anybodey tell me this  by expert expert id magic number 123450  name or EA , i have magic number but dont know name of EA anybodey tell me pls tnx
 

Hi

You can use this function: use symbol and magic and typed the trade as parameters - you don’t need the name of the EA to close trades from it.

void closeOrders(string symbol, int oMagic,int oType) 
{
   bool close;
   for(int i=OrdersTotal()-1; i>=0; i--) 
   {
      if(OrderSelect(i,SELECT_BY_POS)) 
      {
         if(OrderMagicNumber()==oMagic || oMagic<0) 
         {
            if (OrderSymbol() == symbol || symbol==""){            
               if(OrderType()==oType || oType<0) 
               {
                  RefreshRates();
                  if(OrderType()==OP_BUY) 
                  {
                     close = OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID),(int)MarketInfo(OrderSymbol(), MODE_DIGITS)),100);
                  }
                  if(OrderType()==OP_BUYSTOP && checkPrice(OrderType(), OrderOpenPrice())) 
                  {
                     close = OrderDelete(OrderTicket());
                  }
                  if(OrderType()==OP_BUYLIMIT && checkPrice(OrderType(), OrderOpenPrice())) 
                  {
                     close = OrderDelete(OrderTicket());
                  }
                  if(OrderType()==OP_SELL) 
                  { 
                     close = OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(MarketInfo(OrderSymbol(), MODE_ASK),(int)MarketInfo(OrderSymbol(), MODE_DIGITS)),100);
                  }
                  if(OrderType()==OP_SELLSTOP && checkPrice(OrderType(), OrderOpenPrice())) 
                  {
                     close = OrderDelete(OrderTicket());
                  }
                  if(OrderType()==OP_SELLLIMIT && checkPrice(OrderType(), OrderOpenPrice())) 
                  {
                     close = OrderDelete(OrderTicket());
                  }
               }
            }
         }
      }
   }
}

Best regards