Get previous two values from another indicator

 

Hello,

I have been struggling with this problem for too long now. How do I properly get previous two values of an indicator?

The code I came up with is below, but it goes out of range on the last candle, and I can't figure it out why.

//+------------------------------------------------------------------+
//|                                       TestIndicator_DoNotUse.mq5 |
//+------------------------------------------------------------------+
#property copyright "Matija Bosnic"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window

input int      ATR_Period=14;
input int      ATR_Factor=4;

int ATR_Handle;
double ATR_Buffer[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0, ATR_Buffer, INDICATOR_CALCULATIONS);
   //--- get ATR handle
   ATR_Handle = iATR(Symbol(), 0, ATR_Period);
   if(ATR_Handle == INVALID_HANDLE) {
      Print("Failed to get ATR handle!", GetLastError());
   }
//---
   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[])
  {
//---
   //--- check if there are enough bars for indicator
   if(rates_total < ATR_Period + 2)
      return 0;
      
   //--- how many ATR values needs copying
   int to_copy;
   if(prev_calculated > rates_total || prev_calculated <= 0) {
      to_copy = rates_total;
   } else {
      to_copy = rates_total - prev_calculated;
      if(prev_calculated > 0)
         to_copy=+ 2;
   }
   
   //--- get ATR values
   if(CopyBuffer(ATR_Handle, 0, 0, rates_total, ATR_Buffer) <= 0) {
      Print("Failed to get ATR! Error ", GetLastError());
      return(0);
   } 
   
   //--- how many bars need to be calculated
   int limit = prev_calculated -1;
   if(limit < 0)
      limit = 0;
      
   //--- main loop
   for(int i = limit; i < rates_total && !IsStopped(); i++) {   
      //--- Which price to calculate with
      
      Print("Buffer size: ", ArraySize(ATR_Buffer));
      Print("i value: ", i);
      double currentATR = ATR_Buffer[i];
      double previousATR = ATR_Buffer[i+1]; //array out of range

   }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+