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