Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 263
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
"then loop fill"
If it's not too much trouble, a small example, may I?
You have been told that to fill a dynamic array MACDBuffer[], you should make it static with ArrayResize() (otherwise the result of assignment will always be zeros),
and, to ensure stability in adequacy of calculations, it's better to initialize the array with ArrayInitialize().
You have been told that to fill a dynamic array MACDBuffer[], you should make it static with ArrayResize() (otherwise the result of assignment will always be zeros),
and further, to ensure stability in adequacy of calculations, it's better to initialize the array with ArrayInitialize().
Thank you!
The matter is that there is a working Expert Advisor.
int CD=0; //order number of a closed trade
double Balance=0; // result of a closed trade
double Mas[]; // array of values for the results of a closed trade
How to fill Mas[] with results of closed transactions in int Start()?
Maybe something needs to be written in init() and deinit()? An initializer or something else?
What would the correct code look like?
The essence of the matter is that there is a running Expert Advisor.
int CD=0; // sequential number of a closed trade
double Balance=0; // result of a closed trade
double Mas[]; // array of values for the results of a closed trade
How in int Start() fill in Mas[] with results of closed trades?
Maybe, I need to add something in init() and deinit()? An initializer or something else?
What would the correct code look like?
Everyone has a different notion of "right".
Maybe you want Mas[CD]=Balance in initialization and assignment to Mas[CD]=Balance at start when you change CD value?
Or maybe you want to have a two-dimensional array where all necessary data (balance, time, points, max equity, min equity ....) will be stored for each transaction.
You have been told that to fill a dynamic array MACDBuffer[], you should make it static with ArrayResize() (otherwise the result of assignment will always be zeros),
and further, to ensure stability of adequacy of calculations, it's better to initialize the array with ArrayInitialize().
ArrayResize() changes the size of the array. In mql4 all arrays are static by default.
I was wrong. The tutorial confirms it:
If the size of a one-dimensional array to be initialized is not specified, it is determined by the compiler, based on the size of the initializing sequence. Array initialization can also be done using the standard ArrayInitialize() function. All arrays are static, i.e. have a static form, even if not specified explicitly during initialization. It means that all the elements of the array keep their values in the interval between calls of the function, in which the array is declared.
But why then won't such a "static" array assign values? The result is null as expected because of the ArrayResize(buffer,0) from the compiler?
I was wrong. The textbook confirms it:
But then why doesn't such a "static" array assign values ??? The result is null as expected because of ArrayResize(buffer,0) from the compiler?
Because the array is dynamic. Its size in the first dimension can be viewed with ArraySize() - for a one-dimensional array, and ArrayRange() - for a specified dimension of a multidimensional array.
Static does not mean "default size". Don't confuse the two concepts "static" and "dynamic". They are not antonyms. They are from different fields of application here. Analogy: A static array is a "static variable". A dynamic array is "a size that can be set and changed programmatically".
Thank you!
The matter is that there is a working Expert Advisor.
int CD=0; // ordinal number of a closed trade
double Balance=0; // result of a closed trade
double Mas[]; // array of values of closed trade results
How to fill Mas[] with results of closed transactions in int Start()?
Maybe something needs to be written in init() and deinit()? An initializer or something else?
What would the correct code look like?
I was wrong. The textbook confirms it:
But then why doesn't such a "static" array assign values ??? The result is null as expected because of ArrayResize(buffer,0) from the compiler?
Because it's not an array, to be precise. If you want more details - see description of the C language. I'm referring to it because from the very beginning the developers said that whatever is not clear or is incompletely explained - see C standard.
When you declare variable double buffer[]; then memory is allocated for one variable, which in C terms is called "pointer", has integer type and stores address of memory cell from which an array begins. If you check the dimensionality of the variable in MCL, you get the value 0. This means that the array itself isn't allocated memory and when you try to assign a value, there's nowhere to write it: In MCL there's no address arithmetic and pointers can't be handled. In C you can write it, but with "unallocated" memory it usually leads to a system crash. When you apply AreiResize(array,N), (N>0), this allocates memory for the array (at least one element) and creates space for writing values. Then, when checked, the array's dimension will be different from 0.
The concept of static array in terms of C has several meanings:
1. a static array (as well as a static variable/type) are such variables/arrays whose dimensions are known at the compilation stage (precompilation for MKL4) . For example, when double buff[10000]; is described, the compiler is told the size of the variable and can immediately allocate memory. In contrast to this concept, there is the concept of a "dynamic array", that is, such an array whose size is unknown at compile time and becomes known only at run-time; these are all arrays of variable size. When accessing such arrays, memory must be allocated beforehand. Memory is allocated outside the static program memory.
In C/C++, if an array is described in a block, its visibility is limited by the block - be it a static or a dynamic array. Errors in programs such as "memory leaks" are associated with dynamic arrays defined locally, i.e. when a memory pointer is destroyed because it leaves the visibility area, but the memory allocated to it remains occupied.
2 The concept of "static" array as a variable described with static modifier - such variables are not destroyed when leaving the scope of the block - they are located in a separate memory area - hence the values in them are preserved. The risk of memory leaks is reduced. An array declared as dynamic in the sense of p.1 (i.e. the array size is not known at the compilation stage), can also be static in the sense of p.2, that is, declared with the modifier "static".
When terminating programs, dynamically allocated memory must be released to avoid memory leaks. I don't know if developers of MKL follow this, but in my programs I always free memory - habit from C - ArreyResize(array,0);
In MKL all arrays are static in the sense of point 2.
Now regarding your question: "Whycan't a static array double buffer[]; be assigned values ?". Because no memory is allocated for it (in fact, it's a dynamic array with a static modifier). Before assigning values, memory should be allocated, i.e. ArrayResize with size greater than 0 should be applied to this array. For arrays, you need to control the memory overrun (outside the array).
SZ I hope I've made myself clear. I tried to keep it as simple as possible.