# This source code is subject to the terms of the MIT License at https://opensource.org/licenses/MIT # /u/HurlTeaInTheSea v1.0 # Intraday Relative Volume (RVol) indicator: # https://stockbeep.com/how-to-calculate-relative-volume-rvol-for-intraday-trading # 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 = 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] + 1; } 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 0; # visuals def upColorCondition = if _colorBasedOnPrevClose then close >= close[1] else close >= open; Plot RVol = _rVol - 1; RVol.DefineColor("Up", Color.GREEN); RVol.DefineColor("Down", Color.RED); RVol.DefineColor("Up-Dim", Color.DARK_GREEN); RVol.DefineColor("Down-Dim", Color.DARK_RED); RVol.AssignValueColor(if _rVol >= rVolThres then if upColorCondition then RVol.Color("Up") else RVol.Color("Down") else if upColorCondition then RVol.Color("Up-Dim") else RVol.Color("Down-Dim")); RVol.SetPaintingStrategy(PaintingStrategy.HISTOGRAM); # mark new sessions on intraday charts AddVerticalLine(isNewDay && GetAggregationPeriod() < AggregationPeriod.DAY, "", Color.GRAY, curve.SHORT_DASH); #Plot Ref = 1; #Ref.DefineColor("Reference", Color.GRAY); #Ref.AssignValueColor(Ref.color("Reference")); #Ref.HideBubble(); #Ref.HideTitle(); #Plot Zero = 0; #Zero.DefineColor("Zero", Color.GRAY); #Zero.AssignValueColor(Zero.color("Zero")); #Zero.HideBubble(); #Zero.HideTitle();