Array out of range error

Marcell Petras  

Hello everyone! I'm new to MQL5 programming and have been struggling with this issue for a while. I tried everything but couldn't get from point A to point B. Essentially, I want to loop through the previous six candles and add them to an array. However, I always get an array out of range error. I just don't get why. Can anyone lend me a hand? It would be really appreciated. 

#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

datetime timestamp;   
string candles_array[5];

    
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }


void OnDeinit(const int reason)
  {

   
  }


void OnTick()
  {
  
   
   datetime time = iTime(_Symbol,PERIOD_M1,0);
      
   if(timestamp != time){ //block to run after each new candle
   timestamp = time;
   
   for(int i = 1; i <= 6; i++){ //looping over the past 6 candles
      DetermineCandle(i-1);   
  /* 
      i : 1 -> array 0 
      i : 2 -> array 1
      i : 3 -> array 2
      i : 4 -> array 3
      i : 5 -> array 4
      i : 6 -> array 5
  */ 
   }
   
   
   } // block end
   
   
   Comment(candles_array[0],"\n",candles_array[1],"\n",candles_array[2],"\n",candles_array[3],"\n",candles_array[4],"\n",candles_array[5]);
    } //on tick end
  
   
   
 void DetermineCandle (int candle_index) {
   double open = iOpen(_Symbol,PERIOD_M1,candle_index);  
   double close = iClose(_Symbol,PERIOD_M1,candle_index);
   
   if (close > open) { //bullish candle
      candles_array[candle_index] = "Bullish";
      Print("Bullish candle added to the array");
   }
    
   else if (close < open){ //bearish candle
      candles_array[candle_index] = "Bearish";
      Print("Bearish candle added to the array");
   }
   
 
   
  }
   
   
Files:
William Roeder  
string candles_array[5];
⋮
   for(int i = 1; i <= 6; i++){ //looping over the past 6 candles
      DetermineCandle(i-1);   
  1. The array has five (5) elements; their indexes are [0 … 4]. When i equals six (6), DetermineCandle is called with five (5). It tries to update the array[5], array exceeded.
  2. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

Marcell Petras  
William Roeder #:
  1. The array has five (5) elements; their indexes are [0 … 4]. When i equals six (6), DetermineCandle is called with five (5). It tries to update the array[5], array exceeded.
  2. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)


Oh I see, thanks a lot!
Reason: