Looking at ADX exactly 20 minutes ago...Help Please

 

I am currently working on making my EA able to look at the ADX exactly 20 minutes ago.  This is a continued idea from https://www.mql5.com/en/forum/144585 but I have worked over the weekend on this and think its appropriate to open another thread (if I'm wrong here, sorry administrators)

My code is close except when I backtest (over one day since it will have so many values) the spread from the instantaneous ADX value (from the iADX) to looking at the value 20 minutes in the future is on average 2 minutes early. This is not always the case so just adding a few more spaces to my array is not the answer. I have already tried that.  This is close, but any advice/suggestions are highly valued.  Thank you in advance. 

   static double ADX15minValues[81];
   static double ADX15eaSec;
   int second = TimeSeconds(TimeCurrent()); int minute = TimeMinute(TimeCurrent());
   
   int n=81; //20 minutes mult by 15 sec between each ADX value **(20*4)**
   int i;

   for(i=n-2;i>=0;i--)
   {
    ADX15minValues[i+1]=ADX15minValues[i];
   }
  

   if(second%15 == 0) //every 15 seconds
    {
     double ADX15Value = iADX(NULL,PERIOD_M15,5,PRICE_CLOSE,MODE_MAIN,0);
     ADX15minValues[0]=ADX15Value;
    /* string ADX15NOW = DoubleToStr(ADX15Value,6);
     Print("the instantaneous value of the 15M ADX is, ",ADX15NOW);*/
    }
    
   if(minute%2==0 && second%30==0 && ADX15minValues[80]!=NULL) //if array is maxed out and print every 2 minutes
   {
    string MINagoADX = DoubleToStr(ADX15minValues[80],6);
    Print("the ADX for EXACTLY 20 minutes ago was, ",MINagoADX);
   }
 

Maybe the following modified code will help (I find it simpler and thus, less prone to error and perhaps easier for another person to spot any issue(s) ... but this is just my opinion):

static double ADX15minValues[81];
static int count = 0;

int interval = 15;//15 seconds
int timeBack = 60 * 20;//20 minutes

static int waitTime = 0;
static bool isFirst = true;
if ( isFirst ) {//have to use isFirst for waitTime static to prevent TimeCurrent() - initialization expected error
   waitTime = TimeCurrent() + interval;
   isFirst = false;
}

if ( TimeCurrent() >= waitTime ) {
   waitTime = TimeCurrent() + interval;
   ADX15minValues[count] = iADX(NULL, PERIOD_M15, 5, PRICE_CLOSE, MODE_MAIN, 0);
   count++;
}

if ( count >= (timeBack / 15) ) {
   string MINagoADX = DoubleToStr(ADX15minValues[0], 6);
   Print("the ADX for EXACTLY 20 minutes ago was,  ", MINagoADX) ;
   
   count = 0;//reset count
}

Additionally, your 2 minute average delay could be attributed to other issues. For example, if the market is slow you may not be receiving ticks every second, thus it is quite possible that some of your 15 second intervals could actually be larger. You may want to try adding a tick sender script. The above code seemed to work for me when using a tick sender (seemed to be within +/-20 seconds or so) ...

Reason: