Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 808

 
Kos Mos:

So I'm trying to find out what it is I'm failing at, I understand that no one is likely to tell me anything for free - capitalism, for fuck's sake)). I have nothing against it, I just need to understand why trades open on every candle in the buy direction.

I was not answering to you.

If you want your skis to work, you have at least to print the values of the variables that are important when the signal is received and look in the log to see what happens with them.

 

There is a loop for(int i=0;i<ArrayRange(arr2,0);i++), so I want to know how many times this calculation ArrayRange(arr2,0) is performed: once and the machine remembers the value, or every time the loop is running, the value is returned from ArrayRange(arr2,0) function? Example:

1st option.

i=0,i<(returned value), increment, then

i<(return value again), increment, then

i<(again returns a value), increment this way,

2nd variant.

or, we return a value from this calculation ArrayRange(arr2,0) once, let it return 5 and the machine remembers this 5 and get the following result

i=0,i<(returned value)i<5, increment, then

i<5, increment, then

i<5, increment - is this how it works?

I'm asking why: if everything happens by the first variant, then it makes sense to extract this value before the loop and store it in a variable, so that the program won't calculate the ArrayRange(arr2,0) function on every loop pass.

 
Seric29:

There is a loop for(int i=0;i<ArrayRange(arr2,0);i++), so I want to know how many times this calculation ArrayRange(arr2,0) is performed: once and the machine remembers the value, or every time the loop is running, the value is returned from ArrayRange(arr2,0) function? Example:

1st option.

i=0,i<(returned value), increment, then

i<(return value again), increment, then

i<(again returns a value), increment this way,

2nd variant.

or, we return a value from this calculation ArrayRange(arr2,0) once, let it return 5 and the machine remembers this 5 and get the following result

i=0,i<(returned value)i<5, increment, then

i<5, increment, then

i<5, increment - is this how it works?

Why I'm asking: if everything happens on the first variant, then it makes sense before the loop to first extract this value and store it in a variable, so that on each pass of the loop the program doesn't make the function ArrayRange(arr2,0).

in general case - 1st case.

PS/ 2nd is possible only for "optimizing" compilers, which can understand that if there is no ArrayResize inside a loop, then ArrayRange:=const ; I'm not sure that mql compiler already digs so deep into code, though for its "native" functions this is theoretically possible.

PPS/ The ArrayRange() example isn't quite correct, because there probably won't be a direct function call, the compiler just has to reduce it to comparing i to an internal array variable, because it knows everything about arrays.

 
Maxim Kuznetsov:

in general case - 1st variant.

PS/ 2nd is possible only for "optimizing" compilers, which can understand that if there is no ArrayResize inside loop, then ArrayRange:=const ; not sure if mql compiler already digs that deep into code, though for its "native" functions it is theoretically possible.

PPS/ example with ArrayRange() is not quite correct, because there probably won't be a direct function call, compiler just has to reduce it to comparison of i with internal array variable, because it knows everything about arrays.

Let's take the5+5*5 expression

the 1st variant.

i=0,i<(5+5*5), increment, then

i<(5+5*5), increment, then

i<(5+5*5), increment this way,

2nd variant.

or, in this case, the result is a single return of 5+5*5, let them return 30 and the machine remembers this 30-ku, so we get the following results

i=0,i<(returned value) i<30, increment, next (the engine automatically remembers 30 because I didn't save 30 somewhere)

i<30, increment, then

i<30, increment, is this how it works?

I take it it will be the 1 st variant? 5+5*5 is an expression that will be evaluated on every iteration of the loop. You can calculate this expression before the loop and store 30 in a variable and substitute it in the loop, and this will be easier for the program to use, unless it automatically calculates this 30 and stores it in its memory.


If there is no function call, the compiler can not check the boolean expression i<5, so probably the first option will work.

 

The result of a function(ArrayRange) can be changed within a loop or function.
And the constant expression(5+5*5) will always be the same.

Therefore, the function will be calculated on every pass.
And the constant value will be substituted into the code by the compiler at compilation time.

 
Taras Slobodyanik:

The result of a function(ArrayRange) can be changed within a loop or function.
And the constant expression(5+5*5) will always be the same.

Therefore, the function will be calculated on each pass.
And the constant value will be substituted into the code by the compiler at compilation time.

5+5*5 is also an expression and it needs to be calculated at each pass; as for the function, it is expected to return the same result; the point is that at each pass of the loop the compiler will still calculate both the function and the expression 5+5*5. So it will be the first option - i.e. the calculation is performed at each pass of the loop, so it makes sense to count this value before the loop and store it in a variable and use the variable as the same static value so no calculations take place at each pass of the loop, this eventually makes my

int k=5+5*5;//k=30

for(int i=0;i<k;i++){}

where k is always 30 or

int k=ArrayRange(arr2,0)//k=5

for(int i=0;i<k;i++){}

where k always equals 5.

 
Seric29:

5+5*5 is also an expression and should be calculated on every pass, as for the function it is supposed to return the same result, the point is that on every pass of the loop the compiler will still count both the function and the expression 5+5*5. So it will be the first option - i.e. the calculation is performed on every pass of the loop, so it makes sense to count this value before the loop and store it in a variable and use the variable as the same static value so no calculations take place on every pass of the loop, it eventually can

int k=5+5*5;//k=30

for(int i=0;i<k;i++){}

where k is always 30 or

int k=ArrayRange(arr2,0)//k=5

for(int i=0;i<k;i++){}

where k always equals 5.

You could simply expand the loop and not create extra variables.

 
Seric29:

5+5*5 is also an expression and needs to be calculated at each pass, as for the function it is supposed to return the same result, we are talking about the fact that at each pass of the cycle the compiler will still count both the function and the expression 5+5*5. So there will be the first option - i.e. the calculation is done at each pass of the cycle, so it makes sense to count this value before the cycle and store in a variable and use the variable as the same static value so there are no calculations at each pass of the cycle, it eventually mo

The compiler always works once - at the moment of compiling the text into the code.
(as far as I remember MT4 already compiles directly to code too)

5+5*5 - this is an expression with constants, they don't need to be recalculated - so it will only be calculated once, at compile time, and the code will contain the number 30

5+5*5+i is an expression with a variable, and this will be calculated in the code on each pass as 30+i

TheArrayRange function returns a value, but the compiler doesn't know what this value will be, so it will substitute the function call in the code, and at each pass (in the code) the function will be called
 
Taras Slobodyanik:

The compiler always works once - at the moment of text compilation into code.
(as far as I remember MT4 already compiles directly to code)

5+5*5 is an expression with constants, they don't need to be recalculated - so this will only be calculated once, at compile time, and in the code it will be 30

5+5*5+i is an expression with a variable, and it will be calculated in the code on each pass as 30+i

TheArrayRange function returns a value, but the compiler does not know what this value will be, so it will substitute the function call in the code, and at each pass (in the code) the function will be called

I see. Thank you. It is not a serious question, of course, but there are subtleties.

 
Artyom Trishkin:

You can simply expand the loop and not create extra variables.

in my case, there are 2 mathematical expressions 1st-ArrayRange(arr2,0) 2nd5+5*5 my goal is to create conditions under which the compiler will work with less load

for(int i=0;i<ArrayRange(arr2,0);i++) in this case the comparison value is returned at each pass

int k=ArrayRange(arr2,0)//k=5 for(int i=0;i<k;i++){}in this case the comparison value is returned 1 time instead of 4 times, but a variable is created, nevertheless the load is lower, especially when we are talking about thousands or millions.

Can you show us how to expand a loop so that we don't have to create variables?

Reason: