How many days (bars) since lower low, higher high, with date (time)

roadskater

New member
Thanks to those who have contributed posts about counting consecutive and cumulative conditions and other articles on the site, about how to color bars based on conditions. I am having a bit of trouble understanding how to use a static variable (for today's low) and compare that successively with previous days' lows into the past until finding the most recent bar as low or lower than the low of the currently building bar.

Like when the cohosts banter about the volatility:
"Well you know, Adam, if you thought you had missed out, [AAPL, for example] hasn't been this low since [bars] days (bars) ago."
"Right, Eve, on [date]."

After deciding I wanted this, I did go looking extensively here, and in looking, I found the following on tradingview, which is more than I had wanted initially, but it's nice. I'd be looking for something like this Goo Search for this...
QyPmcjKa-Lowest-Highest-From-Widget/

I've seen how to go backward in time comparing each bar to the previous bar using the recursive variables to find consecutive instances of a condition. But I want to repeatedly compare today's (or this bar's) low (or high) to previous candles until finding a lower (or higher) one. I'd want to create a label with the number of bars and the date for each, at least when in daily aggregation. (I'm not as worried about the time, even intraday, as that complicated it...mainly by thinking in bars I'm allowing for it to be used on other aggregations). I can do most of this, but I have not cracked the piece that repeatedly compares the LATEST bar to the 2nd latest, then the 3rd latest, &c.

I would be using AddLabel to put this info on the chart (how many bars, at least), I guess, but am open to other cool ways of showing this info, and the learning that comes with it. I would likely eventually used a trimmed down version to show days since hh or ll in a watchlist, which is beyond that for which I am asking help currently.
I know that there are easy ways using thinkorswim functions to find the lowest low for a certain number of bars, and I had considered using a counter within that command to increase the number of periods until I got a match. I'm still working on these ideas, but decided to give in and ask if anyone with more thinkorswim chops to throw me an idea or two.
I'll take as little or as much as you want to offer, and I will learn it and use it again. The key piece is how to keep comparing the latest bar with successively more distant in the past bars. I'm sure I've repeated myself, in hopes of being clear!

Thanks in advance.
 
Last edited:
Solution
i don't have time to create an answer for you know, so here are 2 studies that use a fold loop to look at previous bars and compare them to the current bar.

in the fold loop, as long as the formula after 'while' is true , the fold loop will keep looping. so the thing to figure out is how many bars back do you want to look at, and put that number +1 as the fold 'to' parameter.

engulfing
find prev bars smaller than current
https://usethinkscript.com/threads/...st-65-most-recent-days.7483/page-2#post-72474

3 bar net
find prev bars , lower high and lower low
https://usethinkscript.com/threads/help-coding-joseph-stowell-3-bar-net-cup-cap.5746/#post-98976

Here is a less eloquent...
Thanks to those who have contributed posts about counting consecutive and cumulative conditions and other articles on the site, about how to color bars based on conditions. I am having a bit of trouble understanding how to use a static variable (for today's low) and compare that successively with previous days' lows into the past until finding the most recent bar as low or lower than the low of the currently building bar.

Like when the cohosts banter about the volatility:
"Well you know, Adam, if you thought you had missed out, [AAPL, for example] hasn't been this low since [bars] days (bars) ago."
"Right, Eve, on [date]."

After deciding I wanted this, I did go looking extensively here, and in looking, I found the following on tradingview, which is more than I had wanted initially, but it's nice. I'd be looking for something like this Goo Search for this...
QyPmcjKa-Lowest-Highest-From-Widget/

I've seen how to go backward in time comparing each bar to the previous bar using the recursive variables to find consecutive instances of a condition. But I want to repeatedly compare today's (or this bar's) low (or high) to previous candles until finding a lower (or higher) one. I'd want to create a label with the number of bars and the date for each, at least when in daily aggregation. (I'm not as worried about the time, even intraday, as that complicated it...mainly by thinking in bars I'm allowing for it to be used on other aggregations). I can do most of this, but I have not cracked the piece that repeatedly compares the LATEST bar to the 2nd latest, then the 3rd latest, &c.

I would be using AddLabel to put this info on the chart (how many bars, at least), I guess, but am open to other cool ways of showing this info, and the learning that comes with it. I would likely eventually used a trimmed down version to show days since hh or ll in a watchlist, which is beyond that for which I am asking help currently.
I know that there are easy ways using thinkorswim functions to find the lowest low for a certain number of bars, and I had considered using a counter within that command to increase the number of periods until I got a match. I'm still working on these ideas, but decided to give in and ask if anyone with more thinkorswim chops to throw me an idea or two.
I'll take as little or as much as you want to offer, and I will learn it and use it again. The key piece is how to keep comparing the latest bar with successively more distant in the past bars. I'm sure I've repeated myself, in hopes of being clear!

Thanks in advance.

i don't have time to create an answer for you now, so here are 2 studies that use a fold loop to look at previous bars and compare them to the current bar.

in the fold loop, as long as the formula after 'while' is true , the fold loop will keep looping. so the thing to figure out is how many bars back do you want to look at, and put that number +1 as the fold 'to' parameter.

engulfing
find prev bars smaller than current
https://usethinkscript.com/threads/...st-65-most-recent-days.7483/page-2#post-72474

3 bar net
find prev bars , lower high and lower low
https://usethinkscript.com/threads/help-coding-joseph-stowell-3-bar-net-cup-cap.5746/#post-98976
 
Last edited:
i don't have time to create an answer for you know, so here are 2 studies that use a fold loop to look at previous bars and compare them to the current bar.

in the fold loop, as long as the formula after 'while' is true , the fold loop will keep looping. so the thing to figure out is how many bars back do you want to look at, and put that number +1 as the fold 'to' parameter.

engulfing
find prev bars smaller than current
https://usethinkscript.com/threads/...st-65-most-recent-days.7483/page-2#post-72474

3 bar net
find prev bars , lower high and lower low
https://usethinkscript.com/threads/help-coding-joseph-stowell-3-bar-net-cup-cap.5746/#post-98976

Here is a less eloquent possible solution not using fold that may be useful.

Capture.jpg
Ruby:
def bn      = BarNumber();
def last    = highestall(if IsNaN(close[-1]) then bn else 0);
#Reverse count
def counter = if BarNumber() == 1 then last else if counter[1] > 1 then counter[1] - 1 else 0;
#Find Lows Lower than Previous Lowest Low
def ll = if counter == 1
         then low else
         if last >= last - counter[1] + 1 and low < low[1]
         then low else
         ll[1];

#Define Last Lowest Lows
def lo0 = if ll != ll[1] then ll[1] else lo0[1];
def lo1 = if lo0 != lo0[1] then lo0[1] else lo1[1];
def lo2 = if lo1 != lo1[1] then lo1[1] else lo2[1];
def lo3 = if lo2 != lo2[1] then lo2[1] else lo3[1];
def lo4 = if lo3 != lo3[1] then lo3[1] else lo4[1];

input showlabels = yes;
AddLabel(showlabels, "Last x Lowest Lows: " + ll + " " + lo0 + " " + lo1 + " " + lo2 + " " + lo3 + " " + lo4, color.white);

def low1       = if ll != ll[1] then ll else Double.NaN;
def lowbar     = if IsNaN(low1)
                 then lowbar[1] else
                 if low == low1
                 then BarNumber() else
                 lowbar[1];
def lowbardiff = if !IsNaN(low1) then lowbar - lowbar[1] else Double.NaN;
def lowdate    = if low == low1 then GetYYYYMMDD() else Double.NaN;

input bubblemover = 20;
AddChartBubble(low == low1, low - ticksize()*bubblemover, "L: " + low1 + " \n Bars: " + lowbardiff + " \n Date: " + AsPrice(lowdate), up = no, color = Color.WHITE);
 
Solution
i don't have time to create an answer for you know, so here are 2 studies that use a fold loop to look at previous bars and compare them to the current bar.

in the fold loop, as long as the formula after 'while' is true , the fold loop will keep looping. so the thing to figure out is how many bars back do you want to look at, and put that number +1 as the fold 'to' parameter.

engulfing
find prev bars smaller than current
https://usethinkscript.com/threads/...st-65-most-recent-days.7483/page-2#post-72474

3 bar net
find prev bars , lower high and lower low
https://usethinkscript.com/threads/help-coding-joseph-stowell-3-bar-net-cup-cap.5746/#post-98976
Thank you for the reply. I will take a look at this. I've seen your many excellent replies, for which, thanks.

I have found much code that says "as long as a condition is true" but part of my very basic, elementary question is with code like this:
def lower = low < low[1];
def lowerCount = if lower then lowerCount[1] + 1 else 0;
def higher = high > high[1];
def higherCount = if higher then higherCount[1] + 1 else 0;

As this loops, low < low[1] seems to compare low[0] vs low[1] then low[1] vs. low[2], and so on. Is that correct?

I want it to look at look at low[0] vs. low[1] then low[0] vs. low[2], and so on. I'll continue to experiment as time allows and after looking at the examples you suggested.

I want to look at as many bars as it takes to find the most recent lower low (or at least as many bars as are available on the chart). I found some code on the site that used '9999' or similar as the lookback number so that's a good enough idea.

Thanks again.
 
Here is a less eloquent possible solution not using fold that may be useful.
Thanks for the reply! I will look at this the next time I have brainspace! It looks interesting. I hope to learn fold with while and do, plus other ways. I am new to how thinkscript works vs. other programming. I wish thinkorswim had a very detailed explanation of how thinkscript walks through a chart and the parameters associated with that. Lacking that, I'm glad for this site. This looks like a great example for me to study, and it shows how I might be able to use AddChartBubble as part of my debugging or learning process. This item may address my question about how to compare the current low repeatedly with previous lows, instead of comparing in adjacent pairs backward through time. Thanks again.
 

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
338 Online
Create Post

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