# Plot only the high from the last 65 most recent days

#### Pensar

##### Expert
VIP
Ruby:
``plot line = highestall(if !isnan(close) and isnan(close[-1]) then highest(high,65) else double.nan);``
The fold function isn't my strong point, so I can't help on where the code you posted needs changing. Never could understand how it worked.

I just learned thinkscript this weekend and this is my first script. I am trying to plot a horizontal line across the chart for the high of the last 65 bars going back from the most recent bar only. On a daily chart of 10 years, I determined by brute force that the lastbarnumber would be 2517. I have hardcoded this value just for simplicity. For example, on a daily chart of SCCO for 10 years, the high going back 65 bars from the bar of 8/6/21(I am not including today) would have been 83.29. I am therefore aiming to plot a horizontal line at 83.29 going across the chart. the sethiding feature fails. Can anyone tell me how I might accomplish what I am trying to do. I do not want to see any lines except the one at 83.29!

def zoldhigh = 0;

def zcurhigh = fold j = 2516 to 2517 do
if high > zoldhigh then highest(high,65) else zoldhigh;
plot zmaxhigh =if barnumber()==2517 then zcurhigh else 0;
zmaxhigh.sethiding(zmaxhigh==0);

I modified your code to produce the result you expected. I am impressed that you used the fold code, as many users are unsure how to use it.
1. You can define the last bar showing on the chart using the lastbar code below rather than the brute force method.
2. Once the highest high was found you can display it with highestall(zcurhigh). However, using your code to display it across the entire chart, you need to find the last bar on the chart, including the blank space to the right of the last bar with highestall(barnumber())

Lastly, you could also have used the plot x code to produce the same results

[Edited with corrected Code 20210809]
Ruby:
``````input lookback = 90;
def zoldhigh = 0;

def lastbar  = HighestAll(if !IsNaN(close) then BarNumber() else Double.NaN);
def zcurhigh = fold j = 0 to lastbar do
if getvalue(high,j) > zoldhigh then Highest(getvalue(high, j),lookback) else zoldhigh;
plot zmaxhigh = highestall(zcurhigh);
zmaxhigh.SetHiding(zmaxhigh == 0);

input debug = yes;
AddLabel(debug, lastbar + " " + zcurhigh, color.white);``````

Last edited:
@Ecantor Welcome to the forum. You show amazing intuitive understanding for scripting. I look forward to your contributions.

Thank you guys!! Both solutions worked. By the way, I had spent an hour chit-chatting with ThinkorSwim Tech support this morning and got the opinion that this could not be done!! Now I will use the same code to put the lowest low of the last 65 bars onto the chart. This will give me a trading range that does not have to be drawn by hand for each chart. Traders practicing Wyckoff may find this trading range useful as its high and low lines can then be put into a scan to determine which stocks are crossing the bottom or top of the range!!

I am also impressed that you included a loop in your first attempt, what other programming languages do you know?

Line through entire chart at same level
Ruby:
``````def LastBar = HighestAll(if !IsNaN(close) then BarNumber() else Double.NaN);
def BarRem = LastBar - BarNumber();
def High65 = if !BarRem then Highest(high,65) else Double.NaN;
plot Line = HighestAll(High65);``````

Same line, over last 65 only, hidden otherwise
Ruby:
``````def LastBar = HighestAll(if !IsNaN(close) then BarNumber() else Double.NaN);
def BarRem = LastBar - BarNumber();
def High65 = if !BarRem then Highest(high,65) else Double.NaN;
plot Line = if BarRem <= 65 then HighestAll(High65) else Double.NaN;``````

The limitations; variables can't be reassigned, all if/else branches are compulsory, no defined arrays dynamic or otherwise, etc., don't worry, you'll discover them all eventually.

I think that before I retired, I learned about 21 different programming languages over the course of my career as a developer and/or debugger. But at 73, I'm hoping this will be the last There was just no way I was going to put upper and lower lines on some 500 charts and set alerts around them!!! And ThinkorSwim told me as well that you can't reference in a scan a priceline that is drawn manually... And Joshua, I already discovered all of those problems when I was up until 4:00AM Saturday night trying to figure this out!!:

I just found a bug in the code by @SleepyZ and my own code. @Joshua continues to work fine. The bug can be seen if you switch the chart to the symbol CPRI. The faulty code will plot a line for this at the chart high which is over \$100. The correct code will plot the line at 60.74 which is the highest high limited to a lookback of just the last 90 days.

The fault with my own code is that I didn't actually use j in the fold loop properly so really the loop didn't do what I wanted it to do. I wanted it to find the high value for bar number 2517 which it doesn't actually do. I'm not quite versed enough yet to change the code of @SleepyZ, but I'm quite content to have one working solution

Last edited by a moderator:
Here is the final code I have to plot a price range using the highest and lowest value of the approximately the last 90 days:

