# How to check if EA on other chart have opened an order?

30

I have attach my EA to 5 charts, all different currencies.

My question is how to make an EA to check EA on all other charts if have already open an order, before opening a new order? This is to ensure that I'll not blow my account by opening too many orders. FYI, my lotsize will depends on the risk.

I'd appreciate so much if anybody have any ideas or input how to do this.

Moderator
13965

Merdeka:

I have attach my EA to 5 charts, all different currencies.

My question is how to make an EA to check EA on all other charts if have already open an order, before opening a new order? This is to ensure that I'll not blow my account by opening too many orders. FYI, my lotsize will depends on the risk.

I'd appreciate so much if anybody have any ideas or input how to do this.

If all EAs use the same magic number, you can check by magic number and symbol to see what is already open.

30

Keith Watford:

If all EAs use the same magic number, you can check by magic number and symbol to see what is already open.

Hi Keith,

I've done it like this. In theory looks ok. What do u think?

```bool ExistOtherEAOpenOrder()
{
int total = OrdersTotal();
bool result = false;

for(int i = 0; i < total; i++)
{
{
if(OrderMagicNumber() == MagicNumber) //All other EA on different pair must use same MagicNumber for this to work.
{
result = true;
}
}
}
return(result);
}```
15907

Merdeka:

try this

```bool ExistOtherEAOpenOrder(int magic) {
for(int i = OrdersTotal()-1; i >= 0; i--){
if(OrderSelect(i,SELECT_BY_POS) && OrderMagicNumber() == magic)
return true;
break;
}
return false;
}```
30

try this

So this means the latest open order is actually the last, not i=0?

1877

try this

You can omit the break statement since it is impossible to ever be executed.

Just for fun, here's a different way to write the function

```bool ExistOtherEAOpenOrder(int magic) {
int i = 0;
while(OrderSelect(i++, SELECT_BY_POS) && OrderMagicNumber() != magic);
return i <= OrdersTotal();
}```
30

nicholi shen:

You can omit the break statement since it is impossible to ever be executed.

Just for fun, here's a different way to write the function

Thank you guys for the help. I've learned a more efficient way to write codes.

One more question, what is the correct way to select the latest open order and return the open profit? I did it below, what do u think?

```double LastEAOrderOpenProfit(int magic)
{
double result = 0;

if(OrderSelect(OrdersTotal()-1, SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == Symbol() && OrderMagicNumber() == magic)
result = OrderProfit();

return(result);
}```
1877

Merdeka:

Thank you guys for the help. I've learned a more efficient way to write codes.

One more question, what is the correct way to select the latest open order and return the open profit? I did it below, what do u think?

You still have to loop, and you cannot ever assume the ordering of tickets by any metric.

```double lastEaOrderOpenProfit(int magic)
{
double profit = 0.0;
datetime time = 0;
for(int i=OrdersTotal()-1; i>=0; --i){
if(OrderSelect(i, SELECT_BY_POS)
&& OrderSymbol() == Symbol()
&& OrderMagicNumber() == magic
&& OrderOpenTime() > time
){
time = OrderOpenTime();
profit = OrderProfit();
}
}
return profit;
}```
30

nicholi shen:

You still have to loop, and you cannot ever assume the ordering of tickets by any metric.

I see. Thank you so much for your help.
30

nicholi shen:

You still have to loop, and you cannot ever assume the ordering of tickets by any metric.

Hi,

Sorry I do not understand this part,  '&& OrderOpenTime() > time

Isn't all the order will result this as true? How to know if it's the latest order?

1877

Merdeka:

Hi,

Sorry I do not understand this part,  '&& OrderOpenTime() > time

Isn't all the order will result this as true? How to know if it's the latest order?

For the first order, yes. But then we look for an order with a greater open time than that order on subsequent loops...and so on..until we end up with the order with the highest open time (most recent order).