#Previous_Day_HighLowClose_RTHrs_ExtHrs_Sessions_for_Futures_v1
#[email protected]
#Modified by Sleepyz
#Extended Lines across chart
#Added Today's RTH open
#Added Mode choice between RTHrs v EXTrs Sessions
#Exthrs mode only works on TODAY's Premarket (1800 - 929) for futures
input agg                       = {default DAY, WEEK, MONTH};
input mode                      = {default RTHrs, EXTHrs};
input extend_lines_across_chart = yes;
input rthbegin                  = 0930;
input rthend                    = 1600;
input extend                    = 1800;
input showBubbles               = no;
input showValuesInBubbles       = yes;
input showLabels                = yes;
input showValuesInLabels        = yes;
plot todayrthOpen;
plot prevClose;
plot prevLow;
plot prevHigh;
def NA    = Double.NaN;
def bn    = BarNumber();
def exp   = IsNaN(close);
def rth   = SecondsFromTime(rthbegin) > - 0 and SecondsFromTime(rthend) <= 0;
def start = GetDayOfWeek(GetYYYYMMDD());
def period  =
if agg == agg.MONTH then GetMonth()
else if agg == agg.WEEK then GetWeek()
else GetYYYYMMDD();
def count = if !IsNaN(close) and period != period[1] then count[1] + 1 else count[1];
def cond  = HighestAll(count) - count;
################# RTH Open
def rthopen      = if mode == mode.RTHrs and (GetDay() == GetLastDay() or exp) and SecondsFromTime(rthbegin) == 0
                   then open
                   else rthopen[1];
todayrthOpen     = if extend_lines_across_chart == yes
                   then HighestAll(rthopen)
                   else if exp
                   then rthopen
                   else NA;
################# close
def extclose = close(period = agg)[1];
def rthclose = CompoundValue(1, if cond == 1 and SecondsFromTime(rthend) == 0 then close[1] else rthclose[1], close);
def hpdclose = if mode == mode.RTHrs and GetDay() == GetLastDay()
               then rthclose[1]
               else if mode == mode.EXTHrs and GetDay() == GetLastDay()
               then  close("period" = agg)[1]
               else hpdclose[1];
prevClose    = if extend_lines_across_chart == yes and mode == mode.RTHrs
               then HighestAll(hpdclose)
               else if exp and mode == mode.RTHrs
               then hpdclose
               else NA;
################# Low
def extlowtoday = CompoundValue(1,
                  if cond == 0 and cond[1] == 1
                  then low
                  else if SecondsFromTime(0929) <= 0
                  then Min(low, extlowtoday[1])
                  else extlowtoday[1], low);
def rthlowday   = CompoundValue(1,
                  if cond[1] == 2 then Double.NaN
                  else if cond == 1 and SecondsFromTime(rthbegin) == 0
                  then low
                  else if cond >= 1 and rth == 1 and low < rthlowday[1]
                  then low else rthlowday[1], low);
def rthlowweek = if cond == 1 then if start[1] != 1 and start == 1 then NA
                 else if start == 1 and SecondsFromTime(rthbegin) == 0 then low
                 else if start >= 1 and rth and low < rthlowweek[1] then low
                 else rthlowweek[1]
                 else NA;
def rthlowmon  = if cond == 1 then if cond[1] == 2 and cond == 1 then NA
                 else if start == 1 and SecondsFromTime(rthbegin) == 0 then low
                 else if start >= 1 and rth and low < rthlowmon[1] then low
                 else rthlowmon[1]
                 else NA;
def hpdlow     = if mode == mode.RTHrs and agg == agg.DAY
                 then HighestAll(if IsNaN(close[-1]) then rthlowday else NA)
                 else if mode == mode.RTHrs and agg == agg.WEEK
                 then LowestAll(if cond == 1 then rthlowweek else NA)
                 else if mode == mode.RTHrs and agg == agg.MONTH
                 then LowestAll(if cond == 1 then rthlowmon else NA)
                 else if mode == mode.EXTHrs
                 then HighestAll(if IsNaN(close[-1]) then extlowtoday else NA)
                 else hpdlow[1];
prevLow        = if extend_lines_across_chart == yes
                 then HighestAll(hpdlow)
                 else if exp
                 then HighestAll(hpdlow)
                 else NA;
################# High
def exthightoday = if cond == 0 and cond[1] == 1
                   then high
                   else if SecondsFromTime(0929) <= 0
                   then Max(high, exthightoday[1])
                   else exthightoday[1];
def rthhighday   = CompoundValue(1,
                   if cond == 1 and SecondsFromTime(rthbegin) == 0
                   then high
                   else if cond == 1 and rth
                   then Max(high, rthhighday[1])
                   else rthhighday[1], high);
