I agree, looks buggy.
Method:
Compile while indicator is attached to 30m chart
No printout (ok)
Change to H1
Printout occurs
Error occurs.
...
Compile while indicator is attached to H1 chart
Printout occurs
no error
Change to 30m
No printout (ok)
change to H1
Printout occurs
No error
...
4002 error noted
ERR_ARRAY_INDEX_OUT_OF_RANGE | 4002 | Attempt to access an array item number of which is out of the array range |
Was going to say something else, but then reproduced phy's scenario.
PS: another "workaround" would be to declare global variables.
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.
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.