#VPOC_VAH_VAL_AVG_xDays_v1
#Added timeperperofile (D,W,M), Avg Plots, Bubbles
input timeperprofile = {default DAY, WEEK, MONTH};
input showlabels = yes;
input showtestplots = no;
input showAVGplots = yes;
input showbubbles = yes;
def na = Double.NaN;
def mode;
switch (timeperprofile) {
case DAY:
mode = GetYYYYMMDD();
case WEEK:
mode = GetWeek();
case MONTH:
mode = GetMonth();
}
def volpoc = reference VolumeProfile("time per profile" = timeperprofile, "on expansion" = no, "price per row height mode" = "TICKSIZE").POC;
def volvah = reference VolumeProfile("time per profile" = timeperprofile, "on expansion" = no, "price per row height mode" = "TICKSIZE").VAHigh;
def volval = reference VolumeProfile("time per profile" = timeperprofile, "on expansion" = no, "price per row height mode" = "TICKSIZE").VALow;
#Define Mose Periods
def count = if !IsNaN(volpoc) and mode != mode[1] then count[1] + 1 else count[1];
def cond = HighestAll(count) - count ;
#POC
def p1 = if cond == 1 then volpoc else p1[1];
plot poc1 = p1;
def p2 = if cond == 2 then volpoc else p2[1];
plot poc2 = p2;
def p3 = if cond == 3 then volpoc else p3[1];
plot poc3 = p3;
def p4 = if cond == 4 then volpoc else p4[1];
plot poc4 = p4;
#VAH
def h1 = if cond == 1 then volvah else h1[1];
plot hva1 = h1;
def h2 = if cond == 2 then volvah else h2[1];
plot hva2 = h2;
def h3 = if cond == 3 then volvah else h3[1];
plot hva3 = h3;
def h4 = if cond == 4 then volvah else h4[1];
plot hva4 = h4;
#VAL
def l1 = if cond == 1 then volval else l1[1];
plot lva1 = l1;
def l2 = if cond == 2 then volval else l2[1];
plot lva2 = l2;
def l3 = if cond == 3 then volval else l3[1];
plot lva3 = l3;
def l4 = if cond == 4 then volval else l4[1];
plot lva4 = l4;
#Plot Average POC, VAH and VAL
defineGlobalColor("H", color.green);
defineGlobalColor("P", color.cyan);
defineGlobalColor("L", color.red);
plot AvgVAH = if showAVGplots and cond < 1 then highestall((h1 + h2 + h3 + h4) / 4) else na;
plot AvgPOC = if showAVGplots and cond < 1 then highestall((p1 + p2 + p3 + p4) / 4) else na;
plot AvgVAL = if showAVGplots and cond < 1 then highestall((l1 + l2 + l3 + l4) / 4) else na;
AvgVAH.setdefaultColor(globalColor("H"));
AvgPOC.setdefaultColor(globalColor("P"));
AvgVAL.setdefaultColor(globalColor("L"));
#Labels
AddLabel(showlabels, "Prior 4 " + timeperprofile + "s", Color.WHITE);
AddLabel(showlabels, "AVG VAH: " + AsDollars((h1 + h2 + h3 + h4) / 4), Color.GREEN);
AddLabel(showlabels, "AVG POC: " + AsDollars((p1 + p2 + p3 + p4) / 4), Color.CYAN);
AddLabel(showlabels, "AVG VAL: " + AsDollars((l1 + l2 + l3 + l4) / 4), Color.RED);
#Bubbles
input bubblemover = 2;
def b = bubblemover;
def mover = showbubbles and isnan(close[b]) and !isnan(close[b + 1]);
addchartBubble(mover, AvgVAH, "H: " + asdollars(AVGVAH), AVGVAH.takevaluecolor());
addchartBubble(mover, AvgPOC, "P: " + asdollars(AVGPOC), AVGPOC.takevaluecolor());
addchartBubble(mover, AvgVAL, "L: " + asdollars(AVGVAL), AVGVAL.takevaluecolor());
#Hide Plots
poc1.SetHiding(!showtestplots);
poc2.SetHiding(!showtestplots);
poc3.SetHiding(!showtestplots);
poc4.SetHiding(!showtestplots);
hva1.SetHiding(!showtestplots);
hva2.SetHiding(!showtestplots);
hva3.SetHiding(!showtestplots);
hva4.SetHiding(!showtestplots);
lva1.SetHiding(!showtestplots);
lva2.SetHiding(!showtestplots);
lva3.SetHiding(!showtestplots);
lva4.SetHiding(!showtestplots);
#