#day_range_midline
#https://usethinkscript.com/threads/day-range-calculation.19678/
#Day Range calculation
def na = double.nan;
def bn = barnumber();
def big = 99999;
def d = getday();
def istoday = d == getlastday();
def prestart = 0400;
def start = 0930;
def end = 1600;
def postend = 2000;
def pretime = if secondsfromTime(prestart) >= 0 and secondstillTime(start) > 0 then 1 else 0;
def daytime = if secondsfromTime(start) >= 0 and secondstillTime(end) > 0 then 1 else 0;
def posttime = if secondsfromTime(end) >= 0 and secondstillTime(postend) > 0 then 1 else 0;
input today_only = yes;
def today2 = if today_only then istoday else 1;
input include_pre_market_levels = no;
input include_post_market_levels = no;
#def x = if include_pre_market_levels then (pretime or daytime) else daytime;
def x = today2 and ((if include_pre_market_levels then pretime else 0) or
(if include_post_market_levels then posttime else 0) or
daytime);
def hi = if bn == 1 then high
else if d != d[1] then high
else if x and !x[1] then high
else if x then max(hi[1], high)
else 0;
def lo = if bn == 1 then low
else if d != d[1] then low
else if x and !x[1] then low
else if x then min(lo[1], low)
else 0;
def mid = if x then (hi + lo)/2 else 0;
plot zhi = if hi > 0 then hi else na;
zhi.SetDefaultColor(Color.cyan);
#zhi.setlineweight(1);
zhi.hidebubble();
plot zlo = if lo < big and lo > 0 then lo else na;
zlo.SetDefaultColor(Color.cyan);
#zlo.setlineweight(1);
zlo.hidebubble();
plot zmid = if mid > 0 then mid else na;
zmid.SetDefaultColor(Color.yellow);
zmid.setlineweight(2);
zmid.hidebubble();
plot zd = if 0 and daytime then high*1.01 else double.nan;
zd.SetPaintingStrategy(PaintingStrategy.POINTS);
zd.SetDefaultColor(Color.white);
# x.setlineweight(1);
# x.hidebubble();
#