Potential bug with 1st tick of new bar in strategy tester

 

When a new bar appears in the strategy tester, I'm seeing that indicator buffers don't appear to get filled correctly.

For the first tick of a new bar, if I call CopyRates function I can correctly see index 0 in the array contains the values for the opening tick of the bar, and index 1 contains the closing values from the previous bar. 

However if I call CopyBuffer for any MT5 standard indicator for the same tick, the returned data has not updated to reflect the new bar and index 0 still refers to the previous bar. I have to wait for one more tick to appear.

I've tried two different versions of MT5 from two different brokers, with different timeframes, and so far all produce the same issue. 

Here is a simple EA.


int                MAPeriod=200;
ENUM_MA_METHOD     MAType = MODE_EMA;
ENUM_APPLIED_PRICE PriceType=PRICE_TYPICAL;

int               _handle;

long _newBarTick;
long _tickCount = 0;
long _currentBarCount = 0;

datetime _previousTime;

int OnInit()
{      
   _handle = iTEMA(_Symbol,PERIOD_CURRENT,MAPeriod,0,PriceType);
   //_handle = iMA(_Symbol,PERIOD_CURRENT,MAPeriod,0,MODE_EMA,PriceType);
   
   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
   IndicatorRelease(_handle);   
}

void OnTick()
{   
   _tickCount++;
   
   datetime currentBarTime = iTime(_Symbol,PERIOD_CURRENT,0);        
   bool isActualNewBar = (currentBarTime != _previousTime);
      
   if (isActualNewBar)
   {
      _previousTime = currentBarTime;            
      _currentBarCount++;            
                
      _newBarTick = _tickCount;

      MqlRates currentRates[];
      ArraySetAsSeries(currentRates,true);
      int count = CopyRates(_Symbol,_Period,0,5,currentRates);
      
      double buffer[];
      ZeroMemory(buffer);
      ArraySetAsSeries(buffer,true);
            
      int bufferCount = CopyBuffer(_handle,0,0,5,buffer);
      bool b = true;      
   }
   return;
}

I just put a breakpoint after the CopyBuffer function so I can view the results.


I attached some screenshots captured from the strategy tester at the first tick of a new bar. The chart hasn't refreshed at this point as execution is waiting on the breakpoint. We can see the chart value for the indicator for the previous bar (datetime at 01:18) is at 37761.600.

And from the watchlist we can see that the currentRates array shows index 0 has rolled into the new bar time of 01:19, however the buffer array at index 0 for the indicator is still showing the value for the previous bar.

Can anyone verify if they see the same behaviour?

I can obviously work around this by waiting for the 2nd tick, however this is currently having a clear impact on results in the strategy tester that trigger off a new bar.


Files:
image1.png  19 kb
image2.png  29 kb
 
Known bug, and MetaQuotes is already aware about it.
 

@Alain Verleyen thanks for your reply.

I searched on this forum and couldn't see any references to the same issue. How do you know it's a known bug? They've made multiple releases and not fixed the issue, are you sure they know about it?

 
jedster7594 #:

@Alain Verleyen thanks for your reply.

I searched on this forum and couldn't see any references to the same issue. How do you know it's a known bug? They've made multiple releases and not fixed the issue, are you sure they know about it?

Yes. It's documented. And it's communicated.

Until now the bug exists.