def rthhighweek = if cond == 1 then
                  if start[1] != 1 and start == 1
                  then Double.NaN
                  else if start == 1 and SecondsFromTime(0930) == 0
                  then high
                  else if start >= 1 and rth and high > rthhighweek[1]
                  then high
                  else rthhighweek[1]
                  else NA;
def rthhighmon  = if cond == 1 then
                  if cond[1] == 2 and cond == 1
                  then Double.NaN
                  else if start == 1 and SecondsFromTime(0930) == 0
                  then high
                  else if start >= 1 and rth and high > rthhighmon[1]
                  then high
                  else rthhighmon[1]
                  else NA;
def hpdhigh  = if mode == mode.RTHrs and agg == agg.DAY
               then HighestAll(if IsNaN(close[-1]) then rthhighday else NA)
               else if mode == mode.RTHrs and agg == agg.WEEK
               then HighestAll(if cond == 1 then rthhighweek else NA)
               else if mode == mode.RTHrs and agg == agg.MONTH
               then HighestAll(if cond == 1 then rthhighmon else NA)
               else if mode == mode.EXTHrs
               then HighestAll(if IsNaN(close[-1]) then exthightoday else Double.NaN)
               else hpdhigh[1];
prevHigh     = if extend_lines_across_chart == yes
               then HighestAll(hpdhigh)
               else if exp
               then HighestAll(hpdhigh)
               else NA;
#Labels
AddLabel(showLabels, mode, Color.WHITE);
AddLabel(showLabels, if mode == mode.EXTHrs then "TODAY's PREMKT" else "PREV " + agg, Color.LIGHT_GRAY);
AddLabel(showLabels and mode == mode.RTHrs,
if showValuesInLabels and mode == mode.RTHrs then "RO $" + todayrthOpen
else "TO", todayrthOpen.TakeValueColor());
AddLabel(showLabels,
(if showValuesInLabels and mode == mode.EXTHrs
then "H $" + exthightoday
else if showValuesInLabels and mode == mode.RTHrs
then "H $" + prevHigh
else "H"), if mode == mode.EXTHrs then GlobalColor("EH") else GlobalColor("PH"));
AddLabel(showLabels,
(if showValuesInLabels and mode == mode.EXTHrs
then "L $" + extlowtoday
else if showValuesInLabels
then "L $" + prevLow
else "L"), if mode == mode.EXTHrs then GlobalColor("EL") else GlobalColor("PL"));
AddLabel(showLabels,
(if showValuesInLabels and mode == mode.RTHrs then "RPC $" + prevClose
else ""), GlobalColor("PC"));
#Bubbles
input bubblemover = 1;
def b  = bubblemover;
def b1 = b + 1;
AddChartBubble(showBubbles and IsNaN(close[b]) and !IsNaN(close[b1]) , todayrthOpen[b],
if showValuesInBubbles then "TO $" + todayrthOpen[b] else "TO", GlobalColor("TO"), yes);
AddChartBubble(showBubbles and IsNaN(close[b]) and !IsNaN(close[b1]) , prevClose[b],
(if showValuesInBubbles
then if mode == mode.EXTHrs
     then ""
     else "RPC $" + prevClose[b]
else if mode == mode.RTHrs
then "RC"
else ""), GlobalColor("PC"), yes);
AddChartBubble(showBubbles and IsNaN(close[b]) and !IsNaN(close[b1]) , prevLow[b],
(if showValuesInBubbles
then if mode == mode.EXTHrs
     then "ETL $" + prevLow[b]
     else "RPL $" + prevLow[b]
else if mode == mode.RTHrs
then "RPL"
else "ETL"), if mode == mode.EXTHrs then GlobalColor("EL") else GlobalColor("PL"), yes);
AddChartBubble(showBubbles and IsNaN(close[b]) and !IsNaN(close[b1]) , prevHigh[b],
(if showValuesInBubbles
then if mode == mode.EXTHrs
     then "ETH $" + prevHigh[b]
     else "RPH $" + prevHigh[b]
else if mode == mode.RTHrs
then "RPH"
else "ETH"), if mode == mode.EXTHrs then GlobalColor("EH") else GlobalColor("PH"), yes);
#Colors
DefineGlobalColor("TO", Color.WHITE);
DefineGlobalColor("PL", Color.RED);
DefineGlobalColor("PH", Color.GREEN);
DefineGlobalColor("PC", Color.YELLOW);
DefineGlobalColor("EH", Color.CYAN);
DefineGlobalColor("EL", Color.MAGENTA);
todayrthOpen.SetDefaultColor(GlobalColor("TO"));
prevLow.AssignValueColor(if mode == mode.EXTHrs then GlobalColor("EL") else GlobalColor("PL"));
prevHigh.AssignValueColor(if mode == mode.EXTHrs then GlobalColor("EH") else GlobalColor("PH"));
prevClose.SetDefaultColor(GlobalColor("PC"));
prevLow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
prevHigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
prevClose.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#