![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
MetaTrader Version: 4.00 Build 224 (15 May 2009)
Metaeditor Version: 4.00 Build 224 (14 May 2009)
I have encountered a very strange bug in MQL that has me completely baffled, so much so that I am looking back at ALL my old code to make sure that it is written to avoid this problem.
I hope someone can shed some light on what exactly is going on here.
Here is the code, a simple indicator that essentially does nothing. However in the init() section I have placed a call to 2 functions when the chart period is H1.
The two functions are identical in all but one respect. One has a static int array defined and the other has a non-static int array defined. All the functions do is print out the size of its locally defined array, assign some values to the array elements and then print them out.
#property indicator_chart_window
void afunc() {
int i;
int array[4];
array[0] = 100;
array[1] = 101;
array[2] = 102;
array[3] = 103;
Print("afunc array[4] size="+ArraySize(array));
for (i = 0; i < 4; i++) {
Print("afunc array["+i+"]="+array[i]);
}
}
void afunc_static() {
int i;
static int array[4];
array[0] = 100;
array[1] = 101;
array[2] = 102;
array[3] = 103;
Print("afunc_static array[4] size="+ArraySize(array));
for (i = 0; i < 4; i++) {
Print("afunc_static array["+i+"]="+array[i]);
}
}
int init() {
if (Period() == PERIOD_H1) {
afunc();
afunc_static();
}
return(0);
}
int deinit() {
return(0);
}
int start() {
return(0);
}
However, if I attach the indicator to an M5 chart and then switch to the H1 timeframe I get the following output.
2009.05.23 18:12:47 afunc EURUSD,H1: afunc_static array[3]=103
2009.05.23 18:12:47 afunc EURUSD,H1: afunc_static array[2]=102
2009.05.23 18:12:47 afunc EURUSD,H1: afunc_static array[1]=101
2009.05.23 18:12:47 afunc EURUSD,H1: afunc_static array[0]=100
2009.05.23 18:12:47 afunc EURUSD,H1: afunc_static array[4] size=4
2009.05.23 18:12:47 afunc EURUSD,H1: afunc array[3]=0
2009.05.23 18:12:47 afunc EURUSD,H1: afunc array[2]=0
2009.05.23 18:12:47 afunc EURUSD,H1: afunc array[1]=0
2009.05.23 18:12:47 afunc EURUSD,H1: afunc array[0]=0
2009.05.23 18:12:47 afunc EURUSD,H1: afunc array[4] size=0
2009.05.23 18:12:47 afunc EURUSD,M5: uninit reason 3
2009.05.23 18:12:47 afunc EURUSD,M5: deinitialized
2009.05.23 18:12:45 afunc EURUSD,M5: initialized
2009.05.23 18:12:43 afunc EURUSD,M5: loaded successfully
Notice that I have defined 'static int array[4]' in afunc_static() and the size is correctly shown as 4 as are the values I assigned to the elements of the array. This function works fine.
Bottom line. If you define a non-static array in a function and if that function does not get called by your indicator then you could have a problem.
If you subsequently switch the chart timeframe and then call the function the size of any locally defined arrays in the function will be zero irresepective of what you have defined the size of the arrays to be.
The only way around this problem as far as I can see is to make sure that any locally defined arrays in your functions are defined as being static arrays. Nuts I know, but I cannot see any other way to avoid the problem.
I really would appreciate any feedback on this issue.
Regards,
Laurence.