Using CompoundValue() Function


Well-known 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
HI all,

I have a strategy that I've backtested and it works pretty well, and I was hoping to make it almost automated using conditional orders. The problem is that the study contains CompoundValue, which the conditional order creator says doesn't exist. Is there any sort of workaround for this?
Last edited by a moderator:
I am too facing same issue, I tried to achieve my logic using barNumber as CompoundValue was giving the below error

Error processing referenced script NV_MarketPulse: No such function: CompoundValue at 1:1

but barNumber is giving the same error.

Error processing referenced script NV_MarketPulse: No such function: barNumber at 1:1

I am not able to follow how same functions work in studies, but not in conditional orders when we use those studies. Hope somebody from thinkorswim will be able to fix this.
@korygill - I looked at one of your code using compound value and tried something, but in vain. Could you please help with below?

I am using this code in a watch list column on a 1min time frame.

def ct_5_up = CompoundValue(1, if EMA8 > EMA21 and EMA21 > EMA50 and EMA50 > EMA100 and EMA100 > EMA200 then ct_5_up[1] + 1 else 0, 1);
def ct_3_up = CompoundValue(1, if EMA50 > EMA100 and EMA100 > EMA200 then ct_5_up[1] + 1 else 0, 1);

Basically, I am trying to count the number of bars how long the moving averages are aligned as per the condition above. First I am checking for the ct_5_up and if it is not true, then I am checking next one, ct_3_up. But I want to re-initialize the other one whenever the other condition is true.

It is working fine as expected - but the ct_5_up should be reset to 0, when the ct_3_up condition is met. I tried to use if-else condition but not able to clear the other variable.

Hope I am able to explain what I am trying to achieve and where I am fumbling.

Please suggest. Thank you for your time and help in this regard.
Is there a way to rewrite the CompoundValue function such that it would work in a conditional order? Would this manual calculation work?
Hello I am trying to use the compoundvalue function to start an aggregation of a function. When I use a length value other than 1 (default) there seems to be a 19 bar offset before the condition becomes true. for instance if I use the the following

plot x = compoundvalue(360, 10,0);

Lets say a 2year chart x will equal 10 at barnumber 341! Has anyone else experienced this? What I'd like to do is capture pivotlow and pivothigh barnumbers and start an aggregation of various values.

Try using barNumber()

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
434 Online
Create Post

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.