OrdersTotal() in MQL5 is always giving me 0

To add comments, please log in or register
mariogharib
28
mariogharib  

Dear Metatraders, 

I am trying to write a code in MQL5 that will allow me to calculate the current profit on all my open trades,

In back-testing, the OrdersTotal() is giving me 0 knowing that I have multiple open trades, 

Any idea what seems the problem ?

Many Thanks

Alain Verleyen
39060
Alain Verleyen  
mariogharib:

Dear Metatraders, 

I am trying to write a code in MQL5 that will allow me to calculate the current profit on all my open trades,

In back-testing, the OrdersTotal() is giving me 0 knowing that I have multiple open trades, 

Any idea what seems the problem ?

Many Thanks

The problem is you didn't read the documentation.

Do not confuse current pending orders with positions, which are also displayed on the "Trade" tab of the "Toolbox" of the client terminal. An order is a request to conduct a transaction, while a position is a result of one or more deals.

What you need to check is positions, no orders.

fxsaber
14686
fxsaber  
mariogharib:

I am trying to write a code in MQL5 that will allow me to calculate the current profit on all my open trades,

// MQL4&5-code

#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006

double GetCurrentProfit()
{
  double Res = 0;
  
  for (int i = OrdersTotal() - 1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS))
      Res += OrderProfit() + OrderSwap() + OrderCommission();
      
  return(Res);
}
To add comments, please log in or register