How to mark price level at highest closing price within time range

tctrader15

New member
how would I go about creating an indicator that places a horizontal line at the highest (and lowest) closing prices within a specified date range and chart aggregation? Currently trying to mark the highest monthly closing price within the pre-covid time frame (11/01/2019 - 02/01/2020) as well as at the post-covid crash time frame (03/01/2020 - 05/01/2020).

I've been trying to take chunks from the code presented in another thread called "Calculate Price % from pre-Covid crash" which pulls the price from a specified day. with me being completely new to this, I haven't gotten far haha.

thanks in advance for any guidance
 
Solution
how would I go about creating an indicator that places a horizontal line at the highest (and lowest) closing prices within a specified date range and chart aggregation? Currently trying to mark the highest monthly closing price within the pre-covid time frame (11/01/2019 - 02/01/2020) as well as at the post-covid crash time frame (03/01/2020 - 05/01/2020).

I've been trying to take chunks from the code presented in another thread called "Calculate Price % from pre-Covid crash" which pulls the price from a specified day. with me being completely new to this, I haven't gotten far haha.

thanks in advance for any guidance

there probably are similar studies on this site somewhere. i could have searched the site or my files...
how would I go about creating an indicator that places a horizontal line at the highest (and lowest) closing prices within a specified date range and chart aggregation? Currently trying to mark the highest monthly closing price within the pre-covid time frame (11/01/2019 - 02/01/2020) as well as at the post-covid crash time frame (03/01/2020 - 05/01/2020).

I've been trying to take chunks from the code presented in another thread called "Calculate Price % from pre-Covid crash" which pulls the price from a specified day. with me being completely new to this, I haven't gotten far haha.

thanks in advance for any guidance

there probably are similar studies on this site somewhere. i could have searched the site or my files and found something.
sometimes i like to see how quick i can make a study from an idea. i think a little code typing is good for learning and retaining what you know.
----------------------------------

here is a basic version, to draw high low lines between 2 dates
can pick the price type

it is based on putting a formula within a highestall() .
this will find the highest price, just for the desired date range.
def rnghi1 = highestall(if inrng1 then high_price1 else 0);

Ruby:
# date_rng_hi_lo_01

def na = double.nan;
input start_date = 20220101;
input stop_date = 20220301;
input high_price = close;
input low_price = close;

# read the date on each bar
def dat = GetYYYYMMDD();

# are bars within the date range ?
def inrng = if dat >= start_date and dat <= stop_date then 1 else 0;

def big = 999999;
def rnghi = highestall(if inrng then high_price else 0);
def rnglo = lowestall(if inrng then low_price else big);

plot hiz = if inrng and rnghi > 0 then rnghi else na;
plot loz = if inrng and rnglo > 0 then rnglo else na;
hiz.setdefaultcolor(color.light_gray);
loz.setdefaultcolor(color.light_gray);
#

5DRzfBQ.jpg
 
Last edited:
Solution

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

how would I go about creating an indicator that places a horizontal line at the highest (and lowest) closing prices within a specified date range and chart aggregation? Currently trying to mark the highest monthly closing price within the pre-covid time frame (11/01/2019 - 02/01/2020) as well as at the post-covid crash time frame (03/01/2020 - 05/01/2020).

I've been trying to take chunks from the code presented in another thread called "Calculate Price % from pre-Covid crash" which pulls the price from a specified day. with me being completely new to this, I haven't gotten far haha.

thanks in advance for any guidance

the OP mentioned 2 date ranges... and sometimes i can't help myself from tweaking a study a little more...
so i made another version, that can draw 3 sets of lines.


this is version 2
it can draw 3 sets of high low lines, each set in a different color.

can show labels, listing formatted date ranges, in a matching color.
can show vertical lines, at the start and stop of a range, with a date number.
can turn off each date range, (ex. enable range1)

this is meant for a day chart. if chart time is less than day, there will be multiple vertical lines, 1 on each bar for the day.
the default dates are close to the OP's dates. i changes some to be actual trading days, so the vertical lines would show up.

i think i could have checked the day of week of the dates and adjusted them if needed,... but i stopped myself.


Ruby:
# date_rng_hi_lo_02

# ver2 - 3 sets of lines

