Check magicnumber of open position

 

Hi guys,

I want to check for every position to open if there is a position (magicnumber + symbol) already open.

Who can help?

julyhedge.mq4

Best Regards,

Files:
julyhedge.mq4  3 kb
 
This function will return the number of open positions for a current symbol and a required magic number
int countOpenedOrders(int MagicNumber)

{

int openedOrders=0;

for(int i=0; i < OrdersTotal(); i++)

{

if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false) break;

if(OrderMagicNumber() != MagicNumber) continue;

if(OrderSymbol() != Symbol()) continue;

//

//

//

//

//

if(OrderType() == OP_BUY || OrderType() == OP_SELL) openedOrders++;

}

return(openedOrders);

}

If it returns 0, there are no opened positions for that magic number + symbol combination

jimbofx7:
Hi guys,

I want to check for every position to open if there is a position (magicnumber + symbol) already open.

Who can help?

julyhedge.mq4

Best Regards,
 

Hi mladen,

many thanks for your help. I have some problems to put in on the right position of the code. Do I have to put your code after in start()?

Best regards,

 

Place it at the end of the EA

But since you use 2 magic numbers and 2 symbols that function has to be changed a bit and should be something like this :
int countOpenedOrders(int magicNumber, string symbol)

{

int openedOrders=0;

for(int i=0; i < OrdersTotal(); i++)

{

if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false) break;

if(OrderMagicNumber() != magicNumber) continue;

if(OrderSymbol() != symbol) continue;

//

//

//

//

//

if(OrderType() == OP_BUY || OrderType() == OP_SELL) openedOrders++;

}

return(openedOrders);

}

Anyway, changed your code a bit so it allows only one opened order per magic number + symbol opened at a time (if that is what you had in mind)

regards

jimbofx7:
Hi mladen,

many thanks for your help. I have some problems to put in on the right position of the code. Do I have to put your code after in start()?

Best regards,
Files:
 
Reason: