how to return the value of the previous high from an undefined length of bars

greco26

Active member
I am trying to compare today's high to the next previous high and determine if its > 2. The next previous high could have been any number of bars ago. I don't want to return the highest all though. Just the next high previous to today's high. anyone have any hints on how to accomplish this?
 
I am trying to compare today's high to the next previous high and determine if its > 2. The next previous high could have been any number of bars ago. I don't want to return the highest all though. Just the next high previous to today's high. anyone have any hints on how to accomplish this?

This is using 'fold' (someone who is more familiar could likely do better than the following, but it seems to work).

Capture.jpg

Ruby:
input period = 100;
def lasthigh = if IsNaN(close[-1]) and !IsNaN(close) then high else Double.NaN;
def Data     = fold i = 0 to period
                        with price = Double.NaN
                       while IsNaN(price)
                          do if GetValue(high, -i) > HighestAll(lasthigh)
                             then GetValue(high, -i)
                             else Data[1];
def Databar = fold j = 0 to period
                        with p = Double.NaN
                       while IsNaN(p)
                          do if GetValue(high, -j) > HighestAll(lasthigh)
                             then BarNumber()
                             else Double.NaN;

AddChartBubble(high == HighestAll(lasthigh), high, high);
AddChartBubble(BarNumber() == HighestAll(Databar), high, high);

AddLabel(1, "Higher High " + Data + " Last High " + lasthigh, Color.WHITE);
 
this works perfectly. What if I wanted to exclude/skip yesterday's high in this study?

This should exclude the next bar

Capture.jpg
Ruby:
input period = 100;
def lasthigh = if IsNaN(close[-1]) and !IsNaN(close) then high else Double.NaN;
def nexthigh = if IsNaN(close[-1]) and !IsNaN(close) then high[1] else Double.NaN;
def Data     = fold i = 0 to period
                        with price = Double.NaN
                       while IsNaN(price)
                          do if GetValue(high, -i) > HighestAll(lasthigh) and
                                GetValue(high, -i) > HighestAll(nexthigh)
                             then GetValue(high, -i)
                             else Data[1];
def Databar = fold j = 0 to period
                        with p = Double.NaN
                       while IsNaN(p)
                          do if GetValue(high, -j) > HighestAll(lasthigh) and
                                GetValue(high, -j) > HighestAll(nexthigh)
                             then BarNumber()
                             else Double.NaN;

AddChartBubble(high == HighestAll(lasthigh), high, high);
AddChartBubble(BarNumber() == HighestAll(Databar), high, high);

AddLabel(1, "Higher High " + Data + " Last High " + lasthigh, Color.WHITE);
 
This should exclude the next bar
Hey! I was wondering if you can take a look at this script and give me some pointers, when you have time?
This current script I tweaked from BenTen plots out a line from the current Day's High at the exact candle bar it occurs.
However, I'm wondering how it can be tweaked to plot out a line beginning from Yesterday's High Candle and extends to the right, and doesn't have the price line go any more to left?

Code:
DEF DH = high;
DEF bar = BarNumber();
DEF Today = SecondsFromTime(0930);
DEF Dayhigh = if Today and !Today[1] then DH else if Today and DH > Dayhigh[1] then DH else Dayhigh[1];
DEF DayHighBar = if Today and DH == Dayhigh then bar else Double.NaN;
DEF DayHighest = if BarNumber() == HighestAll(DayHighBar)  then Dayhigh else DayHighest[1];

PLOT DayHigh = if (DayHighest > 0) then DayHighest else Double.NaN;
 
Hey! I was wondering if you can take a look at this script and give me some pointers, when you have time?
This current script I tweaked from BenTen plots out a line from the current Day's High at the exact candle bar it occurs.
However, I'm wondering how it can be tweaked to plot out a line beginning from Yesterday's High Candle and extends to the right, and doesn't have the price line go any more to left?

Code:
DEF DH = high;
DEF bar = BarNumber();
DEF Today = SecondsFromTime(0930);
DEF Dayhigh = if Today and !Today[1] then DH else if Today and DH > Dayhigh[1] then DH else Dayhigh[1];
DEF DayHighBar = if Today and DH == Dayhigh then bar else Double.NaN;
DEF DayHighest = if BarNumber() == HighestAll(DayHighBar)  then Dayhigh else DayHighest[1];

PLOT DayHigh = if (DayHighest > 0) then DayHighest else Double.NaN;

Try this. Added input Day (0=Today, 1=Yesterday, etc) and getday()==getlastday()-Day and Plot to DayHigh1

Screenshot-2021-08-16-170415.jpg
Ruby:
def DH    = high;
def bar   = BarNumber();
input Day = 0;
def Today = GetDay() == GetLastDay() - Day and SecondsFromTime(0930);
def Dayhigh    = if Today and !Today[1] then DH else if Today[1] and DH > Dayhigh[1] then DH else Dayhigh[1];
def DayHighBar = if Today and DH == Dayhigh then bar else Double.NaN;
def DayHighest = if BarNumber() == HighestAll(DayHighBar)  then Dayhigh else DayHighest[1];

plot DayHigh1 = if (DayHighest > 0) then DayHighest else Double.NaN;
 
