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.
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
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 };
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
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 }; };
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
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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".