Fold function Learning

Human

Member
I feel the need to learn the fold function very well. I have some basic understanding, but the documentation I have found so far does not help me enough. Could someone please point me to the very most complete documentation about the fold function?
 
Solution
@MerryDay @halcyonguy . I great appreciate the generous help both of you have given to everyone.
This fold function is one of many examples.
I have a general question to put to you and other experienced thinkscript admins and member coders This is from a beginning, isolated user without background but lots of motivation to learn; either by demo's , threads, examples, videos. Perhaps others would benefit:
Q.Would you be kind enough to share a few suggestions about where to start in a strategic sense to get some basics of thinkscript, with those of us that are really struggling to "put it together". For myself,I have some practical knowledge in hacking some of my favorite indicators and algo's, but I am there are times I...
I feel the need to learn the fold function very well. I have some basic understanding, but the documentation I have found so far does not help me enough. Could someone please point me to the very most complete documentation about the fold function?
Here are the "FOLD" threads on this forum sorted by popularity.
https://usethinkscript.com/search/1215433/?q=fold&t=post&c[title_only]=1&o=replies&g=1

No, there is no other "complete documentation" on the "FOLD" function.
 

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

I feel the need to learn the fold function very well. I have some basic understanding, but the documentation I have found so far does not help me enough. Could someone please point me to the very most complete documentation about the fold function?

it can be hard to learn to use fold. best thing is to experiment with some simple loops and create some bubbles to display some values. (that worked for me. i can read something all day and not get it. but if i experiment and try things, it starts to make sense.)


here is a fold study to experiment with. change values and see what happens.

Code:
# fold_test2

# count how many bars in the previous 20 bars have a smaller candle than current bar
# comparing absolute size, not engulfing.

input qty = 20;
 
def x = fold i = 1 to qty+1
 with p
 #while ...
 do p + (if (high-low) > (getvalue(high, i) - getvalue(low, i)) then 1 else 0);
# test the loop iterations. disable prev do p  line..  and enable the following line. it will count to 20 and show 20 in bubbles, verifying how many times the loop runs.
# do p + 1;

# within a fold, getvalue() is used to read from another bar. 
#  getvalue( high , offset number )
# using a normal offset within a fold, may result in unknown values.  high[i]


addchartbubble(1, low,
"count\n" +
x
, (if x > 10 then color.cyan else color.yellow), no);

# fold doesn't process the 'to' number , 20. so if you want a loop to run 1 to 20, it has to be set up as 1 to 21.
#


some resources/links

jshingler
https://jshingler.github.io/TOS-and...t Collection.html#the-fold-function-explained

here are a few links talking about fold
https://usethinkscript.com/threads/simple-explanation-of-fold-trying-to-learn.8044/#post-76663

https://usethinkscript.com/threads/...onfirming-a-thinkscript-line.6662/#post-64430

https://usethinkscript.com/threads/calculating-price-density-index-pdi.12965/#post-72474

https://usethinkscript.com/threads/ask-line-20.14356/#post-119865
 
it can be hard to learn to use fold. best thing is to experiment with some simple loops and create some bubbles to display some values. (that worked for me. i can read something all day and not get it. but if i experiment and try things, it starts to make sense.)


here is a fold study to experiment with. change values and see what happens.

Code:
# fold_test2

# count how many bars in the previous 20 bars have a smaller candle than current bar
# comparing absolute size, not engulfing.

input qty = 20;
 
def x = fold i = 1 to qty+1
 with p
 #while ...
 do p + (if (high-low) > (getvalue(high, i) - getvalue(low, i)) then 1 else 0);
# test the loop iterations. disable prev do p  line..  and enable the following line. it will count to 20 and show 20 in bubbles, verifying how many times the loop runs.
# do p + 1;

# within a fold, getvalue() is used to read from another bar.
#  getvalue( high , offset number )
# using a normal offset within a fold, may result in unknown values.  high[i]


addchartbubble(1, low,
"count\n" +
x
, (if x > 10 then color.cyan else color.yellow), no);

# fold doesn't process the 'to' number , 20. so if you want a loop to run 1 to 20, it has to be set up as 1 to 21.
#


some resources/links

jshingler
https://jshingler.github.io/TOS-and-Thinkscript-Snippet-Collection/TOS & Thinkscript Collection.html#the-fold-function-explained

here are a few links talking about fold
https://usethinkscript.com/threads/simple-explanation-of-fold-trying-to-learn.8044/#post-76663

https://usethinkscript.com/threads/...onfirming-a-thinkscript-line.6662/#post-64430

https://usethinkscript.com/threads/calculating-price-density-index-pdi.12965/#post-72474

https://usethinkscript.com/threads/ask-line-20.14356/#post-119865
This is great! Thanks for the resources and the code for experimentation.
 
@MerryDay @halcyonguy . I great appreciate the generous help both of you have given to everyone.
This fold function is one of many examples.
I have a general question to put to you and other experienced thinkscript admins and member coders This is from a beginning, isolated user without background but lots of motivation to learn; either by demo's , threads, examples, videos. Perhaps others would benefit:
Q.Would you be kind enough to share a few suggestions about where to start in a strategic sense to get some basics of thinkscript, with those of us that are really struggling to "put it together". For myself,I have some practical knowledge in hacking some of my favorite indicators and algo's, but I am there are times I get lost in the forest, so to speak, for lack of fundamentals. The map is definitely not the territory, as I find the official documentation manual incomprehensible much of the time, since I have no coding background. With much appreciation to the compassionate Bodhisattvas that hang around to assist those of us that struggle to "see the light and delight of learning". Best to all MagicQuotes.:)
 
