Help with fold statements

Ginu09

Member
Hi all, I was wondering, I’ve always only seen fold statements increment some counter after repeatedly evaluating a condition over x length.

I’m looking to store the hl2 of candles whose volume exceeds the average 50 period volume, and to repaint the current candle if it closes above any of those candles in that array (and previous candle closed below).

Can a fold statement be used to iterate over my array of hl2 of candles whose volume exceeds the average 50 period volume, and repaint if it closes above any of those candles?

I’ve tried something like:
Code:
def HL2Array = if (newDay) then 0 else if (marketHours and volume > average(volume,50)) then hl2 else 0;

plot NextClose = fold i = 1 to 195 with price = Double.NaN while IsNaN(price) do if (GetValue(close, 1, 0) < HL2Array[i] and GetValue(close, 0, 0) > HL2Array[i]) then getValue(close, 0, 0) else Double.NaN;

AssignPriceColor(if !isNaN(NextClose) then Color.MAGENTA else Color.CURRENT);
 
Last edited:
thoughts
you seem to be focused on using fold, when it may not be necessary. list out your rules , then try to code them.
if you want a variable to be less than all values in a 2nd variable, then all you need is to find the lowest value of the 2nd. then compare to that. you don't need a fold to look at many bars , for every bar.

i made some changes that may help
when debugging, i like to separate out formulas.
i added a vertical line when a bar is colored, so it's easier to find.
added test bubbles to display hl2 values
changed the fold formula

i added a volume average line, to have a quick visual to verify volume was above it.
the test bubbles show up when volume is above the average.
Ruby:
#volavg
input vollen = 50;
def volavg = average(volume, vollen);
plot z = volavg;


Ruby:
input vollen = 50;
def volavg = average(volume, vollen);

#def HL2Array = if (newDay) then 0 else if (marketHours and volume > average(volume,50)) then hl2 else 0;
# since you are looking for the lowest value of hl2array, i changed the default 0 value to be a big number
def big = 99999;
def HL2Array = if volume > volavg then hl2 else big;

input show_bubbles = no;
addchartbubble(show_bubbles and hl2array < big , low, hl2array, color.cyan, no);

#def NextClose = fold i = 1 to 195
#   with price = Double.NaN
#   while IsNaN(price)
#   do if (GetValue(close, 1, 0) < HL2Array[i] and GetValue(close, 0, 0) > HL2Array[i]) then getValue(close, 0, 0) else Double.NaN;

def NextClose = fold i = 1 to 195
   with p
   do if (close[1] < HL2Array[i] and close > HL2Array[i]) then close else Double.NaN;

AssignPriceColor(if !isNaN(NextClose) then Color.MAGENTA else Color.CURRENT);

addverticalline(!isNaN(NextClose), "xx" , color.cyan);
#
 
thoughts
you seem to be focused on using fold, when it may not be necessary. list out your rules , then try to code them.
if you want a variable to be less than all values in a 2nd variable, then all you need is to find the lowest value of the 2nd. then compare to that. you don't need a fold to look at many bars , for every bar.

i made some changes that may help
when debugging, i like to separate out formulas.
i added a vertical line when a bar is colored, so it's easier to find.
added test bubbles to display hl2 values
changed the fold formula

i added a volume average line, to have a quick visual to verify volume was above it.
the test bubbles show up when volume is above the average.
Ruby:
#volavg
input vollen = 50;
def volavg = average(volume, vollen);
plot z = volavg;


Ruby:
input vollen = 50;
def volavg = average(volume, vollen);

#def HL2Array = if (newDay) then 0 else if (marketHours and volume > average(volume,50)) then hl2 else 0;
# since you are looking for the lowest value of hl2array, i changed the default 0 value to be a big number
def big = 99999;
def HL2Array = if volume > volavg then hl2 else big;

input show_bubbles = no;
addchartbubble(show_bubbles and hl2array < big , low, hl2array, color.cyan, no);

#def NextClose = fold i = 1 to 195
#   with price = Double.NaN
#   while IsNaN(price)
#   do if (GetValue(close, 1, 0) < HL2Array[i] and GetValue(close, 0, 0) > HL2Array[i]) then getValue(close, 0, 0) else Double.NaN;

def NextClose = fold i = 1 to 195
   with p
   do if (close[1] < HL2Array[i] and close > HL2Array[i]) then close else Double.NaN;

AssignPriceColor(if !isNaN(NextClose) then Color.MAGENTA else Color.CURRENT);

addverticalline(!isNaN(NextClose), "xx" , color.cyan);
#
Hi, thanks for taking the time.

My rule is to plot a variable if it is less than any [not all] values in a 2nd variable so I thought I had to iterate through all values in the 2nd variable with a fold. If there’s a simpler option I’m all ears.

The reason I used 0 for HL2Array is close[1] will never be less than 0.
 
Last edited:
my mistake, i was ( and still am) confused on what you are trying to do.
if you want to check if a value is less than any value in a group, then you want to find the largest value in the group and compare to it.

def z = highest(hl2array, 195)
def w = if close > z then .....
 
my mistake, i was ( and still am) confused on what you are trying to do.
if you want to check if a value is less than any value in a group, then you want to find the largest value in the group and compare to it.

def z = highest(hl2array, 195)
def w = if close > z then .....
I’m looking to identify reversals by highlighting the candle whose previous candle closes below the midpoint of a big volume candle but recaptures the hl2 of the big volume candle on the next candle.

