How to check magic numbers of open positions

Hi guys,

I need your coding help. I have different open positions from different EAs with unique magic numbers.

Now I want to let check every EA if the EA wants to open a new position to check if there is already an open position with a given magic number.

How can help?

Bset regards,

int i = OrdersTotal();
while(i>0) {
i–;
OrderSelect(i,SELECT_BY_POS);
if (OrderMagicNumber()==yourmagicnumber) {
do something
}
}

Hi Buckscoder,

thanks for the code. How can I place it into my EA?
Please help again.

Kind regards,
jimbofx7

//±-----------------------------------------------------------------+
//| OpenHedge EA.mq4 |
//| Copyright © 2010, MetaQuotes Software Corp. |
//| MetaTrader 4 Trading Platform / MetaQuotes Software Corp. |
//±-----------------------------------------------------------------+
#property copyright “Copyright © 2010, MetaQuotes Software Corp.”
#property link “http://www.metaquotes.net

extern int Slippage=5;
extern int MagicNumber_1=1000;
extern int MagicNumber_2=2000;

double dXPoint=1;
//±-----------------------------------------------------------------+
//| expert initialization function |
//±-----------------------------------------------------------------+
int init()
{
if(Digits==3 || Digits==5)
{
dXPoint=10;
}

if(Digits==3 || Digits==5)
{
dXPoint=10;
int SLIPPAGE=SLIPPAGE*dXPoint;
}
return(0);
}
//±-----------------------------------------------------------------+
//| expert deinitialization function |
//±-----------------------------------------------------------------+
int deinit()
{
return(0);
}
//±-----------------------------------------------------------------+
//| expert start function |
//±-----------------------------------------------------------------+
int start()
{

if(iMA(NULL,0,13,8,MODE_SMMA,PRICE_MEDIAN,0)<iMA(NULL,0,20,8,MODE_SMMA,PRICE_MEDIAN,0))
{

  OrderSend("EURUSD",OP_BUY,1,MarketInfo("EURUSD",MODE_ASK),2,NULL,NULL,"Magic Number 1000",MagicNumber_1,0,CLR_NONE);

  Sleep(1000);

  OrderSend("USDCHF",OP_BUY,1,MarketInfo("USDCHF",MODE_ASK),2,NULL,NULL,"Magic Number 1000",MagicNumber_1,0,CLR_NONE);

  Sleep(1000);

  OrderSend("EURUSD",OP_SELL,1,MarketInfo("EURUSD",MODE_BID),2,NULL,NULL,"Magic Number 2000",MagicNumber_2,0,CLR_NONE);

  Sleep(1000);

  OrderSend("USDCHF",OP_SELL,1,MarketInfo("USDCHF",MODE_BID),2,NULL,NULL,"Magic Number 2000",MagicNumber_2,0,CLR_NONE);

  Sleep(1000);

  //RefreshRates();

 }

//------------- 2

if(iMA(NULL,0,13,8,MODE_SMMA,PRICE_MEDIAN,0)>iMA(NULL,0,20,8,MODE_SMMA,PRICE_MEDIAN,0))

 {
  OrderSend("EURUSD",OP_SELL,1,MarketInfo("EURUSD",MODE_BID),2,NULL,NULL,"Magic Number 1000",MagicNumber_1,0,CLR_NONE);

  Sleep(1000);

  OrderSend("USDCHF",OP_SELL,1,MarketInfo("USDCHF",MODE_BID),2,NULL,NULL,"Magic Number 1000",MagicNumber_1,0,CLR_NONE);

  Sleep(1000);

  OrderSend("EURUSD",OP_BUY,1,MarketInfo("EURUSD",MODE_ASK),2,NULL,NULL,"Magic Number 2000",MagicNumber_2,0,CLR_NONE);

  Sleep(1000);

  OrderSend("USDCHF",OP_BUY,1,MarketInfo("USDCHF",MODE_ASK),2,NULL,NULL,"Magic Number 2000",MagicNumber_2,0,CLR_NONE);

  Sleep(1000);
  
}

return(0);
}
//±-----------------------------------------------------------------+

I would create a function that evaluates the condition. Presumably you want to check by symbol and magic number before each order is placed.

bool OrderAlreadyOpen(int magicnumber, string symname) {
   for (int i = 0; i < OrdersTotal; i++) {
      if(OrderSelect(i,SELECT_BY_POS)) {
         if (OrderMagicNumber()==magicnumber && OrderSymbol() == symname) {
            return(true);
         }
      }
   }
   return(false);
}

From your code you would place a block around each entry. Starting with this:


OrderSend("EURUSD",OP_BUY,1,MarketInfo("EURUSD",MO DE_ASK),2,NULL,NULL,"Magic Number 1000",MagicNumber_1,0,CLR_NONE);
Sleep(1000);

block quote like this:


if (OrderAlreadyOpen(MagicNumber_1, "EURUSD") == false) {
   OrderSend("EURUSD",OP_BUY,1,MarketInfo("EURUSD",MO DE_ASK),2,NULL,NULL,"Magic Number 1000",MagicNumber_1,0,CLR_NONE);
   Sleep(1000);
}

Do the same with each symbol and each magic number (block the OrderSend within a call to the OrderAlreadyOpen function.

HTH!

Read more: 301 Moved Permanently

Hi jimbofx7,

ur welcome. Well, sry I have a hard time understanding others code. Just try to embed it or you could for instance look at my foreposters solution or here for help:

http://forums.babypips.com/expert-advisors-automated-trading/35051-i-can-create-your-expert-advisor-free.html

Hi FXEZ,

thanks for the code. I will check it out.

Kind regards,

Hi FXEZ,

on which position of the code I have to fill in your code?

Thanks in advance.

Kind regards,
jimbofx7

Hi guys,

who can help me to put the code by FXEZ into my code?

Best regards,