Average volume for First bar of the Day

apaul015

New member
I'm trying to create a code where you can input a "X" number of days, and I can get the average Volume of the First bar for those "X" days.
For example, I want to get the average Volume of the first Bar for the last 5 days, beginning with yesterday.

I feel like this should be pretty simple, but I can't seem to get the code right. Any help would be appreciated!
 
Solution
@apaul015
That took a minute.
Had to figure out how to define a moving range.
Averages first bar of day volume for past X number of prior days at the opening bar of every day.
The first X number of days on a chart will be blank as there isn't enough data until the day after X number of days.
ilKCM7K.png

Ruby:
#Averages first bar of day volume for past X number of prior days at the opening bar of every day.
#Svanoy

input Number_Of_Signals_To_Be_Averaged = 5;
input Value_To_Be_Averaged = volume;
input Time_Of_First_Bar_Of_Day = 0930;

def NotToday = GetDay() <> GetLastDay();
def BarToBeAveraged = (secondsFromTime(Time_Of_First_Bar_Of_Day)==0) and NotToday;
def SignalBar = (secondsFromTime(Time_Of_First_Bar_Of_Day)==0);
def...

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

@apaul015
Post #7 here: https://usethinkscript.com/threads/math-on-indicator-bars-only.9943/ should do what you need. Just replace <Signal> with criteria for which bar you want to average.

Okay, so I've been messing around with your code and I can't seem to get it to work right. It keeps returning N/A for the volume. My <Signal> is the Five Minute Opening Candle, which I'm setting as:

def five = secondsFromTime(0930)>=0 and secondstilltime(0935) >0;

Is my signal wrong?

To give a bit more info, I'm trying to backtest an OPR Breakout strategy based on the Average Volume of the Opening 5 Minute Candles, and I want to play with the number of days back I'm averaging.

I came up with a different code after messing around, but this one doesn't seem to trigger right for past instances where my criteria is met. Here it is below to help you understand what I'm trying to do a bit more. I think this code is only recognizing todayvolume as literally today only, and not todayvolume of say, last Thursday if my setup triggered on that day.

def bn = barnumber();

input five_qty = 6;


def five = secondsFromTime(0930)>=0 and secondstilltime(0935) >0;

def fivecnt =
if bn == 1 then 0
else if five then fivecnt[1] + 1
else fivecnt[1];

def fivecntlast = highestall(fivecnt);
def fiverevcnt = fivecntlast - fivecnt+1;

# add up desired values
def fivesum =
if bn == 1 then 0
else if (fiverevcnt <= five_qty and five) then fivesum[1] + volume
else fivesum[1];

def todayvolume = if five then volume else todayvolume[1];

def fiveavg = round((fivesum-todayvolume) / (five_qty-1), 2);

input rvol = 1;

def inplay = if todayvolume > fiveavg*rvol then yes else no;
 
Okay, so I've been messing around with your code and I can't seem to get it to work right. It keeps returning N/A for the volume. My <Signal> is the Five Minute Opening Candle, which I'm setting as:

def five = secondsFromTime(0930)>=0 and secondstilltime(0935) >0;

Is my signal wrong?

To give a bit more info, I'm trying to backtest an OPR Breakout strategy based on the Average Volume of the Opening 5 Minute Candles, and I want to play with the number of days back I'm averaging.

I came up with a different code after messing around, but this one doesn't seem to trigger right for past instances where my criteria is met. Here it is below to help you understand what I'm trying to do a bit more. I think this code is only recognizing todayvolume as literally today only, and not todayvolume of say, last Thursday if my setup triggered on that day.

def bn = barnumber();

input five_qty = 6;


def five = secondsFromTime(0930)>=0 and secondstilltime(0935) >0;

def fivecnt =
if bn == 1 then 0
else if five then fivecnt[1] + 1
else fivecnt[1];

def fivecntlast = highestall(fivecnt);
def fiverevcnt = fivecntlast - fivecnt+1;

# add up desired values
def fivesum =
if bn == 1 then 0
else if (fiverevcnt <= five_qty and five) then fivesum[1] + volume
else fivesum[1];

