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.
..can't include plot, addlabel, addchartbubble,...


this may help. i explain a fold statement to someone.
https://usethinkscript.com/threads/...onfirming-a-thinkscript-line.6662/#post-64428


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.
https://usethinkscript.com/threads/...st-65-most-recent-days.7483/page-2#post-72474
 
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://usethinkscript.com/threads/conditional-triggers-using-the-fold-function.5920/
https://stackoverflow.com/questions/55679224/how-to-use-fold-statement-index-in-function-call
 
Last edited:
Last edited:
Solution
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.
then add more words, details.
eventually writing code to solve each section.
 
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://usethinkscript.com/threads/conditional-triggers-using-the-fold-function.5920/
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.
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
284 Online
Create Post

Similar threads

Similar threads

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.
Back
Top