# min_left_in_day
# https://usethinkscript.com/threads/market-hours-thinkscript.15454/
#Market Hours Thinkscript
#Does anyone have a thinkscript to identify when the current symbol is about to close?
#----------------------
script hrmin {
input m = 1;
input zone = 0;
# localhouradj
# def hr1 = m/60;
def hr1 = (m+(zone*60))/60;
def hr2 = floor(hr1);
def min2 = (hr1-hr2)*60;
plot hr = hr2;
plot min = min2;
}
#-----------------------
def na = double.nan;
def bn = barnumber();
# -------------------
# misc
def lastbar = (!isnan(close) and isnan(close[-1]));
def lastcls = if isnan(close) then lastcls[1] else close;
def newday = getday() <> getday()[1];
#def newweek = getweek() <> getweek()[1];
def currentday = (getday() == getlastday());
#def currentweek = (Getweek() == Getlastweek());
# minutes from midnight
def t0 = 0;
def t = secondsfromTime(t0);
def m = t/60;
# where to look for period start and stop times
# 2 day before current day
def days_back_date = 2;
def dayx = (getday() + days_back_date) == getlastday();
# restrict calculations to just recent days
def recent = (getlastday() - getday()) <= days_back_date;
#-------------------------
input timezone = { "EST", default "CST", "MST", "PST"};
#def starthour = (if timezone == timezone."EST" then 9
# else if timezone == timezone."CST" then 8
# else if timezone == timezone."MST" then 7
# else 6) ;
def localhouradj = (if timezone == timezone."EST" then 0
else if timezone == timezone."CST" then -1
else if timezone == timezone."MST" then -2
else -3);
#-----------------------
def hr_est = hrmin(m).hr;
def min_est = hrmin(m).min;
def hr_local = hrmin(m, localhouradj).hr;
def min_local = hrmin(m, localhouradj).min;
#-----------------------
# chart symbol trading hours , or some manual entered times
input time_period = { default chart , start_stop_times };
# open/close times (EST)
input start = 0930;
input end = 1600;
def manualtime;
if end > start then {
manualtime = if secondsfromTime(start) >= 0 and secondstillTime(end) > 0 then 1 else 0;
} else {
manualtime = if (secondsfromTime(start) >= 0 and secondstillTime(2359) >= 0) or (secondsfromTime(0) >= 0 and secondstillTime(end) > 0) then 1 else 0;
}
# daytime_regstartstop
def charttime = (GetTime() >= RegularTradingStart(GetYYYYMMDD()) and GetTime() < RegularTradingEnd(GetYYYYMMDD()));
def daytime = if time_period == time_period.chart then charttime else manualtime;
#------------------------
# m = minutes after midnight
def startm = if bn == 1 then 0 else if dayx and !daytime[1] and daytime then m else startm[1];
def endm = if bn == 1 then 0 else if dayx and daytime[1] and !daytime[0] then m else endm[1];
# EST times
def hr_st = hrmin(startm).hr;
def min_st = hrmin(startm).min;
def hr_end = hrmin(endm).hr;
def min_end = hrmin(endm).min;
# local times
def local_hr_st = hrmin(startm, localhouradj).hr;
def local_min_st = hrmin(startm, localhouradj).min;
def local_hr_end = hrmin(endm, localhouradj).hr;
def local_min_end = hrmin(endm, localhouradj).min;
#------------------------
def minutes_period = if endm > startm then (endm - startm)
else if endm < startm then ((endm + 1440) - startm)
else 0;
def periodhr = hrmin(minutes_period).hr;
def periodmin = hrmin(minutes_period).min;
#-----------------------
# minutes from a time
def minafterstart = if daytime and m > startm then (m - startm)
else if daytime and m < startm then ((m + 1440) - startm)
else 0;
def minbeforeend = if daytime and endm > startm then (endm - m)
else if daytime and endm < startm then ((endm + 1440) - m)
else 0;
def minafterend = if !daytime and m > endm then (m - endm)
else if !daytime and m < endm then ((m + 1440) - endm)
else 0;
def minbeforestart = if !daytime and startm > m then (startm - m)
else if !daytime and startm < m then ((startm + 1440) - m)
else 0;
#------------------------
addlabel(1, " ", color.black);
addlabel(1, (hr_st + ":" + min_st + " to " + hr_end + ":" + min_end) + " EST", color.magenta);
addlabel(1, " ", color.black);
addlabel(1, "period minutes " + minutes_period, color.cyan);
addlabel(1, periodhr + ":" + periodmin, color.cyan);
addlabel(1, " ", color.black);
addlabel(1, (local_hr_st + ":" + local_min_st + " to " + local_hr_end + ":" + local_min_end) + " " + timezone, color.yellow);
addlabel(1, " ", color.black);
addlabel(daytime, minafterstart + " minutes after period start", color.yellow);
addlabel(daytime, minbeforeend + " minutes until period end", color.yellow);
addlabel(!daytime, minafterend + " minutes after period end", color.gray);
addlabel(!daytime, minbeforestart + " minutes before period start", color.gray);
#--------------------------
input test_daytime_dots = yes;
plot zd = if test_daytime_dots and daytime then high * 1.004 else Double.NaN;
zd.SetPaintingStrategy(PaintingStrategy.POINTS);
zd.SetDefaultColor(Color.WHITE);
# x.setlineweight(1);
# x.hidebubble();
input test1 = no;
addchartbubble(test1, low*0.999,
dayx + " D\n" +
currentday + " c\n" +
recent + " r"
, (if dayx then color.magenta else if recent then color.yellow else color.gray), no);
input test2_stats = no;
addchartbubble(test2_stats and recent, low*0.999,
#dayx + "\n" +
m + " m\n" +
(hr_est + ":" + min_est) + " EST\n" +
(hr_local + ":" + min_local) + " " + timezone + "\n" +
startm + " st\n" +
endm + " end\n" +
minafterstart + " aftst\n" +
minbeforeend + " toend\n" +
minafterend + " aftend\n" +
minbeforestart + " tost\n"
, (if daytime then color.yellow else color.gray), no);
#