#hint length would be about 90 days on daily chart
input length = 65;

def LastBar = HighestAll(if !IsNaN(close) then BarNumber() else Double.NaN);
def BarRem = LastBar - BarNumber();
def High65 = if !BarRem then Highest(high,length) else Double.NaN;
plot Line = if BarRem <= length then HighestAll(High65) - HighestAll(High65) * dAdjuster else Double.NaN;

def low65 = if !BarRem then Lowest(low,65) else
Double.Nan;
plot line2 = if BarRem <= length then LowestAll(Low65) +
else Double.Nan;

input debug = yes;
AddLabel(debug, "Line" + " " + line, color.white);

I use an adjuster value to move the upper line down a little bit and the lower line up a little bit. This is so I can catch what Wyckoff called uptrusts and springs. These are events which occur when price moves just above a previous high and then back into the price range or moves just below the previous low and then moves back into price range.

I just learned thinkscript this weekend and this is my first script. I am trying to plot a horizontal line across the chart for the high of the last 65 bars going back from the most recent bar only. On a daily chart of 10 years, I determined by brute force that the lastbarnumber would be 2517. I have hardcoded this value just for simplicity. For example, on a daily chart of SCCO for 10 years, the high going back 65 bars from the bar of 8/6/21(I am not including today) would have been 83.29. I am therefore aiming to plot a horizontal line at 83.29 going across the chart. the sethiding feature fails. Can anyone tell me how I might accomplish what I am trying to do. I do not want to see any lines except the one at 83.29!

def zoldhigh = 0;

def zcurhigh = fold j = 2516 to 2517 do
if high > zoldhigh then highest(high,65) else zoldhigh;
plot zmaxhigh =if barnumber()==2517 then zcurhigh else 0;
zmaxhigh.sethiding(zmaxhigh==0);
Since your code I modified and I posted seemed to work, I stopped there. See if this works for you in your testing. It seems to work in what few I tested.

Ruby:
``````input lookback = 90;
def zoldhigh = 0;

def lastbar  = HighestAll(if !IsNaN(close) then BarNumber() else Double.NaN);
def zcurhigh = fold j = 0 to lastbar do
if getvalue(high,j) > zoldhigh then Highest(getvalue(high, j),lookback) else zoldhigh;
plot zmaxhigh = highestall(zcurhigh);
zmaxhigh.SetHiding(zmaxhigh == 0);

input debug = yes;
AddLabel(debug, lastbar + " " + zcurhigh, color.white);``````

Since your code I modified and I posted seemed to work, I stopped there. See if this works for you in your testing. It seems to work in what few I tested.
That looks like it will work fine and I will test it. I'm learning quite a bit from the code you guys are posting!!! Thank you.

That looks like it will work fine and I will test it. I'm learning quite a bit from the code you guys are posting!!! Thank you.11
Thank you for the feedback. Please let me know if you find it does not work in any testing. Although I am fairly experienced in Thinkscript, I am not as experienced as others may be in using 'fold'.

I just learned thinkscript this weekend and this is my first script. I am trying to plot a horizontal line across the chart for the high of the last 65 bars going back from the most recent bar only. On a daily chart of 10 years, I determined by brute force that the lastbarnumber would be 2517. I have hardcoded this value just for simplicity. For example, on a daily chart of SCCO for 10 years, the high going back 65 bars from the bar of 8/6/21(I am not including today) would have been 83.29. I am therefore aiming to plot a horizontal line at 83.29 going across the chart. the sethiding feature fails. Can anyone tell me how I might accomplish what I am trying to do. I do not want to see any lines except the one at 83.29!

def zoldhigh = 0;

def zcurhigh = fold j = 2516 to 2517 do
if high > zoldhigh then highest(high,65) else zoldhigh;
plot zmaxhigh =if barnumber()==2517 then zcurhigh else 0;
zmaxhigh.sethiding(zmaxhigh==0);

@Ecantor , congrats on trying a fold function after a day of studying. it took me a couple of weeks of experimenting with fold before it clicked.

i started my version a few hours ago. i'm just getting back now and finished it.
it draws an arrow and a vertical line on the first bar in the look_back_bars series

Ruby:
``````# test_highestbn_05b

# find the highest high, in the most rect x bars
# draw a horz line at that level

# in the fold loop, this uses i and other variables to calculate a changing offset, a different offset for each bar in the series, such that for each bar, the loop looks back to the first bar in the series, and not beyond it.
# when the current bar is the first in the lookback period, it doesn't look at previous bars. on the 2nd bar in the series, it only looks back 1 bar (for the high).  when the loop gets to the last bar, it looks back 'look_back_bars ' bars.

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

input look_back_bars = 65;
# is the current bar within the lookback quantity of bars? true/false
def rng = (lastbar - bn <= look_back_bars);

def hi = fold i = 0 to look_back_bars
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);