@MagicQuotes

@halcyonguy's response to fold questions:
https://usethinkscript.com/threads/fold-function-learning.16047/#post-128118

Answer to learning the intricacies of ThinkScript
The original intent of ToS was simple plots on a chart.
It is the users, who have come up with amazing ways to expand its capabilities.
But these uses are not conveniently cataloged in one place. :(

The contributors here seem to be able to continually come up with unique solutions to every insurmountable obstacle that I have run across.
So, keep searching through the forum for examples and keep asking questions.
 
Last edited:
@MerryDay @halcyonguy . I great appreciate the generous help both of you have given to everyone.
This fold function is one of many examples.
I have a general question to put to you and other experienced thinkscript admins and member coders This is from a beginning, isolated user without background but lots of motivation to learn; either by demo's , threads, examples, videos. Perhaps others would benefit:
Q.Would you be kind enough to share a few suggestions about where to start in a strategic sense to get some basics of thinkscript, with those of us that are really struggling to "put it together". For myself,I have some practical knowledge in hacking some of my favorite indicators and algo's, but I am there are times I get lost in the forest, so to speak, for lack of fundamentals. The map is definitely not the territory, as I find the official documentation manual incomprehensible much of the time, since I have no coding background. With much appreciation to the compassionate Bodhisattvas that hang around to assist those of us that struggle to "see the light and delight of learning". Best to all MagicQuotes.:)

i didn't just read a book to learn thinkscript. i spent many months, searching for code examples, typing them in, changing them, see what happens. then repeat, over and over.
i googled thinkscript , and went to many sites that I found. I am not a professional programmer, so my views may be different.

you need to be able to make up a list of rules that need to happen in a study. the hard and tedious part is writing the actual program code to do each rule.


What I think the steps are to make a program, (where, what, when)
1. list out the basics,
..WHERE do you want to see something? chart , column, scan,..
..WHAT do you want to see? lines, bubbles, clouds,...
..WHEN do you wanna see it? on every bar, after the bars, at top,...

2. make a set of rules from the objectives. can include formulas and logic to describe it. ( if close > average then blue bubble)

3. create the code for each rule


a program is just lots of decision statements. reading 2 numbers and comparing them.
if a condition is true then the value is this else it is that.


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

start with the manual
https://tlc.thinkorswim.com/center/reference/thinkScript/tutorials/Basic

then look at the functions and try the example codes. change them, see what happens. yes there are 100s. yes i have looked at most of them.
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions

i agree, the official site for thinkscript is poorly written. the manual section on the functions is horrible. the code examples are overly complicated, include too many other functions, that add confusion.

I think what helped me the most was using addlabel() and addchartbubble() to display variable values, so I could get feedback on what the code was doing. And to show me sometimes that it was doing something I didn't expect.

https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/AddLabel
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/AddChartBubble

Here is an example test code that uses the \n and a formula to generate colors.

Code:
# ex.  \n forces text after it to start on the next line
# Uses a formula in the color parameter to display different colors.
addchartbubble(1, low, 
open + " opn\n" + 
close + " cls"
, (if close > close[1] then color.green else color.red), no);


this post has 3 links to great info on thinkscript
https://usethinkscript.com/threads/...l-languages-to-thinkscript.12376/#post-106494
onenote
jshingler
robert payne

another site is hahn-tech
 
Solution
@halcyonguy . I found your suggestions to be greatly correlated with my learning experience.They are a great Map to follow, all your suggestions likewise!

Risking bored response from others that follow this thread, I can share my isolated experience since I started hacking away on ThinkorSwim:
I use ThinkorSwim for developing tools in the manner along the lines of what you outlined above. I've examined hundreds-perhaps thousands of indicators-. I am not lazy, but motivated by love of learning (and want to translate that into consistent profitable trading.

I know I feel compelled to do my own coding and mapping as I experienced from the very beginning of my learning path that my reception of sense impressions, of thinking about numbers, all of which form impressions and pictures and sequences, which call forth other sequences, each of which calls forth another sequence.This process, I have come to understand as useful, but it is not yet "thinking". It is
a combination of logical thinking and intuition.
When many such sequences turn up certain pictures, then precisely by such consistent return in many combination of sequences, when this certain "pictures-a pattern-shows up- " in that it provides a connection, a confluence of threads connecting such sequences (bursts of buying and selling), then it becomes an organizing element for such sequences in that it connects the sequence-in themselves at first that seem unrelated to each other. This element then becomes a tool, a concept.
This is, for example how I integrated market intraday Tick, Advance Decline and Up/down volume with other indicators in order to make the map of order flow more reliable and consistent. At first these intraday Internals & Indicators available on TOS Market ( of which there are hundreds available) dominated and fascinated me. Like a monkey with new toys to play with at first.) Now their are part of an integrated background perspective that only came in the type of hard grinding real time work you describe above.
I hope @MerryDay and other active administrators on the Usethinkscript.com highlight your response to me for other visitors and members. Your comments are always thoughtful, wise, generous and are right to the point.
Thanks again to everyone for their positive energy, mindfulness and generosity.
MagicQuotes. Save this the Thread, it is gold!
https://usethinkscript.com/threads/fold-function-learning.16047/#post-128726
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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