Trouble adapting a time-relative volume indicator

pkerrant

New member
VIP
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):

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;
 
Solution
Uh...to answer my own question: I made a typo. The above modification works if you replace k with j in the if clause:

Code:
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 (
           # (GetTime() - RegularTradingStart(GetYYYYMMDD()) >= GetValue(GetTime(), i) - #RegularTradingStart(GetValue(GetYYYYMMDD(), i))
#&&
            Floor(
                    (
                        GetTime() 
                        - 
                        RegularTradingStart(GetYYYYMMDD())
                    )/_15minMS
                 ) 
            == 
            Floor(
                    (...
Uh...to answer my own question: I made a typo. The above modification works if you replace k with j in the if clause:

Code:
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 (
           # (GetTime() - RegularTradingStart(GetYYYYMMDD()) >= GetValue(GetTime(), i) - #RegularTradingStart(GetValue(GetYYYYMMDD(), i))
#&&
            Floor(
                    (
                        GetTime() 
                        - 
                        RegularTradingStart(GetYYYYMMDD())
                    )/_15minMS
                 ) 
            == 
            Floor(
                    (
                        GetValue(GetTime(), j) 
                        - 
                        RegularTradingStart(GetValue(GetYYYYMMDD(), j))
                    )/_15minMS
                )
,
    k + GetValue(volume, j) / days, 
    k);
 
Solution

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
270 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