OrdersHistoryTotal() not working properly in the Strategy Tester

 

Hi, I'm writing an EA and for filtering purposes I need to know if the last closed trade was a winner or a loser. I'm using OrdersHistoryTotal() and debugging with Print(). My EA is having for instance 40% winners, 60% losers, but when I have a look at the Journal because I just see that all the prints refers to the very last trade, for instance trade number 53, no matter the hour of the bar.

I haven't done anything weird. This is the code:

bool IsLastTradeAWinner()
{
   int last_trade=OrdersHistoryTotal();

   if(last_trade > 0)
   {
      if(OrderSelect(last_trade - 1, SELECT_BY_POS, MODE_HISTORY) == true)
      {  
         OrderPrint();
         return(OrderProfit() > 0);
      }
   }
   
   return(true); // there is no last trade or we couldn't select it.
}

I also tried another variation running through all the trades:

bool checkforlosses()                   
{                                       
   int losses=0;                        
   int i;                               
   for(i=0; i<OrdersHistoryTotal(); i++) 
   {                                                                          
       OrderSelect(i,SELECT_BY_POS,MODE_HISTORY); 
       
       if(OrderProfit()<0) losses++;
       else                losses=0; 
   }
   if(losses>0)  return(true);          
                                        
   return(false);                     
}

Is anyone aware about this kind of issue with OrdersHistoryTotal() in the Strategy Tester? Many thanks in advance.

 

An example of what my Journal looks like is this:

2014.02.13 03:29:20.485 2013.01.30 23:45 AtrWithEma GBPJPY,M15: #53 2013.01.25 14:45:00 buy 0.30 GBPJPY 144.064 143.899 144.339 2013.01.25 14:52:30 143.899 0.00 0.00 -48.45 Buy Order[sl] 123

2014.02.13 03:29:20.485 2013.01.30 23:30 AtrWithEma GBPJPY,M15: #53 2013.01.25 14:45:00 buy 0.30 GBPJPY 144.064 143.899 144.339 2013.01.25 14:52:30 143.899 0.00 0.00 -48.45 Buy Order[sl] 123

2014.02.13 03:29:20.485 2013.01.30 23:15 AtrWithEma GBPJPY,M15: #53 2013.01.25 14:45:00 buy 0.30 GBPJPY 144.064 143.899 144.339 2013.01.25 14:52:30 143.899 0.00 0.00 -48.45 Buy Order[sl] 123

2014.02.13 03:29:20.485 2013.01.30 23:00 AtrWithEma GBPJPY,M15: #53 2013.01.25 14:45:00 buy 0.30 GBPJPY 144.064 143.899 144.339 2013.01.25 14:52:30 143.899 0.00 0.00 -48.45 Buy Order[sl] 123

2014.02.13 03:29:20.485 2013.01.30 22:45 AtrWithEma GBPJPY,M15: #53 2013.01.25 14:45:00 buy 0.30 GBPJPY 144.064 143.899 144.339 2013.01.25 14:52:30 143.899 0.00 0.00 -48.45 Buy Order[sl] 123

2014.02.13 03:29:20.485 2013.01.30 22:30 AtrWithEma GBPJPY,M15: #53 2013.01.25 14:45:00 buy 0.30 GBPJPY 144.064 143.899 144.339 2013.01.25 14:52:30 143.899 0.00 0.00 -48.45 Buy Order[sl] 123

2014.02.13 03:29:20.485 2013.01.30 22:15 AtrWithEma GBPJPY,M15: #53 2013.01.25 14:45:00 buy 0.30 GBPJPY 144.064 143.899 144.339 2013.01.25 14:52:30 143.899 0.00 0.00 -48.45 Buy Order[sl] 123

2014.02.13 03:29:20.485 2013.01.30 22:00 AtrWithEma GBPJPY,M15: #53 2013.01.25 14:45:00 buy 0.30 GBPJPY 144.064 143.899 144.339 2013.01.25 14:52:30 143.899 0.00 0.00 -48.45 Buy Order[sl] 123

2014.02.13 03:29:20.485 2013.01.30 21:45 AtrWithEma GBPJPY,M15: #53 2013.01.25 14:45:00 buy 0.30 GBPJPY 144.064 143.899 144.339 2013.01.25 14:52:30 143.899 0.00 0.00 -48.45 Buy Order[sl] 123

2014.02.13 03:29:20.485 2013.01.30 21:30 AtrWithEma GBPJPY,M15: #53 2013.01.25 14:45:00 buy 0.30 GBPJPY 144.064 143.899 144.339 2013.01.25 14:52:30 143.899 0.00 0.00 -48.45 Buy Order[sl] 123

2014.02.13 03:29:20.485 2013.01.30 21:15 AtrWithEma GBPJPY,M15: #53 2013.01.25 14:45:00 buy 0.30 GBPJPY 144.064 143.899 144.339 2013.01.25 14:52:30 143.899 0.00 0.00 -48.45 Buy Order[sl] 123

