#lines_5x_1bar_each_day
#https://usethinkscript.com/threads/horizontal-line-at-midpoint-of-1-min-bar-for-last-5-days.19394/
#Horizontal line at midpoint of 1 min. bar for last 5 days
# a script to plot 5 horizontal lines,
# on today's chart,
# of the midpoint of a user-selected 1-min. bar,
# during the the last 5 days.
# There would be 5 lines - one for each of the midpoints over the last 5 days.
def na = Double.NaN;
def bn = BarNumber();
def d = GetDay();
def istoday = if GetLastDay() == d then 1 else 0;
def newday = if d != d[1] then 1 else 0;
def daycnt = if bn == 1 then 1 else if IsNaN(close) then 0 else if (!IsNaN(close) and newday) then daycnt[1] + 1 else daycnt[1];
def maxdays = HighestAll(daycnt);
def revdaycnt = maxdays - daycnt + 1;
# EST time HHMM
input time = 1115;
def t1 = if SecondsFromTime(time)[1] < 0 and SecondsFromTime(time) >= 0 then 1 else 0;
def hr = floor(time / 100);
def mn = time - (hr * 100);
def start1minttl = (hr * 60) + mn;
def hrs = start1minttl/60;
addlabel(yes, (hr + ":" + mn), color.magenta);
def days_back = 5;
def mid = MidBodyVal();
#def mid = (high + low)/2;
def d1 = if bn == 1 or isnan(close) then 0 else if revdaycnt == 1 and t1 then mid else d1[1];
def d2 = if bn == 1 or isnan(close) then 0 else if revdaycnt == 2 and t1 then mid else d2[1];
def d3 = if bn == 1 or isnan(close) then 0 else if revdaycnt == 3 and t1 then mid else d3[1];
def d4 = if bn == 1 or isnan(close) then 0 else if revdaycnt == 4 and t1 then mid else d4[1];
def d5 = if bn == 1 or isnan(close) then 0 else if revdaycnt == 5 and t1 then mid else d5[1];
input today_only = yes;
def x = if !today_only then 1 else istoday;
def dayz = revdaycnt <= days_back and revdaycnt >= 1;
plot z1 = if x and d1 > 0 then d1 else na;
plot z2 = if x and d2 > 0 then d2 else na;
plot z3 = if x and d3 > 0 then d3 else na;
plot z4 = if x and d4 > 0 then d4 else na;
plot z5 = if x and d5 > 0 then d5 else na;
z1.SetDefaultColor(getColor(1));
z2.SetDefaultColor(getColor(2));
z3.SetDefaultColor(getColor(3));
z4.SetDefaultColor(getColor(4));
z5.SetDefaultColor(getColor(5));
#----------------------------
def daystoomany = if days_back < 1 or days_back > maxdays then 1 else 0;
AddLabel(daystoomany, " ONLY " + maxdays + " DAYS ON CHART. enter a different number for days back", Color.CYAN);
input test1 = no;
AddChartBubble(test1, low,
newday + " new\n" +
daycnt + " cnt\n" +
revdaycnt + " rev\n" +
maxdays + " max\n"
, (if dayz then Color.YELLOW else color.gray), no);
input test2_line = no;
addverticalline((test2_line and t1 and dayz), time, color.cyan);
#