Array out of range?

 

In my mind I have the for loop and the array set correctly what is causing this array out of range error?

Keep in mind I am brand new to programming and this is my first attempt at a functioning program.

//+------------------------------------------------------------------+
//|                               My_Currency_Strength_Indicator.mq4 |
//|                                               Erick_Fenstermaker |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Erick_Fenstermaker"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#include <Arrays\ArrayObj.mqh>


//+------------------------------------------------------------------+
//| 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[])

  {

//declare variables to hold the value of each individual currency strength
   int GBP = 0;
   int EUR = 0;
   int AUD = 0;
   int NZD = 0;
   int USD = 0;
   int CAD = 0;
   int CHF = 0;
   int JPY = 0;

//Gathers the EUR Symbol Values
   double EURGBP = SymbolInfoDouble("EURGBP",SYMBOL_ASK);
   double EURAUD = SymbolInfoDouble("EURAUD",SYMBOL_ASK);
   double EURNZD = SymbolInfoDouble("EURNZD",SYMBOL_ASK);
   double EURUSD = SymbolInfoDouble("EURUSD",SYMBOL_ASK);
   double EURCAD = SymbolInfoDouble("EURCAD",SYMBOL_ASK);
   double EURCHF = SymbolInfoDouble("EURCHF",SYMBOL_ASK);
   double EURJPY = SymbolInfoDouble("EURJPY",SYMBOL_ASK);
//Gathers the GBP Symbol Values
   double GBPAUD = SymbolInfoDouble("EURAUD",SYMBOL_ASK);
   double GBPNZD = SymbolInfoDouble("EURNZD",SYMBOL_ASK);
   double GBPUSD = SymbolInfoDouble("EURUSD",SYMBOL_ASK);
   double GBPCAD = SymbolInfoDouble("EURCAD",SYMBOL_ASK);
   double GBPCHF = SymbolInfoDouble("EURCHF",SYMBOL_ASK);
   double GBPJPY = SymbolInfoDouble("EURJPY",SYMBOL_ASK);
//Gathers the AUD Symbol Values
   double AUDNZD = SymbolInfoDouble("EURNZD",SYMBOL_ASK);
   double AUDUSD = SymbolInfoDouble("EURUSD",SYMBOL_ASK);
   double AUDCAD = SymbolInfoDouble("EURCAD",SYMBOL_ASK);
   double AUDCHF = SymbolInfoDouble("EURCHF",SYMBOL_ASK);
   double AUDJPY = SymbolInfoDouble("EURJPY",SYMBOL_ASK);
//Gathers the NZD Symbol Values
   double NZDUSD = SymbolInfoDouble("EURUSD",SYMBOL_ASK);
   double NZDCAD = SymbolInfoDouble("EURCAD",SYMBOL_ASK);
   double NZDCHF = SymbolInfoDouble("EURCHF",SYMBOL_ASK);
   double NZDJPY = SymbolInfoDouble("EURJPY",SYMBOL_ASK);
//Gathers the USD Symbol Values
   double USDCAD = SymbolInfoDouble("EURCAD",SYMBOL_ASK);
   double USDCHF = SymbolInfoDouble("EURCHF",SYMBOL_ASK);
   double USDJPY = SymbolInfoDouble("EURJPY",SYMBOL_ASK);
//Gathers the CAD Symbol Values
   double CADCHF = SymbolInfoDouble("EURCHF",SYMBOL_ASK);
   double CADJPY = SymbolInfoDouble("EURJPY",SYMBOL_ASK);
//Gathers the CHF Symbol Values
   double CHFJPY=SymbolInfoDouble("EURJPY",SYMBOL_ASK);
   
//declares arrays to store symbol values
double eurTOsymbols[7];

//Stores the EUR symbol values in an array
eurTOsymbols[0] = EURGBP;
eurTOsymbols[1] = EURAUD;
eurTOsymbols[2] = EURNZD;
eurTOsymbols[3] = EURUSD;
eurTOsymbols[4] = EURCAD;
eurTOsymbols[5] = EURCHF;
eurTOsymbols[6] = EURJPY;

   
//+------------------------------------------------------------------+
//|                      EUR vs All other pairs                      |
//+------------------------------------------------------------------+


//Compares all the EUR currency pairs to all the GBP currency pairs and assigns a value to the EUR variable

   for(int i=0; i < eurTOsymbols[7]; i++)
     {
      if(GBPAUD > eurTOsymbols[i])
        {
         EUR--;
        }
      else if(GBPAUD < eurTOsymbols[i])
        {
         EUR++;
        }

      if(GBPNZD > eurTOsymbols[i])
        {
         EUR--;
        }
      else if(GBPNZD < eurTOsymbols[i])
        {
         EUR++;
        }

      if(GBPUSD > eurTOsymbols[i])
        {
         EUR--;
        }
      else if(GBPUSD < eurTOsymbols[i])
        {
         EUR++;
        }

      if(GBPCAD > eurTOsymbols[i])
        {
         EUR--;
        }
      else if(GBPCAD < eurTOsymbols[i])
        {
         EUR++;
        }

      if(GBPCHF > eurTOsymbols[i])
        {
         EUR--;
        }
      else if(GBPCHF < eurTOsymbols[i])
        {
         EUR++;
        }

      if(GBPJPY > eurTOsymbols[i])
        {
         EUR--;
         JPY++;
        }
      else if(GBPJPY < eurTOsymbols[i])
        {
         EUR++;
         JPY--;
        }

//Compares all the EUR currency pairs to all the AUD currency pairs and assigns a value to the EUR variable

      if(AUDNZD > eurTOsymbols[i])
        {
         EUR--;
        }
      else if(AUDNZD < eurTOsymbols[i])
        {
         EUR++;
        }

      if(AUDUSD > eurTOsymbols[i])
        {
         EUR--;
        }
      else if(AUDUSD < eurTOsymbols[i])
        {
         EUR++;
        }

      if(AUDCAD > eurTOsymbols[i])
        {
         EUR--;
        }
      else if(AUDCAD < eurTOsymbols[i])
        {
         EUR++;
        }

      if(AUDCHF>eurTOsymbols[i])
        {
         EUR--;
        }
      else if(AUDCHF<eurTOsymbols[i])
        {
         EUR++;
        }

      if(AUDJPY > eurTOsymbols[i])
        {
         EUR--;
         JPY++;
        }
      else if(AUDJPY < eurTOsymbols[i])
        {
         EUR++;
         JPY--;
        }

//Compares all the EUR currency pairs to all the NZD currency pairs and assigns a value to the EUR variable

      if(NZDUSD > eurTOsymbols[i])
        {
         EUR--;
        }
      else if(NZDUSD < eurTOsymbols[i])
        {
         EUR++;
        }

      if(NZDCAD > eurTOsymbols[i])
        {
         EUR--;
        }
      else if(NZDCAD < eurTOsymbols[i])
        {
         EUR++;
        }

      if(NZDCHF > eurTOsymbols[i])
        {
         EUR--;
        }
      else if(NZDCHF < eurTOsymbols[i])
        {
         EUR++;
        }

      if(NZDJPY > eurTOsymbols[i])
        {
         EUR--;
         JPY++;
        }
      else if(NZDJPY < eurTOsymbols[i])
        {
         EUR++;
         JPY--;
        }
        
//Compares all the EUR currency pairs to all the USD currency pairs and assigns a value to the EUR variable

      if(USDCAD > eurTOsymbols[i])
        {
         EUR--;
        }
      else if(USDCAD < eurTOsymbols[i])
        {
         EUR++;
        }

      if(USDCHF > eurTOsymbols[i])
        {
         EUR--;
        }
      else if(USDCHF < eurTOsymbols[i])
        {
         EUR++;
        }

      if(USDJPY > eurTOsymbols[i])
        {
         EUR--;
         JPY++;
        }
      else if(USDJPY < eurTOsymbols[i])
        {
         EUR++;
         JPY--;
        }

//Compares all the EUR currency pairs to all the CAD currency pairs and assigns a value to the EUR variable

      if(CADCHF > eurTOsymbols[i])
        {
         EUR--;
        }
      else if(CADCHF < eurTOsymbols[i])
        {
         EUR++;
        }

      if(CADJPY > eurTOsymbols[i])
        {
         EUR--;
        }
      else if(CADJPY < eurTOsymbols[i])
        {
         EUR++;
        }
        
//Compares all the EUR currency pairs to all the CHF currency pairs and assigns a value to the EUR variable
        
      if(CHFJPY > eurTOsymbols[i])
        {
         EUR--;
         JPY++;
        }
      else if(CHFJPY < eurTOsymbols[i])
        {
         EUR++;
         JPY++;
        }
        Print("The EUR relative strength is: ", EUR);
        }

//--- return value of prev_calculated for next call

   return(rates_total);
  }