2014.02.13 03:29:20.484 2013.01.30 21:00 AtrWithEma GBPJPY,M15: #53 2013.01.25 14:45:00 buy 0.30 GBPJPY 144.064 143.899 144.339 2013.01.25 14:52:30 143.899 0.00 0.00 -48.45 Buy Order[sl] 123

2014.02.13 03:29:20.484 2013.01.30 20:45 AtrWithEma GBPJPY,M15: #53 2013.01.25 14:45:00 buy 0.30 GBPJPY 144.064 143.899 144.339 2013.01.25 14:52:30 143.899 0.00 0.00 -48.45 Buy Order[sl] 123

2014.02.13 03:29:20.484 2013.01.30 20:30 AtrWithEma GBPJPY,M15: #53 2013.01.25 14:45:00 buy 0.30 GBPJPY 144.064 143.899 144.339 2013.01.25 14:52:30 143.899 0.00 0.00 -48.45 Buy Order[sl] 123

2014.02.13 03:29:20.484 2013.01.30 20:15 AtrWithEma GBPJPY,M15: #53 2013.01.25 14:45:00 buy 0.30 GBPJPY 144.064 143.899 144.339 2013.01.25 14:52:30 143.899 0.00 0.00 -48.45 Buy Order[sl] 123

2014.02.13 03:29:20.484 2013.01.30 20:00 AtrWithEma GBPJPY,M15: #53 2013.01.25 14:45:00 buy 0.30 GBPJPY 144.064 143.899 144.339 2013.01.25 14:52:30 143.899 0.00 0.00 -48.45 Buy Order[sl] 123

2014.02.13 03:29:20.484 2013.01.30 19:45 AtrWithEma GBPJPY,M15: #53 2013.01.25 14:45:00 buy 0.30 GBPJPY 144.064 143.899 144.339 2013.01.25 14:52:30 143.899 0.00 0.00 -48.45 Buy Order[sl] 123

2014.02.13 03:29:20.484 2013.01.30 19:30 AtrWithEma GBPJPY,M15: #53 2013.01.25 14:45:00 buy 0.30 GBPJPY 144.064 143.899 144.339 2013.01.25 14:52:30 143.899 0.00 0.00 -48.45 Buy Order[sl] 123

2014.02.13 03:29:20.483 2013.01.30 19:15 AtrWithEma GBPJPY,M15: #53 2013.01.25 14:45:00 buy 0.30 GBPJPY 144.064 143.899 144.339 2013.01.25 14:52:30 143.899 0.00 0.00 -48.45 Buy Order[sl] 123

2014.02.13 03:29:20.483 2013.01.30 19:00 AtrWithEma GBPJPY,M15: #53 2013.01.25 14:45:00 buy 0.30 GBPJPY 144.064 143.899 144.339 2013.01.25 14:52:30 143.899 0.00 0.00 -48.45 Buy Order[sl] 123

2014.02.13 03:29:20.483 2013.01.30 18:45 AtrWithEma GBPJPY,M15: #53 2013.01.25 14:45:00 buy 0.30 GBPJPY 144.064 143.899 144.339 2013.01.25 14:52:30 143.899 0.00 0.00 -48.45 Buy Order[sl] 123


 

You don't loop through the closed trades, so you can only expect the one trade to be printed

In your second piece of code, you don't print anything

 
GumRai:

You don't loop through the closed trades, so you can only expect the one trade to be printed

In your second piece of code, you don't print anything


Hi mate, the second one was just another way to do it. The Print message is just informative. Both functions return a boolean to indicate if the last one is a winner (the first snippet) or if the last trade is a loser (second snippet).

The point is that the functions always return the same value, which corresponds to the last trade in the whole period (number 53) instead of the last trade at the moment of calling the function. How is it possible that trade 53 is printed at 18:45 when that trade is executed in the tester at 22:45?

Can you put the code of how you would do it? I don't get what you mean. the second snippet (checkforlosses) loops through all the closed trades and it returns always true because trade 53 is a loser, when I have about 40% winners.

 
fonx:


Hi mate, the second one was just another way to do it. The Print message is just informative. Both functions return a boolean to indicate if the last one is a winner (the first snippet) or if the last trade is a loser (second snippet).

The point is that the functions always return the same value, which corresponds to the last trade in the whole period (number 53) instead of the last trade at the moment of calling the function. How is it possible that trade 53 is printed at 18:45 when that trade is executed in the tester at 22:45?

Did you look in the log file ? the Journal can't keep up when you are printing so frequently . ..
 
RaptorUK:
Did you look in the log file ? the Journal can't keep up when you are printing so frequently . ..


You hit the nail RaptorUK. What a huge headache yesterday trying to find the problem. MT4 Journal doesn't display the right information but the log does and that is what I need. Many thanks!
Reason: