Calculating the number of bars since a lower low (or higher high)

TNTrader5159

Active member
How would I calculate the number of bars since a lower low vs the present bar? I take it to subtract the bar number of that most recent occurrence of a lower low from the current bar number to determine the range. I do not know how to write that, and the official tutorials are not indicative enough to my somewhat untrained eye.

Then I would also want to calculate the number of bars since both the highest and lowest prices within that range (assuming it is big enough to be worth counting, which I already know how to qualify). I believe HighestAll would be in order, but again I am unsure of the parameters of that function.

The objective is to find low and high pivot points and measure the number of bars higher/lower on each side AND find the relative locations of the highest and lowest prices within each of those ranges going back until the most recent occurrence of a lower/higher price that would truncate the range.

Please enlighten me with my advance thanks!

Matt
 
Solution
How would I calculate the number of bars since a lower low vs the present bar? I take it to subtract the bar number of that most recent occurrence of a lower low from the current bar number to determine the range. I do not know how to write that, and the official tutorials are not indicative enough to my somewhat untrained eye.

Then I would also want to calculate the number of bars since both the highest and lowest prices within that range (assuming it is big enough to be worth counting, which I already know how to qualify). I believe HighestAll would be in order, but again I am unsure of the parameters of that function.

The objective is to find low and high pivot points and measure the number of bars higher/lower on each side...
I appreciate your response and the resolution. I've been watching this etf (Labu) nonstop for the last 30 minutes. When the up arrow appears (
at least during the time I was watching, the up arrow didn't get repainted)., the alert occasionally works and occasionally doesn't.
I came up with the answer myself. The alert always functioned with this code.

Alert(Valley within 2 bars , "Long within 2 bars", Alert.BAR, Sound.ring);
 

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

Comparing past highs to current high

In case anyone is thinking about trading this Labor Day weekend...
<p>
How would you express a comparison of past highs with the current high? I would say that "if the high of any past bar is greater than the current bar's high to return the bar number of that high and subtract it from the current bar number to yield how many bars ago it occurred." I'm actually looking for the most recent fulfillment of that condition and how many bars back it occurred.
</p>
<p>
Thanks!
</p>
 
Last edited by a moderator:
Comparing past highs to current high

In case anyone is thinking about trading this Labor Day weekend...
<p>
How would you express a comparison of past highs with the current high? I would say that "if the high of any past bar is greater than the current bar's high to return the bar number of that high and subtract it from the current bar number to yield how many bars ago it occurred." I'm actually looking for the most recent fulfillment of that condition and how many bars back it occurred.

EDIT code,

forgot the ,no) at the end of bubble
change to , with p = 1
if offset = barnumber, then no higher high was found, set to 0
..................

see if this produces the offsets you are looking for,
untested.

on every bar,
find offset of most recent , prev high,
that is > current high.
on each bar, run a loop that looks at previous bars and compares the highs.
as long as the prev high is < current high, it keeps looping. as long as the while code line is true.


Code:
def bn = barnumber();
def prev_hi_offset = fold i = 1 to bn
with p = 1
while getvalue(high, i) < high
do p + 1;

def adj_off = if prev_hi_offset == bn then 0 else prev_hi_offset;

addchartbubble(1, low*0.990,
bn + "\n" +
high + "\n" +
"prev higher hi \n" +
adj_off + "\n" +
#prev_hi_offset + "\n" +
"bars ago"
, (if adj_off > 0 then color.yellow else color.gray), no);
#
 
Last edited:
see if this produces the offsets you are looking for,
untested.

on every bar,
find offset of most recent , prev high,
that is > current high.
on each bar, run a loop that looks at previous bars and compares the highs.
as long as the prev high is < current high, it keeps looping. as long as the while code line is true.

if the count is off, try changing
with p = 0
to
with p = 1

Code:
def bn = barnumber();
def prev_hi_offset = fold i = 1 to bn
with p = 0
while getvalue(high, i) < high
do p + 1;

addchartbubble(1, low*0.994,
bn + "\n" +
high + "\n" +
"prev higher hi \n" +
prev_hi_offset + "\n" +
"bars ago"
, color.yellow);

Good to see you again halcyonguy and thank you! (My HTML code fell flat) The only challenge remaining now is determining which came first -- the high or the low of a bar without complex divisions of the current bar into smaller aggregates. I'm still failing to understand how best to determine that. In real time it would generate a signal when both conditions have been met intrabar, but it would still not note which was met FIRST. Will GetTime work for determining a high's or low's time of occurrence for comparison purposes?
 
