MQL4 How can I return the current profit on an open order based on the order comment

 

I'm trying to return the current profit on an open order based on the order comment. Right now, my code below is getting the profit of all open orders as opposed to just the order with a specific comment.

I have an order with order comment "Testing". So, what I want to return is the profit for only the order with order comment "Testing".


double Profit=0;

for(int i=0; i<OrdersTotal(); i++ )
      {
      if(OrderSelect(i, SELECT_BY_POS)==true)
     {
if (OrderComment()=="Testing")
      Profit+= (OrderProfit()+OrderSwap()+OrderCommission());
}


 
Kevin Johnson :

I'm trying to return the current profit on an open order based on the order comment. Right now, my code below is getting the profit of all open orders as opposed to just the order with a specific comment.

I have an order with order comment "Testing". So, what I want to return is the profit for only the order with order comment "Testing".




Try with magic number instead of comment

 
Kevin Johnson: I'm trying to return the current profit on an open order based on the order comment. Right now, my code below is getting the profit of all open orders as opposed to just the order with a specific comment. I have an order with order comment "Testing". So, what I want to return is the profit for only the order with order comment "Testing".

Comments can and will usually be changed by the broker. Some may even erase them completely.

So don't rely on Order comments. Instead learn, to assign a magic number to your order to later be able to identify a group or a batch of orders.

Magic number is especially used to identify the orders from each EA so that they don't clash.

OrderMagicNumber

Returns an identifying (magic) number of the currently selected order

int  OrderSend(
   string   symbol,              // symbol
   int      cmd,                 // operation
   double   volume,              // volume
   double   price,               // price
   int      slippage,            // slippage
   double   stoploss,            // stop loss
   double   takeprofit,          // take profit
   string   comment=NULL,        // comment
   int      magic=0,             // magic number
   datetime expiration=0,        // pending order expiration
   color    arrow_color=clrNONE  // color
   );
 

Hi

As was mentioned above – it’s safer to use magic number instead of comment.

But if you insist on using comments and you’re sure that it was not “removed” after sending the order, you could try checking this by StringFind(OrderCommnent(), “Testing”)>=0 instead of OrderComment()==”Testing”.

This way if the comment has added some other elements (like “sl” or some number – you will still include this trade in calculation.