#Modification request
#Averages total volume for first 6 bars of day of past X number of prior days at the opening bar of every day.
#Svanoy
input Number_Of_Signals_To_Be_Averaged = 5;
input Number_Of_Bars_To_Total_Per_Day = 6;
input Value_To_Be_Averaged = volume;
input Time_Of_First_Bar_Of_Day = 0930;
def NotToday = GetDay() <> GetLastDay();
def BarCounter = if secondsFromTime(Time_Of_First_Bar_Of_Day)==0 then 1
else if BarCounter[1] == Number_Of_Bars_To_Total_Per_Day or BarCounter[1] == 0 then 0
else BarCounter[1] + 1;
def RangeTotalVolume= if BarCounter == 1 then Value_To_Be_Averaged
else if (BarCounter and Barcounter <= Number_Of_Bars_To_Total_Per_Day ) then RangeTotalVolume[1] + Value_To_Be_Averaged
else double.nan;
def SignalBar = BarCounter == Number_Of_Bars_To_Total_Per_Day;
def BarToBeAveraged = BarCounter == Number_Of_Bars_To_Total_Per_Day and NotToday;
def TotalSignalCount = if BarToBeAveraged then TotalSignalCount[1] + 1 else TotalSignalCount[1];
def bar = BarNumber();
def BarNum = if !IsNaN(close) and BarNumber()>0 then bar else BarNum[1];
def VBar = HighestAll(BarNum);
def SignalRangeTotal;
if BarNumber() == 0 {
SignalRangeTotal = 0;
} else if BarToBeAveraged {
SignalRangeTotal = SignalRangeTotal[1] + RangeTotalVolume;
} else {
SignalRangeTotal = SignalRangeTotal[1];
}
def TotalToBeRemoved = fold i = 0 to VBar while ((TotalSignalCount - GetValue(TotalSignalCount,i)) <= Number_Of_Signals_To_Be_Averaged) and TotalSignalCount > Number_Of_Signals_To_Be_Averaged do if BarToBeAveraged then TotalToBeRemoved[1] + GetValue(RangeTotalVolume,i) else TotalToBeRemoved[1];
def RangeAverage = if SignalBar then ((SignalRangeTotal[1]-TotalToBeRemoved[1]) / Number_Of_Signals_To_Be_Averaged) else Double.NaN;
AddChartBubble(SignalBar and TotalSignalCount >= Number_Of_Signals_To_Be_Averaged and NotToday,high,"Total Signals: "+TotalSignalCount,color.white);
AddChartBubble(SignalBar and TotalSignalCount >= Number_Of_Signals_To_Be_Averaged and NotToday,high,"Cumulative Avg: "+SignalRangeTotal,color.light_green);
AddChartBubble(SignalBar and TotalSignalCount >= Number_Of_Signals_To_Be_Averaged ,high,"Average of" + "\n" + "Total Volume for First 6 Bars" + "\n" + "of Last "+Number_Of_Signals_To_Be_Averaged+" Days. Starting Yesterday: "+rangeaverage,color.light_red);