Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This source code is subject to the terms of the MIT License at https://opensource.org/licenses/MIT
- # Based on /u/HurlTeaInTheSea v1.0
- # https://www.reddit.com/r/RealDayTrading/comments/ue4ujq/tostv_timebased_relative_volume_rvol_a_better/
- # 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;
- input ComparedWithSecurity = "SPY";
- def days = Max(_nDayAverage, 1);
- #def rVolThres = Max(_rVolHighlightThres, 0);
- def rVolThres = 0;
- # detect new session of day
- def isNewDay = GetYYYYMMDD() != GetYYYYMMDD()[1];
- def cVol; # cumulative volume
- def cVolComp; # cumulative volume of comp security
- def beforeNewDayBars; # save bar number before new day
- def len; # count number of new days
- if isNewDay {
- cVol = volume;
- cVolComp = volume(symbol = ComparedWithSecurity);
- beforeNewDayBars = BarNumber() - 1;
- len = len[1] + 1;
- } else {
- cVol = cVol[1] + volume;
- cVolComp = cVolComp[1] + volume(symbol = ComparedWithSecurity);
- 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 aVolComp = fold j = skip to Max(skip, BarNumber())
- with w = 0
- while BarNumber() >= days + 1 && len >= days + 1 && len - 1 - GetValue(len, j) < days
- do If(GetTime() - RegularTradingStart(GetYYYYMMDD()) >= GetValue(GetTime(), j) - RegularTradingStart(GetValue(GetYYYYMMDD(), j)), w + GetValue(volume(symbol = ComparedWithSecurity), j) / days, w);
- def _rVol = if aVol > 0 then cVol / aVol else 0;
- def _rVolComp = if aVolComp > 0 then cVolComp / aVolComp else 0;
- # visuals
- def upColorCondition = if _colorBasedOnPrevClose then close >= close[1] else close >= open;
- Plot RVol = _rVol - _rVolComp;
- 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();
Add Comment
Please, Sign In to add comment