# compare_pre_vol_01
# compare accumulated volume of the day to after hours volume.
# https://usethinkscript.com/threads/current-day-volume.10512/
def bn = barnumber();
def na = double.nan;
def v = volume;
def cls = close;
def diffday = if getday() != getday()[1] then 1 else 0;
input ah_volume_type = { "Pre Vol", "Post Vol", default "Pre + Post Vol" };
def start = 0930;
def end = 1600;
def daystart = if SecondsFromTime(start) == 0 then 1 else 0;
def daytime = if SecondsFromTime(start) >= 0 and SecondsTillTime(end) > 0 then 1 else 0;
def start2 = 0;
# pre time - midnight to 9:30am
def pretime = if SecondsFromTime(start2) >= 0 and SecondsTillTime(start) > 0 then 1 else 0;
def end2 = 2359;
# post time - 4pm to midnight
def poststart = if SecondsFromTime(end) == 0 then 1 else 0;
def posttime = if SecondsFromTime(end) >= 0 and SecondsTillTime(end2) > 0 then 1 else 0;
def midnite = if SecondsFromTime(end2) == 0 then 1 else 0;
# ------------------------------------
# calc mid level for chart
# this makes study complex
#def big = 999999;
#def highest = HighestAll(if daytime then high else 0);
#def lowest = LowestAll(if daytime then low else big);
#def rng = highest - lowest;
#def mid = lowest + (rng / 2);
# calc mid level for each day, for colored line
def agg = aggregationPeriod.day;
def highest = high(period = agg);
def lowest = low(period = agg);
def rng = highest - lowest;
def mid = lowest + (rng / 2);
#def open1 = open(period = agg);
#def close1 = close(period = agg);
#def cls_rng_per = 100*((cls-lowest)/rng);
# ----------------------------------------
#4pm to midnight
def volpost =
if bn == 1 then 0
else if poststart then v
else if posttime then volpost[1] + v
else volpost[1];
#midnight to 9.30
def volpre =
if bn == 1 then 0
else if diffday then v
else if pretime then volpre[1] + v
else volpre[1];
#9.30 to 4pm
def vol =
if bn == 1 then 0
else if daystart then v
else if daytime then vol[1] + v
else vol[1];
# ------------------------------------
# input prevolume = { default "Pre Vol", "Post Vol", "Pre + Post Vol" };
#def prevol2;
def vol_ah;
def vtype;
switch ( ah_volume_type) {
case "Pre Vol":
vol_ah = volpre;
vtype = 1;
case "Post Vol":
vol_ah = volpost;
vtype = 2;
case "Pre + Post Vol":
vol_ah = volpre + volpost;
vtype = 3;
}
# ---------------------------------
input show_AH_volume_type_label = yes;
addlabel(show_AH_volume_type_label, " " , color.black);
addlabel(show_AH_volume_type_label, "Pre Vol data: " +
( if vtype == 1 then "Pre, midnight to 9:30am"
else if vtype == 2 then "Post, 4pm to midnight"
else if vtype == 3 then "Pre + Post, all after hours"
else "-")
, color.yellow);
#daytime vol % of pre vol
def dayvolper = round(100 * (vol / vol_ah),1);
# percent levels, for colors
def per1 = 50;
def per2 = 100;
def per3 = 500;
def per4 = 1000;
DefineGlobalColor("color01", color.gray);
DefineGlobalColor("color02", color.yellow);
DefineGlobalColor("color03", color.green);
DefineGlobalColor("color04", color.cyan);
DefineGlobalColor("color05", color.magenta);
input show_percent_level_legend_labels = yes;
addlabel(show_percent_level_legend_labels, " " , color.black);
addlabel(show_percent_level_legend_labels, "Percent levels" , color.orange);
addlabel(show_percent_level_legend_labels, " 0 " , globalColor("color01"));
addlabel(show_percent_level_legend_labels, ">" + per1 + " %" , globalColor("color02"));
addlabel(show_percent_level_legend_labels, ">" + per2 + " %" , globalColor("color03"));
addlabel(show_percent_level_legend_labels, ">" + per3 + " %" , globalColor("color04"));
addlabel(show_percent_level_legend_labels, ">" + per4 + " %" , globalColor("color05"));
input show_current_vol_ratio_label = yes;
addlabel(show_current_vol_ratio_label, " " , color.black);
AddLabel(show_current_vol_ratio_label, dayvolper + " %"
, ( if dayvolper < per1 then globalColor("color01")
else if dayvolper >= per1 and dayvolper < per2 then globalColor("color02")
else if dayvolper >= per2 and dayvolper < per3 then globalColor("color03")
else if dayvolper >= per3 and dayvolper < per4 then globalColor("color04")
else globalColor("color05")));
# color line , diff colors for each % rng
input volume_ratio_colored_line = yes;
plot cc = if volume_ratio_colored_line and daytime then mid else na;
cc.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
cc.AssignValueColor(
( if dayvolper < per1 then globalColor("color01")
else if dayvolper >= per1 and dayvolper < per2 then globalColor("color02")
else if dayvolper >= per2 and dayvolper < per3 then globalColor("color03")
else if dayvolper >= per3 and dayvolper < per4 then globalColor("color04")
else globalColor("color05")));
cc.setlineweight(2);
cc.hidebubble();
# -----------------------------------------------------
# -----------------------------------------------------
# test data
input test_data = 0;
# -----------------------------
# identify the 3 time periods pre, daytime, post
# if pre+post - then change, make them same color . in 2nd row. 1st row, dont draw
def y4 = 0.003;
input test1_bubble_rows_of_vol_per_period = no;
# post vol
addchartbubble( test1_bubble_rows_of_vol_per_period and vtype == 2, (mid * ( 1 - y4)),
volpost + " post" , (if posttime then Color.dark_ORANGE else color.gray), no);
# pre vol
addchartbubble( test1_bubble_rows_of_vol_per_period and vtype == 1, (mid * ( 1 - y4)),
volpre + " pre", (if pretime then Color.plum else Color.gray), no);
# pre+post vol
addchartbubble( test1_bubble_rows_of_vol_per_period and vtype == 3, (mid * ( 1 - y4)),
(volpre + volpost) + " pre", (if posttime then Color.dark_ORANGE else if pretime then Color.plum else Color.gray), no);
addchartbubble( test1_bubble_rows_of_vol_per_period and daytime, (mid * ( 1 - y4)),
vol + " v", (if daytime then Color.dark_green else Color.gray), no);
# -----------------------
# def dayvolper = round(100 * (vol - vol_ah)/vol_ah,1);
input test2_vol_and_ratio_bubbles = no;
addchartbubble(test2_vol_and_ratio_bubbles and daytime, (mid * ( 1 + y4)),
vol_ah + "\n" +
vol + "\n" +
dayvolper
,( if dayvolper < per1 then globalColor("color01")
else if dayvolper >= per1 and dayvolper < per2 then globalColor("color02")
else if dayvolper >= per2 and dayvolper < per3 then globalColor("color03")
else if dayvolper >= per3 and dayvolper < per4 then globalColor("color04")
else globalColor("color05")), yes);
#input test5_highest_lowest_labels = no;
#addlabel(test5_highest_lowest_labels, " ", color.black);
#addlabel(test5_highest_lowest_labels, "Hi " + highest, color.yellow);
#addlabel(test5_highest_lowest_labels, "Lo " + lowest, color.yellow);
#addlabel(test5_highest_lowest_labels, "rng " + rng, color.yellow);
#addlabel(test5_highest_lowest_labels, "mid " + mid, color.yellow);
# color line , diff colors for each time , pre, day, post
#input test3_period_color_line = no;
#plot uu = if test3_period_color_line then mid+3 else na;
#uu.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
#uu.AssignValueColor( if daytime then Color.GREEN else if pretime then Color.MAGENTA else Color.ORANGE);
#uu.setlineweight(2);
#