# Script regarding highest closing price in the last 60 days

##### New member
Hi guys,
I am looking for a script that only labels the highest closing price in the last 60 days. I tried to write the script to no avail and I would appreciate all the help. Thank you

Solution
This is what I tried
AddLabel(yes,"Previous Close: " + High[1], 60 Color.WHITE); or
AddLabel(yes,"High Close: " + High[1], 60 Color.WHITE);

making a different version, that will work for any chart time period of 1 day or less.
------------------------------

i tried this with 2nd aggregation, but didn't have any luck with highest().
maybe i'm missing something, or ... but this didn't return valid numbers.

input agg = AggregationPeriod.DAY;
def daycls = close(period = agg);
input days = 60;
def day_hi = highest(daycls, days);

----------------------------------

so i went a different way, calculate how many chart bars are in a day, then multiply it by the length (day quantity).

here is version 2 of a chart...
This is what I tried
AddLabel(yes,"Previous Close: " + High[1], 60 Color.WHITE); or
AddLabel(yes,"High Close: " + High[1], 60 Color.WHITE);

i'm going to cover a few things, that i hope will help you.

i may have been wrong to suggest using 2nd aggregation for finding what you want. i was thinking of trying to get it to work, when the chart time is not set to day. i am working on another study to work on any chart time. if i finish it, i will post later.

------------------------------------

notes on a study that is set to day

AddLabel(yes,"Previous Close: " + High[1], 60 Color.WHITE);

a couple things on your code
...you have , Previous Close: , and High[1] for a variable.
....if you want a close price from the past, you would use close[] with some offset , not high[]
...high[1] will read the high from the previous bar
...you have 60 after a comma then a color. addlabel wants just a color for the 3rd parameter

if you want the highest price of close for the past 60 bars,
def len = 60;
def x = highest(close, len);
addlabel(1, "highest " + x, color.white);

by default, a label will show the values calculated from the last bar on the chart

notice i said bars, not days. all charts work with data in variables, on each bar.
if you want that highest() formula to find the highest value over 60 days, the chart time will need to be set to days.

here is a study that,
..pick a number of bars to look back over
..show a label of the highest close
..yes/no show a verical line when the highest close is found
..yes/no show an arrow when the highest close is found
..yes/no show a verical line when the start of the period

Code:
``````# daychart60hicls_00

# use this on a chart set to day

def na = Double.NaN;
def bn = BarNumber();

def barCount = HighestAll(If(IsNaN(close), 0, bn));
# find the highest bar number on the chart. find highest bar number, when close is not an error

input price = close;
input len = 60;

def clshi = Highest(price, len);
# on each bar, look back 60 bars and find the highest close

addlabel(1, "highest close " + clshi, color.white);

# ================================

def clshi2 = if barcount == bn then clshi else 0;
# save the highest value , for the past 60 days, just on the last bar

def clshi3 = HighestAll(clshi2);
# find the highest value for past 60 days. this copies the value to a variable, to all bars. this allows a value defined in the future, to be used on earlier bar formulas.
# since only the lastbar of clshi2 will have a value, and the rest will be 0, that value will be used in the highestall().

def clshi4 = if ((bn >= (barcount - len) and bn <= barcount) ) then clshi3 else 0;
# if a bar is within 60 bars of the last bar, then set clshi4 equal to the high close value

def hibar = (close == clshi4);
# if the close of the bar is equal to the highest close value , then it will be true ( or 1)

input show_vertical_line_at_highest = yes;
AddVerticalLine( show_vertical_line_at_highest and hibar, "highest " + close, Color.CYAN);
# plot a vertical line just before te bar with the highest close

input show_arrow_at_highest = yes;
plot z = if show_arrow_at_highest then hibar else na;
z.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
z.SetDefaultColor(Color.WHITE);
# draw an arrow above the bar with the highest close

def periodstart = ( !isnan(close[-len]) and isnan(close[-(len+1)]) );
# find a bar 'len' (60) bars before the last bar

input show_vertical_line_start_of_period = yes;
AddVerticalLine(show_vertical_line_start_of_period and periodstart, len + " bars back" , Color.yellow);
# draw a vertical line 60 bars back from last bar

input test_values = no;
addchartbubble(test_values, low * 0.99, bn + "\n" + clshi3 + "\n" + periodstart, (if hibar then Color.YELLOW else Color.GRAY), no);
#``````

AAPL day chart length = 60

