#Range Bars HLOC
input lookback = 1;
input showtodayonly = yes;
def ymd = GetYYYYMMDD();
def count = if !IsNaN(close) and ymd != ymd[1] then count[1] + 1 else count[1];
def cond = HighestAll(count) - count ;
## High
def hh = if IsNaN(close) then hh[1] else
CompoundValue(1, if cond == lookback and SecondsFromTime(0930)[1] < 0 and SecondsFromTime(0930) >= 0
then high
else if cond == lookback and SecondsFromTime(1600) < 0 and high > hh[1]
then high
else hh[1] , high);
def hhbn = if cond == lookback and high == hh
then BarNumber()
else hhbn[1];
plot hhplot = if showtodayonly and GetDay() != GetLastDay()
then Double.NaN
else if BarNumber() >= HighestAll(hhbn)
then hh
else Double.NaN;
## Low
def ll = if IsNaN(close) then ll[1] else
CompoundValue(1, if cond == lookback and SecondsFromTime(0930)[1] < 0 and SecondsFromTime(0930) >= 0
then low
else if cond == lookback and SecondsFromTime(1600) < 0 and low < ll[1]
then low
else ll[1] , low);
def llbn = if cond == lookback and low == ll
then BarNumber()
else llbn[1];
plot llplot = if showtodayonly and GetDay() != GetLastDay()
then Double.NaN
else if BarNumber() >= HighestAll(llbn)
then ll
else Double.NaN;
## Open
def oo = if IsNaN(close) then oo[1] else
CompoundValue(1, if cond == lookback and SecondsFromTime(0930)[1] < 0 and SecondsFromTime(0930) >= 0
then open
else oo[1] , open);
def oobn = if cond == lookback and SecondsFromTime(1600) < 0 and open == oo
then BarNumber()
else oobn[1];
plot ooplot = if showtodayonly and GetDay() != GetLastDay()
then Double.NaN
else if cond <= lookback and BarNumber() >= HighestAll(oobn)
then oo
else Double.NaN;
## Close
def cc = if IsNaN(close) then cc[1] else
CompoundValue(1, if cond == lookback and SecondsFromTime(0930)[1] < 0 and SecondsFromTime(0930) >= 0
then close
else if cond == lookback and SecondsFromTime(1600) < 0
then close
else cc[1] , close);
def ccbn = if cond == lookback and SecondsFromTime(1600) < 0 and close == cc
then BarNumber()
else ccbn[1];
plot ccplot = if showtodayonly and GetDay() != GetLastDay()
then Double.NaN
else if cond <= lookback and BarNumber() >= HighestAll(ccbn)
then cc
else Double.NaN;
hhplot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
llplot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ooplot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ccplot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
## Labels
input showlabels = 1;
AddLabel(showlabels, "High: " + hh + " Low: " + ll + " Open: " + oo + " Close: " + cc, Color.WHITE);
## Bubbles
input showbubbles = yes;
input bubblemover = 2;
def mover = showbubbles and IsNaN(close[bubblemover]) and !IsNaN(close[bubblemover + 1]);
AddChartBubble(mover, hhplot, "H-" + lookback + " " + astext(hhplot), hhplot.TakeValueColor());
AddChartBubble(mover, llplot, "L-" + lookback + " " + astext(llplot), llplot.TakeValueColor());
AddChartBubble(mover, ooplot, "O-" + lookback + " " + astext(ooplot), ooplot.TakeValueColor());
AddChartBubble(mover, ccplot, "C-" + lookback + " " + astext(ccplot), ccplot.TakeValueColor());
#