OnTester code not working

 

Hello,

I have written code to output a custom value using the Strategy Tester in MQL4. The following is only part of the code which failed (calculating R2), it seems to go already wrong with calculating Y (total profit). It correctly calculates the number of trades (so correctly excludes all cancelled trades) but adding up the profits of all closed trades does not give the same value as what the Strategy Tester outputs as Profit. Any suggestions where I went wrong?

double OnTester()
  {
   double X = 0;
   double Y = 1000;

   for(PositionIndex = OrdersHistoryTotal() - 1; PositionIndex >=0; PositionIndex --)
     {
      OrderSelect(PositionIndex,SELECT_BY_POS,MODE_HISTORY);

      if(OrderType() == OP_BUY || OrderType() == OP_SELL)
        {
         X += 1;
         Y += OrderProfit();
        }
     }
   return (Y);
 }
 

Returning X Returning Y

I have attached the output of OnTester results in Strategy Tester (returning X or returning Y).

 
double OnTester()
  {
   double X = 0;
   double Y = 1000;

   for(PositionIndex = OrdersHistoryTotal() - 1; PositionIndex >=0; PositionIndex --)
     {
      OrderSelect(PositionIndex,SELECT_BY_POS,MODE_HISTORY);

      if(OrderType() == OP_BUY || OrderType() == OP_SELL)
        {
         X += 1;
         Y += (OrderProfit()+OrderCommision()+OrderSwap());
        }
     }
   return (Y);
 }

You are missing commision and swap.

 
Yashar Seyyedin #:

You are missing commision and swap.

I will give that a go, I didn’t realise the Strategy Tester included commission and swap.
 
hraczv #:
I will give that a go, I didn’t realise the Strategy Tester included commission and swap.
Tester counts commission and swap.