for

 
double resulttotal;

double resultaverage;

int totalbar = 10;



for(int x = 1; x <= totalbar; x++)

{resulttotal = resulttotal + iClose(NULL, 0 , x);

}



resultaverage = resulttotal / totalbar;

how the MT4 distinguish between the "resultTotal" before the = as a new total that will have a new value assigned to it after operating the left side after the equal, and "resultTotal" that is on the left side after the equal as old value?
 

Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button



 
Sergey Golubev:

noted , shall I repost the code ? 

 
Michael:

noted , shall I repost the code ? 

EDIT your original post, please do not just post the code correctly in a new post.

 
Keith Watford:

EDIT your original post, please do not just post the code correctly in a new post.

Done thank you

 
Michael:

how the MT4 distinguish between the "resultTotal" before the = as a new total that will have a new value assigned to it after operating the left side after the equal, and "resultTotal" that is on the left side after the equal as old value?
resulttotal = resulttotal + iClose(NULL, 0 , x);

I am not sure what your question is about.

It knows which is the new total because it is followed by =.

If a=2

What does a+1 equal? Obviously it is 3 because the original value no longer applies.

Incidentally, you can also write your calculation as

resulttotal += iClose(NULL, 0 , x);
 
Keith Watford:

I am not sure what your question is about.

It knows which is the new total because it is followed by =.

If a=2

What does a+1 equal? Obviously it is 3 because the original value no longer applies.

Incidentally, you can also write your calculation as

if a = 2 and then a + 1 then a = 3, yes thats right

what confusing me is that in the example I set resulttotal will have 2 different values because of the looping

if we said that resulttotal will start off by being equal to zero and we have a candle close that is equal to 1.9231, so it will be as follow


resulttotal = resulttotal + iClose(NULL, 0 , x);

            = 0           + 1.9231

so now I think the resulttotal will be equal to 1.9231

But on the second loop I see it as follow

1.9231      = 1.9231      + 1.9341

Which I does not understand unless I see the "resulttotal" that is before the equal as variable empty but it is not because it has a value of 1.9231 and this value has been applied by the system to the "resulttotal" that came after the = sign on the same line

So my question how the system see the first "resulttotal" that is before the equal still as a variable that can be changed while the "resulttotal" after the equal as a variable that has a value

 
Michael:

if a = 2 and then a + 1 then a = 3, yes thats right

what confusing me is that in the example I set resulttotal will have 2 different values because of the looping

if we said that resulttotal will start off by being equal to zero and we have a candle close that is equal to 1.9231, so it will be as follow



I think you confuse between between new value and current value;

//first loop
double resultTotal = 0;

for (int i=1; i<12;i++){
   resultTotal <--(new value) = resultTotal <--(current value = 0) + i;
}

In the first loop, resultTotal on the left hand side is equal to current value of resultTotal which is 0 plus i = 1;

So new value of resultTotal = 0 + 1; Which mean for a second loop, current value of resultTotal = 0 + 1;

//2nd loop
double resultTotal = 0;

for (int i=1; i<12;i++){
   resultTotal <--(new value) = resultTotal <--(current value = 1) + i;
}

in the second loop, resultTotal on the left hand side is equal to current value (= 1) plus i (=2);

So, the new value of resultTotal for a second loop = 1 + 2; Which mean for a third loop, current value for resultTotal now is = 1 + 2;

//3rd loop
double resultTotal = 0;

for (int i=1; i<12;i++){
   resultTotal <--(new value) = resultTotal <--(current value = 3) + i;
}

The same thing goes on and on until loop is finished or until the break command is called.

if you're thinking, why not resultTotal be reset to 0 on each of the loop?

you can do that by reassign resultTotal to zero value as follow:

double resultTotal = 0;

for (int i=1; i<12;i++){
   resultTotal = 0; //<-- this will assign 0 to the resultTotal which mean current value for resultTotal will always be 0 whenever the loop started and repeat.
   print(resultTotal);
   resultTotal <--(new value) = resultTotal <--(current value = 0) + i;
   print(resultTotal);
}

you can always check out the number using print(resultTotal) or comment(resultTotal);

Hope that help. Cheers.
 

 
Ahmad Zuhairdi Noh:


I think you confuse between between new value and current value;

In the first loop, resultTotal on the left hand side is equal to current value of resultTotal which is 0 plus i = 1;

So new value of resultTotal = 0 + 1; Which mean for a second loop, current value of resultTotal = 0 + 1;

in the second loop, resultTotal on the left hand side is equal to current value (= 1) plus i (=2);

So, the new value of resultTotal for a second loop = 1 + 2; Which mean for a third loop, current value for resultTotal now is = 1 + 2;

The same thing goes on and on until loop is finished or until the break command is called.

if you're thinking, why not resultTotal be reset to 0 on each of the loop?

you can do that by reassign resultTotal to zero value as follow:

you can always check out the number using print(resultTotal) or comment(resultTotal);

Hope that help. Cheers.
 

Thanks for the clarification I understand it , but what I am struggling with is since both are named "resulttotal" how the system could distinguish between the two as current value(old value resulttotal) and new value of resulttotal and compile correctly?

I mean if we set the equation to be as follow

resultTotal + i = resulttotal;

would it still compile and loop correctly and the "resulttotal" on the left side will be considered as the current value (old value of resulttotal) in this case and the right one is the new value of resulttotal?

if yes, I just need to know how the system was able to make this distinguish?

 
Michael:

I mean if we set the equation to be as follow

resultTotal + i = resulttotal;

No, because that doesn't make sense.

You wouldn't write 

2+1=2

would you?

 
Keith Watford:

No, because that doesn't make sense.

You wouldn't write 

2+1=2

would you?

no I would not and thats why I am asking how with the below the underlined "resulttotal" has been distinguished as value

resulttotal + i=
resulttotal;

while below has been distinguished as a variable?

resulttotal= resulttotal + i;
Reason: