Get Highs and lows of a specific bar range

Daryl

New member
I'm attempting to get the high and low prices over a specific range of bars, specifically, the highest high and lowest low from 26 bars in the past to 78 bars in the past. In other words, attempting to search from high[26] to high[78] to get the highest value within that range and the lowest low value from within that range.

If I could get a fold command that would go from 26 bars back to 78 bars back and get the highest high and lowest low values that would work but I can't seem to get such a fold command to work. Even attempting 2 fold commands, one for highest high and one for lowest low has been difficult.
 
I'm attempting to get the high and low prices over a specific range of bars, specifically, the highest high and lowest low from 26 bars in the past to 78 bars in the past. In other words, attempting to search from high[26] to high[78] to get the highest value within that range and the lowest low value from within that range.

If I could get a fold command that would go from 26 bars back to 78 bars back and get the highest high and lowest low values that would work but I can't seem to get such a fold command to work. Even attempting 2 fold commands, one for highest high and one for lowest low has been difficult.

Hopefully modified @halcyonguy's fold code to help.
He is so helpful and an expert wizard with Thinkscript!

Added : input look_back_bars_min = 26; and verticals and labels
Modified: def rng = (lastbar - bn <= look_back_bars_max) and (lastbar - bn >= look_back_bars_min);

Screenshot 2024-02-25 093447.png
Code:
# test_highestbn_05e
# @halcyonguy's code
# https://usethinkscript.com/threads/plot-only-the-high-from-the-last-65-most-recent-days.7483/
# find the highest high, in the most rect x bars

# Sleepyz HighestH_LowestL_using_fold_for_range_of_bars
#         modified to find HH/LL betweeen bars look_back_bar max/min; added vericals and labels

input verticals = yes;
input labels    = yes;

def na = Double.NaN;
def bn = BarNumber();
def lastbar = HighestAll(if !IsNaN(close) then bn else na);

input look_back_bars_max = 78;
input look_back_bars_min = 26;
# is the current bar within the lookback quantity of bars? true/false
def rng = (lastbar - bn <= look_back_bars_max) and (lastbar - bn >= look_back_bars_min);

def hi = fold i = 0 to look_back_bars_max
with p
do if !rng then 0 else if GetValue(high, (bn - (lastbar - i))) > p then GetValue(high, (bn - (lastbar - i))) else p;

plot hix = HighestAll(hi);
hix.SetDefaultColor(Color.YELLOW);
hix.SetStyle(Curve.MEDIUM_DASH);

AddLabel(labels, "HIghestHigh: " + hix, hix.TakeValueColor());
# test code -------------------------
# identify the first bar in the lookback group
AddVerticalLine(verticals and bn == HighestAll(lastbar - look_back_bars_max ), look_back_bars_max , Color.WHITE);
AddVerticalLine(verticals and bn == HighestAll(lastbar - look_back_bars_min), look_back_bars_min, Color.WHITE);

def firstbar = (!IsNaN(close[-look_back_bars_max]) and IsNaN(close[-(look_back_bars_max + 1)]));
plot f = firstbar;
f.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
f.SetDefaultColor(Color.YELLOW);
f.SetLineWeight(4);

AddVerticalLine(firstbar, "xx" , Color.CYAN );

input show_barnumber_bubbles = no;
AddChartBubble(show_barnumber_bubbles, low * 0.995, bn + "\n" + "x", Color.CYAN, no);

#addchartbubble(1, high * 1.01, hi, color.cyan, yes);
#

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

# lowest from prev x bars

def lolo = LowestAll(if rng then low else na);
plot lox = lolo;
lox.SetDefaultColor(Color.MAGENTA);
lox.SetStyle(Curve.MEDIUM_DASH);

AddLabel(labels, "LowestLow: " + lox, lox.TakeValueColor());
#
 
I'm attempting to get the high and low prices over a specific range of bars, specifically, the highest high and lowest low from 26 bars in the past to 78 bars in the past. In other words, attempting to search from high[26] to high[78] to get the highest value within that range and the lowest low value from within that range.

If I could get a fold command that would go from 26 bars back to 78 bars back and get the highest high and lowest low values that would work but I can't seem to get such a fold command to work. Even attempting 2 fold commands, one for highest high and one for lowest low has been difficult.

thanks @SleepyZ , i am still learning from your codes.
here is another way
using a variable for a length works in this case, because all the parts of the len formula originate as constants.

Code:
#past_26to78bars

input start_bar = 26;
input end_bar = 78;
def len = end_bar - start_bar + 1;

def hi = highest(getvalue(high, start_bar) , len);
def lo = lowest(getvalue(low, start_bar) , len);

plot zhi = hi;
plot zlo = lo;
#
 

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