# test code -------------------------
# identify the first bar in the lookback group
def firstbar = (!IsNaN(close[-look_back_bars]) and IsNaN(close[-(look_back_bars + 1)]));
plot f = firstbar;
f.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
f.SetDefaultColor(Color.yellow);
f.setlineweight(4);

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);
#``````

Thanks so much guys!! This is a beautiful thing: 1) I belong to a group that does options. Now, with this code, I can look at a chart that our options guru makes a call on and instantly see the upper and lower range of the stock for the last 90 days and where the stock is at the moment within the range.
2) I can learn from all this code because there is such a paucity of examples in the tutorials 3) I have been able to use this study to write scans that pick out instantly all the stocks that might meet the Wyckoff criteria that I am interested in. So thanks so very much!!

def High65 = if !BarRem then Highest(high,65) else Double.NaN;

Am I correct that this if statement becomes true if there is an expansion area on the chart and studies look at these future bars? Or is the reasoning behind this line different??

@Ecantor we don't have a contributor named Ruby. When we share code in a post, we click on this icon at the top of our post, when ruby syntax is selected, the code section header is ruby. I believe you might be wanting to address the awesome @halcyonguy

K, awesome @halcyonguy, please let me know if I am correct in your reasoning for that line of code

It might be easier written as:

def High65 = if BarRem == 0 then Highest(high,65) else Double.NaN;

It becomes true when the bar-by-bar iteration has reached he final bar on the chart.

In which case, it will return the highest high within a look-back period of 65 bars.

Prior to the final bar on the chart, it returns Double.NaN

Oh, I see that now. Sweet coding

K, awesome @halcyonguy, please let me know if I am correct in your reasoning for that line of code

heh , thanks @MerryDay

that code is from joshua , post #8

i'll just add a little to what he said,
the barrem variable is a number , 0 to the quantity of bars on the chart.
def BarRem = LastBar - BarNumber();

when the current bar is before the last bar , it has a value > 0 . when combined with the not symbol , ! , it is changed to a 0, so false.

when it gets to the last bar on the chart, barrem is 0 , so !0 is equal to 1 ,
true. and it triggers the 'then' part.

@Ecantor , congrats on trying a fold function after a day of studying. it took me a couple of weeks of experimenting with fold before it clicked.

i started my version a few hours ago. i'm just getting back now and finished it.
it draws an arrow and a vertical line on the first bar in the look_back_bars series

Ruby:
``````# test_highestbn_05b

# find the highest high, in the most rect x bars
# draw a horz line at that level

# in the fold loop, this uses i and other variables to calculate a changing offset, a different offset for each bar in the series, such that for each bar, the loop looks back to the first bar in the series, and not beyond it.
# when the current bar is the first in the lookback period, it doesn't look at previous bars. on the 2nd bar in the series, it only looks back 1 bar (for the high).  when the loop gets to the last bar, it looks back 'look_back_bars ' bars.

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

input look_back_bars = 65;
# is the current bar within the lookback quantity of bars? true/false
def rng = (lastbar - bn <= look_back_bars);

def hi = fold i = 0 to look_back_bars
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);

# test code -------------------------
# identify the first bar in the lookback group
def firstbar = (!IsNaN(close[-look_back_bars]) and IsNaN(close[-(look_back_bars + 1)]));
plot f = firstbar;
f.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
f.SetDefaultColor(Color.yellow);
f.setlineweight(4);

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);
#``````

Can you please provide the codes for the lower line?
@Ecantor , congrats on trying a fold function after a day of studying. it took me a couple of weeks of experimenting with fold before it clicked.

i started my version a few hours ago. i'm just getting back now and finished it.
it draws an arrow and a vertical line on the first bar in the look_back_bars series

Ruby:
``````# test_highestbn_05b

# find the highest high, in the most rect x bars
# draw a horz line at that level

# in the fold loop, this uses i and other variables to calculate a changing offset, a different offset for each bar in the series, such that for each bar, the loop looks back to the first bar in the series, and not beyond it.
# when the current bar is the first in the lookback period, it doesn't look at previous bars. on the 2nd bar in the series, it only looks back 1 bar (for the high).  when the loop gets to the last bar, it looks back 'look_back_bars ' bars.

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

input look_back_bars = 65;
# is the current bar within the lookback quantity of bars? true/false
def rng = (lastbar - bn <= look_back_bars);

def hi = fold i = 0 to look_back_bars
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);

# test code -------------------------
# identify the first bar in the lookback group
def firstbar = (!IsNaN(close[-look_back_bars]) and IsNaN(close[-(look_back_bars + 1)]));
plot f = firstbar;
f.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
f.SetDefaultColor(Color.yellow);
f.setlineweight(4);

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);
#``````

can you please provide the code for plotting he low from the last 65 most recent days?

87k+ Posts
434 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?