• Memorial Day Sale! Claim the biggest discount of the year with $50 off VIP using code MEM50. Sign up here.

Is it possible to use a dynamic value as constant in aggregation?

Zac

New member
Example code to demonstrate the problem.
I am trying to get the equivalent of:
Code:
average (AbsValue(hl2[0] - hl2[0]), AbsValue(hl2[1] - hl2[0]), AbsValue(hl2[2] - hl2[0]))
If you try:
Code:
plot b = average (AbsValue(hl2 - hl2[0]), 3);
Then hl2[0] is dynamic, which means it is evaluated as:
Code:
average (AbsValue(hl2[0] - hl2[0]), AbsValue(hl2[1] - hl2[1]), AbsValue(hl2[2] - hl2[2]))
If I can make hl2[0] as constant, then it will work:
Code:
average (AbsValue(hl2 - hl2[0]), 3);
                  ^     ^
            dynamic     static
However I do not see a way to do that.
Also tried a script, hoping that input will force the value to be constant inside the script, but it is still dynamic.
Code:
script c{
    input cons = 0.0;
    plot b = average (AbsValue(hl2 - cons), 100);
}
plot b = c(hl2);
Is there any solution for that?
 
Solution
Its not necessarily dynamic in regard to dynamic versus constant. Dynamic would be something like hl2[x + 1 * y] which is not allowed. To get a dynamic offset you would use GetValue() which would read as; GetValue(hl2,x + 1 * y). The issue is that you're dealing with how the script is iterated on a bar by bar basis. You would need to reference all of the prior bars from the current bar, rather than relying on the iteration as it passes through the chart. Also, hl2 and hl2[0] are the same thing.

Hard coded, it would be something like;

Ruby:
(AbsValue(hl2 - hl2[1]) + AbsValue(hl2 - hl2[2]) + AbsValue(hl2 - hl2[3])) / 3

But you would be much better off with something more like this;

Ruby:
Input Length...

Joshua

Well-known member
VIP
Its not necessarily dynamic in regard to dynamic versus constant. Dynamic would be something like hl2[x + 1 * y] which is not allowed. To get a dynamic offset you would use GetValue() which would read as; GetValue(hl2,x + 1 * y). The issue is that you're dealing with how the script is iterated on a bar by bar basis. You would need to reference all of the prior bars from the current bar, rather than relying on the iteration as it passes through the chart. Also, hl2 and hl2[0] are the same thing.

Hard coded, it would be something like;

Ruby:
(AbsValue(hl2 - hl2[1]) + AbsValue(hl2 - hl2[2]) + AbsValue(hl2 - hl2[3])) / 3

But you would be much better off with something more like this;

Ruby:
Input Length = 3;
def TotalHL =
    fold Index = 0 to Length
    with HL do
    HL + (hl2 - getValue(hl2,Index))
;
plot AvgHL = TotalHL / Length;
 
Solution

Zac

New member
Thanks, that solved it.

Is it possible to use fold in a study and use that study as a scan study filter?

Currently the version of the study without the fold works in the scan, and the version with the fold returns no results. On the chart both studies are identical to the last digit.
 
Top