//+------------------------------------------------------------------+
 
OK just shooting in the dark here but I am thinking that maybe the for loop i variable is actually looking at the value stored in the array position rather than the array position numerical value itself?
 
For start : index of last element of
eurTOsymbols

is 6 not 7 - as in this line :

eurTOsymbols[7]
 
True, however in my for loop I'm saying less than 7 which would be 6 correct?
 
Mladen Rakic:
For start : index of last element of

is 6 not 7 - as in this line :

So do you mean that I need to go by the index of the last element? because there are a total of 7 numbers if you include the first index of zero correct?

 
SirFency:
. . . 

So this line is your offending line:

for(int i=0; i < eurTOsymbols[7]; i++)

As  @Mladen Rakic pointed out, you cannot reference eurTOsymbols[7] because there are only 7 elements total, i.e. 0, 1, 2, 3, 4, 5, and 6.

However, there is additionally something fundamentally wrong with this line. You want i to loop over the array, so you want:
for(int i=0; i < 7; i++)

Another way to say it is:

for(int i=0; i < ArraySize(eurTOsymbols); i++)
You really should read an MQL introduction book or tutorial page on arrays before posting additional questions on this forum.
 
Anthony Garot:

So this line is your offending line:

As  @Mladen Rakic pointed out, you cannot reference eurTOsymbols[7] because there are only 7 elements total, i.e. 0, 1, 2, 3, 4, 5, and 6.

However, there is additionally something fundamentally wrong with this line. You want i to loop over the array, so you want:

Another way to say it is:

You really should read an MQL introduction book or tutorial page on arrays before posting additional questions on this forum.

Thank you for providing me with the tutorial page and explaining how to use ArraySize. I was trying to use eurTOsymbols.length for the same purpose earlier but this was not the correct syntax. This has fixed the issue with the error being in the for loop itself. Now however, I get the same error in my if statement. It runs though 7 times then throws the error. Why would it continue to run if it has reached the end of the for loop? Shouldn't it stop? There is something basic I'm missing here and the documentation is not clear to me.

 
I found the issue. Thank you for the help.
Reason: