PureBytes Links
Trading Reference Links
|
> diff=c-c[1];
> diff=average(diff,3);
> plot1(diff1,"diff");
>
> Someone said the problem happens because "diff" has already been
> used. I was trying to be thrifty (:-). However, I thought you were
> allowed to change the value of a variable by referencing itself such
> as:
> count=count+1;
>
> So I assumed my version would work. Is there some obtuse aspect of
> EL at work here?
No, just a programming error.
The average function calculates the average of the value of diff
on the last 3 bars. You want the 3-bar average of c-c[1]. But
what is the value of "diff" for the last 3 bars? On the current
bar, it's c-c[1]. But on the previous two bars, it's NOT c-c[1],
but the average you calculated on those bars!
So instead of calculating the average of (c-c[1]), (c-c[1])[1],
and (c-c[1])[2], which is what you wanted, you were calculating
the average value of (c-c[1]), average(diff,3)[1], and
average(diff,3)[2].
When you added in the additional diff1 variable, you no longer
overwrote diff with the average, so diff still contained the
difference of c-c[1], and the average of diff was correct.
If you want to use two separate vars like that, I suggest you
change diff1 to avgdiff or something like that so it's clear what
it is.
diff=c-c[1];
avgdiff=average(diff,3);
plot1(avgdiff,"avgdiff");
Gary
|