I am trying to compare today's high to the next previous high and determine if its > 2. The next previous high could have been any number of bars ago. I don't want to return the highest all though. Just the next high previous to today's high. anyone have any hints on how to accomplish this?

here is another way to find previous highs. it finds the highest high. then it finds the 2 previous highs.
it draws 3 bubbles, with high prices and bar numbers.
you can pick a number for bars back (an offset), which shifts the start x bars back from the last bar number.
if an offset is used, it draws a vertical line, to show the starting bar from which if looks back in time.

i'm not sure what you mean by > 2 , so i didn't do anything for it.

Ruby:
# prevhighest_00

# https://usethinkscript.com/threads/how-to-return-the-value-of-the-previous-high-from-an-undefined-length-of-bars.7355/
# compare today's high to the next previous high and determine if its > 2.

def bn = barnumber();
def na = double.nan;
def hi = high;

input bars_back = 0;

# highest high
def max1val = HighestAll( if isnan(high[-bars_back]) then 0 else high );
def max1bn = HighestAll(if (hi == max1val and !isnan(high[-bars_back])) then bn else 0);

# prev highest
def max2val = HighestAll(if bn < max1bn then hi else 0);
def max2bn = HighestAll(if (hi == max2val and bn < max1bn) then bn else 0);

# prev highest
def max3val = HighestAll(if bn < max2bn then hi else 0);
def max3bn = HighestAll(if (hi == max3val and bn < max2bn) then bn else 0);


addlabel(1, "highest " + max1val + "  " + "highest bar# " + max1bn, color.cyan);
addlabel(1, "prev highest " + max2val + "  " + "prev highest bar# " + max2bn, color.cyan);
addlabel(1, "prev highest " + max3val + "  " + "prev highest bar# " + max3bn, color.cyan);

plot z1 = if bn == max1bn then low else na;
z1.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
z1.SetDefaultColor(Color.yellow);
z1.setlineweight(1);
z1.hidebubble();

plot z2 = if bn == max2bn then low else na;
z2.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
z2.SetDefaultColor(Color.yellow);
z2.setlineweight(1);
z2.hidebubble();

plot z3 = if bn == max3bn then low else na;
z3.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
z3.SetDefaultColor(Color.yellow);
z3.setlineweight(1);
z3.hidebubble();

def x = (bn == max1bn) or (bn == max2bn) or (bn == max3bn);
addchartbubble(x, high * 1.005, high + "\nbar# " + bn, color.gray, yes);

# draw a vertical line if a starting offset is used
def t = !isnan(high[-bars_back]) and isnan(high[-(bars_back+1)]);
addverticalline( (t>0), "starting bar", color.gray, Curve.MEDIUM_DASH);
#

NVDA 2HR . bars back = 144
hnoTgIo.jpg
 
This should exclude the next bar
@SleepyZ , my brain continues to not want to understand how folds work. I hate to ask for more help from you but I was wondering if you might be able to add comments to different parts of the code describing what each line of the fold means? I want to add more prior highs but I'm not sure how to do it. Every time I add code, it doesn't change the fold the way I expect which means Im still not fundamentally understanding how a fold works. As always, any help you can lend would be just awesome!!
 
@SleepyZ , my brain continues to not want to understand how folds work. I hate to ask for more help from you but I was wondering if you might be able to add comments to different parts of the code describing what each line of the fold means? I want to add more prior highs but I'm not sure how to do it. Every time I add code, it doesn't change the fold the way I expect which means Im still not fundamentally understanding how a fold works. As always, any help you can lend would be just awesome!!

read through this
https://jshingler.github.io/TOS-and...t Collection.html#the-fold-function-explained
 
@SleepyZ , my brain continues to not want to understand how folds work. I hate to ask for more help from you but I was wondering if you might be able to add comments to different parts of the code describing what each line of the fold means? I want to add more prior highs but I'm not sure how to do it. Every time I add code, it doesn't change the fold the way I expect which means Im still not fundamentally understanding how a fold works. As always, any help you can lend would be just awesome!!


part of the code from post #2

input period = 100;
def lasthigh = if IsNaN(close[-1]) and !IsNaN(close) then high else Double.NaN;

def Data = fold i = 0 to period
#..on each bar on chart
#..data will be set = to the final result of the fold loop.
#..i counts from 0 to (period-1) , 0 to 99

with price = Double.NaN
#..with, specifies a temporary variable, price.
#..if it is set = to a value, that will be the starting value.
#..If a value is omitted, then the default value is 0

while IsNaN(price)
#..isnan() is true when price is not a valid number.
#..as long as IsNaN(price) is true, stay in loop and evaluate the do line

do
if GetValue(high, -i) > HighestAll(lasthigh)
then GetValue(high, -i)
else Data[1];
#..
#..check if a future high value is greater than the highest value of lasthigh
#..
#..GetValue(high, -i)
#..getvalue is used in fold loops, to read values from a variable, based on an offset. above , as the loop i counts through 0-99, those numbers are used as offsets . equivelants, high[0] , high[-1], high[-2], high[-3], ... since the offset is negative, it is reading values from future bars.
#..
 

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