script onh {
input daysback = 1;
input openingTime = 1600;
input closingTime = 0930;
def sec1 = SecondsFromTime(openingTime);
def sec2 = SecondsFromTime(closingTime);
def isTime1 = (sec1 >= 0 and sec1[1] < 0) or (sec1 < sec1[1] and sec1 >= 0);
def isTime2 = (sec2 >= 0 and sec2[1] < 0) or (sec2 < sec2[1] and sec2 >= 0);
def inRange = CompoundValue(1, if isTime1 then 1 else if isTime2 then 0 else inRange[1], 0);
def ymd = inrange;
def candles = !IsNaN(close);
def capture = candles and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay = (HighestAll(dayCount) - dayCount) ;
#thisday.setpaintingStrategy(paintingStrategy.VALUES_BELOW);
def rhi = if inRange and !inRange[1]
then high
else if inRange[1] and high > rhi[1]
then high else rhi[1];
def rHighBar = if thisday == daysback and inRange and high == rhi then BarNumber() else Double.NaN;
def rHighest = if BarNumber() == HighestAll(rHighBar) then rhi else rHighest[1];
plot rangehigh = if (rHighest > 0) then rHighest else Double.NaN;
rangehigh.setpaintingStrategy(paintingStrategy.HORIZONTAL);
rangehigh.setlineWeight(2);
def rlow = if inRange and !inRange[1]
then low
else if inRange[1] and low < rlow[1]
then low else rlow[1];
def rLowBar = if thisday == daysback and inRange and low == rlow then BarNumber() else Double.NaN;
def rlowest = if BarNumber() == HighestAll(rLowBar) then rlow else rlowest[1];
plot rangelow = if (rlowest > 0) then rlowest else Double.NaN;
rangelow.setpaintingStrategy(paintingStrategy.HORIZONTAL);
rangelow.setlineWeight(2);
}
input show_plots_for_daysback = 3;
plot onh0 = if show_plots_for_daysback>=1 then onh(daysback = 1).rangehigh else double.nan;
plot onh1 = if show_plots_for_daysback>=3 then onh(daysback = 3).rangehigh else double.nan;
plot onh2 = if show_plots_for_daysback>=5 then onh(daysback = 5).rangehigh else double.nan;
plot onh3 = if show_plots_for_daysback>=7 then onh(daysback = 7).rangehigh else double.nan;
onh0.SetpaintingStrategy(paintingStrategy.DASHES);
onh1.SetpaintingStrategy(paintingStrategy.DASHES);
onh2.SetpaintingStrategy(paintingStrategy.DASHES);
onh3.SetpaintingStrategy(paintingStrategy.DASHES);
onh0.SetDefaultColor(Color.WHITE);
onh1.SetDefaultColor(Color.WHITE);
onh2.SetDefaultColor(Color.WHITE);
onh3.SetDefaultColor(Color.WHITE);
input lineweight = 4;
onh0.SetLineWeight(lineweight);
onh1.SetLineWeight(lineweight);
onh2.SetLineWeight(lineweight);
onh3.SetLineWeight(lineweight);
AddChartBubble(IsNaN(onh0[1]) and !IsNaN(onh0), onh0, onh0, CreateColor(204, 204, 255));
AddChartBubble(IsNaN(onh1[1]) and !IsNaN(onh1), onh1, onh1, CreateColor(204, 204, 255));
AddChartBubble(IsNaN(onh2[1]) and !IsNaN(onh2), onh2, onh2, CreateColor(204, 204, 255));
AddChartBubble(IsNaN(onh3[1]) and !IsNaN(onh3), onh3, onh3, CreateColor(204, 204, 255));
plot onl0 = if show_plots_for_daysback>=1 then onh(daysback = 1).rangelow else double.nan;
plot onl1 = if show_plots_for_daysback>=3 then onh(daysback = 3).rangelow else double.nan;
plot onl2 = if show_plots_for_daysback>=5 then onh(daysback = 5).rangelow else double.nan;
plot onl3 = if show_plots_for_daysback>=7 then onh(daysback = 7).rangelow else double.nan;
onl0.SetpaintingStrategy(paintingStrategy.DASHES);
onl1.SetpaintingStrategy(paintingStrategy.DASHES);
onl2.SetpaintingStrategy(paintingStrategy.DASHES);
onl3.SetpaintingStrategy(paintingStrategy.DASHES);
onl0.SetDefaultColor(Color.BLUE);
onl1.SetDefaultColor(Color.BLUE);
onl2.SetDefaultColor(Color.BLUE);
onl3.SetDefaultColor(Color.BLUE);
onl0.SetLineWeight(lineweight);
onl1.SetLineWeight(lineweight);
onl2.SetLineWeight(lineweight);
onl3.SetLineWeight(lineweight);
AddChartBubble(IsNaN(onl0[1]) and !IsNaN(onl0), onl0, onl0, Color.RED, no);
AddChartBubble(IsNaN(onl1[1]) and !IsNaN(onl1), onl1, onl1, Color.RED, no);
AddChartBubble(IsNaN(onl2[1]) and !IsNaN(onl2), onl2, onl2, Color.RED, no);
AddChartBubble(IsNaN(onl3[1]) and !IsNaN(onl3), onl3, onl3, Color.RED, no);