def todayvolume = if five then volume else todayvolume[1];

def fiveavg = round((fivesum-todayvolume) / (five_qty-1), 2);

input rvol = 1;

def inplay = if todayvolume > fiveavg*rvol then yes else no;

in the future, just post the code you are trying, so others can see it and help guide you.
if you abandon one way and hop to another method, it is confusing.

i made a new study with just your 1st bar trigger formula. and a bubble. it worked.

Code:
def five = secondsFromTime(0930)>=0 and secondstilltime(0935) >0;
input test1_firstbar = no;
addchartbubble(test1_firstbar , low, five, ( if five then color.yellow else color.gray), no);


then i copied svanoy's code, addid it to my test code, and changed a couple variable names. it works.
i added a variable signal and set it = to five.
i added a variable to define the data, data = volume.
it will calculate an average for the whole chart, not just x days.

is this close to what you had?

Ruby:
#
def five = secondsFromTime(0930)>=0 and secondstilltime(0935) >0;

def signal = five;
def data = volume;

# -----------------------

def Runningdata;
def SignalCount;

If Signal {
Runningdata = Runningdata[1] + data;
SignalCount = SignalCount[1] + 1;
} else {
Runningdata = Runningdata[1];
SignalCount = SignalCount[1];}

plot Average1 = Runningdata / SignalCount;
Average1.SetPaintingStrategy(PaintingStrategy.Horizontal);

# ---------------------------

input test1_firstbar = no;
addchartbubble(test1_firstbar , low, five, ( if five then color.yellow else color.gray), no);

addlabel(1, "avg vol " + Average1, color.yellow);
#
 
Last edited:
in the future, just post the code you are trying, so others can see it and help guide you.
if you abandon one way and hop to another method, you (and others) aren't leaning.

i made a new study with just your 1st bar trigger formula. and a bubble. it worked.

Code:
def five = secondsFromTime(0930)>=0 and secondstilltime(0935) >0;
input test1_firstbar = no;
addchartbubble(test1_firstbar , low, five, ( if five then color.yellow else color.gray), no);


then i copied svanoy's code, addid it to my test code, and changed a couple variable names. it works.
i added a variable signal and set it = to five.
i added a variable to define the data, data = volume.
it will calculate an average for the whole chart, not just x days.

is this close to what you had?

Ruby:
#
def five = secondsFromTime(0930)>=0 and secondstilltime(0935) >0;

def signal = five;
def data = volume;

# -----------------------

def Runningdata;
def SignalCount;

If Signal {
Runningdata = Runningdata[1] + data;
SignalCount = SignalCount[1] + 1;
} else {
Runningdata = Runningdata[1];
SignalCount = SignalCount[1];}

plot Average1 = Runningdata / SignalCount;
Average1.SetPaintingStrategy(PaintingStrategy.Horizontal);

# ---------------------------

input test1_firstbar = no;
addchartbubble(test1_firstbar , low, five, ( if five then color.yellow else color.gray), no);

addlabel(1, "avg vol " + Average1, color.yellow);
#

@halcyonguy

Yeah makes sense. Sorry I'm all over the place. Just a little frustrating trying to get my ideas to work the way I intended. Thanks for the help!

So the code you posted is close to what I had, but then I still wanted the ability to test the average of an "x" number of first candle of the day bars before. I actually went back to @Svanoy's original code he referred me to and I realized I was dumb, and the "Lookback" line was for the number of candles based on the aggregation period. It was returning N/A because I didn't input enough "Lookback" bars to recognize the first 5 minute bar for the previous day or several days.

So now my problem is trying to get the code to work for every opening bar on the chart. For example I'm looking for the average of the past 5 bars for each first bar. Any tips? I'm messing around with Svanoy's and your codes and trying to piece together codes, but none have been working.
 
@apaul015
You may run into a problem with my code and looking back that far if you are using smaller aggregations.
There is a limlt to how many bars a FOLD statement can calculate correctly. TOS will return a "Too many iterations error" if I exceed that limit in the code. Right now it somewhere around 1500 bars, (I think my code specifies 1000). Lower aggregations, looking back 5 days will easily have more than 1500 bars.
 
