input showOnlyToday = YES;
input Market_Open_Time = 0931;
input Market_Close_Time = 1615;
input Market_Close_Time2 = 1600;
def OpeningBell = if SecondsTillTime(Market_Open_Time)[1] > 0 and SecondsTillTime(Market_Open_Time) <= 0 or (SecondsTillTime(Market_Open_Time)[1] < SecondsTillTime(Market_Open_Time) and SecondsTillTime(Market_Open_Time)[1] > 0) then 1 else 0;
def day = GetDay();
def lastDay = GetLastDay();
def isToday = If(day == lastDay, 1, 0);
def shouldPlot = If(showOnlyToday and isToday, 1, If(!showOnlyToday, 1, 0));
def pastOpen = If((SecondsTillTime(Market_Open_Time) > 0), 0, 1);
def pastClose = If((SecondsTillTime(Market_Close_Time) > 0), 0, 1);
def pastClose2 = If((SecondsTillTime(Market_Close_Time2) > 0), 0, 1);
def marketOpen = If(pastOpen and !pastClose, 1, 0);
def firstBar = If (day[1] != day, 1, 0);
def closingBell = if SecondsTillTime(Market_Close_Time)[1] > 0 and SecondsTillTime(Market_Close_Time) <= 0 or (SecondsTillTime(Market_Close_Time)[1] < SecondsTillTime(Market_Close_Time) and SecondsTillTime(Market_Close_Time)[1] > 0) then 1 else 0;
def closingBell2 = if SecondsTillTime(Market_Close_Time2)[1] > 0 and SecondsTillTime(Market_Close_Time2) <= 0 or (SecondsTillTime(Market_Close_Time2)[1] < SecondsTillTime(Market_Close_Time2) and SecondsTillTime(Market_Close_Time2)[1] > 0) then 1 else 0;
rec regHoursHigh = If(high > regHoursHigh[1] and marketOpen, high, If(marketOpen and !firstBar, regHoursHigh[1], high));
rec regHoursLow = If(low < regHoursLow[1] and marketOpen, low, If(marketOpen and regHoursLow[1] > 0 and !firstBar, regHoursLow[1], low));
rec runningClose = CompoundValue(1, if closingBell then close[1] else runningClose[1], close);
rec prevClose = CompoundValue(1, if closingBell then runningClose else prevClose[1], close);
#rec runningClose2 = compoundValue(1, if closingBell2 then close[1] else runningClose2[1], close);
#rec prevClose2 = compoundValue(1, if closingBell2 then #runningClose2 else prevClose2[1], close);
rec prevHigh = CompoundValue(1, if closingBell then regHoursHigh[1] else prevHigh[1], high);
rec prevLow = CompoundValue(1, if closingBell then regHoursLow[1] else prevLow[1], low);
plot pc = if shouldPlot then prevClose else Double.NaN;
pc.SetStyle(Curve.SHORT_DASH);
pc.SetDefaultColor(Color.WHITE);
#plot pc2 = if shouldPlot then prevClose2 else double.nan;
#pc2.SetStyle(curve.SHORT_DASH);
#pc2.setDefaultColor(color.pink);
plot pl = if shouldPlot then prevLow else Double.NaN;
pl.SetDefaultColor(Color.DARK_RED);
pl.SetStyle(Curve.SHORT_DASH);
plot ph = if shouldPlot then prevHigh else Double.NaN;
ph.SetStyle(Curve.SHORT_DASH);
ph.SetDefaultColor(Color.DARK_GREEN);
rec runningOpen = CompoundValue(1, if OpeningBell then open[1] else runningOpen[1], open);
rec prevOpen = CompoundValue(1, if OpeningBell then runningOpen else prevOpen[1], open);
plot Open = if shouldPlot then prevOpen else Double.NaN;
Open.SetStyle(Curve.SHORT_DASH);
Open.SetDefaultColor(Color.YELLOW);