Lets say HL2Array has three values from three big volume candles: 1.75, 2 and 2.25. If the previous candle closes below 1.75 and the next candle closes above 1.75, I want to highlight this current candle. But let’s say price never broke below 1.75 and bounced at say 1.80 to 2.5. I want to highlight the candle that closes above 2 instead (as the previous candle close will be less than 2). When it goes up to 2.5, I want to also highlight the candle that closes above 2.25 (previous close will have been less than 2.25).

So I want to highlight any candles whose previous close is below an HL2Array value but whose current close is above that value so I thought I needed a fold statement.
 
my mistake, i was ( and still am) confused on what you are trying to do.
if you want to check if a value is less than any value in a group, then you want to find the largest value in the group and compare to it.

def z = highest(hl2array, 195)
def w = if close > z then .....
And since the fold executes on each candle, it lets me check whether the current candle meets the condition for any big volume candle stored in HL2Array.

But I have a question, if the condition is met for any value in HL2Array, is it possible to stop the fold statement from continuing? As there’s no need to check all other values for that candle.
 
Last edited:
aah, you edited your post to include looking at the previous candle. sometimes i copy a post, then think and write offline.
i think my post#2 code does what you want.

with a 'normal' fold, the do formula is executed on every loop.
if you put a condition after 'while', then the do formula executes , i think, when the while formula is true.


you can put your compare formula after 'while' so do only executes when while triggers.
and 'do' becomes do i .
the value becomes the offset to the candle with the desired value.

then find the desired value of some variable
def t = getvalue(hl2, i)


https://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/fold

7. You can also add a condition within the while block of the operator. If this condition is violated, the result is assigned the last known value of variable and the loop is terminated.


https://jshingler.github.io/TOS-and...t Collection.html#the-fold-function-explained
 
aah, you edited your post to include looking at the previous candle. sometimes i copy a post, then think and write offline.
i think my post#2 code does what you want.

with a 'normal' fold, the do formula is executed on every loop.
if you put a condition after 'while', then the do formula executes , i think, when the while formula is true.


you can put your compare formula after 'while' so do only executes when while triggers.
and 'do' becomes do i .
the value becomes the offset to the candle with the desired value.

then find the desired value of some variable
def t = getvalue(hl2, i)


https://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/fold

7. You can also add a condition within the while block of the operator. If this condition is violated, the result is assigned the last known value of variable and the loop is terminated.


https://jshingler.github.io/TOS-and-Thinkscript-Snippet-Collection/TOS & Thinkscript Collection.html#the-fold-function-explained
Maybe then I can add a condition:

while (close[1] < HL2Array[i-1] and close > HL2Array[i-1]) != 1

That way if it was found for the previous iteration, it will quit.
 
thoughts
you seem to be focused on using fold, when it may not be necessary. list out your rules , then try to code them.
if you want a variable to be less than all values in a 2nd variable, then all you need is to find the lowest value of the 2nd. then compare to that. you don't need a fold to look at many bars , for every bar.

i made some changes that may help
when debugging, i like to separate out formulas.
i added a vertical line when a bar is colored, so it's easier to find.
added test bubbles to display hl2 values
changed the fold formula

i added a volume average line, to have a quick visual to verify volume was above it.
the test bubbles show up when volume is above the average.
Ruby:
#volavg
input vollen = 50;
def volavg = average(volume, vollen);
plot z = volavg;


Ruby:
input vollen = 50;
def volavg = average(volume, vollen);

#def HL2Array = if (newDay) then 0 else if (marketHours and volume > average(volume,50)) then hl2 else 0;
# since you are looking for the lowest value of hl2array, i changed the default 0 value to be a big number
def big = 99999;
def HL2Array = if volume > volavg then hl2 else big;

input show_bubbles = no;
addchartbubble(show_bubbles and hl2array < big , low, hl2array, color.cyan, no);

#def NextClose = fold i = 1 to 195
#   with price = Double.NaN
#   while IsNaN(price)
#   do if (GetValue(close, 1, 0) < HL2Array[i] and GetValue(close, 0, 0) > HL2Array[i]) then getValue(close, 0, 0) else Double.NaN;

def NextClose = fold i = 1 to 195
   with p
   do if (close[1] < HL2Array[i] and close > HL2Array[i]) then close else Double.NaN;

AssignPriceColor(if !isNaN(NextClose) then Color.MAGENTA else Color.CURRENT);

addverticalline(!isNaN(NextClose), "xx" , color.cyan);
#

Reading up on fold, it seems p above would be replaced after each iteration i and only be left with the final value so I think it would need to be stored before the next iteration. I’ve modified it as follows:


Code:
def NextClose = fold i = 1 to 196
   with p
   while (p==0)
   do p+(close[1] < HL2Array[i] and close > HL2Array[i]);

So if any iteration is successful p will equal 1 and I’ve added a while statement so that if anytime p isn’t equal to 0, the fold statement breaks.

My only remaining concern is that I’m using 196 as there are 195 two-minute candles (I use the 2 min chart) and during the day, “i” will go back into the previous day. How can I add a condition in the while statement that the iteration being evaluated is on the current day only and not part of the HL2Array going back to the previous day?
 
this will be true if the bar is on the last day ( current)
try adding it to while formula with and.
def istoday = if GetLastDay() == GetDay() then 1 else 0;

while (p==0 and GetLastDay() == GetDay() )

or might be able to calculate the loop length from ( barnumber - first barnumber of day)
 

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
490 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