#https://usethinkscript.com/threads/how-to-mark-price-level-at-highest-closing-price-within-time-range.11412/
#how would I go about creating an indicator that places a horizontal line at the highest (and lowest) closing prices within a specified date range and chart aggregation?
# mark the highest monthly closing price within the pre-covid time frame (11/01/2019 - 02/01/2020)
# as well as at the post-covid crash time frame (03/01/2020 - 05/01/2020).

script date_data {
input data = 0;
def year = Round(data/10000, 0);
def month = Round((data % 10000) / 100, 0);
def day = (data % 100);
plot yr = year;
plot mo = month;
plot dy = day;
}

def na = double.nan;
def big = 999999;
input show_date_lines = yes;
input show_date_labels = yes;
#-------------------------------------

input enable_range1 = yes;
input start_date1 = 20191101;
input stop_date1 = 20200203;
input high_price1 = close;
input low_price1 = close;

# read the date on each bar
def dat1 = GetYYYYMMDD();

def start1_month = date_data(start_date1).mo;
def start1_day = date_data(start_date1).dy;
def start1_year = date_data(start_date1).yr;
def stop1_month = date_data(stop_date1).mo;
def stop1_day = date_data(stop_date1).dy;
def stop1_year = date_data(stop_date1).yr;

# are bars within the date range ?
def inrng1 = if enable_range1 and dat1 >= start_date1 and dat1 <= stop_date1 then 1 else 0;

def rnghi1 = highestall(if inrng1 then high_price1 else 0);
def rnglo1 = lowestall(if inrng1 then low_price1 else big);

plot hiz1 = if inrng1 and rnghi1 > 0 then rnghi1 else na;
plot loz1 = if inrng1 and rnglo1 > 0 then rnglo1 else na;
hiz1.setdefaultcolor(color.yellow);
loz1.setdefaultcolor(color.yellow);

addverticalline((show_date_lines and enable_range1 and dat1 == start_date1), "    " + start_date1 , color.yellow);
addverticalline((show_date_lines and enable_range1 and dat1 == stop_date1), "    " + stop_date1 , color.yellow);

#addlabel(show_date_labels, start_date1 + " to " + stop_date1, color.yellow);
addlabel(show_date_labels and enable_range1, " ", color.black);
addLabel(show_date_labels and enable_range1, "range1: " + start1_month + "/" + start1_day + "/" + AsPrice(start1_year) + " to " + stop1_month + "/" + stop1_day + "/" + AsPrice(stop1_year), color.yellow);

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

input enable_range2 = yes;
input start_date2 = 20200228;
input stop_date2 = 20200501;
input high_price2 = close;
input low_price2 = close;

# read the date on each bar
def dat2 = GetYYYYMMDD();

def start2_month = date_data(start_date2).mo;
def start2_day = date_data(start_date2).dy;
def start2_year = date_data(start_date2).yr;
def stop2_month = date_data(stop_date2).mo;
def stop2_day = date_data(stop_date2).dy;
def stop2_year = date_data(stop_date2).yr;

# are bars within the date range ?
def inrng2 = if enable_range2 and dat2 >= start_date2 and dat2 <= stop_date2 then 1 else 0;

def rnghi2 = highestall(if inrng2 then high_price2 else 0);
def rnglo2 = lowestall(if inrng2 then low_price2 else big);

plot hiz2 = if inrng2 and rnghi2 > 0 then rnghi2 else na;
plot loz2 = if inrng2 and rnglo2 > 0 then rnglo2 else na;
hiz2.setdefaultcolor(color.cyan);
loz2.setdefaultcolor(color.cyan);

addverticalline((show_date_lines and enable_range2 and dat2 == start_date2), "    " + start_date2 , color.cyan);
addverticalline((show_date_lines and enable_range2 and dat2 == stop_date2), "    " + stop_date2 , color.cyan);

addlabel(show_date_labels and enable_range2, " ", color.black);
addLabel(show_date_labels and enable_range2, "range2: " + start2_month + "/" + start2_day + "/" + AsPrice(start2_year) + " to " + stop2_month + "/" + stop2_day + "/" + AsPrice(stop2_year), color.cyan);

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

input enable_range3 = yes;
input start_date3 = 20220301;
input stop_date3 = 20220330;
input high_price3 = close;
input low_price3 = close;

