# Coding Help: Fold vs CompoundValue

#### pupu

##### Member
I am always very confused with the iteration functions provided by thinkscript.

If possible, I wish to use a common programming language to let me understand how Fold and CompoundValue works, and their differences (which seems very similar, if not the same).

From my understanding, the following computes the same?

CompoundValue:

Code:
``````def length = 2;
def x = CompoundValue(
length,
x[1] + x[2],
1
);``````

Fold:

Code:
``````def length = 2;

def x =
fold i = 1 to length
with x
do x[1] + x[2];``````

Generic Language:

Code:
``````chart_bar_number = 100;
length = 2;
output = 0;

For (index = 1; index <= chart_bar_number; index++)
{
if(index > length){
output[index] = output[index - 1] + output[index - 2];
}else{
output[index] = 1;
}
}``````

Please give me some help on the above! Thank you!

Solution
compoundvalue() info
post10

compoundvalue is like an if then.

---------------------
fold

Simple explanation of Fold? Trying to learn....
greco26
Sep 21, 2021
post1

post5 includes the following 2 links

-----------------

explain the code in a couple of fold loops
Intraday Flag Indicator to Python - Need Help Confirming a ThinkScript Line
windowshopr
May 20, 2021
post2

--------------------

an example using fold...
compoundvalue() info
post10

compoundvalue is like an if then.

---------------------
fold

Simple explanation of Fold? Trying to learn....
greco26
Sep 21, 2021
post1

post5 includes the following 2 links

-----------------

explain the code in a couple of fold loops
Intraday Flag Indicator to Python - Need Help Confirming a ThinkScript Line
windowshopr
May 20, 2021
post2

--------------------

an example using fold
post22
here is another fold example. this one uses while.
...while the condition is true, it processes the do formula.

-----------------
fold info

mvp ants, a study with a nested fold loop
post18

------------

a good source of info
https://jshingler.github.io/TOS-and...t Collection.html#the-fold-function-explained
hal_foldx

Last edited:

87k+ Posts
316 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?