Last edited:
This is what I tried
AddLabel(yes,"Previous Close: " + High[1], 60 Color.WHITE); or
AddLabel(yes,"High Close: " + High[1], 60 Color.WHITE);

making a different version, that will work for any chart time period of 1 day or less.
------------------------------

i tried this with 2nd aggregation, but didn't have any luck with highest().
maybe i'm missing something, or ... but this didn't return valid numbers.

input agg = AggregationPeriod.DAY;
def daycls = close(period = agg);
input days = 60;
def day_hi = highest(daycls, days);

----------------------------------

so i went a different way, calculate how many chart bars are in a day, then multiply it by the length (day quantity).

here is version 2 of a chart study, that will work for any chart time period of 1 day or less.
this will find the highest close,
..either from the last bar of the day or any bar,
..within some quantity of days.

this study,
..input quantity days to look back over
..input, pick which bars to look at, last bar or all bars
..show a label of the highest close
..yes/no show a verical line when the highest close is found
..yes/no show an arrow when the highest close is found
..yes/no show a verical line at the start of the period of days
..
..if chart time is more than 1 day, don't find a price. display a warning label.
..if the chart has too few bars, don't find a price. display a warning label.

Code:
``````# day60hi_00

def na = Double.NaN;
def bn = BarNumber();
def barCount = HighestAll(If(IsNaN(close), 0, bn));

# last bar  ( most recent)
#def lastbar = !isnan(close[0]) and isnan(close[-1]);

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

input chart_stat_labels = yes;

# 390min in a day
def chartagg = GetAggregationPeriod();
def chartmin = (chartagg / 1000) / 60;
AddLabel(chart_stat_labels, "chartmin " + chartmin, Color.MAGENTA);

def daybarqty = if chartmin <= 1440 then roundup(390 / chartmin, 0) else 0;
AddLabel(chart_stat_labels, "bars per day " + daybarqty, Color.MAGENTA);

# days on chart ,  assume after hours are off
def chartdays = round(barcount / daybarqty, 1);
AddLabel(chart_stat_labels, "chart days " + chartdays, Color.MAGENTA);

AddLabel((daybarqty == 0), ">>>>>> chart time is more than 1 day. change settings <<<<<<", Color.cyan);

#----------------------------
# find first and last bar of a day
# GetDayValues
# Author: Kory Gill, @korygill
#def nan = Double.NaN;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def lastBarOfDay = if
(afterEnd[-1] == 1 and afterEnd == 0) or
(isRollover[-1] and firstBarOfDay[-1])
then 1
else 0;
# ----------------------------

# choose , use data from only last bar of day, or all bars
input which_bars_to_read = { default day_last , all };

def barz;
def barpos;
case day_last:
barz = if chartmin < 1440 then lastbarofday else if chartmin == 1440 then 1 else 0;
barpos = 1;
case all:
barz = if chartmin > 1440 then 0 else 1;
barpos = 2;
}

addlabel(1, (if barpos == 1 then "prices from last bar of the days" else "prices from all bars"), color.orange);

# 1440 min in a day
# if barz is 0, then don't look for a high

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

input price = close;
input look_back_days = 60;

def daybars = (look_back_days * daybarqty);

addlabel(1, "look back days " + look_back_days , color.yellow);
addlabel(1, "look back bars " + daybars , color.yellow);

# check if enough bars on chart
def few = (daybars > barcount);
AddLabel(few, ">>>>>> not enough bars on chart. change settings <<<<<<", Color.cyan);

def period1 = if ((bn >= (barcount - daybars) and bn <= barcount) ) then 1 else 0;
# if a bar is within 60 bars of the last bar, then set equal to 1

#def period_prices = if (lastbarofday and period1) then price else 0;
# if not enough bars, all values will be 0
def period_prices = if (!few and barz and period1) then price else 0;

def period_start_bar = if (barz and (bn == (barcount - daybars))) then 1 else 0;
input show_line_at_period_start = yes;
AddVerticalLine(show_line_at_period_start and period_start_bar, "   PERIOD  START , FOR  " + look_back_days + "  DAYS" , Color.yellow);

def period_hi = HighestAll(period_prices);
def hibar = ((price == period_hi) and period1);

input test_bubbles1 = no;
addchartbubble(test_bubbles1 and period1, low*0.98, bn + "\n" + period_prices + "\n" + period_hi + "\n" + hibar, (if hibar then Color.YELLOW else Color.GRAY), no);