# read the date on each bar
def dat3 = GetYYYYMMDD();

def start3_month = date_data(start_date3).mo;
def start3_day = date_data(start_date3).dy;
def start3_year = date_data(start_date3).yr;
def stop3_month = date_data(stop_date3).mo;
def stop3_day = date_data(stop_date3).dy;
def stop3_year = date_data(stop_date3).yr;

# are bars within the date range ?
def inrng3 = if enable_range3 and dat3 >= start_date3 and dat3 <= stop_date3 then 1 else 0;

def rnghi3 = highestall(if inrng3 then high_price3 else 0);
def rnglo3 = lowestall(if inrng3 then low_price3 else big);

plot hiz3 = if inrng3 and rnghi3 > 0 then rnghi3 else na;
plot loz3 = if inrng3 and rnglo3 > 0 then rnglo3 else na;
hiz3.setdefaultcolor(color.magenta);
loz3.setdefaultcolor(color.magenta);

addverticalline((show_date_lines and enable_range3 and dat3 == start_date3), "    " + start_date3, color.magenta);
addverticalline((show_date_lines and enable_range3 and dat3 == stop_date3), "    " + stop_date3, color.magenta);

addlabel(show_date_labels and enable_range3, " ", color.black);
addLabel(show_date_labels and enable_range3, "range3: " + start3_month + "/" + start3_day + "/" + AsPrice(start3_year) + " to " + stop3_month + "/" + stop3_day + "/" + AsPrice(stop3_year), color.magenta);
#

# ---------------------------------
# ref code

#from tos chat - mobius
#def data = getYYYYMMDD();
#def year = Round(data/10000, 0);
#def month = Round((data % 10000) / 100, 0);
#def day = (data % 100);
#addLabel(1, "date: " + month + "/" + day + "/" + AsPrice(year), color.white);


# if a date is a weekend,
# could adjust dates with soemthing like this, look for dow = 6 or 7 , then adjust a new date var to be a friday
# day of week
#def dow = GetDayofWeek(GetYYYYMMDD());
# monday = 1  friday = 5
#addchartbubble(1, low, dow, color.cyan, no);

#def dow1 = GetDayofWeek(start_date1);
# calc an offset to friday or original
#def start1_adj = if dow1 == 6 then start_date1-1 else if dow1 == 7 then start_date1-2 else 0;
#addverticalline(dat1 == start1_adj, start1_adj, color.green);
#addchartbubble(1, low,  dow1 + " dow1\n" + start1_adj + " adj\n" , color.cyan, no);

#


SPY , 2 sets of high low lines
DMoLQYF.jpg
 
Last edited:
there probably are similar studies on this site somewhere. i could have searched the site or my files and found something.
sometimes i like to see how quick i can make a study from an idea. i think a little code typing is good for learning and retaining what you know.
----------------------------------

here is a basic version, to draw high low lines between 2 dates
can pick the price type

it is based on putting a formula within a highestall() .
this will find the highest price, just for the desired date range.
def rnghi1 = highestall(if inrng1 then high_price1 else 0);

Ruby:
# date_rng_hi_lo_01

def na = double.nan;
input start_date = 20220101;
input stop_date = 20220301;
input high_price = close;
input low_price = close;

# read the date on each bar
def dat = GetYYYYMMDD();

# are bars within the date range ?
def inrng = if dat >= start_date and dat <= stop_date then 1 else 0;

def big = 999999;
def rnghi = highestall(if inrng then high_price else 0);
def rnglo = lowestall(if inrng then low_price else big);

plot hiz = if inrng and rnghi > 0 then rnghi else na;
plot loz = if inrng and rnglo > 0 then rnglo else na;
hiz.setdefaultcolor(color.light_gray);
loz.setdefaultcolor(color.light_gray);
#

5DRzfBQ.jpg
thanks a ton. I was looking around the site for about an hour trying to find a solution -- your code was the one inside the thread I had imported into TOS to try and configure

quick question though. is there anyway you can extend the price levels out to the right end of the chart?

there's a ton I want to add to the code but what you provided is the basis. want to try and figure things out on the fly to learn thinkscript. any advice you'd have on that path is also greatly appreciated
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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