# prev_day_levels2_00
#https://usethinkscript.com/threads/previous-day-levels.14193/
#Previous Day Premarket Open, Low, High, Close. (0400 to 0930)
#Previous Day regular trading hours Open, Low, High, Close. (0930 to 1600)
#Previous Day post market Open, Low, High, Close. (1600 to 2000)
#Previous Day Open, Low, High, Close. (0400 to 2000)
def na = double.nan;
def bn = barnumber();
def lastbar = (!isnan(close) and isnan(close[-1]));
def newday = getday() <> getday()[1];
def currentday = getday() == getlastday();
#def cls2 = if isnan(close) then cls2[1] else close;
#def istoday = if GetLastDay() == GetDay() then 1 else 0;
#def isweek = if GetLastweek() == Getweek() then 1 else 0;
#def ismonth = if GetLastmonth() == Getmonth() then 1 else 0;
# 0 = current day , 1 = prev day, 2 = 2 days ago
input days_back_date = 1;
def dayx = (getday() + days_back_date) == getlastday();
#-------------------------------
def start1 = 400;
def end1 = 930;
def daypre = if ( SecondsFromTime(start1) >= 0 and SecondsTillTime(end1) > 0 ) then 1 else 0;
def start2 = 930;
def end2 = 1600;
def dayreg = if ( SecondsFromTime(start2) >= 0 and SecondsTillTime(end2) > 0 ) then 1 else 0;
def start3 = 1600;
def end3 = 2000;
def daypost = if ( SecondsFromTime(start3) >= 0 and SecondsTillTime(end3) > 0 ) then 1 else 0;
def start4 = start1;
def end4 = end3;
def dayall = if ( SecondsFromTime(start4) >= 0 and SecondsTillTime(end4) > 0 ) then 1 else 0;
#--------------------------
def prefirst = if !daypre[1] and daypre then 1 else 0;
def regfirst = if !dayreg[1] and dayreg then 1 else 0;
def postfirst = if !daypost[1] and daypost then 1 else 0;
def allfirst = prefirst;
def prelast = if daypre and !daypre[-1] then 1 else 0;
def reglast = if dayreg and !dayreg[-1] then 1 else 0;
def postlast = if daypost and !daypost[-1] then 1 else 0;
def alllast = postlast;
#--------------------------
# pre day
def pre_open = if isnan(close) then na else if dayx and prefirst then open else pre_open[1];
def pre_close = if isnan(close) then na else if dayx and prelast then close else pre_close[1];
def pre_high = if isnan(close) then na else if dayx and prefirst then high
else if dayx and daypre and high > pre_high[1] then high
else pre_high[1];
def pre_low = if isnan(close) then na else if dayx and prefirst then low
else if dayx and daypre and low < pre_low[1] then low
else pre_low[1];
#--------------------------
# reg day
def reg_open = if isnan(close) then na else if dayx and regfirst then open else reg_open[1];
def reg_close = if isnan(close) then na else if dayx and reglast then close else reg_close[1];
def reg_high = if isnan(close) then na else if dayx and regfirst then high
else if dayx and dayreg and high > reg_high[1] then high
else reg_high[1];
def reg_low = if isnan(close) then na else if dayx and regfirst then low
else if dayx and dayreg and low < reg_low[1] then low
else reg_low[1];
#--------------------------
# post day
def post_open = if isnan(close) then na else if dayx and postfirst then open else post_open[1];
def post_close = if isnan(close) then na else if dayx and postlast then close else post_close[1];
def post_high = if isnan(close) then na else if dayx and postfirst then high
else if dayx and daypost and high > post_high[1] then high
else post_high[1];
def post_low = if isnan(close) then na else if dayx and postfirst then low
else if dayx and daypost and low < post_low[1] then low
else post_low[1];
#--------------------------
# all day
def all_open = if isnan(close) then na else if dayx and allfirst then open else all_open[1];
def all_close = if isnan(close) then na else if dayx and alllast then close else all_close[1];
def all_high = if isnan(close) then na else if dayx and allfirst then high
else if dayx and dayall and high > all_high[1] then high
else all_high[1];
def all_low = if isnan(close) then na else if dayx and allfirst then low
else if dayx and dayall and low < all_low[1] then low
else all_low[1];
#--------------------------
plot z1 = if currentday then pre_open else na;
plot z2 = if currentday then pre_high else na;
plot z3 = if currentday then pre_low else na;
plot z4 = if currentday then pre_close else na;
z1.SetDefaultColor(getcolor(1));
z1.hidebubble();
z2.SetDefaultColor(getcolor(1));
z2.hidebubble();
z3.SetDefaultColor(getcolor(1));
z3.hidebubble();
z4.SetDefaultColor(getcolor(1));
z4.hidebubble();
plot z5 = if currentday then reg_open else na;
plot z6 = if currentday then reg_high else na;
plot z7 = if currentday then reg_low else na;
plot z8 = if currentday then reg_close else na;
z5.SetDefaultColor(getcolor(4));
z5.hidebubble();
z6.SetDefaultColor(getcolor(4));
z6.hidebubble();
z7.SetDefaultColor(getcolor(4));
z7.hidebubble();
z8.SetDefaultColor(getcolor(4));
z8.hidebubble();
plot z9 = if currentday then post_open else na;
plot z10 = if currentday then post_high else na;
plot z11 = if currentday then post_low else na;
plot z12 = if currentday then post_close else na;
z9.SetDefaultColor(getcolor(9));
z9.hidebubble();
z10.SetDefaultColor(getcolor(9));
z10.hidebubble();
z11.SetDefaultColor(getcolor(9));
z11.hidebubble();
z12.SetDefaultColor(getcolor(9));
z12.hidebubble();
# all day lines will be duplicates of others, so don't need to plot them
#plot z13 = if currentday then all_open else na;
#plot z14 = if currentday then all_high else na;
#plot z15 = if currentday then all_low else na;
#plot z16 = if currentday then all_close else na;
#z13.SetDefaultColor(getcolor(4));
#z13.hidebubble();
#z14.SetDefaultColor(getcolor(4));
#z14.hidebubble();
#z15.SetDefaultColor(getcolor(4));
#z15.hidebubble();
#z16.SetDefaultColor(getcolor(4));
#z16.hidebubble();
addlabel(1, "PRE", getcolor(1));
addlabel(1, "REG", getcolor(4));
addlabel(1, "POST", getcolor(9));
#--------------------------
# create candles, after last bar, that represent time periods, pre, reg, post, all
input pre_offset = 3;
input reg_offset = 6;
input post_offset = 9;
input all_offset = 12;
def pre_x = (!isnan(close[pre_offset]) and isnan(close[pre_offset-1]));
def reg_x = (!isnan(close[reg_offset]) and isnan(close[reg_offset-1]));
def post_x = (!isnan(close[post_offset]) and isnan(close[post_offset-1]));
def all_x = (!isnan(close[all_offset]) and isnan(close[all_offset-1]));
# --------------------------------------
# day pre
def aopn = if pre_x then pre_open[pre_offset] else na;
def acls = if pre_x then pre_close[pre_offset] else na;
def ahi = if pre_x then pre_high[pre_offset] else na;
def alo = if pre_x then pre_low[pre_offset] else na;
def auptop = if acls > aopn then acls else na;
def aupbot = if acls > aopn then aopn else na;
AddChart(high = ahi, low = alo, open = aupbot, close = auptop, type = ChartType.CANDLE, growcolor = color.green);
def adwntop = if acls < aopn then aopn else na;
def adwnbot = if acls < aopn then acls else na;
AddChart(high = ahi, low = alo, open = adwntop, close = adwnbot, type = ChartType.CANDLE, growcolor = color.red);
def aztop = if acls == aopn then aopn else na;
def azbot = if acls == aopn then acls else na;
AddChart(high = ahi, low = alo, open = aztop, close = azbot, type = ChartType.CANDLE, growcolor = color.white);
# --------------------------------------
# day reg
def bopn = if reg_x then reg_open[reg_offset] else na;
def bcls = if reg_x then reg_close[reg_offset] else na;
def bhi = if reg_x then reg_high[reg_offset] else na;
def blo = if reg_x then reg_low[reg_offset] else na;
def buptop = if bcls > bopn then bcls else na;
def bupbot = if bcls > bopn then bopn else na;
AddChart(high = bhi, low = blo, open = bupbot, close = buptop, type = ChartType.CANDLE, growcolor = color.green);
def bdwntop = if bcls < bopn then bopn else na;
def bdwnbot = if bcls < bopn then bcls else na;
AddChart(high = bhi, low = blo, open = bdwntop, close = bdwnbot, type = ChartType.CANDLE, growcolor = color.red);
def bztop = if bcls == bopn then bopn else na;
def bzbot = if bcls == bopn then bcls else na;
AddChart(high = bhi, low = blo, open = bztop, close = bzbot, type = ChartType.CANDLE, growcolor = color.white);
# --------------------------------------
# day post
def copn = if post_x then post_open[post_offset] else na;
def ccls = if post_x then post_close[post_offset] else na;
def chi = if post_x then post_high[post_offset] else na;
def clo = if post_x then post_low[post_offset] else na;
def cuptop = if ccls > copn then ccls else na;
def cupbot = if ccls > copn then copn else na;
AddChart(high = chi, low = clo, open = cupbot, close = cuptop, type = ChartType.CANDLE, growcolor = color.green);
def cdwntop = if ccls < copn then copn else na;
def cdwnbot = if ccls < copn then ccls else na;
AddChart(high = chi, low = clo, open = cdwntop, close = cdwnbot, type = ChartType.CANDLE, growcolor = color.red);
def cztop = if ccls == copn then copn else na;
def czbot = if ccls == copn then ccls else na;
AddChart(high = chi, low = clo, open = cztop, close = czbot, type = ChartType.CANDLE, growcolor = color.white);
#----------------------------------
# day all
def dopn = if all_x then all_open[all_offset] else na;
def dcls = if all_x then all_close[all_offset] else na;
def dhi = if all_x then all_high[all_offset] else na;
def dlo = if all_x then all_low[all_offset] else na;
def duptop = if dcls > dopn then dcls else na;
def dupbot = if dcls > dopn then dopn else na;
AddChart(high = dhi, low = dlo, open = dupbot, close = duptop, type = ChartType.CANDLE, growcolor = color.green);
def ddwntop = if dcls < dopn then dopn else na;
def ddwnbot = if dcls < dopn then dcls else na;
AddChart(high = dhi, low = dlo, open = ddwntop, close = ddwnbot, type = ChartType.CANDLE, growcolor = color.red);
def dztop = if dcls == dopn then dopn else na;
def dzbot = if dcls == dopn then dcls else na;
AddChart(high = dhi, low = dlo, open = dztop, close = dzbot, type = ChartType.CANDLE, growcolor = color.white);
#----------------------------------
addchartbubble(lastbar, all_low*0.998,
"PRE / REG / POST / ALL"
, color.yellow, no);
#----------------------------------
#----------------------------------
#----------------------------------
# test data
addchartbubble(0, low,
getday() + "\n" +
getlastday()
, (if dayx then color.yellow else color.gray), no);
addchartbubble(0 and daypost, low, "x", color.red, no);
addchartbubble(0, low,
prefirst + "\n" +
regfirst + "\n" +
postfirst
, color.yellow, no);
addchartbubble(0 and post_x, 84,
copn + "\n" +
chi + "\n" +
clo + "\n" +
ccls
, color. yellow);
addlabel(0, copn + chi + clo + ccls, color. yellow);
#