# agg_hilo_lines_01
# find the hi and lo of some x qty of agg periods, before the last bar
# find 1st agg period , len qty of periods before last bar
# find 1st bar in each agg period
# find 1st bar , in 1st agg period
def na = double.nan;
def bn = barnumber();
def lastbn = HighestAll(If(IsNaN(close), 0, bn));
def lastbar = if (bn == lastbn) then 1 else 0;
input agg = AggregationPeriod.week;
input len = 52;
#diffday
# on a daily chart, this is always true
def diffday = if getday() != getday()[1] then 1 else 0;
# first bar in len series ?
def first_agg = (!isnan(close(period = agg)[-len]) and isnan(close(period = agg)[-(len+1)]));
# find first day of week,
def dow = GetDayofWeek(GetYYYYMMDD());
def firstdayofweek =
if dow[1] == 5 and dow == 1 then 1
else if dow[1] == 4 and dow == 1 then 1
else if dow[1] == 5 and dow == 2 then 1
else 0;
#def series_first_bar = if firstdayofweek and first_agg then 1 else na;
def series_first_bar = if firstdayofweek and first_agg then 1 else 0;
plot zz = series_first_bar;
zz.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
zz.SetDefaultColor(Color.white);
zz.setlineweight(1);
zz.hidebubble();
def t = if series_first_bar then 1 else 0;
addverticalline(t, "-", color.yellow);
def hi1;
def lo1;
if bn == 1 or isnan(close) then {
hi1 = na;
lo1 = na;
} else if t then {
hi1 = fold i = 0 to lastbn - bn + 1
with p = 0
do ( if getvalue(high, -i) > p then getvalue(high, -i) else p);
lo1 = fold j = 0 to lastbn - bn + 1
with q = 99999
do ( if getvalue(low, -j) < q then getvalue(low, -j) else q);
} else {
hi1 = hi1[1];
lo1 = lo1[1];
}
plot zhi = if !isnan(hi1) and hi1 > 0 then hi1 else na;
zhi.setdefaultcolor(color.cyan);
plot zlo = if !isnan(lo1) and lo1 > 0 then lo1 else na;
zlo.setdefaultcolor(color.cyan);
addlabel(1, "Highest " + round(hi1,2), color.green);
addlabel(1, "Lowest " + round(lo1,2), color.red);
# ------------------------------------
addchartbubble(0, 35,
diffday + " diffd\n" +
dow + " dow\n" +
firstdayofweek + " 1day/wk\n" +
first_agg + "\n" +
series_first_bar
, (#if series_first_bar then color.green else
if firstdayofweek then color.yellow
else if diffday then color.magenta
else color.gray), no);
addchartbubble(0 and bn < 20, 5,
hi1 + "\n" +
zhi + "\n" +
lo1 + "\n" +
zlo
, color.yellow, yes);
addchartbubble(0, low,
#a + "\n" +
first_agg + "\n" +
t
,(if first_agg then color.yellow else color.gray), no);
#