Guest User

Untitled

a guest
Jul 28th, 2022
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. # This source code is subject to the terms of the MIT License at https://opensource.org/licenses/MIT
  2. # Based on /u/HurlTeaInTheSea v1.0
  3. # https://www.reddit.com/r/RealDayTrading/comments/ue4ujq/tostv_timebased_relative_volume_rvol_a_better/
  4.  
  5. # Intraday Relative Volume (RVol) indicator:
  6. # https://stockbeep.com/how-to-calculate-relative-volume-rvol-for-intraday-trading
  7.  
  8. # still works on higher timeframe but it's not a "day" average anymore, so throw error to avoid confusion
  9. addlabel(GetAggregationPeriod() > AggregationPeriod.DAY, "RVol is only valid for daily timeframe or lower");
  10.  
  11. input _nDayAverage = 5;
  12. #input _rVolHighlightThres = 0;
  13. input _colorBasedOnPrevClose = no;
  14. input ComparedWithSecurity = "SPY";
  15.  
  16. def days = Max(_nDayAverage, 1);
  17. #def rVolThres = Max(_rVolHighlightThres, 0);
  18. def rVolThres = 0;
  19.  
  20. # detect new session of day
  21. def isNewDay = GetYYYYMMDD() != GetYYYYMMDD()[1];
  22.  
  23. def cVol; # cumulative volume
  24. def cVolComp; # cumulative volume of comp security
  25. def beforeNewDayBars; # save bar number before new day
  26. def len; # count number of new days
  27. if isNewDay {
  28. cVol = volume;
  29. cVolComp = volume(symbol = ComparedWithSecurity);
  30. beforeNewDayBars = BarNumber() - 1;
  31. len = len[1] + 1;
  32. } else {
  33. cVol = cVol[1] + volume;
  34. cVolComp = cVolComp[1] + volume(symbol = ComparedWithSecurity);
  35. beforeNewDayBars = beforeNewDayBars[1];
  36. len = len[1];
  37. }
  38.  
  39. # starting from last bar of previous session, go back in time and accumulate volume up to current time relative to trading day
  40. # stop after N day cumulative volume average collected
  41. def skip = BarNumber() - beforeNewDayBars;
  42. def aVol = fold i = skip to Max(skip, BarNumber())
  43. with v = 0
  44. while BarNumber() >= days + 1 && len >= days + 1 && len - 1 - GetValue(len, i) < days
  45. do If(GetTime() - RegularTradingStart(GetYYYYMMDD()) >= GetValue(GetTime(), i) - RegularTradingStart(GetValue(GetYYYYMMDD(), i)), v + GetValue(volume, i) / days, v);
  46.  
  47. def aVolComp = fold j = skip to Max(skip, BarNumber())
  48. with w = 0
  49. while BarNumber() >= days + 1 && len >= days + 1 && len - 1 - GetValue(len, j) < days
  50. do If(GetTime() - RegularTradingStart(GetYYYYMMDD()) >= GetValue(GetTime(), j) - RegularTradingStart(GetValue(GetYYYYMMDD(), j)), w + GetValue(volume(symbol = ComparedWithSecurity), j) / days, w);
  51.  
  52. def _rVol = if aVol > 0 then cVol / aVol else 0;
  53. def _rVolComp = if aVolComp > 0 then cVolComp / aVolComp else 0;
  54.  
  55. # visuals
  56. def upColorCondition = if _colorBasedOnPrevClose then close >= close[1] else close >= open;
  57.  
  58. Plot RVol = _rVol - _rVolComp;
  59. RVol.DefineColor("Up", Color.GREEN);
  60. RVol.DefineColor("Down", Color.RED);
  61. RVol.DefineColor("Up-Dim", Color.DARK_GREEN);
  62. RVol.DefineColor("Down-Dim", Color.DARK_RED);
  63. 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"));
  64. RVol.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
  65.  
  66. # mark new sessions on intraday charts
  67. AddVerticalLine(isNewDay && GetAggregationPeriod() < AggregationPeriod.DAY, "", Color.GRAY, curve.SHORT_DASH);
  68.  
  69. #Plot Ref = 1;
  70. #Ref.DefineColor("Reference", Color.GRAY);
  71. #Ref.AssignValueColor(Ref.color("Reference"));
  72. #Ref.HideBubble();
  73. #Ref.HideTitle();
  74.  
  75. Plot Zero = 0;
  76. Zero.DefineColor("Zero", Color.GRAY);
  77. Zero.AssignValueColor(Zero.color("Zero"));
  78. Zero.HideBubble();
  79. Zero.HideTitle();
  80.  
Add Comment
Please, Sign In to add comment