@apaul015
I think the code in post #4 of the link I posted earlier would better suit your needs, just need to use highestall() to pass the barnumber. That should remove the limit on how many bars back it will work, I'll look at it later this evening.
 
Thanks! I won’t be able to play around with it until later this evening as well, but I’ll let you know how it goes.

Interesting about the fold statement. I was thinking that maybe for the purposes of my backtest I should just limit the lookback to a set number of days, say 7 days to get the best results and just use that as the benchmark. Logically I’m really just trying to verify how momentum above and below the opening candle varies whether there is higher relative volume than previous days. Do you think that writing a code in this way would be smoother?
 
@apaul015
Tested it for average volume of the 9:30 bar of the past 7 days, starting with yesterday with 5 minute bars.

zKhdzEG.png

Ruby:
declare lower;

input Number_Of_Signals_To_Be_Averaged = 7;
input Value_To_Be_Averaged = volume;
input Show_Plot = Yes;
input Show_Histogram = Yes;
def NotToday = GetDay() <> GetLastDay();
def five = (secondsFromTime(0930)==0) and NotToday;
def TotalSignalCount = if five 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 FinalCount = fold b = 0 to VBar while !IsNaN(GetValue(close, -b)) do GetValue(TotalSignalCount, -b);

def SignalRangeTotal;
if BarNumber() == 0 {
    SignalRangeTotal = 0;
} else if FinalCount - TotalSignalCount < Number_Of_Signals_To_Be_Averaged and five {
    SignalRangeTotal = SignalRangeTotal[1] + Value_To_Be_Averaged;
} else {
    SignalRangeTotal = SignalRangeTotal[1];
}

def RangeAverage = if (SignalRangeTotal / Number_Of_Signals_To_Be_Averaged) > 0 and (FinalCount - TotalSignalCount) <= 0 then (SignalRangeTotal / Number_Of_Signals_To_Be_Averaged) else Double.NaN;

def FinalAverage = fold c = 0 to VBar while !IsNaN(GetValue(close, -c)) do GetValue(RangeAverage, -c);

plot RangeAveragePlot = If FinalAverage > 0 then FinalAverage else Double.NaN;
RangeAveragePlot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
RangeAveragePlot.SetHiding(!Show_Plot);
plot v = volume;
v.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
v.SetHiding(!Show_Histogram);

addlabel(yes,"Average Volume of Last "+Number_Of_Signals_To_Be_Averaged+" Days. Starting Yesterday: "+rangeaverage,color.white);

plot arrow = if five then volume else double.nan;
arrow.setPaintingStrategy(paintingstrategy.ARROW_DOWN);
arrow.sethiding(!Show_Plot);

If you want to move it to the upper window, turn off the plots.
 
Last edited:
@apaul015
Tested it for average volume of the 9:30 bar of the past 7 days, starting with yesterday with 5 minute bars.

zKhdzEG.png

Ruby:
declare lower;

input Number_Of_Signals_To_Be_Averaged = 7;
input Value_To_Be_Averaged = volume;
input Show_Plot = Yes;
input Show_Histogram = Yes;
def NotToday = GetDay() <> GetLastDay();
def five = (secondsFromTime(0930)==0) and NotToday;
def TotalSignalCount = if five 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 FinalCount = fold b = 0 to VBar while !IsNaN(GetValue(close, -b)) do GetValue(TotalSignalCount, -b);

def SignalRangeTotal;
if BarNumber() == 0 {
    SignalRangeTotal = 0;
} else if FinalCount - TotalSignalCount < Number_Of_Signals_To_Be_Averaged and five {
    SignalRangeTotal = SignalRangeTotal[1] + Value_To_Be_Averaged;
} else {
    SignalRangeTotal = SignalRangeTotal[1];
}

def RangeAverage = if (SignalRangeTotal / Number_Of_Signals_To_Be_Averaged) > 0 and (FinalCount - TotalSignalCount) <= 0 then (SignalRangeTotal / Number_Of_Signals_To_Be_Averaged) else Double.NaN;

