declare on_volume;
declare zerobase;
input includeCurrentDayInAvg = no;
input barsToBeIncluded = {default OnlyCurrentBar, CurrentAndBefore, CurrentAndAfter};
input includePremarket = no;
input includePostmarket = no;
input barwidth = 3;
input showVolume = yes;
input highlightDataPoints = yes;
#Preliminary Declarations -------------------------------
def PremarketTradingHours = RegularTradingStart(GetYYYYMMDD()) > GetTime();
def RegularTradingHours = RegularTradingStart(GetYYYYMMDD()) <= GetTime() and GetTime() < RegularTradingEnd(GetYYYYMMDD());
def PostmarketTradingHours = GetTime() >= RegularTradingEnd(GetYYYYMMDD());
def PremarketSessionStart = PostmarketTradingHours[1] and PremarketTradingHours;
def RegularSessionStart = PremarketTradingHours[1] and RegularTradingHours;
def PostmarketSessionStart = RegularTradingHours[1] and PostmarketTradingHours;
def CurrentDay = GetDay() == GetLastDay(); #this removes current bar from average.
#------------------------------------------------------------------------
# Code that Calculates Correct Population------------
def DayRoll = if GetDay() != GetDay()[1] and !IsNaN(close)
then GetTime()
else DayRoll[1];
def Time = if !IsNaN(close)
then GetTime()
else Double.NaN;
def aroll = HighestAll(DayRoll);
def aTime = HighestAll(Time);
def aBarTime = aTime - aroll;
def Bartime = Time - DayRoll;
def Population;
switch (barsToBeIncluded){
case OnlyCurrentBar:
Population = if !includeCurrentDayInAvg
then !CurrentDay and Bartime == aBarTime
else Bartime == aBarTime;
case CurrentAndBefore:
Population = if !includeCurrentDayInAvg
then !CurrentDay and if includePremarket == no
then Bartime <= aBarTime and !PremarketTradingHours
else Bartime <= aBarTime
else if includePremarket == no
then Bartime <= aBarTime and !PremarketTradingHours
else Bartime <= aBarTime;
case CurrentAndAfter:
Population = if !includeCurrentDayInAvg
then !CurrentDay and if includePostmarket == no
then Bartime >= aBarTime and !PostmarketTradingHours
else Bartime >= aBarTime
else if includePostmarket == no
then Bartime >= aBarTime and !PostmarketTradingHours
else Bartime >= aBarTime;
}
#---------------------------------------------------------------------------
def NumOfInstances = if Population and CurrentDay == no
then NumOfInstances[1] + 1
else NumOfInstances[1];
def CumVol = if NumOfInstances != NumOfInstances[1]
then CumVol[1] + volume
else CumVol[1];
AddVerticalLine(highlightDataPoints and NumOfInstances != NumOfInstances[1], NumOfInstances != NumOfInstances[1]);
AddVerticalLine(highlightDataPoints and Population and CurrentDay == yes, Population and CurrentDay == yes, Color.YELLOW);
def AvgVol = Round(CumVol / NumOfInstances, 0);
def Percent = (volume / AvgVol) * 100;
AddLabel(1, "Current Candle: " + volume, Color.WHITE);
AddLabel(1, Round(Percent, 2) + " %", if
Between(Percent, 100, 110) then CreateColor(136, 252, 172)
else if Between(Percent, 110, 120) then CreateColor(106, 252, 104)
else if Percent >= 120 then Color.GREEN
else if Between(Percent, 80, 90) then Color.PINK
else if Between(Percent, 70, 80) then CreateColor(206, 74, 116)
else if Percent <= 80 then Color.DOWNTICK
else Color.WHITE);
AddLabel(1, "Avg Vol: " + AvgVol, Color.ORANGE);
AddLabel(1, "Prior Candles: " + CumVol, Color.ORANGE);
AddLabel(1, "Avg Over: " + NumOfInstances, Color.ORANGE);
# Plotting of the bar graph--------------------------------------------------------
plot CurrentCandleVolume = if showVolume then volume else Double.NaN;
CurrentCandleVolume.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
CurrentCandleVolume.SetLineWeight(barwidth);
AddVerticalLine(Population,"Same Bar",Color.green);