input show_line_at_high = yes;
AddVerticalLine(show_line_at_high and hibar, "      HIGHEST  " + round(price,2), Color.CYAN);

addlabel(barz and !few, "highest is " + period_hi + ", within " + daybars + " bars", Color.green);

plot z = hibar;
z.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
z.SetDefaultColor(Color.WHITE);

#``````

labels,
prices from.., look back days #, look back bars #,
highest is \$\$ within # bars

labels,
chart minutes, bars/day, days

if the chart has too few bars, this label is displayed, and no price is found.
>>>>> not enough bars on chart. change settings <<<<<<

if the chart time is higher than 1 day, this label is displayed,
>>>>> chart time is more than 1 day. change settings <<<<<<

i'm going to cover a few things, that i hope will help you.

i may have been wrong to suggest using 2nd aggregation for finding what you want. i was thinking of trying to get it to work, when the chart time is not set to day. i am working on another study to work on any chart time. if i finish it, i will post later.

------------------------------------

notes on a study that is set to day

AddLabel(yes,"Previous Close: " + High[1], 60 Color.WHITE);

a couple things on your code
...you have , Previous Close: , and High[1] for a variable.
....if you want a close price from the past, you would use close[] with some offset , not high[]
...high[1] will read the high from the previous bar
...you have 60 after a comma then a color. addlabel wants just a color for the 3rd parameter

if you want the highest price of close for the past 60 bars,
def len = 60;
def x = highest(close, len);
addlabel(1, "highest " + x, color.white);

by default, a label will show the values calculated from the last bar on the chart

notice i said bars, not days. all charts work with data in variables, on each bar.
if you want that highest() formula to find the highest value over 60 days, the chart time will need to be set to days.

here is a study that,
..pick a number of bars to look back over
..show a label of the highest close
..yes/no show a verical line when the highest close is found
..yes/no show an arrow when the highest close is found
..yes/no show a verical line when the start of the period

Code:
``````# daychart60hicls_00

# use this on a chart set to day

def na = Double.NaN;
def bn = BarNumber();

def barCount = HighestAll(If(IsNaN(close), 0, bn));
# find the highest bar number on the chart. find highest bar number, when close is not an error

input price = close;
input len = 60;

def clshi = Highest(price, len);
# on each bar, look back 60 bars and find the highest close

addlabel(1, "highest close " + clshi, color.white);

# ================================

def clshi2 = if barcount == bn then clshi else 0;
# save the highest value , for the past 60 days, just on the last bar

def clshi3 = HighestAll(clshi2);
# find the highest value for past 60 days. this copies the value to a variable, to all bars. this allows a value defined in the future, to be used on earlier bar formulas.
# since only the lastbar of clshi2 will have a value, and the rest will be 0, that value will be used in the highestall().

def clshi4 = if ((bn >= (barcount - len) and bn <= barcount) ) then clshi3 else 0;
# if a bar is within 60 bars of the last bar, then set clshi4 equal to the high close value

def hibar = (close == clshi4);
# if the close of the bar is equal to the highest close value , then it will be true ( or 1)

input show_vertical_line_at_highest = yes;
AddVerticalLine( show_vertical_line_at_highest and hibar, "highest " + close, Color.CYAN);
# plot a vertical line just before te bar with the highest close

input show_arrow_at_highest = yes;
plot z = if show_arrow_at_highest then hibar else na;
z.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
z.SetDefaultColor(Color.WHITE);
# draw an arrow above the bar with the highest close

def periodstart = ( !isnan(close[-len]) and isnan(close[-(len+1)]) );
# find a bar 'len' (60) bars before the last bar

input show_vertical_line_start_of_period = yes;
AddVerticalLine(show_vertical_line_start_of_period and periodstart, len + " bars back" , Color.yellow);
# draw a vertical line 60 bars back from last bar

input test_values = no;
addchartbubble(test_values, low * 0.99, bn + "\n" + clshi3 + "\n" + periodstart, (if hibar then Color.YELLOW else Color.GRAY), no);
#``````

AAPL day chart length = 60
That's amazing, thanks man. By the way, is there a way to exclude the current bar? i.e., not include today's closing price but look back at the previous 60 bars instead.

That's amazing, thanks man. By the way, is there a way to exclude the current bar? i.e., not include today's closing price but look back at the previous 60 bars instead.
No that is not currently available.

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