#day_stats_01
#https://usethinkscript.com/threads/calculate-weekdays-stats-based-on-current-day-opn-vs-closed-prices.20039/
#calculate weekdays stats based on current day opn vs closed prices
def o = open;
def h = high;
def l = low;
def c = close;
def na = double.nan;
def bn = barnumber();
def lastbar = !isnan(close[0]) and isnan(close[-1]);
def lastcls = if !isnan(close) and !lastbar then close else if lastbar then close else lastcls[1];
def dat = getYYYYMMDD();
def d = getday();
def day_start = if d != d[1] then 1 else 0;
def day_end = if d != d[-1] then 1 else 0;
def DOW = GetDayOfWeek(dat);
def week_cnt = if bn == 1 then 0
else if isnan(c) then week_cnt[1]
else if (day_end and DOW == 1) then week_cnt[1] + 1
else week_cnt[1];
addlabel(1, "weeks " + week_cnt, color.yellow);
def s = 0;
def mon_open = if bn == 1 then s else if isnan(c) then mon_open[1] else if dow == 1 and day_start then o else mon_open[1];
def mon_cls = if bn == 1 then s else if isnan(c) then mon_cls[1] else if dow == 1 and day_end then c else mon_cls[1];
def tue_open = if bn == 1 then s else if isnan(c) then tue_open[1] else if dow == 2 and day_start then o else tue_open[1];
def tue_cls = if bn == 1 then s else if isnan(c) then tue_cls[1] else if dow == 2 and day_end then c else tue_cls[1];
def wed_open = if bn == 1 then s else if isnan(c) then wed_open[1] else if dow == 3 and day_start then o else wed_open[1];
def wed_cls = if bn == 1 then s else if isnan(c) then wed_cls[1] else if dow == 3 and day_end then c else wed_cls[1];
def thr_open = if bn == 1 then s else if isnan(c) then thr_open[1] else if dow == 4 and day_start then o else thr_open[1];
def thr_cls = if bn == 1 then s else if isnan(c) then thr_cls[1] else if dow == 4 and day_end then c else thr_cls[1];
def fri_open = if bn == 1 then s else if isnan(c) then fri_open[1] else if dow == 5 and day_start then o else fri_open[1];
def fri_cls = if bn == 1 then s else if isnan(c) then fri_cls[1] else if dow == 5 and day_end then c else fri_cls[1];
def t = 0;
def mon_rng = if bn == 1 then t else if isnan(c) then mon_rng[1] else if dow == 1 and day_end then (mon_cls - mon_open) + mon_rng[1] else mon_rng[1];
def mon_avg = (mon_rng/week_cnt);
def tue_rng = if bn == 1 then t else if isnan(c) then tue_rng[1] else if dow == 2 and day_end then (tue_cls - tue_open) + tue_rng[1] else tue_rng[1];
def tue_avg = (tue_rng/week_cnt);
def wed_rng = if bn == 1 then t else if isnan(c) then wed_rng[1] else if dow == 3 and day_end then (wed_cls - wed_open) + wed_rng[1] else wed_rng[1];
def wed_avg = (wed_rng/week_cnt);
def thr_rng = if bn == 1 then t else if isnan(c) then thr_rng[1] else if dow == 4 and day_end then (thr_cls - thr_open) + thr_rng[1] else thr_rng[1];
def thr_avg = (thr_rng/week_cnt);
def fri_rng = if bn == 1 then t else if isnan(c) then fri_rng[1] else if dow == 5 and day_end then (fri_cls - fri_open) + fri_rng[1] else fri_rng[1];
def fri_avg = (fri_rng/week_cnt);
def avgmax = max(mon_avg, max(tue_avg, max(wed_avg, max(thr_avg, fri_avg))));
def avgmin = min(mon_avg, min(tue_avg, min(wed_avg, min(thr_avg, fri_avg))));
input show_bubble = yes;
addchartbubble(show_bubble and lastbar[3], lastcls,
"Fri " + fri_avg
, (if avgmax == fri_avg then color.green else if avgmin == fri_avg then color.red else color.gray), yes);
addchartbubble(show_bubble and lastbar[3], lastcls,
"Thr " + thr_avg
, (if avgmax == thr_avg then color.green else if avgmin == thr_avg then color.red else color.gray), yes);
addchartbubble(show_bubble and lastbar[3], lastcls,
"Wed " + wed_avg
, (if avgmax == wed_avg then color.green else if avgmin == wed_avg then color.red else color.gray), yes);
addchartbubble(show_bubble and lastbar[3], lastcls,
"Tue " + tue_avg
, (if avgmax == tue_avg then color.green else if avgmin == tue_avg then color.red else color.gray), yes);
addchartbubble(show_bubble and lastbar[3], lastcls,
"Mon " + mon_avg
, (if avgmax == mon_avg then color.green else if avgmin == mon_avg then color.red else color.gray), yes);
addchartbubble(show_bubble and lastbar[3], lastcls, " ", color.black, yes);
addchartbubble(show_bubble and lastbar[3], lastcls,
("day avg ranges\n" +
" over " + week_cnt + " weeks" )
, color.yellow, yes);
#--------------------------------------
# test stuff
input test_day = no;
addchartbubble(test_day and day_start, 0,
(if DOW == 1 then "Mon"
else if DOW == 2 then "Tue"
else if DOW == 3 then "Wed"
else if DOW == 4 then "Thur"
else if DOW == 5 then "Fri"
else "-")
, color.yellow, yes);
#addchartbubble(show_bubble and lastbar[3],
#lastcls,
#("day avg ranges\n" +
#" over " + week_cnt + " weeks\n" +
#"Mon " + mon_avg + "\n" +
#"Tue " + tue_avg + "\n" +
#"Wed " + wed_avg + "\n" +
#"Thr " + thr_avg + "\n" +
#"Fri " + fri_avg )
#, color.yellow, yes);
#plot z1 = mon_open;
#plot z2 = mon_cls;
#plot z3 = if mon_cls > 0 then mon_rng2 else na;
#plot z4 = if mon_cls > 0 then mon_rng else na;
#plot z5 = mon_avg;
#plot z0 = 0;
input test2 = no;
addchartbubble(test2 and (dow == 1 and day_end), 0,
# dow + " dy\n" +
# day_end + " E\n" +
mon_cls + " c\n" +
mon_open + " o\n" +
# mon_rng2 + " r2\n" +
mon_rng + " r\n" +
week_cnt + " cnt\n" +
mon_avg + " av"
, color.yellow, no);
#