"array out of range" error while accessing double &volume[]

 

This is a very simple practice indicator, with minimal code. I drag it to BTCUSD 15m chart and I get "array out of range" error. Why is it so and what is the workaround?


//+------------------------------------------------------------------+
//|                                                         temp.mq4 |
//|                                  Copyright 2025, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   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[])
  {

   if(rates_total > prev_calculated)
   {
      Print(volume[1]); // <-- HERE I GET THE ERROR
   }

   return(rates_total);
  }
 
verynewuser: This is a very simple practice indicator, with minimal code. I drag it to BTCUSD 15m chart and I get "array out of range" error. Why is it so and what is the workaround?

"volume[1]" is accessing the 2nd array element, given that array indexing starts at 0. So what would happen if the "rates_total" was less than 2?

It would give "array out of range" error. So always check that the "rates_total" is equal or greater than the number of elements you need to process.

if( rates_total >= 2 ) {
   Print( volume[ 1 ] );
};
 
Fernando Carreiro #:

"volume[1]" is accessing the 2nd array element, given that array indexing starts at 0. So what would happen if the "rates_total" was less than 2?

It would give "array out of range" error. So always check that the "rates_total" is equal or greater than the number of elements you need to process.

Although the snippet itself didn't solve my issue but was enough as a pointer for me. I logged some Print() statements and can see the size of 'volumes[]' array and 'prev_calculated' variable is ZERO. I can tackle it with a similar `if` condition but wondering why is it so?

 
Fernando Carreiro #:

"volume[1]" is accessing the 2nd array element, given that array indexing starts at 0. So what would happen if the "rates_total" was less than 2?

It would give "array out of range" error. So always check that the "rates_total" is equal or greater than the number of elements you need to process.

I updated the condition to the below and it doesn't print anything now. Can you guide me what is the issue with it?

if(rates_total > prev_calculated && ArraySize(volume) > 1)
   {
      Print("rates_total:", rates_total, " prev_calculated:", prev_calculated, " Array:", ArraySize(volume));
      Print(volume[1]);
   }
 

I replaced 'volume[1]' with "iVolume(0,0,1)" in my original snippet and it works.

But my original question is still there. Why 'volume[]' array is empty always?