iBarShift fails after call of CopyRates

 

Hello,

i came across a problem that iBarShift returns -1, when CopyRates returns less bars than requested and i call subsequently iBarShift.

This is demonstrated by the indicator attached.

This unexpected behaviour should be fixed.

// apply to EURUSD 1 Month
#property version   "1.00"
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
    // apply to EURUSD 1 Month
    datetime dt=iTime(_Symbol,PERIOD_CURRENT,1);
    // idx_last ok
    int idx=iBarShift(_Symbol,PERIOD_CURRENT,dt);
    PrintFormat("first call idx=%d" ,idx);
    
  
    ResetLastError();
    MqlRates         rates_array[];
    int ma_len=200;
    int offset=3 * ma_len;
    int bars_requested=2 * ma_len;
    // make sure count is less than bars_requested
    int count= CopyRates( _Symbol,PERIOD_MN1,offset,bars_requested,rates_array);
    
    // idx NOK
    idx=iBarShift(_Symbol,PERIOD_CURRENT,dt);    
    PrintFormat("second call idx=%d" ,idx);  
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+