def FinalAverage = fold c = 0 to VBar while !IsNaN(GetValue(close, -c)) do GetValue(RangeAverage, -c);

plot RangeAveragePlot = If FinalAverage > 0 then FinalAverage else Double.NaN;
RangeAveragePlot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
RangeAveragePlot.SetHiding(!Show_Plot);
plot v = volume;
v.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
v.SetHiding(!Show_Histogram);

addlabel(yes,"Average Volume of Last "+Number_Of_Signals_To_Be_Averaged+" Days. Starting Yesterday: "+rangeaverage,color.white);

plot arrow = if five then volume else double.nan;
arrow.setPaintingStrategy(paintingstrategy.ARROW_DOWN);
arrow.sethiding(!Show_Plot);

If you want to move it to the upper window, turn off the plots.


Thanks @Svanoy! Don't have much time to look this over tonight, but I'll study it and see how it works!
 
@apaul015
Tested it for average volume of the 9:30 bar of the past 7 days, starting with yesterday with 5 minute bars.

zKhdzEG.png

Ruby:
declare lower;

input Number_Of_Signals_To_Be_Averaged = 7;
input Value_To_Be_Averaged = volume;
input Show_Plot = Yes;
input Show_Histogram = Yes;
def NotToday = GetDay() <> GetLastDay();
def five = (secondsFromTime(0930)==0) and NotToday;
def TotalSignalCount = if five 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 FinalCount = fold b = 0 to VBar while !IsNaN(GetValue(close, -b)) do GetValue(TotalSignalCount, -b);

def SignalRangeTotal;
if BarNumber() == 0 {
    SignalRangeTotal = 0;
} else if FinalCount - TotalSignalCount < Number_Of_Signals_To_Be_Averaged and five {
    SignalRangeTotal = SignalRangeTotal[1] + Value_To_Be_Averaged;
} else {
    SignalRangeTotal = SignalRangeTotal[1];
}

def RangeAverage = if (SignalRangeTotal / Number_Of_Signals_To_Be_Averaged) > 0 and (FinalCount - TotalSignalCount) <= 0 then (SignalRangeTotal / Number_Of_Signals_To_Be_Averaged) else Double.NaN;

def FinalAverage = fold c = 0 to VBar while !IsNaN(GetValue(close, -c)) do GetValue(RangeAverage, -c);

plot RangeAveragePlot = If FinalAverage > 0 then FinalAverage else Double.NaN;
RangeAveragePlot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
RangeAveragePlot.SetHiding(!Show_Plot);
plot v = volume;
v.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
v.SetHiding(!Show_Histogram);

addlabel(yes,"Average Volume of Last "+Number_Of_Signals_To_Be_Averaged+" Days. Starting Yesterday: "+rangeaverage,color.white);

plot arrow = if five then volume else double.nan;
arrow.setPaintingStrategy(paintingstrategy.ARROW_DOWN);
arrow.sethiding(!Show_Plot);

If you want to move it to the upper window, turn off the plots.


@Svanoy

This works great and I'll definitely add this to my indicators when trading live, but still not giving me the results I want. I've been dissecting this code and trying to tweak it to get what I need, but having trouble. Here's a screenshot below of what I'm trying to do.


So say I wanted the average of the last 7 Opening bars for every Opening Bar on the Chart. I'm thinking that I need something like:

(CumulativeVolToday - CumulativeVol7DaysAgo) / 7

I'm just having trouble getting a value for CumulativeVol7DaysAgo....

I've been trying to tweak this part of your code:

Code:
def bar = BarNumber();

def BarNum = if !IsNaN(close) and BarNumber()>0 then bar else BarNum[1];

def VBar = HighestAll(BarNum);

def FinalCount = fold b = 0 to VBar while !IsNaN(GetValue(close, -b)) do GetValue(TotalSignalCount, -b);

but it always returns the Highest number of signals shown on the chart no matter where I'm starting. Any tips to get this to work?

