CompoundValue may not work if you use more than 1 in the same study



Active member
I ran into an issue where CompoundValue does not work if you use more than 1 in the same study. This may not happen all the time, but the workaround I found was to just get rid of that function call, and do the calculations yourself.

You can see the documentation here (that kind of does a poor job describing the function):

Just use BarNumber() and initialize your variable yourself.

The 1st parameter is how many bars to initialize your value for (many times, just 1).
The 2nd parameter is the formula once your variable is initialized.
The 3rd parameter is the value to initialize your variable with.

Here's the code so you can test yourself.

declare lower;

input mode = {default UseCompoundValue, ManualCalculation};

def nan = double.NaN;
def bn = BarNumber();

# UseCompoundValue --------------------------------------------------------------------
def x = CompoundValue(2, x[1] + x[2], 1);
plot FibonacciNumbers1 = if (mode == mode.UseCompoundValue) then x else nan;

AddChartBubble(mode == mode.UseCompoundValue, x, x);

# ManualCalculation --------------------------------------------------------------------
def y;
if (bn == 1 or bn == 2) then {
    y = 1;
} else {
    y = y[1] + y[2];
plot FibonacciNumbers2 = if (mode == mode.ManualCalculation) then y else nan;

AddChartBubble(mode == mode.ManualCalculation, y, y);
And here it is in action!

A link so the image above which included the study/code/and layout as shown.

Compound Value Example

Happy Trading,
Kory Gill, @korygill