0

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

So, what I want to return is the profit for the order that the order comment is "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());
}
Dr.Prog
  • 229
  • 3
  • 13

1 Answers1

1

You should not use OrderComment() for isolating/identifying trades, the broker can overwrite/alter/delete the comments at any time for any reason.

You should also count orders down, not up. If an order is deleted or created mid check, your function will exit with an error or produce unexpected results.

All the above given, the following works when I've tested it.

   double Profit=0;

   for(int i=OrdersTotal()-1; i>=0; i--)
   {
      if(OrderSelect(i, SELECT_BY_POS))
      {
         if(OrderComment()=="Testing") Profit+= (OrderProfit()-OrderSwap()-OrderCommission());
      }
   }
   
   Comment(Profit);
PaulB
  • 1,262
  • 1
  • 6
  • 17