Because you cannot assign values to array elements in the global scope.
Either assign values inside any function
double get_high_or_low[2]; void OnStart() { get_high_or_low[0] = 30.0; get_high_or_low[1] = 20.0; }
or initialize the array correctly
double get_high_or_low[2] = {20.0, 30.0}; void OnStart() { }https://www.mql5.com/en/docs/basis/variables/initialization
Even the documentation is inaccurate. MQL5 Programming Basics: Arrays - MQL5 Articles
This is not documentation, but an article that is more than 10 years old, by the way. Nevertheless:
Array Initialization
It is sometimes necessary to fill an array with values immediately upon its declaration. Suppose you want to create several buttons of the same type and arrange them in a row, with each button having its own text. That is where the great advantages of arrays come into play. There is no need to copy the code for each button (there may be dozens of them), nor there is need to repeatedly call the same function. You can create the necessary number of buttons by iterating over the array in a loop having written the function call code only once.
We simply declare an array and immediately assign values to its elements:
string Variable[] = {"Button 1", "Button 2", "Button 3"};
Declared this way, the array will still be static despite the fact that its size is not specified. This is because the number of its elements is defined by the list of values (in curly brackets).
There will be no mistake if you specify the number of array elements:
string Variable[3] = {"Button 1", "Button 2", "Button 3"};
But it would be better not to do it - in the course of further improvements to the program, you may need to change the list of array values and use greater or lesser number of elements. To determine the size of the array in the parts of the code where it is used, it is recommended to employ the ArraySize() function instead of a certain numeric value. This approach allows you to only change the list of values without interfering with the main code. It will be more appropriate to declare the variable for the array size and assign to it the value obtained by the ArraySize() function when initializing the program.
If a static array cannot be initialized by the list of values, it would be better to use a constant to specify the array size. In general, we follow the principle of reducing the amount of code that would require to be modified should any further improvements of the program be necessary. If you need to fill all array elements with the same values, use the ArrayInitialize() function:
ArrayInitialize(Variable,1);
After executing the above code, all Var array elements will have the value of 1. If the same values need to be assigned only to some of the array elements, we use the ArrayFill() function:
double Variable[4]; ArrayFill(Variable,0,2,1); ArrayFill(Variable,2,2,2);
After executing this code, elements 0 and 1 will have the value of 1, while elements 2 and 3 will have the value of 2.
Right, my patience has snapped.
Why is MQL5 falling to realise that I'm trying to code an array?
This is not an array thing, it is a general programming constraint, look at your code:
double get_high_or_low[2]; <= your array declaration get_high_or_low[0] = 30; <= individual assignments not declarations - so these are general programming statements get_high_or_low[1] = 20; <= and should be inside a function and not in the global scope.
so the same would apply to any variable for instance
int iValue; <= declaration is valid iValue = 0; <= assignment statement is not valid in global scope int iValue = 0; <= declaration with assignment is valid in global scope.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Right, my patience has snapped.
Why is MQL5 falling to realise that I'm trying to code an array?
Seriously, it's the most ridiculous, stupid thing I have ever come across.
Why am I getting,
Why am I getting ,
Why am I getting ,
Why am I getting ,
Why am I getting
This is my code - a MRE.
My array size is correctly initiated, my values are correct, everything is correct - except MQL5 doesn't think so.
I am so tired of seeing the same error messages over and over again.
Why is MQL5 having a major hissy fit over this? Is MQL5 normally as thick as pigshit or is this just an exception?
Even the documentation is inaccurate. MQL5 Programming Basics: Arrays - MQL5 Articles