Sorry for being so difficult haha, I really appreciate the help.
 
@apaul015
That took a minute.
Had to figure out how to define a moving range.
Averages first bar of day volume for past X number of prior days at the opening bar of every day.
The first X number of days on a chart will be blank as there isn't enough data until the day after X number of days.
ilKCM7K.png

Ruby:
#Averages first bar of day volume for past X number of prior days at the opening bar of every day.
#Svanoy

input Number_Of_Signals_To_Be_Averaged = 5;
input Value_To_Be_Averaged = volume;
input Time_Of_First_Bar_Of_Day = 0930;

def NotToday = GetDay() <> GetLastDay();
def BarToBeAveraged = (secondsFromTime(Time_Of_First_Bar_Of_Day)==0) and NotToday;
def SignalBar = (secondsFromTime(Time_Of_First_Bar_Of_Day)==0);
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] + Value_To_Be_Averaged;
} 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(Value_To_Be_Averaged,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 ,high,"Total Signals: "+TotalSignalCount,color.white);
AddChartBubble(SignalBar and TotalSignalCount > Number_Of_Signals_To_Be_Averaged ,high,"Cumulative Avg: "+SignalRangeTotal,color.light_green);
AddChartBubble(SignalBar and TotalSignalCount > Number_Of_Signals_To_Be_Averaged ,high,"Average Volume of Last "+Number_Of_Signals_To_Be_Averaged+" Days. Starting Yesterday: "+rangeaverage,color.light_red);
 
Last edited:
Solution
@Svanoy

Wow, the Totaltoberemoved line was what I was trying to figure out all week. This is amazing, thanks for taking the time to figure this out!!!

I think I need to sharpen my understanding of the fold function and how you were able to create that line of code to work the way it did. Can I ask how you learned? Or do I just need to put in my 10,000 hours of practice?
 
@apaul015
The key to the way I use the FOLD statement is being able to pass BarNumber() of the final bar to every previous bar. After that, it is just basically counting bars. Use temporary chart bubbles to display counters. This will help you visualize what is going on.

I personally have about a year of experience with thinkscript, but probably 10,000+ hrs prior with other programming languages, which does help with understanding the logic.
 
@apaul015
The key to the way I use the FOLD statement is being able to pass BarNumber() of the final bar to every previous bar. After that, it is just basically counting bars. Use temporary chart bubbles to display counters. This will help you visualize what is going on.

I personally have about a year of experience with thinkscript, but probably 10,000+ hrs prior with other programming languages, which does help with understanding the logic.

Oh that makes sense. Other than an intro to C++ class I took in college, I don't really have much experience in coding so the logic goes over my head more times than not. But I'm really trying to get decent at it so I can backtest theories and ideas I have about patterns I see. Guess I just have to keep working at it!
 
@apaul015
Tested it for average volume of the 9:30 bar of the past 7 days, starting with yesterday with 5 minute bars.

zKhdzEG.png

Ruby:
declare lower;

input Number_Of_Signals_To_Be_Averaged = 7;
input Value_To_Be_Averaged = volume;
input Show_Plot = Yes;
input Show_Histogram = Yes;
def NotToday = GetDay() <> GetLastDay();
def five = (secondsFromTime(0930)==0) and NotToday;
def TotalSignalCount = if five 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 FinalCount = fold b = 0 to VBar while !IsNaN(GetValue(close, -b)) do GetValue(TotalSignalCount, -b);

def SignalRangeTotal;
if BarNumber() == 0 {
    SignalRangeTotal = 0;
} else if FinalCount - TotalSignalCount < Number_Of_Signals_To_Be_Averaged and five {
    SignalRangeTotal = SignalRangeTotal[1] + Value_To_Be_Averaged;
} else {
    SignalRangeTotal = SignalRangeTotal[1];
}

def RangeAverage = if (SignalRangeTotal / Number_Of_Signals_To_Be_Averaged) > 0 and (FinalCount - TotalSignalCount) <= 0 then (SignalRangeTotal / Number_Of_Signals_To_Be_Averaged) else Double.NaN;

