# label_bull_bear_time_ratio
# https://usethinkscript.com/threads/bullish-and-bearish-statistics-based-on-time.22186/
#Bullish and Bearish statistics based on time
declare lower;
def na = Double.NaN;
def bn = BarNumber();
#---------------------------------
# get chart agg time
def chartagg = getaggregationperiod();
def chartmin = chartagg/(1000*60);
#---------------------------------
def grn = close > close[1];
def red = close < close[1];
def start = 0930;
def end = 1600;
def daytime = if secondsfromTime(start) >= 0 and secondstillTime(end) > 0 then 1 else 0;
def startbar = (secondsfromTime(start) == 0);
addverticalline(startbar, "-", color.cyan);
def daynum = getday();
def newday = daynum != daynum[1];
def grncnt;
def redcnt;
if startbar then {
grncnt = 0;
redcnt = 0;
} else if daytime and newday then {
# day first bar
grncnt = 0;
redcnt = 0;
} else if daytime and grn then {
grncnt = grncnt[1] + 1;
redcnt = redcnt[1];
} else if daytime and red then {
grncnt = grncnt[1];
redcnt = redcnt[1] + 1;
} else {
grncnt = grncnt[1];
redcnt = redcnt[1];
}
input ratio_factor = 1;
# ratio of green red. adjusted to be cenetered on 0
def xcnt = if (grncnt == 0 or redcnt == 0) then 0
else ((grncnt / redcnt) - 1) * ratio_factor;
input show_grn_red_lines = yes;
input show_ratio_line = yes;
def l1 = if !show_grn_red_lines or newday then na else grncnt;
def l2 = if !show_grn_red_lines or newday then na else redcnt;
def l3 = if !show_ratio_line or newday then na else xcnt;
plot z1 = l1;
plot z2 = l2;
plot z3 = l3;
z1.SetDefaultColor(Color.green);
z1.setlineweight(1);
z1.hidebubble();
z2.SetDefaultColor(Color.red);
z2.setlineweight(1);
z2.hidebubble();
z3.SetDefaultColor(Color.cyan);
z3.setlineweight(1);
z3.hidebubble();
addlabel(1, "daily count", color.white);
addlabel(1, " ", color.black);
addlabel(1, grncnt, z1.takevaluecolor());
addlabel(1, (grncnt * chartmin) + "min", z1.takevaluecolor());
addlabel(1, " ", color.black);
addlabel(1, redcnt, z2.takevaluecolor());
addlabel(1, (redcnt * chartmin) + "min", z2.takevaluecolor());
addlabel(1, " ", color.black);
addlabel(1, "ratio " + xcnt, z3.takevaluecolor());
addlabel(1, " ", color.black);
plot z0 = 0;
z0.SetDefaultColor(Color.GRAY);
input show_bar_dots = yes;
plot zdots = if !show_bar_dots then na else 0;
zdots.AssignValueColor( if grn then color.green else if red then color.red else color.black);
zdots.SetPaintingStrategy(PaintingStrategy.POINTS);
zdots.setlineweight(2);
zdots.hidebubble();
#---------------------------
addchartbubble(0, 4,
"G " + grncnt + "\n" +
"R " + redcnt + "\n" +
xcnt
, color.cyan, 1);
#