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

 

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.

e.g. Maybe by using same MagicNumber? I have no idea how to go about this.

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


Please help. Thanks in advance.

 
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.

e.g. Maybe by using same MagicNumber? I have no idea how to go about this.

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


Please help. Thanks in advance.

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

 
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,

Thanks for your idea.

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(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true)
        {
         if(OrderMagicNumber() == MagicNumber) //All other EA on different pair must use same MagicNumber for this to work.
           {
            result = true;
           }
        }
     }   
   return(result);
  }
 
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;
}
 
Mohamad Zulhairi Baba:

try this

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

 
Mohamad Zulhairi Baba:

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(); 
}
 
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);
  }
 
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;
}
 
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.
 
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?

 
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). 

Reason: