Array out of range

 

I have problem with string array. Its says "array out of range" when I loop the following, It will only open "NZD/USD". Why?

 string symbol[]={"EURUSD","GBPUSD","AUDUSD","NZDUSD"};
 string symbol2[]={"USDJPY","USDCHF","USDCAD"};
  
  for(int a=ArrayResize(symbol,4)-1;a>=0;a--)
  {
   if(CountSymbol(Symbol())<1)
   {
    //OpenTrade........
   }
  }

When I Change it to:

for(int a=ArrayResize(symbol,3)-1;a>=0;a--)

It opens all the pairs expect  "NZD/USD" and no "array out of range" error. Please help I want all pairs to open without any "array out of range". 

 
Scalper8:I have problem with string array. Its says "array out of range" when I loop the following, It will only open "NZD/USD". Why? When I Change it to: It opens all the pairs expect  "NZD/USD" and no "array out of range" error. Please help I want all pairs to open without any "array out of range". 
string symbol[]={"EURUSD","GBPUSD","AUDUSD","NZDUSD"};
string symbol2[]={"USDJPY","USDCHF","USDCAD"};
  
for(int a = ArraySize(symbol)-1; a>=0; a-- )
{
   // something
};

Use "ArraySize" to get the current array size.

"ArrayResize" causes the array to be resized just as the name says.

EDIT: And as @R4tna C has correctly pointed out in the post below, you can only resize dynamic arrays.

 
Scalper8:

I have problem with string array. Its says "array out of range" when I loop the following, It will only open "NZD/USD". Why?

When I Change it to:

It opens all the pairs expect  "NZD/USD" and no "array out of range" error. Please help I want all pairs to open without any "array out of range". 

By creating an array this way, it is a static array so the resize function is not appropriate. I use MQL5 and see this warning, you may see something similar in MQL4.


Why do you need to re-size in the loop anyway? Keep it simple

   string symbol[] = {"EURUSD","GBPUSD","AUDUSD","NZDUSD"};
   string symbol2[] = {"USDJPY","USDCHF","USDCAD"};
   
   for(int a = ArraySize(symbol) - 1; a >= 0; a--)
     {
      PrintFormat("%d = %s", a, symbol[a]);
      //if(CountSymbol(Symbol())<1)
      //{
      // //OpenTrade........
      //}
     }

Output:

2022.08.22 19:48:34.976 3 = NZDUSD

2022.08.22 19:48:34.976 2 = AUDUSD

2022.08.22 19:48:34.976 1 = GBPUSD

2022.08.22 19:48:34.976 0 = EURUSD


 
Fernando Carreiro #:

Use "ArraySize" to get the current array size.

"ArrayResize" causes the array to be resized just as the name says.

EDIT: And as @R4tna C has correctly pointed out in the post below, you can only resize dynamic arrays.

Note taken, Now I want to add my 2nd string of array and add the same loop function but I don't get any error's but "NZDUSD" will not open trades why is that?

string symbol[]={"EURUSD","GBPUSD","AUDUSD","NZDUSD"};
string symbol2[]={"USDJPY","USDCHF","USDCAD"};
  
for(int a = ArraySize(symbol)-1; a>=0; a-- )
for(a = ArraySize(symbol2)-1; a>=0; a-- )
{
  //OpenTradeBuy

  //OpenTradeSell
};
 
R4tna C #:

By creating an array this way, it is a static array so the resize function is not appropriate. I use MQL5 and see this warning, you may see something similar in MQL4.


Why do you need to re-size in the loop anyway? Keep it simple

Output:

2022.08.22 19:48:34.976 3 = NZDUSD

2022.08.22 19:48:34.976 2 = AUDUSD

2022.08.22 19:48:34.976 1 = GBPUSD

2022.08.22 19:48:34.976 0 = EURUSD


I had no idea I had to use Arraysize

 
Scalper8 #:

Note taken, Now I want to add my 2nd string of array and add the same loop function but I don't get any error's but "NZDUSD" will not open trades why is that?

Again keep it simple - do not re-use variable "a" for the nested loop, you will lose track of what's going on. Create a separate loop control variable for every loop

   string symbol[] = {"EURUSD","GBPUSD","AUDUSD","NZDUSD"};
   string symbol2[] = {"USDJPY","USDCHF","USDCAD"};


   for(int a = ArraySize(symbol) - 1; a >= 0; a--)
     {
      for(int b = ArraySize(symbol2) - 1; b >= 0; b--)
        {
         PrintFormat("symbol[%d]=%s  symbol2[%d]=%s", a, symbol[a],  b, symbol2[b]);

         //OpenTradeBuy

         //OpenTradeSell
        };
     };
 
Scalper8 #:

I had no idea I had to use Arraysize

Spend some time reading the documentation and get familiar with navigating it.

I learnt MQL5 quite recently - I find the documentation has been a godsend (same goes for every system I ever worked with)

 
R4tna C #:

Again keep it simple - do not re-use variable a for the nested loop, you will lose track of what's going on. Create a separate loop control variable for every loop

2022.08.22 22:39:54.274 symbol[3]=NZDUSD  symbol2[2]=USDCAD

2022.08.22 22:39:54.274 symbol[3]=NZDUSD  symbol2[1]=USDCHF

2022.08.22 22:39:54.274 symbol[3]=NZDUSD  symbol2[0]=USDJPY

2022.08.22 22:39:54.274 symbol[2]=AUDUSD  symbol2[2]=USDCAD

2022.08.22 22:39:54.274 symbol[2]=AUDUSD  symbol2[1]=USDCHF

2022.08.22 22:39:54.274 symbol[2]=AUDUSD  symbol2[0]=USDJPY

2022.08.22 22:39:54.274 symbol[1]=GBPUSD  symbol2[2]=USDCAD

2022.08.22 22:39:54.274 symbol[1]=GBPUSD  symbol2[1]=USDCHF

2022.08.22 22:39:54.274 symbol[1]=GBPUSD  symbol2[0]=USDJPY

2022.08.22 22:39:54.274 symbol[0]=EURUSD  symbol2[2]=USDCAD

2022.08.22 22:39:54.274 symbol[0]=EURUSD  symbol2[1]=USDCHF

2022.08.22 22:39:54.274 symbol[0]=EURUSD  symbol2[0]=USDJPY


 
R4tna C #:

Again keep it simple - do not re-use variable "a" for the nested loop, you will lose track of what's going on. Create a separate loop control variable for every loop

Will do, thanks for the help!

Reason: