Doubt - Calculate profits during a month for an EA

 

I read articles about it here at this forum, one in particular is this one (https://www.mql5.com/en/forum/154009).  But so far it is not calculating as expected during Strategy Tester.


My goal is to create a method to account the profits accrued during the time the EA is running and check if the the target goal of profitability is or not achieved month by month.

My problem is that I have Closed Orders with profits in mu history, with the Magic Number correct and within the current month and year but when I select it and try to get the OrderProfits I get ZERO.

double Trade::checkActualMonthProfitability(){
   static int lastTicket=0;
   double OrderProfits=0;
   static double retorno = 0;
   static int currentMonth = Month();
   static int currentYear = Year();
   int orderMonth = 0;
   int orderYear = 0;
   logger.debug(__FILE__, __LINE__, StringFormat("currentYear=%d - currentMonth=%d", currentYear, currentMonth));
   if(currentMonth == Month()){
      datetime dataFechamento = NULL;
      for(int i=OrdersTotal()-1; i>=0; i--){
         if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))continue;
         if(OrderMagicNumber() != magicNumber) continue;
         if(OrderSymbol() != Symbol()) continue;
         dataFechamento = OrderCloseTime();
         orderYear =  TimeYear(dataFechamento);
         orderMonth = TimeMonth(dataFechamento);
         logger.debug(__FILE__, __LINE__, StringFormat("OrderCloseYear=%d - OrderCloseMonth=%d",orderYear, orderMonth));
         if (orderYear != currentYear) continue;
         if (orderMonth != currentMonth) continue;
         OrderProfits=OrderProfit()+OrderCommission()+OrderSwap();
         logger.debug(__FILE__, __LINE__, StringFormat("OrderProfit=%f",OrderProfits));
         retorno+=OrderProfits;
      }
   }else{
         currentMonth = Month();
         currentYear = Year();
         retorno = 0;      
      }
   return retorno;
}

Is there any better way to do that?

Is this method working the way it should?


Thanks a lot

Trying to calculate total profit for trades
Trying to calculate total profit for trades
  • 2014.11.29
  • www.mql5.com
having issue with a function that will calculate total profit. this is what i have so far. thanks for any help...
 

First thing.  Revisit your usage if Static variables.  Static Variables are only initalised once.  This means second time you call the function it will hve the value it finished with last time you called the function..  This is a bug.

Secondly, how about you output the value Month().   Could this be the current month?  Ie April right now and not be related to the month your testing in backtester?

Testing trading strategies on real ticks
Testing trading strategies on real ticks
  • www.mql5.com
The article provides the results of testing a simple trading strategy in three modes: "1 minute OHLC" using only Open, High, Low and Close prices of minute bars; detailed modeling in "Every tick" mode, as well as the most accurate "Every tick based on real ticks" mode applying actual historical data. Comparing the results allows us to assess...
 
Cornelis Duiker:

First thing.  Revisit your usage if Static variables.  Static Variables are only initalised once.  This means second time you call the function it will hve the value it finished with last time you called the function..  This is a bug.

Secondly, how about you output the value Month().   Could this be the current month?  Ie April right now and not be related to the month your testing in backtester?

Regarding static variables.

  • The idea of static variable is understood.  Indeed it is initialized only once.  So, the first time it runs it has zero value.  
  • While it keeps running it checks if the initialized variable value currentMonth has changed.
    • If it changed it means I must go inside the else clause.  I zero the return variable, currentYear and currentMonth.
    • Next time it runs it will be with the month correct and should account for the profits in history for the new month.

Maybe the problem is the acquisition of time data;

The MQL4 documentation says

During testing in the Strategy Tester, TimeCurrent() is simulated according to historical data.

The Month and Year instructions work as expected.  I outputed the value of the variables in a log file.  But maybe the speed of strategy tester is faster then desired.  There are many cases when I backtest a EA at highest speed that after the test stops the console output is still receiving instructions and printing things finishing after sometime.  Maybe this could be happening with some instructions as well.

Reason: