# avg_hodlod_xdays_01
# https://usethinkscript.com/threads/average-time-of-hod-or-lod.12499/
# Average Time of HoD or LoD
# look at the last x days,
# find the average time of the HoD (if the daily candle closed red)
# find the average time of the LoD (if the daily candle closed green)
# -----------------------------------
# day count
def bn = barnumber();
def istoday = if GetLastDay() == GetDay() then 1 else 0;
def newday = if getday() != getday()[1] then 1 else 0;
def newdaybn = if newday then bn else newdaybn[1];
def daycnt = if bn == 1 then 1 else if (!isnan(close) and newday) then daycnt[1] + 1 else daycnt[1];
def maxdays = highestall(daycnt);
def revdaycnt = maxdays - daycnt + 1;
input days_back = 14;
def validdays = revdaycnt <= days_back;
def daystoomany = if days_back < 1 or days_back > maxdays then 1 else 0;
addlabel(daystoomany, " ONLY " + MAXDAYS + " DAYS ON CHART. enter a different number for days back", color.cyan);
#-----------------------------------
input aggD = AggregationPeriod.day;
def day_opn = open(period = aggd);
def day_hi = high(period = aggd);
def day_lo = low(period = aggd);
def day_cls = close(period = aggd);
def isdaygrn = (day_cls > day_opn);
def isdayred = (day_cls < day_opn);
input test1_grnred = yes;
addchartbubble(validdays and newday and test1_grnred, high*1.01,
revdaycnt + "\n" +
isdaygrn + " G\n" +
isdayred + " R"
, (if isdaygrn then color.green else if isdayred then color.red else color.gray), yes);
input test2_chartmin = no;
def chartagg = GetAggregationPeriod();
def chartmin = (chartagg / 1000) / 60;
AddLabel(test2_chartmin, "chartmin " + chartmin, Color.MAGENTA);
def daybarqty = roundup(390 / chartmin, 0);
AddLabel(test2_chartmin, "bars per day " + daybarqty, Color.MAGENTA);
#------------------------------------------
# on 1st bar of day, look for the hi & lo of each day
def hi;
def hibn;
def lo;
def lobn;
def hodoffsum;
def lodoffsum;
def hodcnt;
def lodcnt;
def grnday;
def redday;
if !validdays then {
hi = 0;
hibn = 0;
lo = 0;
lobn = 0;
hodoffsum = 0;
lodoffsum = 0;
hodcnt = 0;
lodcnt = 0;
grnday = 0;
redday = 0;
} else if (bn == 1 or newday) and validdays then {
# find high of day
hi = fold i1 = 0 to 400
with p1
while (!isnan(getvalue(close, -i1)) and daycnt == getvalue(daycnt, -i1))
do (if getvalue(high, -i1) > p1 then getvalue(high, -i1) else p1);
# find bn of high of day
hibn = fold i2 = 0 to 400
with p2
while (!isnan(getvalue(close, -i2)) and daycnt == getvalue(daycnt, -i2))
do (if getvalue(high, -i2) == hi then getvalue(bn, -i2) else p2);
# find low of day
lo = fold j1 = 0 to 400
with q1 = 99999
while (!isnan(getvalue(close, -j1)) and daycnt == getvalue(daycnt, -j1))
do (if getvalue(low, -j1) < q1 then getvalue(low, -j1) else q1);
# find bn of low of day
lobn = fold j2 = 0 to 400
with q2
while (!isnan(getvalue(close, -j2)) and daycnt == getvalue(daycnt, -j2))
do (if getvalue(low, -j2) == lo then getvalue(bn, -j2) else q2);
# find the average time of the HoD (if the daily candle closed red)
# HOD, average bar number after open, on red days
# find the average time of the LoD (if the daily candle closed green)
# LOD, average bar number after open, on green days
hodoffsum = if bn == 1 and isdayred then (hibn - newdaybn) else if isdayred then hodoffsum[1] + (hibn - newdaybn) else hodoffsum[1];
lodoffsum = if bn == 1 and isdaygrn then (lobn - newdaybn) else if isdaygrn then lodoffsum[1] + (lobn - newdaybn) else lodoffsum[1];
hodcnt = if bn == 1 and isdayred then 1 else if isdayred then hodcnt[1] + 1 else hodcnt[1];
lodcnt = if bn == 1 and isdaygrn then 1 else if isdaygrn then lodcnt[1] + 1 else lodcnt[1];
grnday = if isdaygrn then grnday[1] + 1 else grnday[1];
redday = if isdayred then redday[1] + 1 else redday[1];
} else {
hi = hi[1];
hibn = hibn[1];
lo = lo[1];
lobn = lobn[1];
hodoffsum = hodoffsum[1];
lodoffsum = lodoffsum[1];
hodcnt = hodcnt[1];
lodcnt = lodcnt[1];
grnday = grnday[1];
redday = redday[1];
}
def hodoffavg = if hodcnt == 0 then 0 else round(hodoffsum/hodcnt,1);
def lodoffavg = if lodcnt == 0 then 0 else round(lodoffsum/lodcnt,1);
addlabel(1, " ", color.black);
# find the average time of the HoD (if the daily candle closed red)
# HOD, average bar number after open, on red days
addlabel(1, hodoffavg + " bars, HOD average bar number after open. only during " + redday + " of " + days_back + " red days", color.red);
addlabel(1, " ", color.black);
# find the average time of the LoD (if the daily candle closed green)
# LOD, average bar number after open, on green days
addlabel(1, lodoffavg + " bars, LOD average bar number after open. only during " + grnday + " of " + days_back + " green days", color.green);
addlabel(1, " ", color.black);
#--------------------------
# time
def hihrs2 = 9.5 + ( hodoffavg * (chartmin/60));
def hihrs = floor(hihrs2);
def himins = (hihrs2-hihrs)*60;
addlabel(1, hihrs + ":" + himins + " 24hr time, HOD average time after open. only during " + redday + " of " + days_back + " red days", color.red);
addlabel(1, " ", color.black);
def lohrs2 = 9.5 + ( lodoffavg * (chartmin/60));
def lohrs = floor(lohrs2);
def lomins = (lohrs2-lohrs)*60;
addlabel(1, lohrs + ":" + lomins + " 24hr time, LOD average time after open. only during " + grnday + " of " + days_back + " green days", color.green);
#--------------------------------------------
input test3_bubbles = yes;
addchartbubble(test3_bubbles and validdays, low*0.99,
bn + "\n" +
high + " H\n" +
hi + " hi\n" +
hibn + " hibn\n" +
hodoffsum + " sum\n" +
hodcnt + " cnt\n" +
hodoffavg + " Hfavg\n" +
lo + " lo\n" +
lobn + " lobn\n" +
lodoffsum + " sum\n" +
lodcnt + " cnt\n" +
lodoffavg + " Lfavg\n" +
newday + " new\n" +
daycnt + " cnt\n" +
revdaycnt + " rev\n"
#maxdays + " max\n"
, (if hi == high then color.green else if lo == low then color.red else color.gray), no);
input test4_bubbles = no;
addchartbubble(test4_bubbles, low,
newday + " new\n" +
daycnt + " cnt\n" +
revdaycnt + " rev\n" +
maxdays + " max\n"
, color.yellow, no);
#
#