I'm trying to adapt an indicator as follows: the original indicator, credited to u/HurlTeaInTheSea on reddit, can be found here; it compares the cumulative volume of the present day to an SMA of cumulative average for previous days up to the same candle. So if it's 10:45, it's comparing the cumulative volume at 10:45 to the cumulative volume at 10:45 of however many specified previous days. I thought I had adapted to this to give me a candle-by-candle SMA (so, an average of all the fifteen minute 10:45 candles for the previous however many days), but it's giving incorrect output and I can't figure out why.
I've included the code of both the original indicator and my adaptation attempt below; the gist of it is that I just tried to replace the condition within the do clause of the defining fold function.
I would also like to be able to calculate an EMA for this indicator, and I have an idea that might work, but I figure I should get the actual thing working first.
Original indicator (I've omitted cosmetic plotting stuff at the end for brevity):
My adaptation attempt:
I've included the code of both the original indicator and my adaptation attempt below; the gist of it is that I just tried to replace the condition within the do clause of the defining fold function.
I would also like to be able to calculate an EMA for this indicator, and I have an idea that might work, but I figure I should get the actual thing working first.
Original indicator (I've omitted cosmetic plotting stuff at the end for brevity):
Code:
# This source code is subject to the terms of the MIT License at https://opensource.org/licenses/MIT
# /u/HurlTeaInTheSea v1.1
# v1.1 (2022-12-27)
# - Fixed bug where days before first bar are counted (if any)
# - Added warning if not enough days in history
# v1.0 (2022-04-28)
# - Initial release
# Intraday Relative Volume (RVol) indicator:
# https://stockbeep.com/blog/post/how-to-calculate-relative-volume-rvol-for-intraday-trading
declare zerobase;
declare lower;
# still works on higher timeframe but it's not a "day" average anymore, so throw error to avoid confusion
addlabel(GetAggregationPeriod() > AggregationPeriod.DAY, "RVol is only valid for daily timeframe or lower");
input _nDayAverage = 5;
input _rVolHighlightThres = 1.0;
input _colorBasedOnPrevClose = no;
def days = Max(_nDayAverage, 1);
def rVolThres = Max(_rVolHighlightThres, 0);
# detect new session of day
def isNewDay = GetYYYYMMDD() != GetYYYYMMDD()[1];
def cVol; # cumulative volume
def beforeNewDayBars; # save bar number before new day
def len; # count number of new days
if isNewDay {
cVol = volume;
beforeNewDayBars = BarNumber() - 1;
len = len[1] + If(BarNumber() >= 1, 1, 0);
} else {
cVol = cVol[1] + volume;
beforeNewDayBars = beforeNewDayBars[1];
len = len[1];
}
# starting from last bar of previous session, go back in time and accumulate volume up to current time relative to trading day
# stop after N day cumulative volume average collected
def skip = BarNumber() - beforeNewDayBars;
def aVol = fold i = skip to Max(skip, BarNumber())
with v = 0
while BarNumber() >= days + 1 && len >= days + 1 && len - 1 - GetValue(len, i) < days
do If(GetTime() - RegularTradingStart(GetYYYYMMDD()) >= GetValue(GetTime(), i) - RegularTradingStart(GetValue(GetYYYYMMDD(), i)), v + GetValue(volume, i) / days, v);
def _rVol = if aVol > 0 then cVol / aVol else Double.NaN;
# visuals
def upColorCondition = if _colorBasedOnPrevClose then close >= close[1] else close >= open;
Plot RVol = _rVol;
My adaptation attempt:
Code:
declare lower;
input _nDayAverage = 10;
input _colorBasedOnPrevClose = no;
def days = Max(_nDayAverage, 1);
def isNewDay = GetYYYYMMDD() != GetYYYYMMDD()[1];
def cVol; # cumulative volume
def beforeNewDayBars; # save bar number before new day
def len; # count number of new days
if isNewDay {
cVol = volume;
beforeNewDayBars = BarNumber() - 1;
len = len[1] + 1;
} else {
cVol = cVol[1] + volume;
beforeNewDayBars = beforeNewDayBars[1];
len = len[1];
}
def skip = BarNumber() - beforeNewDayBars;
def _15minMS = 900000;
def aVol_15mCBC = fold
j = skip to Max(skip, BarNumber())
with k = 0
while BarNumber() >= days + 1 && len >= days + 1 && len - 1 - GetValue(len, j) < days
do
If (
Floor(
(
GetTime()
-
RegularTradingStart(GetYYYYMMDD())
)/_15minMS
)
==
Floor(
(
GetValue(GetTime(), k)
-
RegularTradingStart(GetValue(GetYYYYMMDD(), k))
)/_15minMS
)
,
k + GetValue(volume, j) / days,
k);
plot TSA = aVol_15mCBC;
plot TSAdev = volume/aVol_15mCBC;
plot _vol = volume;