def FinalAverage = fold c = 0 to VBar while !IsNaN(GetValue(close, -c)) do GetValue(RangeAverage, -c);

plot RangeAveragePlot = If FinalAverage > 0 then FinalAverage else Double.NaN;
RangeAveragePlot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
RangeAveragePlot.SetHiding(!Show_Plot);
plot v = volume;
v.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
v.SetHiding(!Show_Histogram);

addlabel(yes,"Average Volume of Last "+Number_Of_Signals_To_Be_Averaged+" Days. Starting Yesterday: "+rangeaverage,color.white);

plot arrow = if five then volume else double.nan;
arrow.setPaintingStrategy(paintingstrategy.ARROW_DOWN);
arrow.sethiding(!Show_Plot);

If you want to move it to the upper window, turn off the plots.

Beautiful code logic....am using the logic for opening range averages not related to volume. Works like a charm, will post when completed and will quote your work.
Regards,
Bob
 
@apaul015
That took a minute.
Had to figure out how to define a moving range.
Averages first bar of day volume for past X number of prior days at the opening bar of every day.
The first X number of days on a chart will be blank as there isn't enough data until the day after X number of days.
ilKCM7K.png

Ruby:
#Averages first bar of day volume for past X number of prior days at the opening bar of every day.
#Svanoy

input Number_Of_Signals_To_Be_Averaged = 5;
input Value_To_Be_Averaged = volume;
input Time_Of_First_Bar_Of_Day = 0930;

def NotToday = GetDay() <> GetLastDay();
def BarToBeAveraged = (secondsFromTime(Time_Of_First_Bar_Of_Day)==0) and NotToday;
def SignalBar = (secondsFromTime(Time_Of_First_Bar_Of_Day)==0);
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] + Value_To_Be_Averaged;
} 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(Value_To_Be_Averaged,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 ,high,"Total Signals: "+TotalSignalCount,color.white);
AddChartBubble(SignalBar and TotalSignalCount > Number_Of_Signals_To_Be_Averaged ,high,"Cumulative Avg: "+SignalRangeTotal,color.light_green);
AddChartBubble(SignalBar and TotalSignalCount > Number_Of_Signals_To_Be_Averaged ,high,"Average Volume of Last "+Number_Of_Signals_To_Be_Averaged+" Days. Starting Yesterday: "+rangeaverage,color.light_red);
Anyway the values for rangeaverage can be added to a watchlist by adjusting the code at all? Seems like this has to be done on the chart itself, or there is no way to pull in the necessary data.
 
@apaul015
That took a minute.
Had to figure out how to define a moving range.
Averages first bar of day volume for past X number of prior days at the opening bar of every day.
The first X number of days on a chart will be blank as there isn't enough data until the day after X number of days.
ilKCM7K.png

Ruby:
#Averages first bar of day volume for past X number of prior days at the opening bar of every day.
#Svanoy

input Number_Of_Signals_To_Be_Averaged = 5;
input Value_To_Be_Averaged = volume;
input Time_Of_First_Bar_Of_Day = 0930;

def NotToday = GetDay() <> GetLastDay();
def BarToBeAveraged = (secondsFromTime(Time_Of_First_Bar_Of_Day)==0) and NotToday;
def SignalBar = (secondsFromTime(Time_Of_First_Bar_Of_Day)==0);
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] + Value_To_Be_Averaged;
} 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(Value_To_Be_Averaged,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 ,high,"Total Signals: "+TotalSignalCount,color.white);
AddChartBubble(SignalBar and TotalSignalCount > Number_Of_Signals_To_Be_Averaged ,high,"Cumulative Avg: "+SignalRangeTotal,color.light_green);
AddChartBubble(SignalBar and TotalSignalCount > Number_Of_Signals_To_Be_Averaged ,high,"Average Volume of Last "+Number_Of_Signals_To_Be_Averaged+" Days. Starting Yesterday: "+rangeaverage,color.light_red);
Looking to modify this to average in the first 15mins of opening only & display only last X number of avg. would appreciate some help!
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
356 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top