declare upper;
script x {
input aggregationPeriod = AggregationPeriod.DAY;
def ymd = GetYYYYMMDD();
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) ;
def bar = BarNumber();
def h = high(period = aggregationPeriod);
def l = low(period = aggregationPeriod);
input days_displayed = 1;
input count = 0;
input extend = yes;
def hext = if thisday > days_displayed - 1 or isnan(close) or thisDay < count then hext[1] else h;
def lext = if thisday > days_displayed - 1 or isnan(close) or thisDay < count then lext[1] else l;
plot h_ = if thisday> days_displayed - 1 or thisDay > count then Double.NaN else if extend == yes then hext else h;
plot l_ = if thisday> days_displayed - 1 or thisDay > count then Double.NaN else if extend == yes then lext else l;
}
input days_displayed = 0;
input extend = yes;
input lineweight = 2;
plot h0 = x(days_displayed = days_displayed, count = 0, extend = extend).h_;
plot h1 = x(days_displayed = days_displayed, count = 1, extend = extend).h_;
plot h2 = x(days_displayed = days_displayed, count = 2, extend = extend).h_;
plot h3 = x(days_displayed = days_displayed, count = 3, extend = extend).h_;
plot h4 = x(days_displayed = days_displayed, count = 4, extend = extend).h_;
plot h5 = x(days_displayed = days_displayed, count = 5, extend = extend).h_;
plot h6 = x(days_displayed = days_displayed, count = 6, extend = extend).h_;
plot h7 = x(days_displayed = days_displayed, count = 7, extend = extend).h_;
plot h8 = x(days_displayed = days_displayed, count = 8, extend = extend).h_;
plot h9 = x(days_displayed = days_displayed, count = 9, extend = extend).h_;
plot l0 = x(days_displayed = days_displayed, count = 0, extend = extend).l_;
plot l1 = x(days_displayed = days_displayed, count = 1, extend = extend).l_;
plot l2 = x(days_displayed = days_displayed, count = 2, extend = extend).l_;
plot l3 = x(days_displayed = days_displayed, count = 3, extend = extend).l_;
plot l4 = x(days_displayed = days_displayed, count = 4, extend = extend).l_;
plot l5 = x(days_displayed = days_displayed, count = 5, extend = extend).l_;
plot l6 = x(days_displayed = days_displayed, count = 6, extend = extend).l_;
plot l7 = x(days_displayed = days_displayed, count = 7, extend = extend).l_;
plot l8 = x(days_displayed = days_displayed, count = 8, extend = extend).l_;
plot l9 = x(days_displayed = days_displayed, count = 9, extend = extend).l_;
h0.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h9.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l0.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l9.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h0.setdefaultColor(color.cyan);
h1.setdefaultColor(color.cyan);
h2.setdefaultColor(color.cyan);
h3.setdefaultColor(color.cyan);
h4.setdefaultColor(color.cyan);
h5.setdefaultColor(color.cyan);
h6.setdefaultColor(color.cyan);
h7.setdefaultColor(color.cyan);
h8.setdefaultColor(color.cyan);
h9.setdefaultColor(color.cyan);
l0.setdefaultColor(color.magenTA);
l1.setdefaultColor(color.magenTA);
l2.setdefaultColor(color.magenTA);
l3.setdefaultColor(color.magenTA);
l4.setdefaultColor(color.magenTA);
l5.setdefaultColor(color.magenTA);
l6.setdefaultColor(color.magenTA);
l7.setdefaultColor(color.magenTA);
l8.setdefaultColor(color.magenTA);
l9.setdefaultColor(color.magenTA);
h0.setlineWeight(lineweight);
h1.setlineWeight(lineweight);
h2.setlineWeight(lineweight);
h3.setlineWeight(lineweight);
h4.setlineWeight(lineweight);
h5.setlineWeight(lineweight);
h6.setlineWeight(lineweight);
h7.setlineWeight(lineweight);
h8.setlineWeight(lineweight);
h9.setlineWeight(lineweight);
l0.setlineWeight(lineweight);
l1.setlineWeight(lineweight);
l2.setlineWeight(lineweight);
l3.setlineWeight(lineweight);
l4.setlineWeight(lineweight);
l5.setlineWeight(lineweight);
l6.setlineWeight(lineweight);
l7.setlineWeight(lineweight);
l8.setlineWeight(lineweight);
l9.setlineWeight(lineweight);