Good to see you again halcyonguy and thank you! (My HTML code fell flat) The only challenge remaining now is determining which came first -- the high or the low of a bar without complex divisions of the current bar into smaller aggregates. I'm still failing to understand how best to determine that. In real time it would generate a signal when both conditions have been met intrabar, but it would still not note which was met FIRST. Will GetTime work for determining a high's or low's time of occurrence for comparison purposes?
As answered in your other thread on this subject:
https://usethinkscript.com/threads/...-first-within-a-single-bar.12431/#post-106753

There are no functions available that will provide the intra-candle calculations that you have been looking for.
The data does not exist.

As explained already, the only work-around is the interpolations of highs and lows using multiple lower timeframes to form a guess-imate.
 
Last edited:
As answered in your other thread on this subject:
https://usethinkscript.com/threads/...-first-within-a-single-bar.12431/#post-106753

There are no functions available that will provide the intra-candle calculations that you have been looking for.
The data does not exist.

As explained already, the only work-around is the interpolations of highs and lows using multiple timeframes to form a guess-imate.
I'm sorry for the redundancy, as I am ill and have holes in my memory. Thank you again for directing me to this information.
 
this will find how many bars back to the recent peak or valley.
i'm not sure what you mean in the last sentence.

Code:
# bars_since_low_0

def na = double.nan;
def bn = BarNumber();
def lastbn = highestall(if isnan(close) then 0 else bn);
def lastcls = if lastbn then close else lastcls[1];
#def lastbar = (!isnan(close) and isnan(close[-1]));

input show_peak_valley_arrows = yes;
def spva = show_peak_valley_arrows;

input len = 4;

#input length = 10;
#def bn = BarNumber();
#def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(len - 1, lastbn - bn);
#def swingLow = low < Lowest(low[1], len - 1) and low == GetValue(Lowest(low, len), -offset);


#--------------------------
#valley section
#def Valley = low < Lowest(low[1], len) and low < Lowest(low[-len], len);
def valley = low < Lowest(low[1], len - 1) and low == GetValue(Lowest(low, len), -offset);

plot ArrowUP = if spva then Valley else na;
ArrowUP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
ArrowUP.SetDefaultColor(Color.WHITE);
ArrowUP.SetLineWeight(2);

#---------------------------
#peak section
#def Peak = high > highest(high[1], len) and high > highest(high[-len], len);
def peak = high < highest(high[1], len - 1) and high == GetValue(highest(high, len), -offset);

plot ArrowDN = if spva then Peak else na;
ArrowDN.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
ArrowDN.SetDefaultColor(Color.WHITE);
ArrowDN.SetLineWeight(2);

# -------------------------

def lolo = if bn == 1 then 9999 else if valley and low < lolo[1] then low else lolo[1];
def lolobn = if bn == 1 then 0 else if lolo[1] != lolo then bn else lolobn[1];

def hihi = if bn == 1 then 0 else if peak and high > hihi[1] then high else hihi[1];
def hihibn = if bn == 1 then 0 else if hihi[1] != hihi then bn else hihibn[1];

def valleybars = if bn == 1 then 0 else if lastbn then bn - lolobn else valleybars[1];
def peakbars = if bn == 1 then 0 else if lastbn then bn - hihibn else peakbars[1];

def new_low = (valley and low < lolo[1]);
def new_high = (peak and high > hihi[1]);

addlabel(1, "bars since valley " + valleybars, color.yellow);
addlabel(1, "bars since peak " + peakbars, color.yellow);


addchartbubble(1, low*0.99,
bn + "\n" +
lastbn + "\n" +
hihi + " hh\n" +
hihibn + " hhbn\n" +
lolo + " ll\n" +
lolobn + " llbn\n"
, (if new_high then color.green else if new_low then color.red else color.gray), no);

#
I believe this code contains a typo. I believe that this
(def peak = high > highest(high[1], len - 1) and high == GetValue(highest(high, len), -offset);) ,
rather than this
(def peak = high < highest(high[1], len - 1) and high == GetValue(highest(high, len), -offset);)
is the proper code. Could you verify? Many thanks
 
I believe this code contains a typo. I believe that this
(def peak = high > highest(high[1], len - 1) and high == GetValue(highest(high, len), -offset);) ,
rather than this
(def peak = high < highest(high[1], len - 1) and high == GetValue(highest(high, len), -offset);)
is the proper code. Could you verify? Many thanks
I'm worn out tonight but will check this tomorrow. Many thanks back.
 
I believe this code contains a typo. I believe that this
(def peak = high > highest(high[1], len - 1) and high == GetValue(highest(high, len), -offset);) ,
rather than this
(def peak = high < highest(high[1], len - 1) and high == GetValue(highest(high, len), -offset);)
is the proper code. Could you verify? Many thanks

you are correct. i made a mistake when copying a formula. thanks for finding it.
i updated my post #2 code
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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