# Simple explanation of Fold? Trying to learn....

#### greco26

##### Active member
Would anyone be able to give me a super simple explanation and example of how to use Fold? I mean super simple... like shapes and colors simple. I've tried to reverse engineer the logic of existing fold examples but I'm having trouble grasping it. I read as much as possible online and I can see how things break down in the fold statement but since coding is fairly new to me still, I just can't understand it with their examples. For example, if I want to find the previous 5 highs which each are higher than the current bar and higher then the high before it and so on. Thank you so much!!

Solution
fold can,
...process a series of formulas and end up with 1 number.
... look at a group of bars (index) and read values of variables.
...use every # between a start and a stop, in formulas. usually used as a bar offset in getvalue(), to retrieve a value of a variable.
...if while is used, then loop runs as long as while is true

fold can't draw objects.

this may help. i explain a fold statement to someone.

there is a function called script , that may help you. it allows you to create a sub program, within the study, that can be called many times. it can produce more than 1 number, from plot(s). ( it can't plot or use 2nd agg)
https://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/script
hal_fold

Last edited:
Are there any good references on how to use fold or loops? I’ve been thinking that might be a way to solve the problem, but do not have much experience using the fold function.
here is an example using fold to count the candles that are smaller than the current one ( engulfing).
on each bar, it looks back 40 bars, comparing the previous candles to the current one. if they are smaller, then it counts +1. if one is bigger, then it stops the loop.

Hands down, ToS is the BEST for charting analysis. But it is horrible at portfolio management and at providing documentation.
Most of the members seem to find the best method for learning the use of functions to be reading through the many indicators using that function and comparing its use in the many other threads. And then cutting and pasting what might work best in what they are building.

You are best off searching the word 'fold' yourself. But if you are looking for technical definitions here are a few links to get you started:
https://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/fold
https://jshingler.github.io/TOS-and-Thinkscript-Snippet-Collection/TOS & Thinkscript Collection.html#the-fold-function-explained
https://stackoverflow.com/questions/55679224/how-to-use-fold-statement-index-in-function-call

Last edited:
Last edited:
Would anyone be able to give me a super simple explanation and example of how to use Fold? I mean super simple... like shapes and colors simple. I've tried to reverse engineer the logic of existing fold examples but I'm having trouble grasping it. I read as much as possible online and I can see how things break down in the fold statement but since coding is fairly new to me still, I just can't understand it with their examples. For example, if I want to find the previous 5 highs which each are higher than the current bar and higher then the high before it and so on. Thank you so much!!

don't get hung up on having to use a specific funtion.
write out all the rules you want the code to follow, like an outline.
eventually writing code to solve each section.

@halcyonguy thank you so much!! I will look at these and hopefully get my head straight about it. Thanks!!!

Hands down, ToS is the BEST for charting analysis. But it is horrible at portfolio management and at providing documentation.
Most of the members seem to find the best method for learning the use of functions to be reading through the many indicators using that function and comparing its use in the many other threads. And then cutting and pasting what might work best in what they are building.

You are best off searching the word 'fold' yourself. But if you are looking for technical definitions here are a few links to get you started:
https://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/fold
https://jshingler.github.io/TOS-and-Thinkscript-Snippet-Collection/TOS & Thinkscript Collection.html#the-fold-function-explained
https://stackoverflow.com/questions/55679224/how-to-use-fold-statement-index-in-function-call
Yea, that's exactly what I did first but I still couldn't understand how it worked or, better yet, some real simple examples. Thats why I posted my question here. I think @halcyonguy gave me some good stuff to look at so I'll be doing that.

87k+ Posts
284 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?