draw a horizontal line from the close of a date
labels show the date , close price, and days back to the date.
set the chart to day with enough years to see the date, 3Y 1D.
i left the 2nd aggregation in, from experimenting.
if need to look at a date farther back, could use a week chart, pick week in edit studies, and enter a monday date.
there are many test dates, commented out in the code, for experimenting.
check for holidays and weekends when entering dates.
Ruby:
# datehorzline_00b
#this is the simple code I used re the % below the 52 week high;
## To calculate % below 52 week high
#Def data1 = Round(close/Highest(high,252)-1);
#AddLabel(1, AsPercent(data1), color.white);
###To plot horizontal line from 2/19/2020, the pre-covid Market collapse
# notes:
# if using aggregation of week, the date needs to be a monday
def na = double.nan;
input agg = AggregationPeriod.day;
def daycls = close(period = agg);
#def daycls = close(period = AggregationPeriod.day);
#------------
# test dates
#------------
#input StartDate1 = 20170227; # monday
#------------
#input StartDate1 = 20180226; # monday
#------------
#input StartDate1 = 20191111; # monday
#------------
#input StartDate1 = 20200217; # monday
input StartDate1 = 20200219; # wed
#------------
# just less than 2 years ago
# need 2yr 1day chart to see this, on day agg
#input StartDate1 = 20200224; # monday
#------------
#input StartDate1 = 20200309; # monday
#input StartDate1 = 20200323; # monday
#------------
#input StartDate1 = 20210215; # monday presidents day
#input StartDate1 = 20210216; # tuesday
#input StartDate1 = 20210219; # friday
#input StartDate1 = 20210222; # monday
#input StartDate1 = 20210301; # monday
#input StartDate1 = 20210309; # monday
#------------
#input StartDate1 = 20220107; # monday
#------------
# this works if the date is visible on chart
def dateclose1 = if GetYYYYMMDD() < StartDate1 then na else if GetYYYYMMDD() == StartDate1 then daycls else dateclose1[1];
plot dateline1 = dateclose1;
dateline1.setdefaultcolor(color.yellow);
#dateline1.hidebubble();
addlabel(1, "draw line from close, on " + asprice(startdate1), (if dateclose1 == 0 then color.cyan else color.yellow));
addlabel(1, (if dateclose1 == 0 then "ERROR , can't read from date" else ("$" + dateclose1)), (if dateclose1 == 0 then color.cyan else color.yellow));
# -------------------
def t = DaysFromDate(StartDate1);
addlabel(1, "days back " + t, color.yellow);
#
i was experimenting with another way, that sort of works, except that it was reading data from the wrong date.
maybe there is something i'm overlooking...?
. get days to date , DaysFromDate(StartDate1)
. read aggregation of chart
. calc bars from last bar to date
. use getvalue to read the data from x bars back
copy this to the end of the first code
Ruby:
# this section sort of works for older dates
# it pulls data , but not from correct date
# calc a bar offset back to date, if chart is day or week
def t = DaysFromDate(StartDate1);
addlabel(1, "days back " + t, color.yellow);
# calc bars back
def dateoffset1;
if chartday == 1 then {
# day chart
dateoffset1 = t;
} else if chartday == 7 then {
# week chart
dateoffset1 = t/7;
# intraday chart times don't work. max days is 360.
#} else if chartmin == 240 then {
# # 4 hour chart, 2 bars/day
# dateoffset1 = t*2;
#} else if chartmin == 60 then {
# # hour chart, 7 bars/day
# dateoffset1 = t*7;
} else {
dateoffset1 = na;
}
# on the last bar, look back x bars and read close (from the date)
def dateclose2 = if lastbar then getvalue(close, dateoffset1) else dateclose2[1];
# https://usethinkscript.com/threads/current-price-line-indicator-for-thinkorswim.8793/
input barsBack = 1600;
def c = if !IsNaN(close) and IsNaN(close[-1])
then dateclose2
else c[1];
plot line = if isNaN(close[-barsBack])
then c[-barsBack]
else Double.NaN;
line.SetLineWeight(2);
line.SetDefaultColor(Color.magenta);
line.SetStyle(Curve.MEDIUM_DASH);
addlabel(1, "close from, " + asprice(StartDate1), color.magenta);
addlabel(1, t + " days ago", color.magenta);
addlabel(1, dateoffset1 + " bars back", color.magenta);
addlabel(1, line, color.magenta);
#
2020 holidays
https://www.nasdaq.com/articles/2020-stock-market-holidays-and-bond-market-holidays-2019-12-28
2021 holidays
https://www.nasdaq.com/market-activity/stock-market-holiday-calendar
2022 holidays
https://www.nasdaq.com/market-activity/2022-stock-market-holiday-calendar